-
Notifications
You must be signed in to change notification settings - Fork 0
/
cstdio
94 lines (73 loc) · 1.82 KB
/
cstdio
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
#pragma once
#include <cstdio>
#ifdef _MSC_VER
# pragma warning( disable : 4996 ) // suppress warning: 'sprintf' was declared deprecated
# define _CRT_SECURE_NO_WARNINGS
#endif
#include <cstdarg>
#if 0
// TODO: Include sps/stdio.h from this
namespace sps {
char* convert(unsigned int, int);
void printf(char* format,...) {
char *traverse;
unsigned int i = 0;
char *s = NULL;
float *f = NULL;
double *d = NULL;
va_list arg;
va_start(arg, format);
for(traverse = format; *traverse != '\0'; traverse++) {
while( *traverse != '%' ) {
putchar(*traverse);
traverse++;
}
traverse++;
switch(*traverse) {
case 'c' : i = va_arg(arg,int); //Fetch char argument
putchar(i);
break;
case 'd' : i = va_arg(arg,int); //Fetch Decimal/Integer argument
if(i<0) {
i = -i;
putchar('-');
}
puts(convert(i, 10));
break;
case 'o': i = va_arg(arg,unsigned int); //Fetch Octal representation
puts(convert(i, 8));
break;
case 's': s = va_arg(arg,char *); //Fetch string
puts(s);
break;
case 'x': i = va_arg(arg,unsigned int); //Fetch Hexadecimal representation
puts(convert(i, 16));
break;
case 'a': f = va_arg(arg, float);
puts('N/A');
// Divde hex by 0xFFFFFF + 1 and multiply by 2**p
}
}
fflush(stdout);
va_end(arg);
}
char *convert(unsigned int num, int base) {
static char Representation[]= "0123456789ABCDEF";
static char buffer[50] = {0};
char *ptr;
ptr = &buffer[49];
*ptr = '\0';
do {
*--ptr = Representation[num%base];
num /= base;
}
while(num != 0);
return(ptr);
}
} // namespace sps
#endif
/* Local variables: */
/* indent-tabs-mode: nil */
/* tab-width: 2 */
/* c-basic-offset: 2 */
/* End: */