| 1 | 
< | 
#include "StringUtils.hpp" | 
| 1 | 
> | 
#include "utils/StringUtils.hpp" | 
| 2 | 
  | 
 | 
| 3 | 
+ | 
using namespace std; | 
| 4 | 
+ | 
using namespace oopse; | 
| 5 | 
+ | 
 | 
| 6 | 
  | 
string UpperCase(const string& S) { | 
| 7 | 
  | 
  string uc = S; | 
| 8 | 
  | 
  unsigned int n = uc.size(); | 
| 22 | 
  | 
  } | 
| 23 | 
  | 
  return lc; | 
| 24 | 
  | 
} | 
| 25 | 
+ | 
 | 
| 26 | 
+ | 
int findBegin(istream theStream, char* startText ){ | 
| 27 | 
+ | 
  const int MAXLEN = 1024; | 
| 28 | 
+ | 
  char readLine[MAXLEN];    | 
| 29 | 
+ | 
  int foundText = 0; | 
| 30 | 
+ | 
  int lineNum; | 
| 31 | 
+ | 
  char* the_token; | 
| 32 | 
+ | 
  char* eof_test; | 
| 33 | 
+ | 
 | 
| 34 | 
+ | 
  // rewind the stream | 
| 35 | 
+ | 
  theStream.seekg (0, ios::beg); | 
| 36 | 
+ | 
  lineNum = 0; | 
| 37 | 
+ | 
   | 
| 38 | 
+ | 
  if (!theStream.eof()) { | 
| 39 | 
+ | 
    theStream.getline(readLine, MAXLEN); | 
| 40 | 
+ | 
    lineNum++; | 
| 41 | 
+ | 
  } else { | 
| 42 | 
+ | 
    printf( "Error fast forwarding stream: stream is empty.\n"); | 
| 43 | 
+ | 
    return -1; | 
| 44 | 
+ | 
  } | 
| 45 | 
+ | 
   | 
| 46 | 
+ | 
  while ( !foundText ) { | 
| 47 | 
+ | 
     | 
| 48 | 
+ | 
    if (theStream.eof()) { | 
| 49 | 
+ | 
      printf("Error fast forwarding stream at line %d: " | 
| 50 | 
+ | 
             "stream ended unexpectedly.\n", lineNum); | 
| 51 | 
+ | 
      return -1; | 
| 52 | 
+ | 
    } | 
| 53 | 
+ | 
 | 
| 54 | 
+ | 
    the_token = strtok( readLine, " ,;\t" ); | 
| 55 | 
+ | 
 | 
| 56 | 
+ | 
    if (!strcasecmp("begin", the_token)) { | 
| 57 | 
+ | 
      the_token = TrimSpaces(strtok( NULL, " ,;\t" )); | 
| 58 | 
+ | 
      foundText = !strcasecmp( startText, the_token ); | 
| 59 | 
+ | 
    } | 
| 60 | 
+ | 
 | 
| 61 | 
+ | 
    if (!foundText) { | 
| 62 | 
+ | 
      if (!theStream.eof()) { | 
| 63 | 
+ | 
        theStream.getline(readLine, MAXLEN); | 
| 64 | 
+ | 
        lineNum++; | 
| 65 | 
+ | 
      } else { | 
| 66 | 
+ | 
        printf( "Error fast forwarding stream at line %d: " | 
| 67 | 
+ | 
                "stream ended unexpectedly.\n", lineNum); | 
| 68 | 
+ | 
        return -1; | 
| 69 | 
+ | 
      } | 
| 70 | 
+ | 
    } | 
| 71 | 
+ | 
  } | 
| 72 | 
+ | 
  return lineNum; | 
| 73 | 
+ | 
} | 
| 74 | 
+ | 
 | 
| 75 | 
+ | 
int countTokens(char *line, char *delimiters) { | 
| 76 | 
+ | 
  /* PURPOSE: RETURN A COUNT OF THE NUMBER OF TOKENS ON THE LINE. */ | 
| 77 | 
+ | 
   | 
| 78 | 
+ | 
  char *working_line;   /* WORKING COPY OF LINE. */ | 
| 79 | 
+ | 
  int ntokens;          /* NUMBER OF TOKENS FOUND IN LINE. */ | 
| 80 | 
+ | 
  char *strtok_ptr;     /* POINTER FOR STRTOK. */ | 
| 81 | 
+ | 
 | 
| 82 | 
+ | 
  strtok_ptr= working_line= strdup(line); | 
| 83 | 
+ | 
 | 
| 84 | 
+ | 
  ntokens=0; | 
| 85 | 
+ | 
  while (strtok(strtok_ptr,delimiters)!=NULL) | 
| 86 | 
+ | 
    { | 
| 87 | 
+ | 
      ntokens++; | 
| 88 | 
+ | 
      strtok_ptr=NULL; | 
| 89 | 
+ | 
    } | 
| 90 | 
+ | 
 | 
| 91 | 
+ | 
  free(working_line); | 
| 92 | 
+ | 
  return(ntokens); | 
| 93 | 
+ | 
} | 
| 94 | 
+ | 
 | 
| 95 | 
+ | 
char* TrimSpaces(char *str) { | 
| 96 | 
+ | 
  size_t len; | 
| 97 | 
+ | 
  char *right, *left; | 
| 98 | 
+ | 
   | 
| 99 | 
+ | 
  if (strlen(str) == 0) return(str); | 
| 100 | 
+ | 
   | 
| 101 | 
+ | 
  /* Trim whitespace from left side */ | 
| 102 | 
+ | 
  for (left = str; isspace(*left); left++); | 
| 103 | 
+ | 
   | 
| 104 | 
+ | 
  /* Trim whitespace from right side */ | 
| 105 | 
+ | 
  if ((len = strlen(left))) | 
| 106 | 
+ | 
    { | 
| 107 | 
+ | 
      right = left + (len - 1); | 
| 108 | 
+ | 
       | 
| 109 | 
+ | 
      while (isspace(*right)) | 
| 110 | 
+ | 
        { | 
| 111 | 
+ | 
          *right = '\0'; | 
| 112 | 
+ | 
          right--; | 
| 113 | 
+ | 
        } | 
| 114 | 
+ | 
    } | 
| 115 | 
+ | 
   | 
| 116 | 
+ | 
  /* Only do the str copy if their was spaces to the left */ | 
| 117 | 
+ | 
  if (left != str) | 
| 118 | 
+ | 
    strcpy(str, left); | 
| 119 | 
+ | 
   | 
| 120 | 
+ | 
  return (str); | 
| 121 | 
+ | 
} | 
| 122 | 
+ | 
 | 
| 123 | 
+ | 
int isEndLine(char *line) { | 
| 124 | 
+ | 
  char *working_line; | 
| 125 | 
+ | 
  char *foo; | 
| 126 | 
+ | 
   | 
| 127 | 
+ | 
  working_line = strdup(line); | 
| 128 | 
+ | 
   | 
| 129 | 
+ | 
  foo = strtok(working_line, " ,;\t"); | 
| 130 | 
+ | 
 | 
| 131 | 
+ | 
 if (!strcasecmp(foo, "end")) return 1; | 
| 132 | 
+ | 
  | 
| 133 | 
+ | 
 return 0; | 
| 134 | 
+ | 
} |