File: generic_printf.h

package info (click to toggle)
tinyos 2.1.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 47,476 kB
  • ctags: 36,607
  • sloc: ansic: 63,646; cpp: 14,974; java: 10,358; python: 5,215; makefile: 1,724; sh: 902; asm: 597; xml: 392; perl: 74; awk: 46
file content (482 lines) | stat: -rw-r--r-- 8,477 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/****************************************************************
  KPIT Cummins Infosystems Ltd, Pune, India. 1-April-2006.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

 *****************************************************************/

/*
   Written By:
   Shrirang Khishti <shrirangk@kpitcummins.com>.

   This is a smaller version of printf
   Positive points about this function
   1. Reduces code size considerably ,very useful in embedded applications
   2. No malloc calls are used
   3. Supports almost all the functionalities of GNU std printf routine.
   4. If user dont want float_support in this customized printf
   just undef macro float_support
 */

#ifndef  __M16C62P_PRINTF_H__
#define __M16C62P_PRINTF_H__
#include <stdarg.h>
#include <string.h>

#define printf _printf
extern int lowlevel_putc(int c); 

int left_val,right_val;

#define condition *format!='f'&&*format!='d'&&*format!='c'&&*format!='s'&&*format!='l'&&*format!='u'&&*format!='\0'&&*format!=' '&&*format!='i'&&*format!='x'&&*format!='X'&&*format!='o'&&*format!='%'&&*format!='p'

#define float_support

long temp_arr[]={100000,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000};


/**
 * @fn
 * @brief 
 *
 * @param c
 */
int
_putchar(int c)
{     
  /* Convert CR to CR/LF */
  if (c == '\n')
    lowlevel_putc('\r');
  lowlevel_putc(c);

  return c;
}


/**
 * @fn void _puts(const char *tempStr)
 * @brief Prints a NULL-erminated string on UART 1
 *
 * @param s The string to output
 *
 */
int
_puts(const char *s)
{
  while( *s != '\0' )
    _putchar(*s++);

  return 0;
}


/**
 * @fn void strrev(char *str)
 * @brief Reverses a string
 *
 * @param str The string to reverse
 */
void
strrev(char *str)
{
  char *temp, c;
  int len=strlen(str) ;
  temp = str + len -1;

  while(str < temp ) {

    c = *str;
    *str = *temp;

    *temp = c;
    str++;
    temp--;
  }
}


static void print_hex_oct( long int temp_var,int _div,int corr_factor,int ret_val,int sign,int *cntr_val)
{
  unsigned long int i1,temp=temp_var;
  int cntr=0,neg_flag=0;
  char s1[40];

  if(sign==1&&temp_var<0)
  {
    temp=-temp_var;
    neg_flag=1;
  }
  if(temp==0)
    s1[cntr++]='0';
  while(temp>0)
  {
    i1=temp%_div;
    temp=temp/_div;
    if(i1<=9)
      s1[cntr]=i1+'0';
    else
      s1[cntr]=i1+corr_factor-9;
    cntr++;
  }

  while((left_val-(right_val>cntr?right_val:cntr+neg_flag))>0)
  {
    _putchar(' ');
    left_val--;
		(*cntr_val)++;
	}

	while(right_val-cntr>0)
	{
		s1[cntr++]='0';
	}

	if(neg_flag==1)
	s1[cntr++]='-';

	s1[cntr]='\0';
	strrev(s1);
	_puts(s1);
	(*cntr_val)+=strlen(s1);
}


#ifdef float_support
static void float_print(long double f1,long double f2,int multi,int *cntr_val)
{
	int temp,cntr=0,i1,neg_flag=0;
	char s1[10];

	 if(f1<0)
	 {
		 f1=f1*-1;
		 neg_flag=1;
		 f2=f1;
	 }
	 temp=(int)f1;

	 f1=f1-temp;
     f1=f1*multi;

     temp=f1;

     if(temp==0)
    	s1[cntr++]='0';
     while(temp>0)
     {
         i1=temp%10;
         temp=temp/10;
         s1[cntr]=i1+0x30;
         cntr++;
     }

     while(right_val<9&&(right_val -cntr)>0)
	   s1[cntr++]='0';
	   s1[cntr]='.';
	   cntr++;

	   temp=(int)f2;
	 if(temp==0)
		s1[cntr++]='0';
	 while(temp>0)
	 {
	      i1=temp%10;
	      temp=temp/10;
	      s1[cntr]=i1+0x30;
	      cntr++;
	 }

	 while(left_val-- -cntr>0)
		{
			_putchar(' ');
			(*cntr_val)++;
		}
			if(neg_flag==1)
	    s1[cntr++]='-';
	    s1[cntr]='\0';
	    cntr--;
	 	strrev(s1);
    	_puts(s1);
		(*cntr_val)+=strlen(s1);
   		neg_flag=0;
}

#endif // float_support

static int format_val(char *temp,long float_flag,int *cntr_val,int flag)
{
left_val=0;
right_val=0;
	if(*temp=='\0'&&flag==1)
	{
		right_val=3;
		return 0;
	}
	while(*temp!='.'&&*temp!='\0')
	{
		if(*temp<'0'||*temp>'9')
		{
			while(*temp)
			{
				_putchar(*temp++);
				(*cntr_val)++;
			}
			return -1;
		}
		else
		left_val=left_val*10+*temp-'0';
    	 temp++;
	}
    if(*temp)
		temp++;
		else
		return left_val;
	while(*temp)
	{
	if(*temp<'0'||*temp>'9')
	{
	while(*temp)
		{
			_putchar(*temp++);
			(*cntr_val)++;
		}
	return -1;
	}
	else
	right_val=right_val*10+*temp-'0';
     temp++;
	}

return 0;
}


