00001 /* P R I N T B . C 00002 * BRL-CAD 00003 * 00004 * Copyright (c) 2004-2006 United States Government as represented by 00005 * the U.S. Army Research Laboratory. 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public License 00009 * as published by the Free Software Foundation; either version 2 of 00010 * the License, or (at your option) any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, but 00013 * WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Library General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this file; see the file named COPYING for more 00019 * information. 00020 */ 00021 00022 /** @addtogroup bitv */ 00023 /*@{*/ 00024 00025 /** @file printb.c 00026 * @brief print bitfields 00027 * 00028 * 00029 * @author Michael John Muuss 00030 * 00031 * @par Source - 00032 * The U. S. Army Research Laboratory 00033 * @n Aberdeen Proving Ground, Maryland 21005-5068 USA 00034 * 00035 */ 00036 00037 #ifndef lint 00038 static const char libbu_printb_RCSid[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/libbu/printb.c,v 14.13 2006/09/03 15:14:07 lbutler Exp $ (ARL)"; 00039 #endif 00040 00041 #include "common.h" 00042 00043 #include <stdio.h> 00044 #include <math.h> 00045 00046 #include "machine.h" 00047 #include "bu.h" 00048 00049 00050 /** 00051 * B U _ V L S _ P R I N T B 00052 * 00053 * Format a value a la the %b format of the kernel's printf 00054 * 00055 * @param vls variable length string to put output in 00056 * @param s title string 00057 * @param v the integer with the bits in it 00058 * @param bits a string which starts with the desired base (8 or 16) 00059 * as \\010 or \\020, followed by 00060 * words preceeded with embedded low-value bytes indicating 00061 * bit number plus one, 00062 * in little-endian order, eg: 00063 * "\010\2Bit_one\1BIT_zero" 00064 */ 00065 void 00066 bu_vls_printb(struct bu_vls *vls, const char *s, register long unsigned int v, register const char *bits) 00067 { 00068 register int i, any = 0; 00069 register char c; 00070 00071 if (*bits++ == 8) 00072 bu_vls_printf( vls, "%s=0%lo <", s, v); 00073 else 00074 bu_vls_printf( vls, "%s=x%lx <", s, v); 00075 while ((i = *bits++)) { 00076 if (v & (1L << (i-1))) { 00077 if (any) 00078 bu_vls_strcat( vls, ","); 00079 any = 1; 00080 for (; (c = *bits) > 32; bits++) 00081 bu_vls_printf( vls, "%c", c); 00082 } else 00083 for (; *bits > 32; bits++) 00084 ; 00085 } 00086 bu_vls_strcat( vls, ">"); 00087 } 00088 00089 /** 00090 * B U _ P R I N T B 00091 * 00092 * Format and print, like bu_vls_printb(). 00093 */ 00094 void 00095 bu_printb(const char *s, register long unsigned int v, register const char *bits) 00096 { 00097 struct bu_vls str; 00098 00099 bu_vls_init(&str); 00100 bu_vls_printb( &str, s, v, bits ); 00101 bu_log("%s", bu_vls_addr(&str) ); 00102 bu_vls_free(&str); 00103 } 00104 /*@}*/ 00105 /* 00106 * Local Variables: 00107 * mode: C 00108 * tab-width: 8 00109 * c-basic-offset: 4 00110 * indent-tabs-mode: t 00111 * End: 00112 * ex: shiftwidth=4 tabstop=8 00113 */