Guest User

scanner

a guest
Apr 22nd, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h> /* for isspace */
  3. #include <string.h> /* for strdup */
  4.  
  5. #include "scanner.h"
  6.  
  7. /* Original Author: J.Lusth
  8. * readInt(FILE *fp)
  9. * - wrapper for fscanf(fp,"%d")
  10. * - returns 0 if end of file, but feof will subsequently return true
  11. * - usage example: int x = readInt(stdin);
  12. * readString(FILE *fp)
  13. * - safe version of fscanf(fp,"%s")
  14. * - returns a malloc'd string
  15. * the caller should free the returned string
  16. * - returns 0 if end of file; feof will subsequently return true
  17. * - usage example: char *x = readToken(stdin);
  18. * allocate(size_t size)
  19. * - wrapper for malloc that will generate an out of memory error
  20. * - usage example: int *x = (int *) allocate(sizeof(int) * count);
  21. * reallocate(void *items,size_t size)
  22. * - wrapper for realloc that will generate an out of memory error
  23. * - usage example: x = (int *) reallocate(x,sizeof(int) * count);
  24. */
  25.  
  26. static void skipWhiteSpace(FILE *);
  27. static void *allocate(size_t);
  28. static void *reallocate(void *,size_t);
  29.  
  30. /********** public functions **********************/
  31.  
  32. int readInt(FILE *fp){
  33. int x,result;
  34. result = fscanf(fp,"%d",&x);
  35. if (result == EOF)
  36. {
  37. return 0;
  38. }
  39. if (result == 0)
  40. {
  41. fprintf(stderr,"SCAN ERROR: attempt to read an integer failed\n");
  42. fprintf(stderr,"offending character was <%c>\n",fgetc(fp));
  43. exit(1);
  44. }
  45. return x;
  46. }
  47.  
  48. /* Safe way of reading in strings from a file, avoids buffer overflows
  49. * starts with an initial buffer of size 80 and reads a character at a time
  50. * if the size of the string goes beyond the size of the buffer, increases
  51. * size of the buffer and continues to read
  52. */
  53. char *readString(FILE *fp)
  54. {
  55. int ch,index;
  56. char *buffer;
  57. int size = 80;
  58.  
  59. skipWhiteSpace(fp);
  60.  
  61. ch = fgetc(fp);
  62. if (ch == EOF) return 0;
  63.  
  64. buffer = allocate(size);
  65.  
  66. index = 0;
  67. while (!isspace(ch))
  68. {
  69. if (ch == EOF) break;
  70. if (index > size - 2)
  71. {
  72. ++size;
  73. buffer = reallocate(buffer,size);
  74. }
  75. buffer[index] = ch;
  76. ++index;
  77. ch = fgetc(fp);
  78. }
  79.  
  80. /* push back the character that got us out of this loop */
  81.  
  82. ungetc(ch,fp);
  83.  
  84. if (index > 0) //there is something in the buffer
  85. clearerr(fp); //so force the read to be good
  86.  
  87. buffer[index] = '\0';
  88.  
  89. return buffer;
  90. }
  91.  
  92. /********** private functions **********************/
  93. //static keyword restricts visibility of function to file
  94.  
  95. static void skipWhiteSpace(FILE *fp)
  96. {
  97. int ch;
  98.  
  99. /* read chars until a non-whitespace character is encountered */
  100.  
  101. while ((ch = fgetc(fp)) != EOF && isspace(ch))
  102. continue;
  103.  
  104. /* a non-space character got us out of the loop, so push it back */
  105.  
  106. ungetc(ch,fp);
  107. }
  108.  
  109. static void *allocate(size_t size)
  110. {
  111. char *s;
  112. s = malloc(size);
  113. if (s == 0)
  114. {
  115. fprintf(stderr,"could not allocate string, out of memory\n");
  116. exit(3);
  117. }
  118.  
  119. return s;
  120. }
  121.  
  122. static void *reallocate(void *s,size_t size)
  123. {
  124. char *t;
  125. t = realloc(s,size);
  126. if (t == 0)
  127. {
  128. fprintf(stderr,"could not reallocate string, out of memory\n");
  129. exit(3);
  130. }
  131.  
  132. return t;
  133. }
Add Comment
Please, Sign In to add comment