forked from WohlSoft/PGE-File-Library-STL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_rw_meta.cpp
223 lines (187 loc) · 7.64 KB
/
file_rw_meta.cpp
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* PGE File Library - a library to process file formats, part of Moondust project
*
* Copyright (c) 2014-2022 Vitaly Novichkov <[email protected]>
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "file_formats.h"
#include "file_strlist.h"
#include "pge_file_lib_private.h"
#include "pge_x.h"
//*********************************************************
//****************READ FILE FORMAT*************************
//*********************************************************
bool FileFormats::ReadNonSMBX64MetaDataF(const PGESTRING &filePath, MetaData &FileData)
{
FileData.meta.ERROR_info.clear();
PGE_FileFormats_misc::TextFileInput file;
if(!file.open(filePath, true))
{
FileData.meta.ERROR_info = "Failed to open file for read";
FileData.meta.ERROR_linedata = "";
FileData.meta.ERROR_linenum = -1;
FileData.meta.ReadFileValid = false;
return false;
}
return ReadNonSMBX64MetaDataFile(file, FileData);
}
bool FileFormats::ReadNonSMBX64MetaDataRaw(PGESTRING &rawdata, const PGESTRING &filePath, MetaData &FileData)
{
FileData.meta.ERROR_info.clear();
PGE_FileFormats_misc::RawTextInput file;
if(!file.open(&rawdata, filePath))
{
FileData.meta.ERROR_info = "Failed to open raw string for read";
FileData.meta.ERROR_linedata = "";
FileData.meta.ERROR_linenum = -1;
FileData.meta.ReadFileValid = false;
return false;
}
return ReadNonSMBX64MetaDataFile(file, FileData);
}
bool FileFormats::ReadNonSMBX64MetaDataFile(PGE_FileFormats_misc::TextInput &in, MetaData &FileData)
{
PGESTRING errorString;
int str_count = 0; //Line Counter
PGESTRING line; //Current Line data
///////////////////////////////////////Begin file///////////////////////////////////////
PGEFile pgeX_Data(in.readAll());
if(!pgeX_Data.buildTreeFromRaw())
{
errorString = pgeX_Data.lastError();
goto badfile;
}
for(pge_size_t section = 0; section < pgeX_Data.dataTree.size(); section++) //look sections
{
PGEFile::PGEX_Entry &f_section = pgeX_Data.dataTree[section];
if(f_section.name == "META_BOOKMARKS")
{
if(f_section.type != PGEFile::PGEX_Struct)
{
errorString = PGESTRING("Wrong section data syntax:\nSection [") + f_section.name + "%1]";
goto badfile;
}
for(pge_size_t sdata = 0; sdata < f_section.data.size(); sdata++)
{
if(f_section.data[sdata].type != PGEFile::PGEX_Struct)
{
errorString = PGESTRING("Wrong data item syntax:\nSection [") +
f_section.name + "]\nData line " +
fromNum(sdata) + ")";
goto badfile;
}
PGEFile::PGEX_Item x = f_section.data[sdata];
Bookmark meta_bookmark;
meta_bookmark.bookmarkName.clear();
meta_bookmark.x = 0;
meta_bookmark.y = 0;
for(const auto &v : x.values) //Look markers and values
{
errorString = PGESTRING("Wrong value syntax\nSection [") +
f_section.name + "]\nData line " +
fromNum(sdata) + "\nMarker " +
v.marker + "\nValue " +
v.value;
if(v.marker == "BM") //Bookmark name
{
if(PGEFile::IsQoutedString(v.value))
meta_bookmark.bookmarkName = PGEFile::X2STRING(v.value);
else
goto badfile;
}
else if(v.marker == "X") // Position X
{
if(PGEFile::IsIntS(v.value))
meta_bookmark.x = toInt(v.value);
else
goto badfile;
}
else if(v.marker == "Y") //Position Y
{
if(PGEFile::IsIntS(v.value))
meta_bookmark.y = toInt(v.value);
else
goto badfile;
}
}
FileData.bookmarks.push_back(meta_bookmark);
}
}
}
///////////////////////////////////////EndFile///////////////////////////////////////
errorString.clear(); //If no errors, clear string;
FileData.meta.ReadFileValid = true;
return true;
badfile: //If file format is not correct
//BadFileMsg(filePath+"\nError message: "+errorString, str_count, line);
FileData.meta.ERROR_info = errorString;
FileData.meta.ERROR_linenum = str_count;
FileData.meta.ERROR_linedata = line;
FileData.meta.ReadFileValid = false;
FileData.bookmarks.clear();
return false;
}
//*********************************************************
//****************WRITE FILE FORMAT************************
//*********************************************************
bool FileFormats::WriteNonSMBX64MetaDataF(const PGESTRING &filePath, MetaData &metaData)
{
metaData.meta.ERROR_info.clear();
PGE_FileFormats_misc::TextFileOutput file;
if(!file.open(filePath, true, false, PGE_FileFormats_misc::TextOutput::truncate))
{
metaData.meta.ERROR_info = "Failed to open file for write";
return false;
}
return WriteNonSMBX64MetaData(file, metaData);
}
bool FileFormats::WriteNonSMBX64MetaDataRaw(MetaData &metaData, PGESTRING &rawdata)
{
metaData.meta.ERROR_info.clear();
PGE_FileFormats_misc::RawTextOutput file;
if(!file.open(&rawdata, PGE_FileFormats_misc::TextOutput::truncate))
{
metaData.meta.ERROR_info = "Failed to open raw string for write";
return false;
}
return WriteNonSMBX64MetaData(file, metaData);
}
bool FileFormats::WriteNonSMBX64MetaData(PGE_FileFormats_misc::TextOutput &out, MetaData &metaData)
{
pge_size_t i;
//Bookmarks
if(!metaData.bookmarks.empty())
{
out << "META_BOOKMARKS\n";
for(i = 0; i < metaData.bookmarks.size(); i++)
{
Bookmark &bm = metaData.bookmarks[i];
//Bookmark name
out << PGEFile::value("BM", PGEFile::WriteStr(bm.bookmarkName));
out << PGEFile::value("X", PGEFile::WriteRoundFloat(bm.x));
out << PGEFile::value("Y", PGEFile::WriteRoundFloat(bm.y));
out << "\n";
}
out << "META_BOOKMARKS_END\n";
}
return true;
}