Definition in file dsp_debug_printf.c.
#include <string.h>
#include <stdarg.h>
#include "dsp.h"
#include "dsp_debug.h"
#include "dsp_debug_shared.h"
Go to the source code of this file.
Defines | |
#define | PAD_RIGHT 1 |
#define | PAD_ZERO 2 |
#define | PRINT_BUF_LEN 12 |
Functions | |
int | dsp16_debug_printf (const char *format,...) |
This function is the printf version for 16-bit Q formatted signed numbers. | |
int | dsp16_debug_sprintf (char *out, const char *format,...) |
This function is the sprintf version for 16-bit Q formatted signed numbers. | |
int | dsp32_debug_printf (const char *format,...) |
This function is the printf version for 32-bit Q formatted signed numbers. | |
int | dsp32_debug_sprintf (char *out, const char *format,...) |
This function is the sprintf version for 32-bit Q formatted signed numbers. | |
static int | print (int size, char **out, const char *format, va_list args) |
static void | printchar (char **str, int c) |
static int | printi (char **out, int i, int b, int sg, int width, int pad, int letbase, int printlimit) |
static int | prints (char **out, const char *string, int width, int pad, int printlimit, Bool IsNumber) |
#define PAD_RIGHT 1 |
Definition at line 66 of file dsp_debug_printf.c.
#define PAD_ZERO 2 |
Definition at line 67 of file dsp_debug_printf.c.
#define PRINT_BUF_LEN 12 |
Definition at line 141 of file dsp_debug_printf.c.
static int print | ( | int | size, | |
char ** | out, | |||
const char * | format, | |||
va_list | args | |||
) | [static] |
Definition at line 196 of file dsp_debug_printf.c.
References DSP16_QA, DSP32_QA, dsp_debug_sprint(), FALSE, PAD_RIGHT, PAD_ZERO, printchar(), printi(), and prints().
00197 { 00198 register int width, pad, printlimit; 00199 register int pc = 0; 00200 char scr[2]; 00201 00202 for(; *format != 0; ++format) 00203 { 00204 if (*format == '%') 00205 { 00206 ++format; 00207 width = pad = printlimit = 0; 00208 // End of string 00209 if (*format == '\0') 00210 break; 00211 // For the % character 00212 if (*format == '%') 00213 goto out; 00214 // Right padding 00215 if (*format == '-') 00216 { 00217 ++format; 00218 pad = PAD_RIGHT; 00219 } 00220 // Zero padding 00221 while(*format == '0') 00222 { 00223 ++format; 00224 pad |= PAD_ZERO; 00225 } 00226 // width of the result 00227 for(; *format >= '0' && *format <= '9'; ++format) 00228 { 00229 width *= 10; 00230 width += *format - '0'; 00231 } 00232 // Limit width of the result 00233 if (*format == '.') 00234 { 00235 ++format; 00236 for(; *format >= '0' && *format <= '9'; ++format) 00237 { 00238 printlimit *= 10; 00239 printlimit += *format - '0'; 00240 } 00241 } 00242 if (0 == printlimit) 00243 printlimit--; 00244 // To print a string 00245 if (*format == 's') 00246 { 00247 register char *s = (char *) va_arg( args, int ); 00248 pc += prints(out, s?s:"(null)", width, pad, printlimit, FALSE); 00249 continue; 00250 } 00251 // To print a signed integer 00252 if (*format == 'd' || *format == 'i') 00253 { 00254 pc += printi(out, va_arg( args, int ), 10, 1, width, pad, 'a', printlimit); 00255 continue; 00256 } 00257 // To print in octal format 00258 if (*format == 'o') 00259 { 00260 pc += printi(out, va_arg( args, int ), 8, 0, width, pad, 'a', printlimit); 00261 continue; 00262 } 00263 // To print in hexadecimal format (lower case) 00264 if (*format == 'x' ) 00265 { 00266 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a', printlimit); 00267 continue; 00268 } 00269 // To print in hexadecimal format (upper case) 00270 if (*format == 'X') 00271 { 00272 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'A', printlimit); 00273 continue; 00274 } 00275 // To print unsigned integer 00276 if (*format == 'u') 00277 { 00278 pc += printi (out, va_arg( args, int ), 10, 0, width, pad, 'a', printlimit); 00279 continue; 00280 } 00281 // To print a character 00282 if (*format == 'c') 00283 { 00284 // char are converted to int then pushed on the stack 00285 scr[0] = (char) va_arg( args, int ); 00286 scr[1] = '\0'; 00287 pc += prints (out, scr, width, pad, printlimit, FALSE); 00288 continue; 00289 } 00290 // To print a fixed-point number 00291 if (*format == 'f') 00292 { 00293 switch(size) 00294 { 00295 // a 16-bit fixed-point data 00296 case 16: 00297 pc += dsp_debug_sprint(out, 16, DSP16_QA, va_arg(args, int)); 00298 continue; 00299 // a 32-bit fixed-point data 00300 case 32: 00301 pc += dsp_debug_sprint(out, 32, DSP32_QA, va_arg(args, int)); 00302 continue; 00303 } 00304 } 00305 } 00306 else 00307 { 00308 // Print the character 00309 out: 00310 printchar (out, *format); 00311 ++pc; 00312 } 00313 } 00314 00315 if (out) 00316 **out = '\0'; 00317 va_end( args ); 00318 return pc; 00319 }
static void printchar | ( | char ** | str, | |
int | c | |||
) | [static] |
static int printi | ( | char ** | out, | |
int | i, | |||
int | b, | |||
int | sg, | |||
int | width, | |||
int | pad, | |||
int | letbase, | |||
int | printlimit | |||
) | [static] |
Definition at line 144 of file dsp_debug_printf.c.
References PAD_ZERO, PRINT_BUF_LEN, printchar(), prints(), and TRUE.
00145 { 00146 char print_buf[PRINT_BUF_LEN]; 00147 register char *s; 00148 register int t, neg = 0, pc = 0; 00149 register unsigned int u = i; 00150 00151 // If the number equals to 0 00152 if (i == 0) 00153 { 00154 print_buf[0] = '0'; 00155 print_buf[1] = '\0'; 00156 return prints(out, print_buf, width, pad, printlimit, TRUE); 00157 } 00158 00159 // in case the result has to be displayed as a negative number 00160 if (sg && b == 10 && i < 0) 00161 { 00162 neg = 1; 00163 u = -i; 00164 } 00165 00166 s = print_buf + PRINT_BUF_LEN-1; 00167 *s = '\0'; 00168 00169 // Print the number with the specified base 00170 while (u) 00171 { 00172 t = u % b; 00173 if( t >= 10 ) 00174 t += letbase - '0' - 10; 00175 *--s = t + '0'; 00176 u /= b; 00177 } 00178 00179 // Negate the result 00180 if (neg) 00181 { 00182 if(width && (pad & PAD_ZERO)) 00183 { 00184 printchar (out, '-'); 00185 ++pc; 00186 --width; 00187 } 00188 else 00189 *--s = '-'; 00190 } 00191 00192 return pc + prints(out, s, width, pad, printlimit, TRUE); 00193 }
static int prints | ( | char ** | out, | |
const char * | string, | |||
int | width, | |||
int | pad, | |||
int | printlimit, | |||
Bool | IsNumber | |||
) | [static] |
Definition at line 70 of file dsp_debug_printf.c.
References FALSE, PAD_RIGHT, PAD_ZERO, printchar(), and TRUE.
00071 { 00072 register int pc = 0, padchar = ' '; 00073 int i, len; 00074 register const char *ptr; 00075 00076 if (width > 0) 00077 { 00078 register int len = 0; 00079 for (ptr = string; *ptr; ++ptr) 00080 ++len; 00081 if (len >= width) 00082 width = 0; 00083 else 00084 width -= len; 00085 if (pad & PAD_ZERO) 00086 padchar = '0'; 00087 } 00088 if (!(pad & PAD_RIGHT)) 00089 { 00090 for(; width > 0; --width) 00091 { 00092 printchar (out, padchar); 00093 ++pc; 00094 } 00095 } 00096 if (FALSE == IsNumber) 00097 { 00098 // The string to print is not the result of a number conversion to ascii. 00099 // For a string, printlimit is the max number of characters to display. 00100 for ( ; printlimit && *string ; ++string, --printlimit) 00101 { 00102 printchar (out, *string); 00103 ++pc; 00104 } 00105 } 00106 if (TRUE == IsNumber) 00107 { 00108 // The string to print represents an integer number. 00109 // In this case, printlimit is the min number of digits to print. 00110 // If the length of the number to print is less than the min nb of i 00111 // digits to display, we add 0 before printing the number. 00112 len = strlen(string); 00113 if (len < printlimit) 00114 { 00115 i = printlimit - len; 00116 for(; i; i--) 00117 { 00118 printchar (out, '0'); 00119 ++pc; 00120 } 00121 } 00122 } 00123 // Else: The string to print is not the result of a number conversion to ascii. 00124 // For a string, printlimit is the max number of characters to display. 00125 for(; printlimit && *string ; ++string, --printlimit) 00126 { 00127 printchar (out, *string); 00128 ++pc; 00129 } 00130 00131 for(; width > 0; --width) 00132 { 00133 printchar (out, padchar); 00134 ++pc; 00135 } 00136 00137 return pc; 00138 }