| 1 | /* |
|---|
| 2 | * File: HighwaysParser.h |
|---|
| 3 | * |
|---|
| 4 | * The HighwaysParser class takes in a character buffer and parses it into a |
|---|
| 5 | * vector of FEP_LINEs and a vector of STATIONS. The buffer is sent in via the |
|---|
| 6 | * constructor and the FEP_LINE and STATION vectors are accessible via public |
|---|
| 7 | * members. |
|---|
| 8 | * |
|---|
| 9 | * @author John A. Torres |
|---|
| 10 | */ |
|---|
| 11 | |
|---|
| 12 | #include "HighwaysParser.h" |
|---|
| 13 | |
|---|
| 14 | // The public stations member, containing the parsed vector of STATIONS |
|---|
| 15 | HighwaysParser::HighwaysParser(char * hwyData) { |
|---|
| 16 | parseLines(hwyData); |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | // Frees all allocated memory in the class |
|---|
| 20 | HighwaysParser::~HighwaysParser() { |
|---|
| 21 | // deallocate FEPLines |
|---|
| 22 | for(int i = 0; i < this->lines.size(); i++) |
|---|
| 23 | { |
|---|
| 24 | delete this->lines.at(i); |
|---|
| 25 | } |
|---|
| 26 | // deallocate stations |
|---|
| 27 | for(int i = 0; i < this->stations.size(); i++) |
|---|
| 28 | { |
|---|
| 29 | // deallocate loops |
|---|
| 30 | for(int j = 0; j < this->stations.at(i)->loops.size(); j++) |
|---|
| 31 | { |
|---|
| 32 | // deallocate loop_loc |
|---|
| 33 | delete this->stations.at(i)->loops.at(j)->loop_loc; |
|---|
| 34 | // deallocate loop |
|---|
| 35 | delete this->stations.at(i)->loops.at(j); |
|---|
| 36 | } |
|---|
| 37 | // deallocate station dataPack message |
|---|
| 38 | delete this->stations.at(i)->dataPack; |
|---|
| 39 | // deallocate station |
|---|
| 40 | delete this->stations.at(i); |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | /** |
|---|
| 45 | * Parses the buffer into FEP_LINE and STATION vectors. |
|---|
| 46 | * |
|---|
| 47 | * @param highwaysData buffer |
|---|
| 48 | */ |
|---|
| 49 | void HighwaysParser::parseLines(char * hwyData) |
|---|
| 50 | { |
|---|
| 51 | // convert buffer to cpp string type |
|---|
| 52 | string highwaysData = hwyData; |
|---|
| 53 | // create buffer stream |
|---|
| 54 | istringstream highwaysStream(highwaysData); |
|---|
| 55 | // get the number of FEPLines |
|---|
| 56 | string currLine; |
|---|
| 57 | getline(highwaysStream, currLine); |
|---|
| 58 | int numLines; |
|---|
| 59 | sscanf(currLine.c_str(), "%d", &numLines); |
|---|
| 60 | |
|---|
| 61 | // declare variables used in parsing lines and stations |
|---|
| 62 | int lineNum = 0; |
|---|
| 63 | long lds = 0; |
|---|
| 64 | long ldsIndex = 0; |
|---|
| 65 | short count = 0; |
|---|
| 66 | int schedule = 0; |
|---|
| 67 | int lineInfo = 0; |
|---|
| 68 | long systemKey = 0; |
|---|
| 69 | long globalSeq = 0; |
|---|
| 70 | long schedleSeq = 0; |
|---|
| 71 | short dropNum = 0; |
|---|
| 72 | int routeNum = 0; |
|---|
| 73 | float postmile = 0; |
|---|
| 74 | long loopID = 0; |
|---|
| 75 | float occ = 0; |
|---|
| 76 | int vol = 0; |
|---|
| 77 | |
|---|
| 78 | // for each line |
|---|
| 79 | for(int lineIndex = 0; lineIndex < numLines; lineIndex++) |
|---|
| 80 | { |
|---|
| 81 | FEP_LINE * newLine = new FEP_LINE; |
|---|
| 82 | |
|---|
| 83 | getline(highwaysStream, currLine); |
|---|
| 84 | int numStations = 0; |
|---|
| 85 | sscanf(currLine.c_str(), "%d %hd %d", &lineNum, &count, &numStations); |
|---|
| 86 | newLine->lineNum = lineNum; |
|---|
| 87 | newLine->count = count; |
|---|
| 88 | newLine->schedule = schedule; |
|---|
| 89 | newLine->lineInfo = lineInfo; |
|---|
| 90 | newLine->systemKey = systemKey; |
|---|
| 91 | newLine->globalSeq = globalSeq; |
|---|
| 92 | newLine->schedleSeq = schedleSeq; |
|---|
| 93 | |
|---|
| 94 | // for each station |
|---|
| 95 | for(int stationIndex = 0; stationIndex < numStations; stationIndex++) |
|---|
| 96 | { |
|---|
| 97 | STATION * newStation = new STATION; |
|---|
| 98 | |
|---|
| 99 | int numLoops = 0; |
|---|
| 100 | getline(highwaysStream, currLine); |
|---|
| 101 | char direction; |
|---|
| 102 | sscanf(currLine.c_str(), "%ld %hd %d %c %f %d", &lds, &dropNum, &routeNum, &direction, |
|---|
| 103 | &postmile, &numLoops); |
|---|
| 104 | newLine->lds.push_back(lds); |
|---|
| 105 | newLine->ldsIndex.push_back(ldsIndex++); |
|---|
| 106 | newStation->lds = lds; |
|---|
| 107 | newStation->line_num = lineNum; |
|---|
| 108 | newStation->drop = dropNum; |
|---|
| 109 | newStation->pos = 0; |
|---|
| 110 | newStation->MlTotVol = 0; |
|---|
| 111 | newStation->OppTotVol = 0; |
|---|
| 112 | // for each loop |
|---|
| 113 | for(int loopIndex = 0; loopIndex < numLoops; loopIndex++) |
|---|
| 114 | { |
|---|
| 115 | LOOP * newLoop = new LOOP; |
|---|
| 116 | |
|---|
| 117 | getline(highwaysStream, currLine); |
|---|
| 118 | newLoop->loop_loc = (char *) malloc(25 * sizeof(char)); |
|---|
| 119 | sscanf(currLine.c_str(), "%ld %f %d %s", &loopID, &occ, &vol, newLoop->loop_loc); |
|---|
| 120 | newLoop->loopID = loopID; |
|---|
| 121 | newLoop->occ = occ; |
|---|
| 122 | newLoop->vol = vol; |
|---|
| 123 | newLoop->spd = 0; |
|---|
| 124 | newStation->loops.push_back(newLoop); |
|---|
| 125 | } |
|---|
| 126 | newStation->length = newStation->loops.size() * 2 + CONTROL_DATA_LEN; |
|---|
| 127 | newStation->dataPack = DataPacker::packData(newStation); |
|---|
| 128 | |
|---|
| 129 | // add new station to stations vector |
|---|
| 130 | this->stations.push_back(newStation); |
|---|
| 131 | } |
|---|
| 132 | // add new line to lines vector |
|---|
| 133 | this->lines.push_back(newLine); |
|---|
| 134 | } |
|---|
| 135 | } |
|---|