Definition in file dsp_sprintf.c.
#include <string.h>
#include <stdarg.h>
#include "dsp.h"
#include "dsp_sprintf.h"
Go to the source code of this file.
Defines | |
#define | PAD_RIGHT 1 |
#define | PAD_ZERO 2 |
#define | PRINT_BUF_LEN 12 |
#define | TEMP_BUFFER_SIZE 32 |
Functions | |
int | dsp16_sprintf (char *out, const char *format,...) |
int | dsp32_sprintf (char *out, const char *format,...) |
static int | dsp_sprint (char **out, int nb_bits, int q, int i) |
static int | dsp_sprint_after_radix (char **out, unsigned int num, unsigned int den, int nb_digits) |
static int | dsp_sprint_fct (char **out, char *str) |
static char * | dsp_sprint_ui (char *out, unsigned int n) |
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 |
#define PAD_ZERO 2 |
#define PRINT_BUF_LEN 12 |
#define TEMP_BUFFER_SIZE 32 |
int dsp16_sprintf | ( | char * | out, | |
const char * | format, | |||
... | ||||
) |
Definition at line 453 of file dsp_sprintf.c.
References print().
00454 { 00455 va_list args; 00456 int n; 00457 char *pdsp_debug_buffer; 00458 00459 // Point on the default buffer 00460 pdsp_debug_buffer = out; 00461 00462 // Compute the printf with specifics arguments 00463 va_start(args, format); 00464 n = print(16, &pdsp_debug_buffer, format, args); 00465 va_end(args); 00466 00467 return n; 00468 }
int dsp32_sprintf | ( | char * | out, | |
const char * | format, | |||
... | ||||
) |
Definition at line 471 of file dsp_sprintf.c.
References print().
00472 { 00473 va_list args; 00474 int n; 00475 char *pdsp_debug_buffer; 00476 00477 // Point on the default buffer 00478 pdsp_debug_buffer = out; 00479 00480 // Compute the printf with specifics arguments 00481 va_start(args, format); 00482 n = print(32, &pdsp_debug_buffer, format, args); 00483 va_end(args); 00484 00485 return n; 00486 }
static int dsp_sprint | ( | char ** | out, | |
int | nb_bits, | |||
int | q, | |||
int | i | |||
) | [static] |
Definition at line 142 of file dsp_sprintf.c.
References buffer, dsp_sprint_after_radix(), dsp_sprint_fct(), dsp_sprint_ui(), and TEMP_BUFFER_SIZE.
Referenced by print().
00143 { 00144 int range; 00145 int n = 0; 00146 char *pdsp_debug_buffer; 00147 char buffer[TEMP_BUFFER_SIZE]; 00148 00149 // if this is a negative number 00150 if (i < 0) 00151 { 00152 // Particulary case 00153 if (i == 0x80000000) 00154 { 00155 i = 0; 00156 range = 1; 00157 } 00158 else 00159 { 00160 // Get its absolute value 00161 i = -i; 00162 // range contains the floor value of the fixed-point number 00163 range = i >> (nb_bits-q); 00164 } 00165 // Print the '-' character to specify that it is a negative number 00166 n += dsp_sprint_fct(out, "-"); 00167 } 00168 else 00169 // range contains the floor value of the fixed-point number 00170 range = i >> (nb_bits-q); 00171 00172 // Print the floor value 00173 pdsp_debug_buffer = dsp_sprint_ui(buffer, range); 00174 n += dsp_sprint_fct(out, pdsp_debug_buffer); 00175 00176 00177 // Print the comma 00178 n += dsp_sprint_fct(out, "."); 00179 // Print the number after the comma 00180 n += dsp_sprint_after_radix(out, i, 1 << (nb_bits-q), 4); 00181 00182 00183 // Retruns the length of the string generated 00184 return n; 00185 }
static int dsp_sprint_after_radix | ( | char ** | out, | |
unsigned int | num, | |||
unsigned int | den, | |||
int | nb_digits | |||
) | [static] |
Definition at line 99 of file dsp_sprintf.c.
References dsp_sprint_fct().
Referenced by dsp_sprint().
00100 { 00101 unsigned int i; 00102 char temp[2]; 00103 int n = 0; 00104 00105 temp[1] = '\0'; 00106 00107 // Go until reaching desired numbers (the one after the comma) 00108 i = num/den; 00109 num -= i*den; 00110 00111 // Then print the numbers after the radix 00112 while(nb_digits-- && num) 00113 { 00114 // This is to minimize the overflows 00115 // If the numerator is not too high, then multiply its value per 10 00116 // Else divide the value of the denominator per 10 which has the same effect 00117 if (num <= 0xFFFFFFFF/10) 00118 num *= 10; 00119 else 00120 den /= 10; 00121 00122 // Get the quotient of the division of num/den 00123 i = num/den; 00124 // Saturate i 00125 if (i > 9) 00126 i = 9; 00127 00128 // Print the number on the default output 00129 temp[0] = (char) i + '0'; 00130 n += dsp_sprint_fct(out, temp); 00131 00132 num -= i*den; 00133 } 00134 00135 // Finish the string 00136 **out = '\0'; 00137 00138 return n; 00139 }
static int dsp_sprint_fct | ( | char ** | out, | |
char * | str | |||
) | [static] |
Definition at line 60 of file dsp_sprintf.c.
Referenced by dsp_sprint(), and dsp_sprint_after_radix().
00061 { 00062 int n = 0; 00063 00064 // Main loop 00065 while(*str) 00066 { 00067 n++; 00068 *(*out)++ = *str++; 00069 } 00070 **out = '\0'; 00071 00072 // Return the length of the string 00073 return n; 00074 }
static char * dsp_sprint_ui | ( | char * | out, | |
unsigned int | n | |||
) | [static] |
Definition at line 78 of file dsp_sprintf.c.
References dsp_debug_buffer, and TEMP_BUFFER_SIZE.
Referenced by dsp_sprint().
00079 { 00080 char *dsp_debug_buffer = out; 00081 int i = TEMP_BUFFER_SIZE-1; 00082 00083 // Point to the end of the buffer and set a EOL sign 00084 dsp_debug_buffer[i] = '\0'; 00085 do 00086 { 00087 // Get the modulo 10 of the number and print this rest 00088 dsp_debug_buffer[--i] = '0' + n%10; 00089 // Divide per 10 this number 00090 n /= 10; 00091 // This until n reachs 0 00092 } while (n); 00093 00094 // Returns a pointer on the begining of the generated string 00095 return &dsp_debug_buffer[i]; 00096 }
static int print | ( | int | size, | |
char ** | out, | |||
const char * | format, | |||
va_list | args | |||
) | [static] |
Definition at line 327 of file dsp_sprintf.c.
References DSP16_QA, DSP32_QA, dsp_sprint(), FALSE, PAD_RIGHT, PAD_ZERO, printchar(), printi(), and prints().
Referenced by dsp16_debug_printf(), dsp16_debug_sprintf(), dsp16_sprintf(), dsp32_debug_printf(), dsp32_debug_sprintf(), and dsp32_sprintf().
00328 { 00329 register int width, pad, printlimit; 00330 register int pc = 0; 00331 char scr[2]; 00332 00333 for(; *format != 0; ++format) 00334 { 00335 if (*format == '%') 00336 { 00337 ++format; 00338 width = pad = printlimit = 0; 00339 // End of string 00340 if (*format == '\0') 00341 break; 00342 // For the % character 00343 if (*format == '%') 00344 goto out; 00345 // Right padding 00346 if (*format == '-') 00347 { 00348 ++format; 00349 pad = PAD_RIGHT; 00350 } 00351 // Zero padding 00352 while(*format == '0') 00353 { 00354 ++format; 00355 pad |= PAD_ZERO; 00356 } 00357 // width of the result 00358 for(; *format >= '0' && *format <= '9'; ++format) 00359 { 00360 width *= 10; 00361 width += *format - '0'; 00362 } 00363 // Limit width of the result 00364 if (*format == '.') 00365 { 00366 ++format; 00367 for(; *format >= '0' && *format <= '9'; ++format) 00368 { 00369 printlimit *= 10; 00370 printlimit += *format - '0'; 00371 } 00372 } 00373 if (0 == printlimit) 00374 printlimit--; 00375 // To print a string 00376 if (*format == 's') 00377 { 00378 register char *s = (char *) va_arg( args, int ); 00379 pc += prints(out, s?s:"(null)", width, pad, printlimit, FALSE); 00380 continue; 00381 } 00382 // To print a signed integer 00383 if (*format == 'd' || *format == 'i') 00384 { 00385 pc += printi(out, va_arg( args, int ), 10, 1, width, pad, 'a', printlimit); 00386 continue; 00387 } 00388 // To print in octal format 00389 if (*format == 'o') 00390 { 00391 pc += printi(out, va_arg( args, int ), 8, 0, width, pad, 'a', printlimit); 00392 continue; 00393 } 00394 // To print in hexadecimal format (lower case) 00395 if (*format == 'x' ) 00396 { 00397 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a', printlimit); 00398 continue; 00399 } 00400 // To print in hexadecimal format (upper case) 00401 if (*format == 'X') 00402 { 00403 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'A', printlimit); 00404 continue; 00405 } 00406 // To print unsigned integer 00407 if (*format == 'u') 00408 { 00409 pc += printi (out, va_arg( args, int ), 10, 0, width, pad, 'a', printlimit); 00410 continue; 00411 } 00412 // To print a character 00413 if (*format == 'c') 00414 { 00415 // char are converted to int then pushed on the stack 00416 scr[0] = (char) va_arg( args, int ); 00417 scr[1] = '\0'; 00418 pc += prints (out, scr, width, pad, printlimit, FALSE); 00419 continue; 00420 } 00421 // To print a fixed-point number 00422 if (*format == 'f') 00423 { 00424 switch(size) 00425 { 00426 // a 16-bit fixed-point data 00427 case 16: 00428 pc += dsp_sprint(out, 16, DSP16_QA, va_arg(args, int)); 00429 continue; 00430 // a 32-bit fixed-point data 00431 case 32: 00432 pc += dsp_sprint(out, 32, DSP32_QA, va_arg(args, int)); 00433 continue; 00434 } 00435 } 00436 } 00437 else 00438 { 00439 // Print the character 00440 out: 00441 printchar (out, *format); 00442 ++pc; 00443 } 00444 } 00445 00446 if (out) 00447 **out = '\0'; 00448 va_end( args ); 00449 return pc; 00450 }
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 275 of file dsp_sprintf.c.
References PAD_ZERO, PRINT_BUF_LEN, printchar(), prints(), and TRUE.
Referenced by print().
00276 { 00277 char print_buf[PRINT_BUF_LEN]; 00278 register char *s; 00279 register int t, neg = 0, pc = 0; 00280 register unsigned int u = i; 00281 00282 // If the number equals to 0 00283 if (i == 0) 00284 { 00285 print_buf[0] = '0'; 00286 print_buf[1] = '\0'; 00287 return prints(out, print_buf, width, pad, printlimit, TRUE); 00288 } 00289 00290 // in case the result has to be displayed as a negative number 00291 if (sg && b == 10 && i < 0) 00292 { 00293 neg = 1; 00294 u = -i; 00295 } 00296 00297 s = print_buf + PRINT_BUF_LEN-1; 00298 *s = '\0'; 00299 00300 // Print the number with the specified base 00301 while (u) 00302 { 00303 t = u % b; 00304 if( t >= 10 ) 00305 t += letbase - '0' - 10; 00306 *--s = t + '0'; 00307 u /= b; 00308 } 00309 00310 // Negate the result 00311 if (neg) 00312 { 00313 if(width && (pad & PAD_ZERO)) 00314 { 00315 printchar (out, '-'); 00316 ++pc; 00317 --width; 00318 } 00319 else 00320 *--s = '-'; 00321 } 00322 00323 return pc + prints(out, s, width, pad, printlimit, TRUE); 00324 }
static int prints | ( | char ** | out, | |
const char * | string, | |||
int | width, | |||
int | pad, | |||
int | printlimit, | |||
Bool | IsNumber | |||
) | [static] |
Definition at line 201 of file dsp_sprintf.c.
References FALSE, PAD_RIGHT, PAD_ZERO, printchar(), and TRUE.
Referenced by print(), and printi().
00202 { 00203 register int pc = 0, padchar = ' '; 00204 int i, len; 00205 register const char *ptr; 00206 00207 if (width > 0) 00208 { 00209 register int len = 0; 00210 for (ptr = string; *ptr; ++ptr) 00211 ++len; 00212 if (len >= width) 00213 width = 0; 00214 else 00215 width -= len; 00216 if (pad & PAD_ZERO) 00217 padchar = '0'; 00218 } 00219 if (!(pad & PAD_RIGHT)) 00220 { 00221 for(; width > 0; --width) 00222 { 00223 printchar (out, padchar); 00224 ++pc; 00225 } 00226 } 00227 if (FALSE == IsNumber) 00228 { 00229 // The string to print is not the result of a number conversion to ascii. 00230 // For a string, printlimit is the max number of characters to display. 00231 for ( ; printlimit && *string ; ++string, --printlimit) 00232 { 00233 printchar (out, *string); 00234 ++pc; 00235 } 00236 } 00237 if (TRUE == IsNumber) 00238 { 00239 // The string to print represents an integer number. 00240 // In this case, printlimit is the min number of digits to print. 00241 // If the length of the number to print is less than the min nb of i 00242 // digits to display, we add 0 before printing the number. 00243 len = strlen(string); 00244 if (len < printlimit) 00245 { 00246 i = printlimit - len; 00247 for(; i; i--) 00248 { 00249 printchar (out, '0'); 00250 ++pc; 00251 } 00252 } 00253 } 00254 // Else: The string to print is not the result of a number conversion to ascii. 00255 // For a string, printlimit is the max number of characters to display. 00256 for(; printlimit && *string ; ++string, --printlimit) 00257 { 00258 printchar (out, *string); 00259 ++pc; 00260 } 00261 00262 for(; width > 0; --width) 00263 { 00264 printchar (out, padchar); 00265 ++pc; 00266 } 00267 00268 return pc; 00269 }