Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include "readFile.h"
- #include "tokenizeString.h"
- #include "GridStruct.h"
- using namespace std;
- void readConfig(GridData** &GridArray, int &sizeX, int &sizeY)
- {
- cout << endl;
- cout << "Please enter config filename : ";
- string inputFilename;
- vector<string> rangeX;
- vector<string> rangeY;
- // when prompted, pls type in 'config.txt'
- cin >> inputFilename;
- fstream inputFile(inputFilename.c_str(), fstream::in);
- string aLine;
- cout << endl;
- while (getline(inputFile, aLine))
- {
- //check range of x-axis
- size_t posX = aLine.find("GridX_IdxRange=");
- if (posX != string::npos) {
- rangeX = tokenizeString(aLine.substr(15), "-");
- sizeX = stoi(rangeX.at(1)) - stoi(rangeX.at(0)) + 1;
- }
- //check range of y-axis
- size_t posY = aLine.find("GridY_IdxRange=");
- if (posY != string::npos) {
- rangeY = tokenizeString(aLine.substr(15), "-");
- sizeY = stoi(rangeY.at(1)) - stoi(rangeY.at(0)) + 1;
- }
- //create 2d array
- GridArray = new GridData * [sizeX];
- for (int i = 0; i < sizeX; i++) {
- GridArray[i] = new GridData[sizeY];
- }
- //check for other configuration files
- size_t pos = aLine.find(".txt");
- if (pos != string::npos)
- readAFile(aLine, GridArray);
- }
- cout << endl;
- }
- void readAFile (const string &filename, GridData** &GridArray)
- {
- fstream inputFile (filename.c_str(), fstream::in);
- cout << endl;
- cout << "Reading contents of file : " << filename << endl;
- cout << endl;
- string aLine;
- vector<string> splitLine;
- int cityData;
- vector<string> coordinates;
- int xCoordinate;
- int yCoordinate;
- while (getline (inputFile, aLine))
- {
- //check filename for corresponding data
- //populate for each data (cloud, pressure, etc.)
- splitLine = tokenizeString(aLine, "-"); // [1, 1]-3-Big_City ==> [1, 1] | 3 | Big_City
- //int to hold data related to the city (id / cloud cover / pressure)
- cityData = stoi(splitLine.at(1));
- coordinates = tokenizeString(splitLine.at(0).substr(1, splitLine.at(0).size()-2), ", "); // [1, 1] ==> 1 | 1
- //int to hold data related to grid coordinates (x, y)
- xCoordinate = stoi(coordinates.at(0));
- yCoordinate = stoi(coordinates.at(1));
- /*
- //Debug [print (x, y) Data: cityData] => all respective data
- //cout << "[" << xCoordinate << " , " << yCoordinate << "] Data: " << cityData << endl;
- */
- if (filename == "citylocation.txt") {
- GridArray[xCoordinate][yCoordinate].cityID = cityData;
- GridArray[xCoordinate][yCoordinate].cityName = splitLine.at(2);
- /*
- Debug [print (x, y) ID = cityData] => ID in this case as this is under citylocation.txt
- cout << coordinates.at(0) << ", " << coordinates.at(1) << " ID = " << stoi(splitLine.at(1)) << endl;
- */
- }
- else if (filename == "cloudcover.txt") {
- GridArray[xCoordinate][yCoordinate].locCloudCover = cityData;
- }
- else if (filename == "pressure.txt") {
- GridArray[xCoordinate][yCoordinate].locPressure = cityData;
- }
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement