Skip to content

Commit

Permalink
storage: Implemeted save and load from file
Browse files Browse the repository at this point in the history
  • Loading branch information
Petr Laštovička committed Apr 30, 2022
1 parent 5fa1c4e commit 5418b78
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 39 deletions.
1 change: 1 addition & 0 deletions Semestralni_prace/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
lastope2
lastope2.zip
test
logs.txt
*.o
*.d
doc/
Expand Down
3 changes: 2 additions & 1 deletion Semestralni_prace/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ compile: $(USERNAME)

# Runs compiled binary
run:
@./$(USERNAME)
@./$(USERNAME) $()

# Tests
compile_test: $(TEST_FILE)
Expand Down Expand Up @@ -120,4 +120,5 @@ clean:
rm -f $(TEST_OBJECTS) $(TEST_DEPENDS) $(TEST_FILE)
rm -f core
rm -fr doc
rm -f "logs.txt"

42 changes: 42 additions & 0 deletions Semestralni_prace/examples/table.vimcsv

Large diffs are not rendered by default.

40 changes: 32 additions & 8 deletions Semestralni_prace/src/cz/lastaapps/vimxel/app.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
#include "app.hpp"
#include "log.hpp"
#include "table/cell.hpp"
#include "storage/storage.hpp"

namespace cz::lastaapps::vimxel {
int App::run(vector<string> args) {
initNCurses();
printArgs(args);
initNCurses();
try {
shared_ptr<table::Table> table = loadTable();
shared_ptr<table::Table> table = loadTable(args);
display::Display display(table->createCellContract());
display.draw();
} catch(...) {
} catch (const std::exception& ex) {
destroyNCurses();
cerr << ex.what() << endl;
return 1;
} catch (const std::string& ex) {
destroyNCurses();
cerr << ex << endl;
return 1;
} catch (...) {
destroyNCurses();
cerr << "Unknown exception" << endl;
return 1;
}
destroyNCurses();
destroyNCurses();
return 0;
}

shared_ptr<table::Table> App::loadTable() {
void App::printArgs(const vector<string> &args) {
mlog << "App args: ";
bool isFirst = true;
for (const auto& arg : args) {
if (isFirst)
isFirst = false;
else
mlog << ", ";
mlog << arg;
}
mlog << endl;
}

shared_ptr<table::Table> App::loadTable(const vector<string> &args) {
auto table = shared_ptr<table::Table>(new table::Table);
for (int i = 0; i < 42; i++)
for (int j = 0; j < 69; j++)
table->updateCell(Coordinates(i, j), TextCell(to_string(i + 1) + ", " + to_string(j + 1)));
if (args.size() >= 2)
storage::Storage::loadData(args.at(1), table);
return table;
}

Expand Down
28 changes: 15 additions & 13 deletions Semestralni_prace/src/cz/lastaapps/vimxel/app.hpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
#ifndef APP_H
#define APP_H
#include <ncurses.h>
#include <vector>

#include <string>
#include <vector>

#include "display/display.hpp"
#include "table/table.hpp"

using namespace std;
namespace cz::lastaapps::vimxel {

class App final {
public:
int run(vector<string> args);
private:

shared_ptr<table::Table> loadTable();
void initNCurses();
void destroyNCurses();
void setupDisplay();

// Storage engine
// Vim
};
public:
int run(vector<string> args);

private:
shared_ptr<table::Table> loadTable(const vector<string> &args);
void printArgs(const vector<string> &args);
void initNCurses();
void destroyNCurses();
void setupDisplay();

// Storage engine
// Vim
};

} // namespace cz::lastaapps::vimxel

Expand Down
6 changes: 6 additions & 0 deletions Semestralni_prace/src/cz/lastaapps/vimxel/log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "log.hpp"

using namespace std;
namespace cz::lastaapps::vimxel {
ofstream mlog = ofstream(LOG_FILE_NAME, ios::out);
}
11 changes: 11 additions & 0 deletions Semestralni_prace/src/cz/lastaapps/vimxel/log.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef H_LOG
#define H_LOG
#include <fstream>

using namespace std;
namespace cz::lastaapps::vimxel {
static string LOG_FILE_NAME = "logs.txt";
extern ofstream mlog;
}

#endif
58 changes: 45 additions & 13 deletions Semestralni_prace/src/cz/lastaapps/vimxel/storage/storage.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "storage.hpp"

#include <filesystem>
#include <ios>
#include <sstream>

#include "../log.hpp"
#include "../table/cell.hpp"
#include "../table/coordinate.hpp"
#include "../table/table.hpp"
Expand All @@ -11,27 +13,46 @@ namespace cz::lastaapps::vimxel::storage {

void Storage::saveData(shared_ptr<table::Table> table, const string& path) {
auto size = table->tableSize();
fstream file;
openFile(path, file);
ofstream file;
openFileForWrite(path, file);
for (size_t x = 0; x < size.x(); x++) {
bool isFirst = true;
for (size_t y = 0; y < size.y(); y++) {
if (isFirst)
isFirst = false;
else
file << ';';
file << DELIMITER;
const table::Cell& cell = table->getCell(table::Coordinates(x, y));
file << escapeText(cell.getContent());
}
file << '\n';
file << LINE_DELIMITER;
}
// drop latests \n
file.close();
auto filePath = filesystem::path(path);
size_t fileSize = file_size(filePath);
if (fileSize != 0)
resize_file(filePath, fileSize - 1);
}

void Storage::exportData(shared_ptr<table::Table> table, const string& path) {
// TODO update after expression evaulation is done
saveData(table, path);
}

void Storage::loadData(const string& path, shared_ptr<table::Table>& table) {
ifstream file;
openFileForRead(path, file);
for (size_t y = 0; file; y++) {
for (size_t x = 0; file; x++) {
bool lineEnd;
string cellContent = move(importText(file, lineEnd));
table->updateCell(table::Coordinates(x, y), table::TextCell(cellContent));
if (lineEnd) break;
}
}
}

string Storage::escapeText(const string& str) {
bool containsDelim = false;
for (const char c : str)
Expand All @@ -52,10 +73,11 @@ string Storage::escapeText(const string& str) {
}

string Storage::importText(const string& str) {
bool tmp;
auto stream = istringstream(str);
return importText(stream);
return importText(stream, tmp);
}
string Storage::importText(istream& stream) {
string Storage::importText(istream& stream, bool& lineEnd) {
stringstream out;
char c;
stream >> noskipws;
Expand All @@ -65,34 +87,44 @@ string Storage::importText(istream& stream) {
if (!isEncapsulated) {
stream.unget();
while (stream >> c) {
if (c == DELIMITER || c == LINE_DELIMITER)
if (c == DELIMITER || c == LINE_DELIMITER) {
lineEnd = c == LINE_DELIMITER;
break;
else
} else {
out << c;
}
}
} else {
bool inEscape = false;
while (stream >> c) {
if (c == DELIMITER || c == LINE_DELIMITER) {
if (!inEscape)
if (!inEscape) {
out << c;
else break;
} else {
lineEnd = c == LINE_DELIMITER;
break;
}
} else if (c == ENCAPSULATOR) {
if (inEscape)
out << c;
inEscape = !inEscape;
} else {
if (!inEscape)
out << c;
else throw SingleEncapsulator();
else
throw SingleEncapsulator();
}
}
if (stream.eof() && !inEscape) throw InvalidEncapsulation();
}
if (stream.eof()) lineEnd = true;
return out.str();
}

void Storage::openFile(const string& str, fstream& stream) {
stream = fstream(str, ios::in | ios::out | ios::trunc);
void Storage::openFileForWrite(const string& str, ofstream& stream) {
stream = ofstream(str, ios::out | ios::trunc);
}
void Storage::openFileForRead(const string& str, ifstream& stream) {
stream = ifstream(str, ios::in);
}
} // namespace cz::lastaapps::vimxel::storage
7 changes: 4 additions & 3 deletions Semestralni_prace/src/cz/lastaapps/vimxel/storage/storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ class Storage final {
public:
static void saveData(shared_ptr<table::Table> table, const string & path);
static void exportData(shared_ptr<table::Table> table, const string & path);
static shared_ptr<shared_ptr<table::Table>> loadData(const string & path);
static void loadData(const string & path, shared_ptr<table::Table>& table);

private:
static constexpr char DELIMITER = ';';
static constexpr char LINE_DELIMITER = '\n';
static constexpr char ENCAPSULATOR = '"';
static void openFile(const string & str, fstream & stream);
static void openFileForWrite(const string & str, ofstream & stream);
static void openFileForRead(const string & str, ifstream & stream);
static string escapeText(const string & str);
static string importText(istream & stream);
static string importText(const string& str);
static string importText(istream & stream, bool & lineEnd);

friend class StorageTester;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,9 @@ size_t Coordinates::pow(size_t a, size_t x) {
return toReturn;
}

ostream& operator<<(ostream& out, const Coordinates& c) {
out << '[' << c.x() << ',' << c.y() << ']';
return out;
}

} // namespace cz::lastaapps::vimxel::table
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef COORDINATE_H
#define COORDINATE_H
#include <string>
#include <iostream>

using namespace std;
namespace cz::lastaapps::vimxel::table {
Expand Down Expand Up @@ -43,5 +44,7 @@ class Coordinates final {
static size_t pow(size_t a, size_t x);
};

ostream& operator<<(ostream& out, const Coordinates& c);

} // namespace cz::lastaapps::vimxel::table
#endif
3 changes: 2 additions & 1 deletion Semestralni_prace/src/cz/lastaapps/vimxel/table/table.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "table.hpp"
#include "cell.hpp"
#include "../log.hpp"

#include <iostream>
#include <map>
Expand Down Expand Up @@ -58,7 +59,7 @@ void Table::updateContracts(const Coordinates& coord) {
Coordinates Table::tableSize() const {
Coordinates biggest;
for (const auto & [key, value] : mMap)
if (key.x() >= biggest.x() && key.y() > biggest.y())
if (key.x() >= biggest.x() && key.y() >= biggest.y())
biggest = key;
return biggest;
}
Expand Down

0 comments on commit 5418b78

Please sign in to comment.