/**
 * @fn int _printf(const char *format, ...)
 * @brief Prints a formatted string on UART1
 *
 * @param format The string
 */
int _printf(const char *format, ...)
{
   int format_cntr=0;
   char temp_str[20];
   int return_flag=0,cntr_val;
   long double f1,f2;
   char *str_temp;
   int *cntr=&cntr_val;

   va_list ap;
   va_start(ap, format);
   *cntr=0;
   while(*format) {
     temp_str[format_cntr]='\0';
     if(*format=='%')
     {
       *format++;
       while(*format==' ')
       {
	 format++;
	 _putchar(' ');
       }
       while(condition)

       {
	 temp_str[format_cntr++]=*format++;
       }
       temp_str[format_cntr]='\0';
       if(*format=='%')
       {
	 _putchar('%');
	 (*cntr)++;
	 format_cntr=0;
	 format++;
	 continue;
       }

       /************** print unsigned ****************/
       else if(*format=='u')
       {
	 return_flag=format_val(temp_str,0,cntr,0);
	 if(return_flag!=-1)

	   print_hex_oct(va_arg(ap,unsigned int),10,0,return_flag,0,cntr);

	 else
	 {
	   _putchar(*format);
	   (*cntr)++;
	 }
	 format++;
	 format_cntr=0;
	 continue;
       }
       /*********** Print Integer Values **************/
       else if(*format=='d'||*format=='i')
       {
	 return_flag=format_val(temp_str,0,cntr,0);

	 if(return_flag!=-1)

	   print_hex_oct(va_arg(ap,int),10,0,return_flag,1,cntr);

	 else
	 {
	   _putchar(*format);
	   (*cntr)++;
	 }
	 format++;
	 format_cntr=0;
	 continue;

       }

       /*********** Print hex,Octal values ******************/
       else if(*format=='x'||*format=='X'||*format=='o'||*format=='p')
       {
	 return_flag=format_val(temp_str,0,cntr,0);
	 if(return_flag!=-1) {

	   if(*format=='x'||*format=='p')
	     print_hex_oct(va_arg(ap,unsigned int),16,0x60,return_flag,0,cntr);
	   else if(*format=='X')
	     print_hex_oct(va_arg(ap,unsigned int),16,0x40,return_flag,0,cntr);
	   else
	     print_hex_oct(va_arg(ap,unsigned int),8,0,return_flag,0,cntr);
	 }
	 else
	 {
	   _putchar(*format);
	   (*cntr)++;
	 }
	 format++;
	 format_cntr=0;
	 continue;
       }

       /************ Character printing ****************88*/
       else if(*format=='c')
       {
	 return_flag=format_val(temp_str,0,cntr,0);
	 if(return_flag!=-1)
	 {
	   while(return_flag-->1)
	   {
	     _putchar(' ');
	     (*cntr)++;
	   }
	   _putchar(va_arg(ap,int));
	   (*cntr)+=2;
	 }
	 else
	 {
	   _putchar(*format);
	   (*cntr)++;
	 }
	 format++;
	 format_cntr=0;
	 continue;
       }

       /*************** Print String *****************/
       else if(*format=='s')
       {
	 return_flag=format_val(temp_str,0,cntr,0);
	 if(return_flag!=-1)
	 {
	   str_temp=va_arg(ap,char*);

	   while((return_flag--  -(int) strlen(str_temp))>0)
	   {
	     _putchar(' ');
	     (*cntr)++;

	   }
	   _puts(str_temp);
	   (*cntr)+=strlen(str_temp);
	 }
	 else
	 {
	   _putchar(*format);
	   (*cntr)++;
	 }
	 format++;
	 format_cntr=0;
	 continue;

       }
       /*************** Print floating point number *****************/
       else if(*format=='f'||(*format=='l'&&*(format+1)=='f'))
       {

	 return_flag=format_val(temp_str,1,cntr,1);
	 if(return_flag!=-1)
	 {
	   if(*format=='l')
	   {
	     f1=va_arg(ap,long double);
	     format+=2;
	   }
	   else
	   {
	     f1=va_arg(ap,double);
	     format++;
	   }
	   f2=f1;
#ifdef float_support
	   right_val++;
	   float_print(f1,f2,temp_arr[right_val%10],cntr);
#endif
	 }
	 else
	 {
	   _putchar(*format++);
	   (*cntr)++;
	 }
	 format_cntr=0;
	 continue;
       }
       else if(*format=='l'&&((*(format+1)=='d')||(*(format+1)=='u')))
       {
	 return_flag=format_val(temp_str,0,cntr,0);

	 if((return_flag=-1)&&(*(format+1)=='d'))
	 {
	   print_hex_oct(va_arg(ap,long int),10,0x00,return_flag,1,cntr);
	 }

	 else if((return_flag=-1)&&(*(format+1)=='u'))
	 {
	   print_hex_oct(va_arg(ap,unsigned long int),10,0x00,return_flag,0,cntr);
	 }

	 else
	 {
	   _putchar(*format);
	   _putchar(*(format+1));
	   (*cntr)+=2;
	 }
	 format+=2;
	 format_cntr=0;
	 continue;
       }
       else
       {
	 _puts(temp_str);
	 format_cntr=0;
	 continue;
       }
     }
     _putchar(*format++);

     (*cntr)++;
   }
   va_end(ap);
   return cntr_val;
}


#endif // __M16C62P_PRINTF_H__