Support von speichern

master
Marko Semet 2019-07-06 11:48:12 +02:00
parent bf9aafd65d
commit 2ba0ffb451
5 changed files with 152 additions and 7 deletions

View File

@ -9,6 +9,8 @@
namespace sirEdit {
extern Gtk::Application* mainApplication;
extern void doSave(bool blocking=false);
extern void loadFile(Gtk::Window* window, Gtk::FileChooserNative* chooser);
extern void runInGui(std::function<void()> func);
/**

View File

@ -276,6 +276,12 @@ namespace {
unordered_map<Field*, sir::FieldLike*> field;
unordered_map<sir::FieldLike*, Field*> fieldInverse;
list<Tool*> toAdd;
list<sir::Tool*> toRemove;
unordered_map<Tool*, sir::Tool*> saveTools;
unordered_map<Tool*, Tool> saveToolData;
template<class TYPE, class SOURCE>
void addSuper(const SOURCE& source) {
for(auto& i : source) {
@ -290,6 +296,10 @@ namespace {
}
}
auto newSirString(const std::string& source) {
return this->sf->strings->add(source.c_str(), source.size());
}
public:
SerializerSIR(std::string path) {
// Open file
@ -422,11 +432,15 @@ namespace {
throw; // TODO:
}
void addBaseTool(Tool* tool) {
auto tmp = this->sf->Tool->add();
this->tools[tool] = tmp;
this->toAdd.push_back(tool);
//auto tmp = this->sf->Tool->add();
this->tools[tool] = nullptr;
}
void removeBaseTool(Tool* tool) {
throw; // TODO:
this->toRemove.push_back(tools[tool]);
//this->sf->free(this->tools[tool]);
this->tools.erase(tool);
delete tool;
}
void getBaseTypes(std::function<void(Type*)> callbackFunc) {
for(auto& i : this->types)
@ -437,10 +451,109 @@ namespace {
callbackFunc(i.first);
}
void prepareSave() {
throw; // TODO:
// New tools
for(auto& i : this->toAdd)
if(this->tools.count(i) != 0)
this->tools[i] = this->sf->Tool->add();
this->toAdd.clear();
// Remove old tools
for(auto& i : this->toRemove)
if(i != nullptr)
this->sf->free(i);
this->toRemove.clear();
// Copy Tool states
this->saveTools = this->tools;
for(auto& i : this->tools)
this->saveToolData.insert({i.first, *(i.first)});
// TODO: Types
}
void save() {
throw; // TODO:
// Copy tool data
for(auto& i : this->saveToolData) {
sir::Tool* sTool = this->saveTools[i.first];
sTool->setName(this->newSirString(i.second.getName()));
sTool->setCommand(this->newSirString(i.second.getCommand()));
sTool->setDescription(this->newSirString(i.second.getDescription()));
// Update types
auto types = new ::skill::api::Map<::sir::UserdefinedType*, ::skill::api::String>();
if(sTool->getSelectedUserTypes() != nullptr)
delete sTool->getSelectedUserTypes();
sTool->setSelectedUserTypes(types);
for(auto& j : i.second.getStatesTypes()) {
switch(get<1>(j.second)) {
case TYPE_STATE::READ:
(*types)[static_cast<sir::UserdefinedType*>(this->types[const_cast<Type*>(j.first)])] = this->newSirString("r");
break;
case TYPE_STATE::WRITE:
(*types)[static_cast<sir::UserdefinedType*>(this->types[const_cast<Type*>(j.first)])] = this->newSirString("w");
break;
case TYPE_STATE::DELETE:
(*types)[static_cast<sir::UserdefinedType*>(this->types[const_cast<Type*>(j.first)])] = this->newSirString("d");
break;
case TYPE_STATE::UNUSED:
(*types)[static_cast<sir::UserdefinedType*>(this->types[const_cast<Type*>(j.first)])] = this->newSirString("u");
break;
case TYPE_STATE::NO:
break;
}
}
// Update fields
{
auto fields = new ::skill::api::Map<::sir::UserdefinedType*, ::skill::api::Map<::sir::FieldLike*, ::skill::api::String>*>();
if(sTool->getSelectedFields() != nullptr)
delete sTool->getSelectedFields();
sTool->setSelectedFields(fields);
// Copy field data
for(auto& j : i.second.getStatesFields()) {
for(auto& k : j.second) {
// Get string
bool found = false;
::skill::api::String s;
switch(k.second) {
case FIELD_STATE::READ:
s = this->newSirString("r");
found = true;
break;
case FIELD_STATE::WRITE:
s = this->newSirString("w");
found = true;
break;
case FIELD_STATE::CREATE:
s = this->newSirString("c");
found = true;
break;
case FIELD_STATE::UNUSED:
s = this->newSirString("u");
found = true;
break;
case FIELD_STATE::NO:
break;
}
if(!found)
continue;
// Insert data
auto tmp = fields->find(static_cast<sir::UserdefinedType*>(this->types[const_cast<Type*>(k.first)]));
if(tmp == fields->end()) {
fields->insert({static_cast<sir::UserdefinedType*>(this->types[const_cast<Type*>(k.first)]), new ::skill::api::Map<::sir::FieldLike*, ::skill::api::String>()});
tmp = fields->find(static_cast<sir::UserdefinedType*>(this->types[const_cast<Type*>(k.first)]));
}
(*(tmp->second))[this->field[const_cast<Field*>(j.first)]] = s;
}
}
}
}
this->saveToolData.clear();
this->saveTools.clear();
// TODO: Types!
this->sf->flush();
}
};
}

View File

@ -15,11 +15,36 @@ using namespace std;
static thread* _loader_thread = new thread();
static thread& loader_thread = *_loader_thread;
static Glib::RefPtr<Gio::File> outputFile;
static std::string filePath;
static sirEdit::data::Serializer* ser;
void sirEdit::doSave(bool blocking) {
// Save function
auto toRunFunc = []() -> void {
char buffer[256];
ifstream input(filePath, ios::binary);
auto output = outputFile->replace();
int read;
while((read = input.readsome(buffer, 256)) > 0) {
output->write(buffer, read);
}
output->close();
input.close();
};
// TODO: Asyc
ser->prepareSave();
ser->save();
toRunFunc();
}
inline void loadFileThread(Gtk::Window* mainWindow, Gtk::Window* popup, Glib::RefPtr<Gio::File> file) {
// TODO: Checks and crashes
outputFile = file;
// Create tmpfile
string fileName = tmpnam(nullptr); // TODO: replace tmpnam
filePath = fileName;
FILE* output = fopen(fileName.c_str(), "w");
// Read data
@ -37,6 +62,7 @@ inline void loadFileThread(Gtk::Window* mainWindow, Gtk::Window* popup, Glib::Re
// Open file
std::unique_ptr<sirEdit::data::Serializer> serializer = std::move(sirEdit::data::getSir(fileName));
ser = serializer.get();
std::mutex mutex;
std::condition_variable cv;
std::unique_lock<std::mutex> lock(mutex);

View File

@ -3,6 +3,7 @@
#include <sirEdit/main.hpp>
#include <list>
#include <unordered_map>
#include <sirEdit/main.hpp>
#include <sirEdit/data/tools.hpp>
using namespace std;
@ -343,6 +344,9 @@ class MainWindow {
this->__builder->get_widget("mainWindow", window);
window->show_all();
sirEdit::mainApplication->add_window(*window);
window->signal_hide().connect([]() -> void {
doSave();
});
}
}
};

@ -1 +1 @@
Subproject commit dd6021fdc102a267128e806ffe3b8e2c0572751a
Subproject commit f105beee5dfaf4cba210fda19806671bbb37bb34