Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove useless double parentheses in function calls.
[simgrid.git] / win32_test_app / src / TStream.c
1 #include <TStream.h>
2
3 extern const char *TOKENS = "$<>#!p&";
4
5 CRITICAL_SECTION cs;
6
7 const char *__metacommandlist[] = {
8   "set timeout ",
9   "enable output checking",
10   "disable output checking",
11   "enable post output checking",
12   "disable post output checking",
13   "expect exit code ",
14   "export ",
15   "unset ",
16   "create console",
17   "create no console",
18   "enable exit code checking",
19   "disable exit code checking",
20   "command line ",
21   NULL
22 };
23
24 /*
25  * Create a new s_Stream struct and return
26  * a pointer to self
27  */
28
29 Stream_t Stream_new(void)
30 {
31   Stream_t ptr = (Stream_t) calloc(1, sizeof(s_Stream_t));
32
33   if (NULL == ptr) {
34     setErrno(E_STREAM_ALLOCATION_FAILED);
35     Stream_free(ptr);
36   }
37
38   memset(&cs, 0, sizeof(CRITICAL_SECTION));
39   InitializeCriticalSection(&cs);
40
41   ptr->line = NULL;
42   ptr->line_number = 0;
43
44   return ptr;
45 }
46
47 /*
48  * Returns true if the current text line is blank.
49  */
50 bool Stream_lineIsBlank(Stream_t stream)
51 {
52   size_t i = 0;
53   char *p = (char *) stream->line;
54
55   while (p[i] != '\n') {
56     if (!Stream_isBlankChar(p[i]))
57       return false;
58     i++;
59
60   }
61
62   return true;
63 }
64
65 /* 
66  * Return true if the caracter is space or tab.
67  */
68 bool Stream_isBlankChar(char ch)
69 {
70   return ((ch == ' ') || ch == ('\t'));
71 }
72
73 /*
74  * Return E_SUCCESS if the file is valid. 
75  * Otherwise the fuction returns E_INVALID_FILE.
76  */
77 errno_t Stream_isValidFile(const char *file_name)
78 {
79   WIN32_FIND_DATA wfd = { 0 };
80   HANDLE hFile = FindFirstFile(file_name, &wfd);
81
82   if (INVALID_HANDLE_VALUE == hFile)
83     return E_FILE_NOT_FOUND;
84
85   FindClose(hFile);
86   return E_SUCCESS;
87 }
88
89 /* 
90  * Return E_SUCCESS is the open file operation succeeded.
91  * Otherwise the functions returns E_OPEN_FILE_FAILED.
92  */
93 errno_t Stream_openFile(Stream_t ptr, const char *file_name)
94 {
95   ptr->file = fopen(file_name, "r");
96
97   if (NULL == ptr->file) {
98     setErrno(E_OPEN_FILE_FAILED);
99     return getErrno();
100   }
101
102   return E_SUCCESS;
103 }
104
105 /*
106  * This function reads an entire line, storing 
107  * the address of the buffer containing the  text into  
108  * *dest. 
109  */
110 ssize_t Stream_getLine(Stream_t stream)
111 {
112   size_t capacity_available;    /* capacity available in the buffer                             */
113   char *pos;                    /* read operation position                                                      */
114   ssize_t size;                 /* the size of the text line (minus the 0 terminal      */
115   static size_t len = 0;
116   register int ch;              /* the current character                                                        */
117   FILE *file = stream->file;
118
119   if (NULL == file) {
120     setErrno(E_INVALID_FILE_Stream);
121     return -1;
122   }
123
124   if (NULL == stream->line) {
125     len = DEFAULT_ALLOC_SIZE;
126     stream->line = (char *) calloc(1, len);
127
128     if (NULL == stream->line) {
129       setErrno(E_STREAM_LINE_ALLOCATION_FAILED);
130       return -1;
131     }
132   } else {
133     memset(stream->line, 0, len);
134   }
135
136   capacity_available = len;
137   pos = stream->line;
138
139   while (true) {
140     ch = getc(file);
141
142     /* un byte for the next char and one byte for the zero terminal. */
143     if (capacity_available < 2) {
144       if (len > DEFAULT_ALLOC_SIZE)
145         len = len << 1;
146       else
147         len += DEFAULT_ALLOC_SIZE;
148
149       capacity_available = stream->line + len - pos;
150       stream->line = realloc(stream->line, len);
151
152       if (NULL == stream->line) {
153         setErrno(E_STREAM_LINE_REALLOCATION_FAILED);
154         return -1;
155       }
156
157       pos = stream->line + len - capacity_available;
158     }
159
160     if (ferror(file)) {
161       /* file error exit on error */
162       setErrno(E_STREAM_ERROR);
163       return -1;
164     }
165
166     if ((EOF == ch)) {
167       /* Empty file */
168       if (!strlen(stream->line) && !stream->line_number) {
169         setErrno(E_STREAM_EMPTY);
170         return -1;
171       }
172       /* end of file */
173       else if (!strlen(stream->line) && stream->line_number) {
174         return -1;
175       }
176
177       break;
178     }
179
180     *pos++ = ch;
181     capacity_available--;
182
183     /* we have a line, exit loop */
184     if (ch == '\n')
185       break;
186   }
187
188   /* append the zero terminal */
189
190   *pos = '\0';
191   size = pos - stream->line;
192
193   stream->line_number++;
194
195   /* size of texte line without zero terminal */
196   return size;
197 }
198
199
200 /* 
201  * Free a s_Stream.
202  */
203
204 void Stream_free(Stream_t ptr)
205 {
206   if (NULL == ptr)
207     return;
208
209   if ((NULL != ptr->file) && (stdin != ptr->file))
210     fclose(ptr->file);
211
212   if (NULL != ptr->line)
213     free(ptr->line);
214
215   DeleteCriticalSection(&cs);
216
217   free(ptr);
218 }
219
220 /*
221  * Return true if the current line is a comment.
222  * Otherwise the functions returns false.
223  */
224 bool Stream_lineIsComment(Stream_t stream)
225 {
226   return stream->line[0] == '#';
227 }
228
229 /* Return true if the current line contains a invalide token.
230  * Otherwise, the function returns false.
231  */
232 bool Stream_lineContainsInvalidToken(Stream_t stream)
233 {
234   if (strchr(TOKENS, stream->line[0]) == NULL) {
235     Stream_printLine(stream, invalid_token_line_type);
236     setErrno(E_INVALID_TOKEN);
237     return true;
238   }
239
240   return false;
241 }
242
243 /*
244  * Return true if the text line is a meta command.
245  * Otherwise, the functions returns false.
246  */
247 bool Stream_lineIsMetacommand(Stream_t stream)
248 {
249   return stream->line[0] == '!';
250 }
251
252 /* Retun true if the text line contains a unknown meta command.
253  * Otherwise the function returns false.
254  */
255 bool Stream_lineIsUnknwnMetaCommand(Stream_t stream)
256 {
257   size_t i = 0;
258   while (__metacommandlist[i]) {
259     if (!strncmp
260         (__metacommandlist[i], stream->line + 2,
261          strlen(__metacommandlist[i])))
262       return false;
263     i++;
264   }
265
266   Stream_printLine(stream, unknwn_meta_command_line_type);
267
268   setErrno(E_UNKWN_META_COMMAND);
269   return true;
270 }
271
272 /*
273  * Return true if the text line contains a invalid 
274  * meta command. Otherwise the function returns false.
275  */
276 bool Stream_lineIsInvalidMetaCommand(Stream_t stream)
277 {
278   if (!strncmp("set timeout ", stream->line + 2, strlen("set timeout "))) {
279     return Stream_isInvalidTimeout(stream);
280   } else
281       if (!strncmp
282           ("command line ", stream->line + 2, strlen("command line "))) {
283     Stream_printLine(stream, command_line_line_type);
284   } else
285       if (!strncmp
286           ("enable output checking", stream->line + 2,
287            strlen("enable output checking"))) {
288     Stream_printLine(stream, enable_output_checking_line_type);
289   } else
290       if (!strncmp
291           ("disable output checking", stream->line + 2,
292            strlen("disable output checking"))) {
293     Stream_printLine(stream, disable_output_checking_line_type);
294   } else
295       if (!strncmp
296           ("enable post output checking", stream->line + 2,
297            strlen("enable post output checking"))) {
298     Stream_printLine(stream, enable_post_output_checking_line_type);
299   } else
300       if (!strncmp
301           ("disable post output checking", stream->line + 2,
302            strlen("disable post output checking"))) {
303     Stream_printLine(stream, disable_post_output_checking_line_type);
304   } else
305       if (!strncmp
306           ("expect exit code ", stream->line + 2,
307            strlen("expect exit code "))) {
308     return Stream_isInvalidExpectedCode(stream);
309   } else if (!strncmp("export ", stream->line + 2, strlen("export "))) {
310     return Stream_isInvalidExport(stream);
311   } else if (!strncmp("unset ", stream->line + 2, strlen("unset "))) {
312     return Stream_isInvalidUnset(stream);
313   } else
314       if (!strncmp
315           ("create console", stream->line + 2, strlen("create console"))) {
316     Stream_printLine(stream, create_console_line_type);
317   } else
318       if (!strncmp
319           ("create no console", stream->line + 2,
320            strlen("create no console"))) {
321     Stream_printLine(stream, create_no_console_line_type);
322   } else
323       if (!strncmp
324           ("enable exit code checking", stream->line + 2,
325            strlen("enable exit code checking"))) {
326     Stream_printLine(stream, enable_exit_code_checking_line_type);
327   } else
328       if (!strncmp
329           ("disable exit code checking", stream->line + 2,
330            strlen("disaable exit code checking"))) {
331     Stream_printLine(stream, disable_exit_code_checking_line_type);
332   } else {
333     return true;
334   }
335
336   return false;
337
338 }
339
340
341
342 /*
343  * Print the file line.
344  */
345 void Stream_printLine(Stream_t stream, line_type_t line_type)
346 {
347   char *__date = NULL;
348   __date = (char *) calloc(1, 30);
349
350   __time(__date);
351
352
353   Stream_lock(stream);
354
355   switch (line_type) {
356 #ifdef __VERBOSE
357   case comment_line_type:
358
359     if (*(stream->line + 2) != '\0')
360       printf("%s   <COMMENT                     >  %3d %s", __date,
361              stream->line_number, stream->line + 2);
362     else
363       /* empty comment */
364       printf("%s   <COMMENT                     >  %3d %s", __date,
365              stream->line_number, " \n");
366     break;
367
368   case timeout_value_line_type:
369     printf("%s   <TIMEOUT VALUE IS NOW        >  %3d %s", __date,
370            stream->line_number, stream->line + 2 + strlen("set timeout "));
371     break;
372
373   case exit_code_line_type:
374     printf("%s   <EXPECTED EXIT CODE          >  %3d %s", __date,
375            stream->line_number,
376            stream->line + 2 + strlen("expect exit code "));
377     break;
378
379   case export_line_type:
380     printf("%s   <EXPORT                      >  %3d %s", __date,
381            stream->line_number, stream->line + 2);
382     break;
383
384   case unset_line_type:
385     printf("%s   <UNSET                       >  %3d %s", __date,
386            stream->line_number, stream->line + 2);
387     break;
388
389   case enable_output_checking_line_type:
390     printf("%s   <OUTPUT CHECKING ENABLED     >  %3d\n", __date,
391            stream->line_number);
392     break;
393
394   case disable_output_checking_line_type:
395     printf("%s   <OUTPUT CHECKING DISABLED    >  %3d\n", __date,
396            stream->line_number);
397     break;
398
399   case enable_post_output_checking_line_type:
400     printf("%s   <POST OUTPUT CHECKING ENABLED>  %3d\n", __date,
401            stream->line_number);
402     break;
403
404   case disable_post_output_checking_line_type:
405     printf("%s   <POST OUTPUT CHECKING DISABLED>  %3d\n", __date,
406            stream->line_number);
407     break;
408
409   case create_console_line_type:
410     printf("%s   <CREATE CONSOLE SELECTED     >  %3d\n", __date,
411            stream->line_number);
412     break;
413
414   case create_no_console_line_type:
415     printf("%s   <CREATE NO CONSOLE SELECTED  >  %3d\n", __date,
416            stream->line_number);
417     break;
418
419   case enable_exit_code_checking_line_type:
420     printf("%s   <EXIT CODE CHECKING ENABLED  >  %3d\n", __date,
421            stream->line_number);
422     break;
423
424   case disable_exit_code_checking_line_type:
425     printf("%s   <EXIT CODE CHECKING DISABLED >  %3d\n", __date,
426            stream->line_number);
427     break;
428
429   case change_directory_line_type:
430     printf("%s   <DIRECTORY IS NOW            >  %3d %s\n", __date,
431            stream->line_number, stream->line + 5);
432     break;
433
434   case command_line_line_type:
435     printf("%s   <COMMAND LINE                >  %3d %s", __date,
436            stream->line_number, stream->line + 2);
437     break;
438
439 #endif                          /* #ifdef __VERBOSE */
440
441   case invalid_token_line_type:
442     printf("%s   <INVALIDE TOKEN              >  %3d %s", __date,
443            stream->line_number, stream->line);
444     break;
445
446   case unknwn_meta_command_line_type:
447     printf("%s   <UNKNOWN META COMMAND        >  %3d %s", __date,
448            stream->line_number, stream->line);
449     break;
450
451   case invalid_timeout_value_line_type:
452     printf("%s   <INVALID TIMEOUT VALUE       >  %3d %s", __date,
453            stream->line_number, stream->line);
454     break;
455
456   case invalid_exit_code_line_type:
457     printf("%s   <INVALID EXIT CODE           >  %3d %s", __date,
458            stream->line_number, stream->line);
459     break;
460
461   case invalid_export_line_type:
462     printf("%s   <INVALID EXPORT              >  %3d %s", __date,
463            stream->line_number, stream->line);
464     break;
465
466   case invalid_unset_line_type:
467     printf("%s   <INVALID UNSET               >  %3d %s", __date,
468            stream->line_number, stream->line);
469     break;
470
471   case export_failed_line_type:
472     printf("%s   <EXPORT FAILED               >  %3d %s", __date,
473            stream->line_number, stream->line);
474     break;
475
476   case unset_failed_line_type:
477     printf("%s   <UNSET FAILED                >  %3d %s", __date,
478            stream->line_number, stream->line);
479     break;
480
481     /* default:
482        ASSERT(false);
483      */
484   }
485
486   if (__date)
487     free(__date);
488
489   Stream_unlock(stream);
490 }
491
492
493 /*
494  * Returns true if the timeout value is invalid.
495  * Otherwise the function returns false.
496  */
497 bool Stream_isInvalidTimeout(Stream_t stream)
498 {
499   size_t i = 0;
500   char *p = stream->line + 2 + strlen("set timeout ");
501
502   while (p[i] != '\n') {
503     if (!isdigit(p[i])) {
504       setErrno(E_INVALID_TIMEOUT_VALUE);
505       Stream_printLine(stream, invalid_timeout_value_line_type);
506       return true;
507     }
508
509     i++;
510   }
511
512   Stream_printLine(stream, timeout_value_line_type);
513   return false;
514 }
515
516
517 /*
518  * Returns true if the expected code value is invalid.
519  * Otherwise the function returns false.
520  */
521 bool Stream_isInvalidExpectedCode(Stream_t stream)
522 {
523   size_t i = 0;
524   char *p = stream->line + 2 + strlen("expect exit code ");
525
526   while (p[i] != '\n') {
527     if (!isdigit(p[i])) {
528       setErrno(E_INVALID_EXIT_CODE_VALUE);
529       Stream_printLine(stream, invalid_exit_code_line_type);
530       return true;
531     }
532     i++;
533   }
534
535   Stream_printLine(stream, exit_code_line_type);
536   return false;
537 }
538
539
540 /*
541  * Returns true if the export is invalid.
542  * Otherwise the function returns false.
543  */
544 bool Stream_isInvalidExport(Stream_t stream)
545 {
546   /* todo trim */
547   const char *ptr = strchr(stream->line, '=');
548
549   if (ptr && (*(++ptr) != '\n')) {
550     Stream_printLine(stream, export_line_type);
551     return false;
552   }
553
554   setErrno(E_INVALID_EXPORT);
555   Stream_printLine(stream, invalid_export_line_type);
556   return true;
557 }
558
559 /*
560  * Returns true if the unset is invalid.
561  * Otherwise the function returns false.
562  */
563 bool Stream_isInvalidUnset(Stream_t stream)
564 {
565   /* todo trim */
566   const char *ptr = strchr(stream->line, ' ');
567
568   if ((*(++ptr) != '\n')) {
569     Stream_printLine(stream, unset_line_type);
570     return false;
571   }
572
573   setErrno(E_INVALID_UNSET);
574   Stream_printLine(stream, invalid_unset_line_type);
575
576
577   return true;
578 }
579
580
581 /* 
582  * Return true if the stream line contains a 
583  * expected child output. Otherwhise the function
584  * returns false.
585  */
586
587 bool Stream_lineIsExpectedChildOutput(Stream_t stream)
588 {
589   return stream->line[0] == '>';
590 }
591
592 /* 
593  * Return true if the stream line contains a 
594  * child input. Otherwhise the function
595  * returns false.
596  */
597 bool Stream_lineIsChildInput(Stream_t stream)
598 {
599   return stream->line[0] == '<';
600 }
601
602
603
604 /*
605  * Return true, if the stream line containts a
606  * synchrone test case. otherwise the function
607  * returns false.
608  */
609 bool Stream_lineIsSyncTestCase(Stream_t stream)
610 {
611   return stream->line[0] == '$';
612 }
613
614 bool Stream_lineIsAsyncTestCase(Stream_t stream)
615 {
616   return stream->line[0] == '&';
617 }
618
619 bool Stream_lineIsChangeDir(Stream_t stream)
620 {
621   return ((stream->line[0] == '$')
622           && (!strncmp(stream->line + 2, "cd ", strlen("cd "))));
623 }
624
625 void Stream_lock(Stream_t ptr)
626 {
627   EnterCriticalSection(&cs);
628 }
629
630 void Stream_unlock(Stream_t ptr)
631 {
632   LeaveCriticalSection(&cs);
633 }