aboutsummaryrefslogtreecommitdiff
path: root/works/life/cpp-practicum/Record.cpp
blob: 1041b2311eedb81a6b85eb262d9ba64f127bf555 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "Record.hpp"

void Record::WriteTo(QFile file) {
  file.open(QFile::ReadWrite | QFile::Text | QFile::Truncate);
  QTextStream stream(&file);
  stream.setCodec("UTF-8");

  stream << books_.size() << ' ' << vendors_.size() << '\n';
  for (const auto &book : books_) {
    stream << book << '\n';
  }
  for (const auto &vendor : vendors_) {
    stream << vendor << '\n';
  }
}

void Record::ReadFrom(QFile file) {
  file.open(QFile::ReadOnly | QFile::Text);
  QTextStream stream(&file);
  stream.setCodec("UTF-8");

  books_.clear();
  vendors_.clear();

  int book_count, vendor_count;
  stream >> book_count >> vendor_count;
  stream.skipWhiteSpace();
  for (int i = 0; i < book_count; i++) {
    Book book;
    stream >> book;
    books_.push_back(std::move(book));
  }
  for (int i = 0; i < vendor_count; i++) {
    Vendor vendor;
    stream >> vendor;
    vendors_.push_back(std::move(vendor));
  }
}