Facebook
From Wet Lechwe, 5 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 291
  1. #include "tiledef.hpp"
  2.  
  3. tile_t* ParseTileData(std::string tile) {
  4.         tile_t* t;
  5.         size_t blocksize = 512;
  6.         char* buf = new char[blocksize];
  7.         if(tile.size() == 0) {
  8.                 std::printf("[ERROR]: Tile data corrupt or empty!\n");
  9.                 std::exit(1);
  10.         }
  11.  
  12.         // Parsing the tile data
  13.         while(std::fgets(buf, sizeof buf, stdin)) {
  14.                 uint32_t id = std::strtol(buf, NULL, 0);
  15.                 t->id = id;
  16.  
  17.                 std::printf("[DATA]: \nTileID: \t%d\n", id);
  18.         }
  19.  
  20.     // Check if buffer still contains some data
  21.         delete buf;
  22.  
  23.         return t;
  24. }
  25.  
  26. // Opens the tile table, reads it line-by-line,
  27. // Removes newlines from read string and saves it to the string table;
  28. std::vector<std::string> LoadTiles() {
  29.         std::vector<std::string> tileids;
  30.         std::ifstream file;
  31.         std::string line;
  32.         char c = '\0';
  33.         size_t len = 0;
  34.         size_t lines = 0;
  35.         size_t read = 0;
  36.  
  37.         // Open the file as text
  38.         file.open("tileids.txt", std::ios::in);
  39.  
  40.         // Check whether the file exists, is corrupt or empty
  41.         if(file.bad()) {
  42.                 std::printf("[ERROR]: TileID table corrupt or empty!\n");
  43.                 file.close();
  44.                 std::exit(1);
  45.         } else {
  46.         // Count the number of lines in the file
  47.         lines = CountLines(file);
  48.  
  49.         std::printf("[DEBUG]: Lines: [%zu]\n", lines);
  50.  
  51.         // Reset the position of the file
  52.         file.seekg(std::ios::beg);
  53.  
  54.         // Read the file line by line
  55.         while(std::getline(file, line)) {
  56.             // Remove any unnecessary newlines
  57.             if(line[line.size()-1] == '\n') {
  58.                 line[line.size()-1] = '\0';
  59.             }
  60.  
  61.             // Print the content and the length
  62.             std::printf("[DATA]: \n\t[%s]", line);
  63.             std::printf("\t[LENGTH]: [%zu]\n", read);
  64.  
  65.             // Save the content of the line into a string array
  66.             tileids.push_back(line);
  67.             std::printf("\t[TILEID]: \t[%s]\n", tileids.at(read++));
  68.         }
  69.  
  70.         // Close the file
  71.         file.close();
  72.         }
  73.  
  74.         return tileids;
  75. }
  76.  
  77. int main() {
  78.         std::printf("[DEBUG]: Initialized!\n\n");
  79.  
  80.         std::vector<std::string> tmp;
  81.         tmp = LoadTiles();
  82.  
  83.         std::printf("\n[TILEID LIST]: \n");
  84.  
  85.         for(auto &i: tmp) {
  86.                 std::printf("\t[%s]\n", i);
  87.         }
  88.  
  89.         std::printf("\n");
  90.         return 0;
  91. }
  92.  
  93.  
  94. /* The output:
  95.  * [DEBUG]: Initialized!                                                                                                                                                                                            
  96.  * [DEBUG]: Lines: [2]                                                                                                      
  97.  * [TILEID LIST]:  
  98.  *                                                                                                     
  99.  * Process returned 0 (0x0)   execution time : 0.010 s                                                      
  100.  * Press any key to continue.
  101.  */
  102.