Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5. #include "readFile.h"
  6. #include "tokenizeString.h"
  7. #include "GridStruct.h"
  8.  
  9. using namespace std;
  10.  
  11. void readConfig(GridData** &GridArray, int &sizeX, int &sizeY)
  12. {
  13.     cout << endl;
  14.     cout << "Please enter config filename : ";
  15.  
  16.     string inputFilename;
  17.     vector<string> rangeX;
  18.     vector<string> rangeY;
  19.  
  20.     // when prompted, pls type in 'config.txt'
  21.     cin >> inputFilename;
  22.  
  23.     fstream inputFile(inputFilename.c_str(), fstream::in);
  24.  
  25.     string aLine;
  26.  
  27.     cout << endl;
  28.     while (getline(inputFile, aLine))
  29.     {
  30.         //check range of x-axis
  31.         size_t posX = aLine.find("GridX_IdxRange=");
  32.         if (posX != string::npos) {
  33.             rangeX = tokenizeString(aLine.substr(15), "-");
  34.             sizeX = stoi(rangeX.at(1)) - stoi(rangeX.at(0)) + 1;
  35.         }
  36.  
  37.         //check range of y-axis
  38.         size_t posY = aLine.find("GridY_IdxRange=");
  39.         if (posY != string::npos) {
  40.             rangeY = tokenizeString(aLine.substr(15), "-");
  41.             sizeY = stoi(rangeY.at(1)) - stoi(rangeY.at(0)) + 1;
  42.         }
  43.        
  44.         //create 2d array
  45.         GridArray = new GridData * [sizeX];
  46.         for (int i = 0; i < sizeX; i++) {
  47.             GridArray[i] = new GridData[sizeY];
  48.         }
  49.  
  50.         //check for other configuration files
  51.         size_t pos = aLine.find(".txt");
  52.         if (pos != string::npos)
  53.             readAFile(aLine, GridArray);
  54.  
  55.     }
  56.     cout << endl;
  57. }
  58.  
  59.  
  60. void readAFile (const string &filename, GridData** &GridArray)
  61. {
  62.     fstream inputFile (filename.c_str(), fstream::in);
  63.  
  64.     cout << endl;
  65.     cout << "Reading contents of file : " << filename << endl;
  66.     cout << endl;
  67.  
  68.     string aLine;
  69.  
  70.     vector<string> splitLine;
  71.     int cityData;
  72.  
  73.     vector<string> coordinates;
  74.     int xCoordinate;
  75.     int yCoordinate;
  76.  
  77.     while (getline (inputFile, aLine))
  78.     {
  79.         //check filename for corresponding data
  80.         //populate for each data (cloud, pressure, etc.)
  81.  
  82.         splitLine = tokenizeString(aLine, "-"); // [1, 1]-3-Big_City ==> [1, 1] | 3 | Big_City
  83.  
  84.         //int to hold data related to the city (id / cloud cover / pressure)
  85.         cityData = stoi(splitLine.at(1));
  86.  
  87.         coordinates = tokenizeString(splitLine.at(0).substr(1, splitLine.at(0).size()-2), ", "); // [1, 1] ==> 1 | 1
  88.  
  89.         //int to hold data related to grid coordinates (x, y)
  90.         xCoordinate = stoi(coordinates.at(0));
  91.         yCoordinate = stoi(coordinates.at(1));
  92.  
  93.         /*
  94.             //Debug [print (x, y) Data: cityData] => all respective data
  95.             //cout << "[" << xCoordinate << " , " << yCoordinate << "] Data: " << cityData << endl;
  96.         */
  97.  
  98.         if (filename == "citylocation.txt") {
  99.             GridArray[xCoordinate][yCoordinate].cityID = cityData;
  100.             GridArray[xCoordinate][yCoordinate].cityName = splitLine.at(2);
  101.             /*
  102.                 Debug [print (x, y) ID = cityData] => ID in this case as this is under citylocation.txt
  103.  
  104.                 cout << coordinates.at(0) << ", " << coordinates.at(1) << " ID = " << stoi(splitLine.at(1)) << endl;
  105.             */
  106.         }
  107.         else if (filename == "cloudcover.txt") {
  108.             GridArray[xCoordinate][yCoordinate].locCloudCover = cityData;
  109.         }
  110.         else if (filename == "pressure.txt") {
  111.             GridArray[xCoordinate][yCoordinate].locPressure = cityData;
  112.         }
  113.     }
  114.     cout << endl;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement