-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintf_code.c
More file actions
222 lines (199 loc) · 5.96 KB
/
Copy pathprintf_code.c
File metadata and controls
222 lines (199 loc) · 5.96 KB
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
/**
******************************************************************************
* @file printf_code.c
* @author Basavaraju B V
* @version V1.0.0
* @date 06-Dec-2013
* @brief This file is having the printf() equivalent function!
* This is simplified printf(). this can be expanded for the different
* data types!
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "printf_code.h"
uint32_t gIntegerMask = 0xFF;
/* Function Definations ------------------------------------------------------*/
/**
* @name display_Character()
* @brief this function will print the char
* @param ch - character needs to be printed!
* @note Here instead of putchar() use the USART/UART put function written by urself!
* @retval None
*/
void display_Character(char ch)
{
USART_PutChar(ch);
//putchar(ch);
}
/**
* @name print_String()
* @brief this function will print the string
* @param *str - the pointer to the string needs to be printed!
* @note -
* @retval None
*/
void print_String(const char *str)
{
while(*str != '\0')
{
display_Character(*str);
str++;
}
}
/**
* @name print_Character()
* @brief this function will print the character only!
* @param data - charater needs to be printed!
* @note -
* @retval None
*/
void print_Character(const char data)
{
display_Character(data);
}
/**
* @name print_Integer()
* @brief this function will print the numbers only!
* @param data - number needs to be printed!
* @param length - lenght of number needs to be printed! bacisally Decimal meaning max lenght is 10 decimanls (assuming 32 bit computer)
* @note right now it will be printing decimal numbers from -2147483648 to 2147483647
* needs some modification for hex numbers and unsigned values!
* @retval None
*/
void print_Integer(const int32_t data, int length)
{
char *s;
int i;
uint32_t val = 0;
data = (data & gIntegerMask);
//Initialize the variables!
i = 0;
s = (char *)(calloc((length+1), sizeof(char)));
if(data != 0)
{
if(length == 10)
{
val = (data<0)? (data * -1): data;
while(val != 0 && i<length)
{
s[i] = (val % 10) + '0';
val/=10;
i++;
}
}
else
{
val = data;
while(val != 0 && i<length)
{
//When nibble is >= 0x0A, you have to subtract 10 (or 0x0A) becuase:
//assume nibble is 0x0A, when oyu add char 'a'(decimal 97) its ascii value will be 107(in decimal)
//But you want yo display char 'a' not char 'k'(decimal 107). By subtracting 10 (0x0A) you will get 'a'
s[i] = (val & 0x0F) >=0x0A? ((val & 0x0F) + 'a') - 10 : (val & 0x0F) + '0';
val = val >> 4;
i++;
}
}
}
else
{
s[i] = data + '0';
i++;
}
s[i]='\0';
if(data<0 && length > 8)
{
display_Character('-');
}
i= i - 1; //as i represents the number of digits
while(i >= 0)
{
display_Character(s[i]);
i--;
}
free(s);
}
/**
* @name print()
* @brief this function will behave similar to printf but the fully functional printf, a partial printf function!
* @param *str - pointer to the string which needs to be analysed and printed!
* @param ... - unknown number of arguments!
* @note for printing unsinged numbers, hex values and long values needs modification!
* For integers typecast the number by (uint32_t) or (int32_t) to print the proper value
* @retval None
*/
void print(const char *str, ...)
{
va_list arg_list;
va_start(arg_list, str);
while(*str != '\0')
{
switch(*str)
{
case '%':
str++;
if(*str != '%')
{
switch(*str)
{
case 'd': //print the Integers!
case 'x':
case 'X':
//assuming 10 digits is max for Decimal format(2^32) and 8 in hexadecimal - 32 bit machine!
//Note: These int or uint variable must have postfixed with _t, like int8_t or uint8_t
// Else the print values may be different from what has been passed!
// if _t is used then size is always fixed to those many bits!
print_Integer(va_arg(arg_list, const int32_t), (*str=='d'? 10: 8));
break;
case 'c':
print_Character(va_arg(arg_list, const int));
break;
case 's':
print_String(va_arg(arg_list, const char *));
break;
default:
display_Character(*str);
break;
}
}
else
{
//Here 2 times % symbol is invalid (you cannot use %%)
print("Error Error Error! cannot print becasue format specifier is entered twice!");
}
break;
default:
if(*str == 0x5C) //Comparing with \ character
{
if(*(str + 1) != 0x22) //COmparing with " quotes
display_Character(*str);
}
else
display_Character(*str);
break;
}
str++;
}
va_end(arg_list);
}
/**
* @name main()
* @brief to test the print() function!
* @param None!
* @note -
* @retval None
*/
/*int main()
{
int i;
i = 567638789;
printf("I am trying to duplicate the printf code\n");
print("I am inside a print code where printf is duplicated! - Bug\n");
print("This is for Integer %d\n", i);
print("This is for Character %c\n", 'C');
print("%s", "This is for String test!\n");
print("Trying to find the Hex number: %x", 0xFFFFFFFF);
print("\n%d", -2147483647);
return 0;
}
*/