| 1 | // ************************************************************************* |
| 2 | // * GSM TA/ME library |
| 3 | // * |
| 4 | // * File: testparser.cc |
| 5 | // * |
| 6 | // * Purpose: Test AT result code parser |
| 7 | // * |
| 8 | // * Author: Peter Hofmann ([email protected]) |
| 9 | // * |
| 10 | // * Created: 17.5.1999 |
| 11 | // ************************************************************************* |
| 12 | |
| 13 | #include <gsmlib/gsm_parser.h> |
| 14 | #include <assert.h> |
| 15 | #include <algorithm> |
| 16 | #include <iostream> |
| 17 | |
| 18 | using namespace std; |
| 19 | using namespace gsmlib; |
| 20 | |
| 21 | void printIntList(vector<bool> &vb) |
| 22 | { |
| 23 | cout << "("; |
| 24 | int j = 0; |
| 25 | bool first = true; |
| 26 | for (vector<bool>::iterator i = vb.begin(); |
| 27 | i != vb.end(); ++i) |
| 28 | { |
| 29 | if (*i) |
| 30 | { |
| 31 | if (! first) cout << ","; |
| 32 | cout << j; |
| 33 | first = false; |
| 34 | } |
| 35 | ++j; |
| 36 | } |
| 37 | cout << ")"; |
| 38 | } |
| 39 | |
| 40 | void printIntRange(IntRange ir) |
| 41 | { |
| 42 | cout << "(" << ir._low << "-" << ir._high << ")"; |
| 43 | } |
| 44 | |
| 45 | void printStringList(vector<string> vs) |
| 46 | { |
| 47 | bool first = true; |
| 48 | cout << "("; |
| 49 | for (vector<string>::iterator i = vs.begin(); |
| 50 | i != vs.end(); ++i) |
| 51 | { |
| 52 | if (! first) cout << ","; |
| 53 | cout << "\"" << *i << "\""; |
| 54 | first = false; |
| 55 | } |
| 56 | cout << ")"; |
| 57 | } |
| 58 | |
| 59 | int main(int argc, char *argv[]) |
| 60 | { |
| 61 | try |
| 62 | { |
| 63 | { |
| 64 | cout << "Test 1" << endl; |
| 65 | Parser p((string)",(\"ME\", \"SM\"," + |
| 66 | "\"Which of the three items does not belong here?\")"); |
| 67 | |
| 68 | vector<string> vs1 = p.parseStringList(true); |
| 69 | p.parseComma(); |
| 70 | vector<string> vs2 = p.parseStringList(); |
| 71 | bool comma = p.parseComma(true); |
| 72 | |
| 73 | printStringList(vs1); |
| 74 | cout << ","; |
| 75 | printStringList(vs2); |
| 76 | if (comma) cout << ","; |
| 77 | cout << endl << endl; |
| 78 | } |
| 79 | { |
| 80 | cout << "Test 2" << endl; |
| 81 | Parser p("(1-5),425,\"+abcd\"efgh\""); |
| 82 | |
| 83 | IntRange ir = p.parseRange(); |
| 84 | p.parseComma(); |
| 85 | int i = p.parseInt(); |
| 86 | p.parseComma(); |
| 87 | string s = p.parseString(false, true); |
| 88 | |
| 89 | printIntRange(ir); |
| 90 | cout << "," << i << ",\"" << s << "\"" << endl << endl; |
| 91 | } |
| 92 | { |
| 93 | cout << "Test 3" << endl; |
| 94 | Parser p("(7,1-5,12-11,25),425,This is a test.\"+a\"ef\" and so forth"); |
| 95 | |
| 96 | vector<bool> vb = p.parseIntList(); |
| 97 | p.parseComma(); |
| 98 | int i = p.parseInt(); |
| 99 | p.parseComma(); |
| 100 | string s = p.parseEol(); |
| 101 | |
| 102 | printIntList(vb); |
| 103 | cout << "," << i << "," << s << endl << endl; |
| 104 | } |
| 105 | { |
| 106 | cout << "Test 4" << endl; |
| 107 | Parser p("(1-125),20,16"); |
| 108 | |
| 109 | vector<bool> vb = p.parseIntList(); |
| 110 | p.parseComma(); |
| 111 | vector<bool> vb2 = p.parseIntList(); |
| 112 | p.parseComma(); |
| 113 | int j = p.parseInt(); |
| 114 | |
| 115 | printIntList(vb); |
| 116 | cout << ","; |
| 117 | printIntList(vb2); |
| 118 | cout << "," << j << endl << endl; |
| 119 | } |
| 120 | { |
| 121 | cout << "Test 5" << endl; |
| 122 | Parser p("SM,7"); |
| 123 | |
| 124 | string s = p.parseString(); |
| 125 | p.parseComma(); |
| 126 | int i = p.parseInt(); |
| 127 | |
| 128 | cout << s << "," << i << endl << endl; |
| 129 | } |
| 130 | { |
| 131 | cout << "Test 6" << endl; |
| 132 | Parser p("(2,\"S TELIA MOBITEL\",\"S TELIA\",\"24001\")"); |
| 133 | |
| 134 | p.parseChar('('); |
| 135 | int status = p.parseInt(); |
| 136 | p.parseComma(); |
| 137 | string longName = p.parseString(true); |
| 138 | p.parseComma(); |
| 139 | string shortName = p.parseString(true); |
| 140 | p.parseComma(); |
| 141 | int numericName; |
| 142 | try |
| 143 | { |
| 144 | numericName = p.parseInt(true); |
| 145 | } |
| 146 | catch (GsmException &e) |
| 147 | { |
| 148 | if (e.getErrorClass() == ParserError) |
| 149 | { |
| 150 | // the Ericsson GM12 GSM modem returns the numeric ID as string |
| 151 | string s = p.parseString(); |
| 152 | numericName = checkNumber(s); |
| 153 | } |
| 154 | else |
| 155 | throw e; |
| 156 | } |
| 157 | p.parseChar(')'); |
| 158 | |
| 159 | cout << "(" << status << ",\"" << longName << "\",\"" |
| 160 | << shortName << "\"," |
| 161 | << numericName << ")" << endl << endl; |
| 162 | } |
| 163 | } |
| 164 | catch (GsmException &p) |
| 165 | { |
| 166 | // these tests shouldn't throw exceptions |
| 167 | assert(0); |
| 168 | } |
| 169 | |
| 170 | // Now some tests that should provoke an error |
| 171 | try |
| 172 | { |
| 173 | Parser p("(4-5"); |
| 174 | p.parseRange(); |
| 175 | } |
| 176 | catch (GsmException &p) |
| 177 | { |
| 178 | cout << "Error 1: " << p.what() << endl << endl; |
| 179 | } |
| 180 | try |
| 181 | { |
| 182 | Parser p("(4-5,3-4-5)"); |
| 183 | p.parseIntList(); |
| 184 | } |
| 185 | catch (GsmException &p) |
| 186 | { |
| 187 | cout << "Error 2: " << p.what() << endl << endl; |
| 188 | } |
| 189 | try |
| 190 | { |
| 191 | Parser p("\"bla\"bla\""); |
| 192 | p.parseString(); |
| 193 | p.checkEol(); |
| 194 | } |
| 195 | catch (GsmException &p) |
| 196 | { |
| 197 | cout << "Error 3: " << p.what() << endl << endl; |
| 198 | } |
| 199 | |
| 200 | } |