| 1 | /********************************************************************** |
| 2 | |
| 3 | FILENAME: uiuc_parsefile.cpp |
| 4 | |
| 5 | ---------------------------------------------------------------------- |
| 6 | |
| 7 | DESCRIPTION: Reads the input file and stores data in a list |
| 8 | gets tokens from each line of the list |
| 9 | |
| 10 | ---------------------------------------------------------------------- |
| 11 | |
| 12 | STATUS: alpha version |
| 13 | |
| 14 | ---------------------------------------------------------------------- |
| 15 | |
| 16 | REFERENCES: |
| 17 | |
| 18 | ---------------------------------------------------------------------- |
| 19 | |
| 20 | HISTORY: 01/30/2000 (BS) initial release |
| 21 | 09/19/2002 (MSS) appended zeros to lines w/ comments |
| 22 | |
| 23 | ---------------------------------------------------------------------- |
| 24 | |
| 25 | AUTHOR(S): Bipin Sehgal <[email protected]> |
| 26 | Michael Selig |
| 27 | |
| 28 | ---------------------------------------------------------------------- |
| 29 | |
| 30 | VARIABLES: |
| 31 | |
| 32 | ---------------------------------------------------------------------- |
| 33 | |
| 34 | INPUTS: * |
| 35 | |
| 36 | ---------------------------------------------------------------------- |
| 37 | |
| 38 | OUTPUTS: * |
| 39 | |
| 40 | ---------------------------------------------------------------------- |
| 41 | |
| 42 | CALLED BY: * |
| 43 | |
| 44 | ---------------------------------------------------------------------- |
| 45 | |
| 46 | CALLS TO: * |
| 47 | |
| 48 | ---------------------------------------------------------------------- |
| 49 | |
| 50 | COPYRIGHT: (C) 2000 by Michael Selig |
| 51 | |
| 52 | This program is free software; you can redistribute it and/or |
| 53 | modify it under the terms of the GNU General Public License |
| 54 | as published by the Free Software Foundation. |
| 55 | |
| 56 | This program is distributed in the hope that it will be useful, |
| 57 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 58 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 59 | GNU General Public License for more details. |
| 60 | |
| 61 | You should have received a copy of the GNU General Public License |
| 62 | along with this program; if not, write to the Free Software |
| 63 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 64 | |
| 65 | **********************************************************************/ |
| 66 | |
| 67 | |
| 68 | #include "uiuc_parsefile.h" |
| 69 | |
| 70 | using std::string; |
| 71 | |
| 72 | |
| 73 | ParseFile :: ParseFile (const string fileName) |
| 74 | { |
| 75 | file.open(fileName.c_str()); |
| 76 | readFile(); |
| 77 | } |
| 78 | |
| 79 | ParseFile :: ~ParseFile () |
| 80 | { |
| 81 | file.close(); |
| 82 | } |
| 83 | |
| 84 | void ParseFile :: removeComments(string& inputLine) |
| 85 | { |
| 86 | string::size_type pos = inputLine.find_first_of(COMMENT); |
| 87 | |
| 88 | if (pos != inputLine.npos) // a "#" exists in the line |
| 89 | { |
| 90 | if (inputLine.find_first_not_of(DELIMITERS) == pos) |
| 91 | { |
| 92 | inputLine = ""; // Complete line a comment |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | inputLine = inputLine.substr(0,pos); //Truncate the comment from the line |
| 97 | // append zeros to the input line after stripping off the comments |
| 98 | // mss added from Bipin email of 9/3/02 |
| 99 | // inputLine += " 0 0 0 0 0 0"; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | |
| 105 | string ParseFile :: getToken(string inputLine, int tokenNo) |
| 106 | { |
| 107 | string::size_type pos = 0; |
| 108 | string::size_type pos1 = 0; |
| 109 | int tokencounter = 0; |
| 110 | |
| 111 | while (tokencounter < tokenNo) |
| 112 | { |
| 113 | if ( (pos1 == inputLine.npos) || (pos1 == string::npos) || (pos == string::npos) ) |
| 114 | return ""; //return an empty string if tokenNo exceeds the No of tokens in the line |
| 115 | |
| 116 | inputLine = inputLine.substr(pos1 , MAXLINE); |
| 117 | pos = inputLine.find_first_not_of(DELIMITERS); |
| 118 | pos1 = inputLine.find_first_of(DELIMITERS , pos); |
| 119 | tokencounter ++; |
| 120 | } |
| 121 | |
| 122 | if (pos1== string::npos || pos == string::npos) |
| 123 | return ""; |
| 124 | else |
| 125 | return inputLine.substr(pos , pos1-pos); // return the desired token |
| 126 | } |
| 127 | |
| 128 | |
| 129 | void ParseFile :: storeCommands(string inputLine) |
| 130 | { |
| 131 | string::size_type pos; |
| 132 | string::size_type pos1; |
| 133 | // int wordlength; |
| 134 | string line; |
| 135 | |
| 136 | inputLine += " "; // To take care of the case when last character is not a blank |
| 137 | pos = inputLine.find_first_not_of(DELIMITERS); |
| 138 | pos1 = inputLine.find_first_of(DELIMITERS); |
| 139 | |
| 140 | while ((pos != inputLine.npos) && (pos1 != inputLine.npos)) |
| 141 | { |
| 142 | line += inputLine.substr(pos , pos1 - pos)+ " "; |
| 143 | inputLine = inputLine.substr(pos1, inputLine.size()- (pos1 - pos)); |
| 144 | pos = inputLine.find_first_not_of(DELIMITERS); |
| 145 | pos1 = inputLine.find_first_of(DELIMITERS , pos); |
| 146 | } |
| 147 | |
| 148 | line += inputLine; // Add the last word to the line |
| 149 | commands.push_back(line); |
| 150 | } |
| 151 | |
| 152 | // void ParseFile :: readFile() |
| 153 | // { |
| 154 | // string line; |
| 155 | |
| 156 | // while (getline(file , line)) |
| 157 | // { |
| 158 | // removeComments(line); |
| 159 | // if (line.find_first_not_of(DELIMITERS) != line.npos) // strip off blank lines |
| 160 | // { |
| 161 | // line += " 0 0 0 0 0"; |
| 162 | // storeCommands(line); |
| 163 | // } |
| 164 | // } |
| 165 | // } |
| 166 | |
| 167 | void ParseFile :: readFile() |
| 168 | { |
| 169 | string line; |
| 170 | |
| 171 | while (getline(file , line)) |
| 172 | { |
| 173 | removeComments(line); |
| 174 | if (line.find_first_not_of(DELIMITERS) != line.npos) // strip off blank lines |
| 175 | { |
| 176 | line += " "; |
| 177 | // append some zeros, but this is doing something strange! |
| 178 | // line += " 0 0 0 0 0 "; |
| 179 | storeCommands(line); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | stack ParseFile :: getCommands() |
| 184 | { |
| 185 | return commands; |
| 186 | } |
| 187 | |
| 188 | //end uiuc_parsefile.cpp |