Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <string>
- #include <list>
- #include <vector>
- #include <iostream>
- void writeStringToFile(std::ostream& os, const std::string& str)
- {
- size_t strLength = str.size();
- os.write(reinterpret_cast<const char*>(&strLength), sizeof(strLength));
- if(strLength != 0)
- {
- os.write(str.data(), strLength);
- }
- }
- void readStringFromFile(std::istream& is, std::string& str)
- {
- size_t strLength = 0;
- is.read(reinterpret_cast<char*>(&strLength), sizeof(strLength));
- if(strLength != 0)
- {
- std::vector<char> rawData;
- rawData.resize(strLength);
- is.read(rawData.data(), strLength);
- str.clear();
- str.assign(rawData.data(), strLength);
- }
- }
- struct FieldInfo
- {
- int intValue = 0;
- std::string stringValue;
- bool isInt = false;
- void writeToFile(std::ostream& os)
- {
- os.write(reinterpret_cast<char*>(&intValue), sizeof(intValue));
- writeStringToFile(os, stringValue);
- os.write(reinterpret_cast<char*>(&isInt), sizeof(isInt));
- }
- void readFromFile(std::istream& is)
- {
- is.read((char*)&intValue, sizeof(intValue));
- readStringFromFile(is, stringValue);
- is.read((char*)&isInt, sizeof(isInt));
- }
- void print()
- {
- if(isInt)
- {
- std::cout << "FieldInfo[Int] - value = " << intValue << std::endl;
- }
- else
- {
- std::cout << "FieldInfo[String] - value = " << stringValue << std::endl;
- }
- }
- };
- struct Row
- {
- std::vector<FieldInfo> m_rowInfo;
- void writeToFile(std::ostream& os)
- {
- size_t rowLength = m_rowInfo.size();
- os.write(reinterpret_cast<char*>(&rowLength), sizeof(rowLength));
- for(size_t i = 0; i < m_rowInfo.size(); i++)
- {
- m_rowInfo[i].writeToFile(os);
- }
- }
- void readFromFile(std::istream& is)
- {
- size_t rowLength = 0;
- is.read((char*)&rowLength, sizeof(rowLength));
- if(rowLength != 0)
- {
- m_rowInfo.clear();
- if(rowLength > m_rowInfo.capacity())
- {
- m_rowInfo.reserve(rowLength);
- }
- for(size_t i = 0; i < rowLength; i++)
- {
- FieldInfo field;
- field.readFromFile(is);
- m_rowInfo.emplace_back(field);
- }
- }
- }
- void print()
- {
- std::cout << "Printing row :" << std::endl;
- for(size_t i = 0; i < m_rowInfo.size(); i++)
- {
- m_rowInfo[i].print();
- }
- }
- void addField(const FieldInfo& f)
- {
- m_rowInfo.emplace_back(f);
- }
- };
- struct Table
- {
- std::string m_tableName;
- std::list<Row> m_rows;
- void writeToFile(std::ostream& os)
- {
- size_t numberOfRecords = m_rows.size();
- os.write(reinterpret_cast<char*>(&numberOfRecords), sizeof(numberOfRecords));
- auto it = m_rows.begin();
- while(it != m_rows.end())
- {
- it->writeToFile(os);
- it++;
- }
- }
- void readFromFile(std::istream& is)
- {
- size_t numberOfRecords = 0;
- is.read((char*)&numberOfRecords, sizeof(numberOfRecords));
- for(size_t i = 0; i < numberOfRecords; i++)
- {
- Row rowToAdd;
- rowToAdd.readFromFile(is);
- m_rows.push_back(rowToAdd);
- }
- }
- void addNewRecord(const Row& r)
- {
- m_rows.push_back(r);
- }
- void print()
- {
- std::cout << "Printing table: " << m_tableName << std::endl;
- auto it = m_rows.begin();
- while(it != m_rows.end())
- {
- it->print();
- it++;
- }
- }
- };
- #define TEST_FILE "./table_test.bin"
- int main()
- {
- FieldInfo field1;
- field1.isInt = true;
- field1.intValue = 10;
- FieldInfo field2;
- field2.isInt = false;
- field2.stringValue = "Test string";
- FieldInfo field3;
- field3.isInt = false;
- field3.stringValue = "Test string 2";
- FieldInfo field4;
- field4.isInt = true;
- field4.intValue = 100;
- Row r1;
- r1.addField(field1);
- r1.addField(field2);
- r1.addField(field3);
- r1.addField(field4);
- Table table;
- table.addNewRecord(r1);
- table.addNewRecord(r1);
- table.m_tableName = "Table 1";
- table.print();
- {
- std::ofstream fileToWrite(TEST_FILE);
- table.writeToFile(fileToWrite);
- fileToWrite.close();
- }
- {
- Table table2;
- table2.m_tableName = "Table 2";
- std::ifstream fileToRead(TEST_FILE);
- table2.readFromFile(fileToRead);
- fileToRead.close();
- table2.print();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement