Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.79 KB | None | 0 0
  1. #include <fstream>
  2. #include <string>
  3. #include <list>
  4. #include <vector>
  5. #include <iostream>
  6.  
  7. void writeStringToFile(std::ostream& os, const std::string& str)
  8. {
  9.     size_t strLength = str.size();
  10.     os.write(reinterpret_cast<const char*>(&strLength), sizeof(strLength));
  11.     if(strLength != 0)
  12.     {
  13.         os.write(str.data(), strLength);
  14.     }
  15. }
  16.  
  17. void readStringFromFile(std::istream& is, std::string& str)
  18. {
  19.     size_t strLength = 0;
  20.     is.read(reinterpret_cast<char*>(&strLength), sizeof(strLength));
  21.     if(strLength != 0)
  22.     {
  23.         std::vector<char> rawData;
  24.         rawData.resize(strLength);
  25.         is.read(rawData.data(), strLength);
  26.         str.clear();
  27.         str.assign(rawData.data(), strLength);
  28.     }
  29. }
  30.  
  31. struct FieldInfo
  32. {
  33.     int intValue = 0;
  34.     std::string stringValue;
  35.     bool isInt = false;
  36.  
  37.     void writeToFile(std::ostream& os)
  38.     {
  39.         os.write(reinterpret_cast<char*>(&intValue), sizeof(intValue));
  40.         writeStringToFile(os, stringValue);
  41.         os.write(reinterpret_cast<char*>(&isInt), sizeof(isInt));
  42.     }
  43.  
  44.     void readFromFile(std::istream& is)
  45.     {
  46.         is.read((char*)&intValue, sizeof(intValue));
  47.         readStringFromFile(is, stringValue);
  48.         is.read((char*)&isInt, sizeof(isInt));
  49.     }
  50.  
  51.     void print()
  52.     {
  53.         if(isInt)
  54.         {
  55.             std::cout << "FieldInfo[Int] - value = " << intValue << std::endl;
  56.         }
  57.         else
  58.         {
  59.             std::cout << "FieldInfo[String] - value = " << stringValue << std::endl;
  60.         }
  61.     }
  62. };
  63.  
  64. struct Row
  65. {
  66.     std::vector<FieldInfo> m_rowInfo;
  67.  
  68.     void writeToFile(std::ostream& os)
  69.     {
  70.         size_t rowLength = m_rowInfo.size();
  71.         os.write(reinterpret_cast<char*>(&rowLength), sizeof(rowLength));
  72.         for(size_t i = 0; i < m_rowInfo.size(); i++)
  73.         {
  74.             m_rowInfo[i].writeToFile(os);
  75.         }
  76.     }
  77.  
  78.     void readFromFile(std::istream& is)
  79.     {
  80.         size_t rowLength = 0;
  81.         is.read((char*)&rowLength, sizeof(rowLength));
  82.         if(rowLength != 0)
  83.         {
  84.             m_rowInfo.clear();
  85.             if(rowLength > m_rowInfo.capacity())
  86.             {
  87.                 m_rowInfo.reserve(rowLength);
  88.             }
  89.             for(size_t i = 0; i < rowLength; i++)
  90.             {
  91.                 FieldInfo field;
  92.                 field.readFromFile(is);
  93.                 m_rowInfo.emplace_back(field);
  94.             }
  95.         }
  96.     }
  97.  
  98.     void print()
  99.     {
  100.         std::cout << "Printing row :" << std::endl;
  101.         for(size_t i = 0; i < m_rowInfo.size(); i++)
  102.         {
  103.             m_rowInfo[i].print();
  104.         }
  105.     }
  106.  
  107.     void addField(const FieldInfo& f)
  108.     {
  109.         m_rowInfo.emplace_back(f);
  110.     }
  111. };
  112.  
  113. struct Table
  114. {
  115.     std::string m_tableName;
  116.     std::list<Row> m_rows;
  117.  
  118.     void writeToFile(std::ostream& os)
  119.     {
  120.         size_t numberOfRecords = m_rows.size();
  121.         os.write(reinterpret_cast<char*>(&numberOfRecords), sizeof(numberOfRecords));
  122.  
  123.         auto it = m_rows.begin();
  124.         while(it != m_rows.end())
  125.         {
  126.             it->writeToFile(os);
  127.             it++;
  128.         }
  129.     }
  130.  
  131.     void readFromFile(std::istream& is)
  132.     {
  133.         size_t numberOfRecords = 0;
  134.         is.read((char*)&numberOfRecords, sizeof(numberOfRecords));
  135.  
  136.         for(size_t i = 0; i < numberOfRecords; i++)
  137.         {
  138.             Row rowToAdd;
  139.             rowToAdd.readFromFile(is);
  140.             m_rows.push_back(rowToAdd);
  141.         }
  142.     }
  143.  
  144.     void addNewRecord(const Row& r)
  145.     {
  146.         m_rows.push_back(r);
  147.     }
  148.  
  149.     void print()
  150.     {
  151.         std::cout << "Printing table: " << m_tableName << std::endl;
  152.         auto it = m_rows.begin();
  153.         while(it != m_rows.end())
  154.         {
  155.             it->print();
  156.             it++;
  157.         }
  158.     }
  159. };
  160.  
  161. #define TEST_FILE "./table_test.bin"
  162.  
  163. int main()
  164. {
  165.     FieldInfo field1;
  166.     field1.isInt = true;
  167.     field1.intValue = 10;
  168.  
  169.     FieldInfo field2;
  170.     field2.isInt = false;
  171.     field2.stringValue = "Test string";
  172.  
  173.     FieldInfo field3;
  174.     field3.isInt = false;
  175.     field3.stringValue = "Test string 2";
  176.  
  177.     FieldInfo field4;
  178.     field4.isInt = true;
  179.     field4.intValue = 100;
  180.  
  181.     Row r1;
  182.     r1.addField(field1);
  183.     r1.addField(field2);
  184.     r1.addField(field3);
  185.     r1.addField(field4);
  186.  
  187.     Table table;
  188.     table.addNewRecord(r1);
  189.     table.addNewRecord(r1);
  190.     table.m_tableName = "Table 1";
  191.  
  192.     table.print();
  193.  
  194.     {
  195.         std::ofstream fileToWrite(TEST_FILE);
  196.         table.writeToFile(fileToWrite);
  197.         fileToWrite.close();
  198.     }
  199.  
  200.  
  201.     {
  202.         Table table2;
  203.         table2.m_tableName = "Table 2";
  204.         std::ifstream fileToRead(TEST_FILE);
  205.         table2.readFromFile(fileToRead);
  206.         fileToRead.close();
  207.         table2.print();
  208.     }
  209.     return 0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement