-
Notifications
You must be signed in to change notification settings - Fork 0
/
sstdin.c
492 lines (474 loc) · 20.3 KB
/
sstdin.c
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
483
484
485
486
487
488
489
490
491
492
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <float.h>
#include <ctype.h>
#include "sstdin.h"
/*fonction qui vide le buffer*/
void clearBuffer()
{
int c = 0;
while (c != '\n' && c != EOF)
{
c = getchar();
}
}
/*prend un pointeur de string et récupère l'entrée utilisateur et renvoie 1 si réussi 0 sinon*/
int readStr(char *str, unsigned long size)
{
char *positionEntree = NULL;
if(fgets(str, size, stdin) != NULL) // fgets renvoie null si il y a une erreur, donc si il n'y a pas d'erreur ...
{
positionEntree = strchr(str, '\n'); // ... strchr fonction qui permet de chercher un caractère dans une chaine
if(positionEntree != NULL) // si on a trouver un caractère entrée...
{
*positionEntree = '\0'; //... on évite d'écrire le caractère entrée car fgets le lit et l'affiche
}
else // si on a pas trouver le caractère entrée ...
{
clearBuffer(); // ... c'est qu'il y a trop de texte tapé donc on affiche ce qui est prévu et on vide le buffer
}
return 1;
}
else
{
clearBuffer(); //erreur non prévu du fgets l'utilisateur aura tapé un truc mais problème alors on vide le buffer et on renvoie un code d'erreur
return 0;
}
}
/*transforme une chaine récupérée en Int*/
void readInt(int *nb)
{
char *endStr, str[50];
long l;
while (1) //boucle infini tant que l'utilisateur n'a pas rentré un nombre valide
{
if (readStr (str, 50)) // si la fonction lire renvoie 1 cad ça a bien marché
{
errno = 0; //restera à zero si pas d'erreur
l = strtol(str,&endStr, 10);
if ((errno == ERANGE && (l == LONG_MAX || l == LONG_MIN)) || (errno != 0 && l == 0)){ //ERANGE est récupéré depuis strtol si on sort du domaine d'un long
printf("Wrong entry, please try again\n");} //on boucle
else if (endStr == str){
printf("Wrong entry, please try again\n");} //on boucle si charactères au début de chaine
else if (l > INT_MAX || l < INT_MIN){
printf("Wrong entry, please try again\n");} // si l trop grand ou trop petit pour etre stocké dans un int
else {
*nb = (int)l;
return;
}
}
else{
printf("Error : unable to get the buffer, please try again\n");
}
printf(">>");
}
}
/*transforme une chaine récupérée en un nombre non signé*/
void readNumber(void *nb, Type t, void* quit)
{
char *endStr, str[320];
double l;
printf(">>");
while (1)
{
if (readStr (str, 320))
{
if (quit && tolower(str[0]) == 'q'){ //si l'utilisateur veut arreter une boucle d'entrée de donnée
*(long long*)quit = 1;
return;
}
else if (tolower(str[0]) == 'q'){
printf("You must choose a number\n");
continue;
}
errno = 0;
l = strtod(str, &endStr);
if ((errno == ERANGE && (l == DBL_MAX || l == DBL_MIN)) || (errno != 0 && l == 0)) {
printf("Wrong entry, please try again\n");}
else if (endStr == str) {
printf("Wrong entry, please try again\n");} //on boucle
else{
if (quit)
*(long long*)quit = 0;
switch (t){
case CHAR:
if (l > 127 || l < -128){
printf("Wrong entry, please try again\n");}
else{
*(char*)nb = (char)l;
return;
}
break;
case UCHAR:
if (l > 255 || l < 0){
printf("Wrong entry, please try again\n");}
else{
*(unsigned char*)nb = (unsigned char)l;
return;
}
break;
case SHRT:
if (l > SHRT_MAX || l < SHRT_MIN){
printf("Wrong entry, please try again\n");}
else{
*(short*)nb = (short)l;
return;
}
break;
case INT:
if (l > INT_MAX || l < INT_MIN){
printf("Wrong entry, please try again\n");}
else{
*(int*)nb = (int)l;
return;
}
break;
case LONG:
if (l > LONG_MAX || l < LONG_MIN){
printf("Wrong entry, please try again\n");}
else{
*(long*)nb = (long)l;
return;
}
break;
case LLONG:
if (l > LLONG_MAX || l < LLONG_MIN){
printf("Wrong entry, please try again\n");}
else{
*(long long*)nb = (long long)l;
return;
}
break;
case FLT: //il y a la précision à prendre en compte, les nombres trop petits sont traités
if (l > FLT_MAX || (l < FLT_MIN && l > -FLT_MIN) || l < -FLT_MAX){
printf("Wrong entry, please try again\n");}
else{
*(float*)nb = (float)l;
return;
}
break;
case DBL: //il ne peut y avoir d'erreur puisqu'on utilise strtod
*(double*)nb = (double)l;
return;
break;
case USHRT:
if (l > USHRT_MAX || l < 0){
printf("Wrong entry, please try again\n");}
else{
*(unsigned short*)nb = (unsigned short)l;
return;
}
break;
case UINT:
if (l > UINT_MAX || l < 0){
printf("Wrong entry, please try again\n");}
else{
*(unsigned int*)nb = (unsigned int)l;
return;
}
break;
case ULONG:
if (l > ULONG_MAX || l < 0){
printf("Wrong entry, please try again\n");}
else{
*(unsigned long*)nb = (unsigned long)l;
return;
}
break;
case ULLONG:
if (l > ULLONG_MAX || l < 0){
printf("Wrong entry, please try again\n");}
else{
*(unsigned long long*)nb = (unsigned long long)l;
return;
}
break;
default:
printf("Wrong type, please try again\n");
break;
}
}
}
else{
printf("Error : unable to get the buffer, please try again\n");
}
printf(">>");
}
}
//lit une certaine quantité de nombres
//si limit vaut 0 alors c'est à l'utilisateur d'arreter la saisie en tapant sur q ou Q
//renvoi la quantité de nombres stockée
//le pointeur fourni est alloué dynamiquement. penser à le libérer.
unsigned long readNumbers(void *tab, Type t, unsigned long limit)
{
if (tab == NULL)
{
printf("Error : null pointer. You have to give a dynamicaly allocated pointer\n");
return 0;
}
void* transition = NULL;
unsigned long numberOfCycle = 0;
char *endStr, str[320];
double l;
limit = abs(limit); //si on met -5 on lira quand même 5 nombres
printf(">>");
while (1)
{
if (readStr (str, 320))
{
if (limit == 0 && tolower(str[0]) == 'q'){ //si l'utilisateur veut arreter une boucle d'entrée de donnée
return numberOfCycle;
}
else if (tolower(str[0]) == 'q'){
printf("You must choose a number\n");
continue;
}
errno = 0;
l = strtod(str, &endStr);
if ((errno == ERANGE && (l == DBL_MAX || l == DBL_MIN)) || (errno != 0 && l == 0)) {
printf("Wrong entry, please try again\n");}
else if (endStr == str) {
printf("Wrong entry, please try again\n");} //on boucle
else{
switch (t){
case CHAR:
if (l > 127 || l < -128){
printf("Wrong entry, please try again\n");}
else{
transition = realloc((char*)tab, numberOfCycle+1);
if(transition != NULL){
tab = transition;
*((char*)tab + numberOfCycle++) = (char)l;
}
else{
printf("Error : cannot allocate more space for the pointer\n");
return 0;
}
}
break;
case UCHAR:
if (l > 255 || l < 0){
printf("Wrong entry, please try again\n");}
else{
transition = realloc((unsigned char*)tab, numberOfCycle+1);
if(transition != NULL){
tab = transition;
*((unsigned char*)tab + numberOfCycle++) = (unsigned char)l;
}
else{
printf("Error : cannot allocate more space for the pointer\n");
return 0;
}
}
break;
case SHRT:
if (l > SHRT_MAX || l < SHRT_MIN){
printf("Wrong entry, please try again\n");}
else{
transition = realloc((short*)tab, numberOfCycle+1);
if(transition != NULL){
tab = transition;
*((short*)tab + numberOfCycle++) = (short)l;
}
else{
printf("Error : cannot allocate more space for the pointer\n");
return 0;
}
}
break;
case INT:
if (l > INT_MAX || l < INT_MIN){
printf("Wrong entry, please try again\n");}
else{
transition = realloc((int*)tab, numberOfCycle+1);
if(transition != NULL){
tab = transition;
*((int*)tab + numberOfCycle++) = (int)l;
}
else{
printf("Error : cannot allocate more space for the pointer\n");
return 0;
}
}
break;
case LONG:
if (l > LONG_MAX || l < LONG_MIN){
printf("Wrong entry, please try again\n");}
else{
transition = realloc((long*)tab, numberOfCycle+1);
if(transition != NULL){
tab = transition;
*((long*)tab + numberOfCycle++) = (long)l;
}
else{
printf("Error : cannot allocate more space for the pointer\n");
return 0;
}
}
break;
case LLONG:
if (l > LLONG_MAX || l < LLONG_MIN){
printf("Wrong entry, please try again\n");}
else{
transition = realloc((long long*)tab, numberOfCycle+1);
if(transition != NULL){
tab = transition;
*((long long*)tab + numberOfCycle++) = (long long)l;
}
else{
printf("Error : cannot allocate more space for the pointer\n");
return 0;
}
}
break;
case FLT: //il y a la précision à prendre en compte, les nombres trop petits ne sont pas traités
if (l > FLT_MAX || (l < FLT_MIN && l > -FLT_MIN) || l < -FLT_MAX){
printf("Wrong entry, please try again\n");}
else{
transition = realloc((float*)tab, numberOfCycle+1);
if(transition != NULL){
tab = transition;
*((float*)tab + numberOfCycle++) = (float)l;
}
else{
printf("Error : cannot allocate more space for the pointer\n");
return 0;
}
}
break;
case DBL: //il ne peut y avoir d'erreur puisqu'on utilise strtod
transition = realloc((double*)tab, numberOfCycle+1);
if(transition != NULL){
tab = transition;
*((double*)tab + numberOfCycle++) = (double)l;
}
else{
printf("Error : cannot allocate more space for the pointer\n");
return 0;
}
break;
case USHRT:
if (l > USHRT_MAX || l < 0){
printf("Wrong entry, please try again\n");}
else{
transition = realloc((unsigned short*)tab, numberOfCycle+1);
if(transition != NULL){
tab = transition;
*((unsigned short*)tab + numberOfCycle++) = (unsigned short)l;
}
else{
printf("Error : cannot allocate more space for the pointer\n");
return 0;
}
}
break;
case UINT:
if (l > UINT_MAX || l < 0){
printf("Wrong entry, please try again\n");}
else{
transition = realloc((unsigned int*)tab, numberOfCycle+1);
if(transition != NULL){
tab = transition;
*((unsigned int*)tab + numberOfCycle++) = (unsigned int)l;
}
else{
printf("Error : cannot allocate more space for the pointer\n");
return 0;
}
}
break;
case ULONG:
if (l > ULONG_MAX || l < 0){
printf("Wrong entry, please try again\n");}
else{
transition = realloc((unsigned long*)tab, numberOfCycle+1);
if(transition != NULL){
tab = transition;
*((unsigned long*)tab + numberOfCycle++) = (unsigned long)l;
}
else{
printf("Error : cannot allocate more space for the pointer\n");
return 0;
}
}
break;
case ULLONG:
if (l > ULLONG_MAX || l < 0){
printf("Wrong entry, please try again\n");}
else{
transition = realloc((unsigned long long*)tab, numberOfCycle+1);
if(transition != NULL){
tab = transition;
*((unsigned long long*)tab + numberOfCycle++) = (unsigned long long)l;
}
else{
printf("Error : cannot allocate more space for the pointer\n");
return 0;
}
}
break;
default:
printf("Wrong type, please try again\n");
break;
}
if(limit != 0){
if(--limit == 0)
return numberOfCycle;
}
}
}
else{
printf("Error : unable to get the buffer, please try again\n");
}
printf(">>");
}
}
void debugList(void* list, Type t, unsigned long size){
for (unsigned int i = 0; i < size; ++i)
{
switch (t){
case CHAR:
printf("%hhd ", *((char*)list+i));
break;
case UCHAR:
printf("%hhu ", *((unsigned char*)list+i));
break;
case SHRT:
printf("%hd ", *((short*)list+i));
break;
case USHRT:
printf("%hu ", *((unsigned short*)(list+i)));
break;
case INT:
printf("%d ", *((int*)list+i));
break;
case UINT:
printf("%u ", *((unsigned int*)list+i));
break;
case LONG:
printf("%ld ", *((long*)list+i));
break;
case ULONG:
printf("%lu ", *((unsigned long*)list + i));
break;
case LLONG:
printf("%lld ", *((long long*)list + i));
break;
case ULLONG:
printf("%llu ", *((unsigned long long*)list + i));
break;
case FLT:
printf("%.2f ", *((float*)list + i));
break;
case DBL:
printf("%.2lf ", *((double*)list + i));
break;
default:
printf("Wrong type given\n");
}
}
printf("\n");
}