Fix DialogBrowse crashing when deleting the last item nullptr. In iOS use std::recursive_mutex instead of the custom check.

This commit is contained in:
2018-10-03 01:02:53 +02:00
parent c9aecf885d
commit f45eefe433
3 changed files with 65 additions and 22 deletions

View File

@@ -1530,11 +1530,30 @@ void ui::Canvas::project_save_thread(std::string file_path)
// static char name[128];
// sprintf(name, "%s/latlong.ppi", data_path.c_str());
FILE* fp = fopen(file_path.c_str(), "wb");
if (!fp)
FILE* fp;
std::string tmp_path = file_path.substr(0, file_path.size() - strlen(".ppi")) + ".tmp.ppi";
bool use_tmp = false;
// check if file already exists
if ((fp = fopen(file_path.c_str(), "rb")))
{
LOG("cannot write project to %s", file_path.c_str());
return;
fclose(fp);
// use tmp file for writing
use_tmp = true;
if (!(fp = fopen(tmp_path.c_str(), "wb")))
{
LOG("cannot write project to %s", file_path.c_str());
return;
}
}
else
{
// write directly to the new file
if (!(fp = fopen(file_path.c_str(), "wb")))
{
LOG("cannot write project to %s", file_path.c_str());
return;
}
}
PPIHeader ppi_header;
@@ -1601,7 +1620,7 @@ void ui::Canvas::project_save_thread(std::string file_path)
};
int ret = stbi_write_png_to_func(callback, &compressed, sz.x, sz.y, 4, snap.image[plane_index].get(), sz.x * 4);
int data_size = compressed.size();
int data_size = (int)compressed.size();
fwrite(&data_size, sizeof(int), 1, fp);
fwrite(compressed.data(), 1, compressed.size(), fp);
@@ -1616,10 +1635,30 @@ void ui::Canvas::project_save_thread(std::string file_path)
}
}
fclose(fp);
LOG("project saved to %s", file_path.c_str());
// App::I.upload(data_path);
// LOG("uploaded");
if (use_tmp)
{
LOG("project saved tmp to %s", tmp_path.c_str());
LOG("swapping to %s", file_path.c_str());
if (std::remove(file_path.c_str()) == 0)
{
if (std::rename(tmp_path.c_str(), file_path.c_str()) == 0)
{
LOG("tmp file swapped succesfully");
}
else
{
LOG("tmp file NOT swapped, original removed");
}
}
else
{
LOG("could not remove %s", file_path.c_str());
}
}
else
{
LOG("project saved to %s", file_path.c_str());
}
m_unsaved = false;
m_newdoc = false;