-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmysnprintf.cpp
164 lines (133 loc) · 5.57 KB
/
mysnprintf.cpp
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
//
// mysnprintf.cpp
// snprintf
//
// Created by Руслан on 5/27/15.
// Copyright (c) 2015 Руслан. All rights reserved.
//
#include "mysnprintf.h"
#define LENGTH_OF_WIDTH 32
#define LENGHT_OF_PRECISION 32
void clearString(char* line, int len);
int length(int num);
int max(int first, int second);
char* mysnprintf(char* res, int countChar, const char* temp, ...){
int currentPosTmp = 0;
int currentPosRes = 0;
int currentNum = 0;
int currentPrecision = 0;
int lentemp = strlen(temp);
char width[LENGTH_OF_WIDTH];
char precision[LENGHT_OF_PRECISION];
va_list ap;
va_start(ap, temp);
while( ( currentPosRes < countChar ) && ( currentPosTmp < lentemp-1 ) ) {
// if next characters is common characters
if ( '%' != *(temp + currentPosTmp) ){
*(res + currentPosRes) = *(temp + currentPosTmp);
currentPosRes++;
currentPosTmp++;
continue;
}
// if two '%' one by one
if ( '%' == *(temp + currentPosTmp + 1) ){
*(res + currentPosRes) = '%';
currentPosRes++;
currentPosTmp += 2;
continue;
}
currentPosTmp++;
clearString(width, LENGTH_OF_WIDTH);
currentNum = 0;
// read width
while( isdigit(*(temp + currentPosTmp)) ){
*(width + currentNum) = *(temp + currentPosTmp);
currentNum++;
currentPosTmp++;
}
//read precision
if('.' == *(temp + currentPosTmp)) {
currentPosTmp++;
while(isdigit(*(temp + currentPosTmp)) ){
*(precision + currentPrecision) = *(temp + currentPosTmp);
currentPrecision++;
currentPosTmp++;
}
}
// insert other part
int precisionLength = atoi(precision), widthLength = atoi(width);
switch( *(temp + currentPosTmp) ){
case 'i': {
case 'd':
int arg = va_arg(ap, int);
if (widthLength == 0) widthLength = length(arg);
if (precisionLength == 0) precisionLength = length(arg);
for(int i=length(arg); i<precisionLength; i++, arg*=10);
for(int i=0; i<widthLength-length(arg); i++, currentPosRes++) {
*(res + currentPosRes) = ' ';
}
for(int i=0, arg_tmp = arg; i<length(arg); i++, currentPosRes++) {
*(res + currentPosRes) = (arg_tmp - (arg_tmp % (int)pow(10, length(arg) - i - 1))) / (int)pow(10, length(arg) - i - 1) + 48;
arg_tmp %= (int)pow(10, length(arg) - i - 1);
}
currentPosRes += length(arg);
break;
}
case 's': {
int length = 0;
char* arg = va_arg(ap, char*);
for(; *(arg+length) != '\0'; length++);
if (widthLength == 0) widthLength = length;
if (precisionLength == 0) precisionLength = length;
for(int i=0; i<widthLength - precisionLength; i++, currentPosRes++, countChar--) {
*(res + currentPosRes) = ' ';
}
for(int i=0; i<precisionLength && i<countChar-1; i++, currentPosRes++) {
*(res + currentPosRes) = *(arg + i);
}
break;
}
case 'c':
if (widthLength == 0) widthLength = 1;
for(int i=0; i < widthLength - 1; i++, currentPosRes++) {
*(res + currentPosRes) = ' ';
}
*(res + currentPosRes) = (char) va_arg(ap, int);
currentPosRes++;
break;
case 'f': {
double arg = va_arg(ap, double), fractpart, intpart;
fractpart = modf(arg , &intpart);
if (precisionLength == 0) precisionLength = 6;
if (widthLength == 0) widthLength = length(intpart) + precisionLength + 1;
for(int i=0; i<widthLength - precisionLength - length(intpart) - 1; i++, currentPosRes++) {
*(res + currentPosRes) = ' ';
}
for(int i=0, intpart_tmp = (int)intpart; i<length(intpart); i++, currentPosRes++) {
*(res + currentPosRes) = (intpart_tmp - (intpart_tmp % (int)pow(10, length(intpart) - i - 1))) / (int)pow(10, length(intpart) - i - 1) + 48;
intpart_tmp %= (int)pow(10, length(intpart) - i - 1);
}
*(res + currentPosRes) = '.';
currentPosRes++;
for(int i=0; i<precisionLength; i++, currentPosRes++) {
*(res + currentPosRes) = ((int)(fractpart * pow(10, i+1))) % 10 + 48;
}
break;
}
default:
fprintf(stderr, "Undefined prefix" );
break;
}
currentPosTmp++;
}
*(res + currentPosRes) = '\0';
return res;
}
int length(int num){ return ( num /= 10 ) ? 1 + length(num) : 1; }
int max(int first, int second){ return (first>second) ? first : second; }
void clearString(char* line, int len){
int i;
for ( i = 0; i < len; ++i){
line[i] = '\0';
}
}