Advertisement
Guest User

mystring.c input function

a guest
Mar 28th, 2014
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. MyString_Status mystring_input(MYSTRING hString,
  2.                                FILE * hFile,
  3.                                int bIgnoreLeadingWhiteSpace,
  4.                                int (*fTerminate)(char ch, int * pbDiscardChar)) {
  5.     char ch = '\0';
  6.     int eofCheck = 0;
  7.     int t, discard;
  8.  
  9.     mystring_truncate(hString, 0);
  10.  
  11.     if(hFile == NULL) return MYSTRING_STATUS_ERROR;
  12.     eofCheck = fscanf(hFile, "%c", &ch);
  13.  
  14.     // If bIgnoreWhiteSpace is true, gobble leading whitespace.
  15.     if(bIgnoreLeadingWhiteSpace) {
  16.         while(isspace(ch)) {
  17.             eofCheck = fscanf(hFile, "%c", &ch);
  18.             if(eofCheck == EOF) return MYSTRING_STATUS_ERROR;
  19.         }
  20.     }
  21.  
  22.     // Add all valid characters to the string, overwriting the old string.
  23.     while(eofCheck != EOF) {
  24.         t = fTerminate(ch, &discard);
  25.         if(discard == 0) mystring_push(hString, ch);
  26.         if(t) return MYSTRING_STATUS_SUCCESS;
  27.         eofCheck = fscanf(hFile, "%c", &ch);
  28.     }
  29.  
  30.     if(eofCheck == EOF) return MYSTRING_STATUS_ERROR;
  31.  
  32.     return MYSTRING_STATUS_SUCCESS;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement