Warning: Can't use blame annotator:
svn blame failed on branches/FEPSimulator/HighwaysParser.cpp: ("Can't find a temporary directory: Internal error", 20014)

source: tmcsimulator/branches/FEPSimulator/HighwaysParser.cpp @ 218

Revision 218, 4.1 KB checked in by jtorres, 9 years ago (diff)

branches/FEPSimulator/HighwaysParser.cpp: garbage collection implemented. We are now deallocating all allocated memory. See: HighwaysParser::~HighwaysParser? (destructor). It will be called whenever the HighwaysParser? object goes out of scope in FEPSim::sendReplys.

RevLine 
1/*
2 * File:   HighwaysParser.cpp
3 * Author: jtorres
4 *
5 * Created on October 28, 2017, 7:23 PM
6 */
7
8#include "HighwaysParser.h"
9     /* 43                   // "number of lines"
10     * 32 0 13              // "line id" "count num" "number of stations"
11     * 1210831 1 5 S 0.9 8  // "station id" "drop num" "route num"...
12     *                      //      ..."direction" "postmile" "number of loops"
13     * 1210832  0.0 0       // "loop id" "occ" "vol"
14     * 1210833  0.0 0       // ..
15     * 1210834  0.0 0       // ..
16     * 1210835  0.0 0       // ..
17     * 1210836  0.0 0       // ..
18     * 1210837  0.0 0       // ..
19     * 1210838  0.0 0       // ..
20     * 1210839  0.0 0       // ..
21      * */
22HighwaysParser::HighwaysParser(char * hwyData) {
23    parseLines(hwyData);
24}
25
26HighwaysParser::~HighwaysParser() {
27    // deallocate FEPLines
28    for(int i = 0; i < this->lines.size(); i++)
29    {
30        delete this->lines.at(i);
31    }
32    // deallocate stations
33    for(int i = 0; i < this->stations.size(); i++)
34    {
35        // deallocate loops
36        for(int j = 0; j < this->stations.at(i)->loops.size(); j++)
37        {
38            // deallocate loop_loc
39            delete this->stations.at(i)->loops.at(j)->loop_loc;
40            // deallocate loop
41            delete this->stations.at(i)->loops.at(j);
42        }
43        // deallocate station dataPack message
44        delete this->stations.at(i)->dataPack;
45        // deallocate station
46        delete this->stations.at(i);
47    }
48}
49
50void HighwaysParser::parseLines(char * hwyData)
51{
52    string highwaysData = hwyData;
53    istringstream highwaysStream(highwaysData);
54    string currLine;
55    getline(highwaysStream, currLine);
56    int numLines;
57    sscanf(currLine.c_str(), "%d", &numLines);
58    int lineNum = 0;
59    long lds = 0;
60    long ldsIndex = 0;
61    short count = 0;
62    int schedule = 0;
63    int lineInfo = 0;
64    long systemKey = 0;
65    long globalSeq = 0;
66    long schedleSeq = 0;
67    short dropNum = 0;
68    int routeNum = 0;
69    float postmile = 0;
70    long loopID = 0;
71    float occ = 0;
72    int vol = 0;
73
74    for(int lineIndex = 0; lineIndex < numLines; lineIndex++)
75    {
76        FEP_LINE * newLine = new FEP_LINE;
77       
78        getline(highwaysStream, currLine);
79        int numStations = 0;
80        sscanf(currLine.c_str(), "%d %hd %d", &lineNum, &count, &numStations);
81        newLine->lineNum = lineNum;
82        newLine->count = count;
83        newLine->schedule = schedule;
84        newLine->lineInfo = lineInfo;
85        newLine->systemKey = systemKey;
86        newLine->globalSeq = globalSeq;
87        newLine->schedleSeq = schedleSeq;
88       
89        for(int stationIndex = 0; stationIndex < numStations; stationIndex++)
90        {
91            STATION * newStation = new STATION;
92   
93            int numLoops = 0;
94            getline(highwaysStream, currLine);
95            char direction;
96            sscanf(currLine.c_str(), "%ld %hd %d %c %f %d", &lds, &dropNum, &routeNum, &direction,
97                    &postmile, &numLoops);
98            newLine->lds.push_back(lds);
99            newLine->ldsIndex.push_back(ldsIndex++);
100            newStation->lds = lds;
101            newStation->line_num = lineNum;
102            newStation->drop = dropNum;
103            newStation->pos = 0;
104            newStation->MlTotVol = 0;
105            newStation->OppTotVol = 0;
106           
107            for(int loopIndex = 0; loopIndex < numLoops; loopIndex++)
108            {
109                LOOP * newLoop = new LOOP;
110               
111                getline(highwaysStream, currLine);
112                newLoop->loop_loc = (char *) malloc(25 * sizeof(char));
113                sscanf(currLine.c_str(), "%ld %f %d %s", &loopID, &occ, &vol, newLoop->loop_loc);
114                newLoop->loopID = loopID;
115                newLoop->occ = occ;
116                newLoop->vol = vol;
117                newLoop->spd = 0;
118                newStation->loops.push_back(newLoop);
119            }
120            newStation->length = newStation->loops.size() * 2 + CONTROL_DATA_LEN;
121            newStation->dataPack = DataPacker::packData(newStation);
122           
123            this->stations.push_back(newStation);
124        }
125        this->lines.push_back(newLine);
126    }
127}
Note: See TracBrowser for help on using the repository browser.