[4597] | 1 | /* |
---|
[3941] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Christian Meyer |
---|
[4597] | 13 | co-programmer: Benjamin Grauer |
---|
| 14 | |
---|
| 15 | 2005-06-10: some naming conventions |
---|
[4220] | 16 | */ |
---|
| 17 | |
---|
| 18 | |
---|
[3941] | 19 | /** |
---|
[4836] | 20 | * breaks a string into parts that were initially seperated by comma |
---|
| 21 | * @param string the string to break into substrings |
---|
[4220] | 22 | */ |
---|
[3941] | 23 | |
---|
[4220] | 24 | #include "substring.h" |
---|
| 25 | |
---|
| 26 | #include <string.h> |
---|
[7211] | 27 | #include <cassert> |
---|
[4220] | 28 | |
---|
[7211] | 29 | SubString::SubString(const std::string& string, char splitter) |
---|
[4220] | 30 | { |
---|
[7211] | 31 | char split[2]; |
---|
| 32 | split[0] = splitter; |
---|
| 33 | split[1] = '\0'; |
---|
| 34 | SubString::splitLine(this->strings, this->offsets, |
---|
| 35 | string, split); |
---|
[4220] | 36 | } |
---|
| 37 | |
---|
[5183] | 38 | |
---|
[3941] | 39 | /** |
---|
[5183] | 40 | * Splits a String into a Substring removing all whiteSpaces |
---|
| 41 | * @param string the String to Split |
---|
| 42 | * @param whiteSpaces MUST BE __TRUE__ |
---|
| 43 | * |
---|
| 44 | */ |
---|
[7211] | 45 | SubString::SubString(const std::string& string, bool whiteSpaces) |
---|
[5183] | 46 | { |
---|
[7211] | 47 | SubString::splitLine(this->strings, this->offsets, |
---|
| 48 | string); |
---|
| 49 | } |
---|
[5183] | 50 | |
---|
[7211] | 51 | SubString::SubString(const std::string& string, const std::string& splitters, char escapeChar,char safemode_char, char comment_char) |
---|
| 52 | { |
---|
| 53 | SubString::splitLine(this->strings, this->offsets, |
---|
| 54 | string, splitters, escapeChar, safemode_char); |
---|
| 55 | } |
---|
[5183] | 56 | |
---|
| 57 | |
---|
[7211] | 58 | /** |
---|
| 59 | * splits line into tokens and stores them in ret. Supports delimiters, escape characters, |
---|
| 60 | * ignores special characters between safemode_char and between comment_char and linend '\n'. |
---|
| 61 | * |
---|
| 62 | * @returns SPLIT_LINE_STATE the parser was in when returning |
---|
| 63 | */ |
---|
| 64 | SPLIT_LINE_STATE SubString::splitLine(std::vector<std::string>& ret, std::vector<unsigned int>& offsets, |
---|
| 65 | const std::string& line, const std::string& delimiters, |
---|
| 66 | char escape_char, char safemode_char, char comment_char, |
---|
| 67 | SPLIT_LINE_STATE start_state) |
---|
| 68 | { |
---|
| 69 | SPLIT_LINE_STATE state = start_state; |
---|
[5183] | 70 | unsigned int i = 0; |
---|
[7211] | 71 | std::string token; |
---|
[5183] | 72 | |
---|
[7211] | 73 | if(start_state != SL_NORMAL && ret.size() > 0) |
---|
[5656] | 74 | { |
---|
[7211] | 75 | token = ret[ret.size()-1]; |
---|
| 76 | ret.pop_back(); |
---|
[5656] | 77 | } |
---|
| 78 | |
---|
[7211] | 79 | while(i < line.size()) |
---|
[5656] | 80 | { |
---|
[7211] | 81 | switch(state) |
---|
[5656] | 82 | { |
---|
[7211] | 83 | case SL_NORMAL: |
---|
| 84 | if(line[i] == escape_char) |
---|
[5656] | 85 | { |
---|
[7211] | 86 | state = SL_ESCAPE; |
---|
[5656] | 87 | } |
---|
[7211] | 88 | else if(line[i] == safemode_char) |
---|
| 89 | { |
---|
| 90 | state = SL_SAFEMODE; |
---|
| 91 | } |
---|
| 92 | else if(line[i] == comment_char) |
---|
| 93 | { |
---|
| 94 | /// FINISH |
---|
| 95 | if(token.size() > 0) |
---|
| 96 | { |
---|
| 97 | ret.push_back(token); |
---|
| 98 | offsets.push_back(i); |
---|
| 99 | token.clear(); |
---|
| 100 | } |
---|
| 101 | token += line[i]; // EAT |
---|
| 102 | state = SL_COMMENT; |
---|
| 103 | } |
---|
| 104 | else if(delimiters.find(line[i]) != std::string::npos) |
---|
| 105 | { |
---|
| 106 | // line[i] is a delimiter |
---|
| 107 | /// FINISH |
---|
| 108 | if(token.size() > 0) |
---|
| 109 | { |
---|
| 110 | ret.push_back(token); |
---|
| 111 | offsets.push_back(i); |
---|
| 112 | token.clear(); |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | else |
---|
| 116 | { |
---|
| 117 | token += line[i]; // EAT |
---|
| 118 | } |
---|
| 119 | break; |
---|
| 120 | case SL_ESCAPE: |
---|
| 121 | if(line[i] == 'n') token += '\n'; |
---|
| 122 | else if(line[i] == 't') token += '\t'; |
---|
| 123 | else if(line[i] == 'v') token += '\v'; |
---|
| 124 | else if(line[i] == 'b') token += '\b'; |
---|
| 125 | else if(line[i] == 'r') token += '\r'; |
---|
| 126 | else if(line[i] == 'f') token += '\f'; |
---|
| 127 | else if(line[i] == 'a') token += '\a'; |
---|
| 128 | else if(line[i] == '?') token += '\?'; |
---|
| 129 | else token += line[i]; // EAT |
---|
| 130 | state = SL_NORMAL; |
---|
| 131 | break; |
---|
| 132 | case SL_SAFEMODE: |
---|
| 133 | if(line[i] == safemode_char) |
---|
| 134 | { |
---|
| 135 | state = SL_NORMAL; |
---|
| 136 | } |
---|
| 137 | else if(line[i] == escape_char) |
---|
| 138 | { |
---|
| 139 | state = SL_SAFEESCAPE; |
---|
| 140 | } |
---|
| 141 | else |
---|
| 142 | { |
---|
| 143 | token += line[i]; // EAT |
---|
| 144 | } |
---|
| 145 | break; |
---|
| 146 | case SL_SAFEESCAPE: |
---|
| 147 | if(line[i] == 'n') token += '\n'; |
---|
| 148 | else if(line[i] == 't') token += '\t'; |
---|
| 149 | else if(line[i] == 'v') token += '\v'; |
---|
| 150 | else if(line[i] == 'b') token += '\b'; |
---|
| 151 | else if(line[i] == 'r') token += '\r'; |
---|
| 152 | else if(line[i] == 'f') token += '\f'; |
---|
| 153 | else if(line[i] == 'a') token += '\a'; |
---|
| 154 | else if(line[i] == '?') token += '\?'; |
---|
| 155 | else token += line[i]; // EAT |
---|
| 156 | state = SL_SAFEMODE; |
---|
| 157 | break; |
---|
| 158 | case SL_COMMENT: |
---|
| 159 | if(line[i] == '\n') |
---|
| 160 | { |
---|
| 161 | /// FINISH |
---|
| 162 | if(token.size() > 0) |
---|
| 163 | { |
---|
| 164 | ret.push_back(token); |
---|
| 165 | offsets.push_back(i); |
---|
| 166 | token.clear(); |
---|
| 167 | } |
---|
| 168 | state = SL_NORMAL; |
---|
| 169 | } |
---|
| 170 | else |
---|
| 171 | { |
---|
| 172 | token += line[i]; // EAT |
---|
| 173 | } |
---|
| 174 | break; |
---|
| 175 | default: |
---|
| 176 | // nothing |
---|
| 177 | break; |
---|
[5656] | 178 | } |
---|
[7211] | 179 | i++; |
---|
[5656] | 180 | } |
---|
| 181 | |
---|
[7211] | 182 | /// FINISH |
---|
| 183 | if(token.size() > 0) |
---|
[5656] | 184 | { |
---|
[7211] | 185 | ret.push_back(token); |
---|
| 186 | offsets.push_back(i); |
---|
| 187 | token.clear(); |
---|
[5656] | 188 | } |
---|
[7211] | 189 | return(state); |
---|
[5656] | 190 | } |
---|
| 191 | |
---|
[5183] | 192 | /** |
---|
[4836] | 193 | * removes the object from memory |
---|
[3941] | 194 | */ |
---|
[4220] | 195 | SubString::~SubString() |
---|
[7211] | 196 | { } |
---|
[3941] | 197 | |
---|
| 198 | /** |
---|
[5200] | 199 | * get a particular substring's offset |
---|
| 200 | * @param i the ID of the substring to get the offset from |
---|
| 201 | * @returns the offset or NULL if an invalid ID was given |
---|
| 202 | */ |
---|
| 203 | unsigned int SubString::getOffset(unsigned int i) |
---|
| 204 | { |
---|
[7211] | 205 | if( i < this->offsets.size() && i >= 0) |
---|
[5200] | 206 | return this->offsets[i]; |
---|
| 207 | else |
---|
| 208 | return 0; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | /** |
---|
[4833] | 212 | * Some nice debug information about this SubString |
---|
| 213 | */ |
---|
| 214 | void SubString::debug() const |
---|
| 215 | { |
---|
[7211] | 216 | printf("Substring-information::count=%d ::", this->strings.size()); |
---|
| 217 | for (unsigned int i = 0; i < this->strings.size(); i++) |
---|
| 218 | printf("s%d='%s'::", i, this->strings[i].c_str()); |
---|
| 219 | printf("\n"); |
---|
[4833] | 220 | } |
---|