[02cde8]: trunk / src / headers / gx_json.h Maximize Restore History

Download this file

gx_json.h    503 lines (471 with data), 18.0 kB

  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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
* Copyright (C) 2009, 2010 Hermann Meyer, James Warden, Andreas Degert
* Copyright (C) 2011 Pete Shorthose
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* --------------------------------------------------------------------------
*/
/* ------- This is the System namespace ------- */
#pragma once
#ifndef SRC_HEADERS_GX_JSON_H_
#define SRC_HEADERS_GX_JSON_H_
namespace gx_engine {
class GxMachine;
class GxMachineRemote;
}
namespace gx_system {
/****************************************************************
** class JsonParser, class JsonWriter, class JsonException
** classes for reading and writing JSON files
*/
class JsonException: public exception {
protected:
Glib::ustring what_str;
public:
JsonException(const Glib::ustring& desc);
~JsonException() throw() { }
virtual const char* what() const throw() { return what_str.c_str(); }
};
class JsonExceptionEOF: public JsonException {
public:
JsonExceptionEOF(const char* desc): JsonException(desc) {}
~JsonExceptionEOF() throw() { }
};
class JsonWriter {
private:
ostream *os;
bool first;
int deferred_nl;
string indent;
void snl(bool v) { if (deferred_nl >= 0) deferred_nl = (v ? 1 : 0); }
void komma();
void space();
void iplus();
void iminus();
public:
JsonWriter(ostream* o = 0, bool enable_newlines = true);
virtual ~JsonWriter();
void reset();
void set_stream(ostream* o) { os = o; }
bool good() { return os->good(); }
void flush();
virtual void close();
bool is_closed() { return !os; }
void write(float v, bool nl = false);
void write(double v, bool nl = false);
void write(int i, bool nl = false);
void write(unsigned int i, bool nl = false);
void write(const char* p, bool nl = false);
void write(const string& s, bool nl = false) { write(s.c_str(), nl); }
void write_kv(const char *key, float v) { write_key(key); write(v, true); }
void write_kv(const char *key, double v) { write_key(key); write(v, true); }
void write_kv(const char *key, int i) { write_key(key); write(i, true); }
void write_kv(const char *key, unsigned int i) { write_key(key); write(i, true); }
void write_kv(const char *key, const char* p) { write_key(key); write(p, true); }
void write_kv(const char *key, const std::string& s) { write_key(key); write(s, true); }
void write_lit(const string& s, bool nl = false);
void begin_object(bool nl = false);
void end_object(bool nl = false);
void begin_array(bool nl = false);
void end_array(bool nl = false);
void write_key(const char* p, bool nl = false);
void write_key(const string& p, bool nl = false);
void write_null(bool nl = false) { write_lit("null", nl); }
void newline() { snl(true); }
};
class JsonStringWriter: public JsonWriter {
private:
ostringstream stream;
public:
JsonStringWriter(): JsonWriter(0, false) { set_stream(&stream); }
void send_notify_begin(const char *method);
void send_notify_end();
void reset() { stream.str(""); }
void finish() { stream << endl; }
std::string get_string() { return stream.str(); }
};
class JsonParser {
public:
JsonParser(istream* i = 0);
virtual ~JsonParser();
virtual void close();
void reset();
bool is_closed() { return !is; }
void set_stream(istream* i) { is = i; }
istream *get_stream() { return is; }
enum token {
no_token = 0x0000,
end_token = 0x0001,
begin_object = 0x002,
end_object = 0x0004,
begin_array = 0x0008,
end_array = 0x0010,
value_string = 0x0020,
value_number = 0x0040,
value_key = 0x0080,
value_null = 0x0100,
value_false = 0x0200,
value_true = 0x0400,
value_bool = 0x0600, // value_false | value_true
};
const char* get_token_name(token tok);
bool good() { return is->good(); }
token next(token expect = no_token);
token peek() { return next_tok; }
streampos get_streampos() { return next_pos + streamoff(-1); }
void set_streampos(streampos pos);
void check_expect(token expect) { if ((cur_tok & expect) == 0) throw_unexpected(expect); }
inline string current_value() const { return str; }
int current_value_int() { return atoi(str.c_str()); }
unsigned int current_value_uint() { return atoi(str.c_str()); }
float current_value_float() {
istringstream b(str);
float f;
b >> f;
return f;
}
double current_value_double() {
istringstream b(str);
double d;
b >> d;
return d;
}
bool read_kv(const char *key, float& v);
bool read_kv(const char *key, double& v);
bool read_kv(const char *key, int& i);
bool read_kv(const char *key, unsigned int& i);
bool read_kv(const char *key, std::string& s);
bool read_kv(const char *key, Glib::ustring& s);
template<class T> inline bool read_kv(const char *key, T& v) {
int i;
if (read_kv(key, i)) {
v = static_cast<T>(i);
return true;
}
return false;
}
void copy_object(JsonWriter& jw);
void skip_object();
void throw_unexpected(token expect);
private:
istream* is;
int depth;
token cur_tok;
string str;
bool nl;
int next_depth;
token next_tok;
string next_str;
streampos next_pos;
const char* readcode();
string readstring();
token read_value_token(char c);
string readnumber(char c);
void read_next();
};
class JsonSubParser: public JsonParser {
private:
std::streampos position;
public:
JsonSubParser(JsonParser& jp, streampos pos);
~JsonSubParser();
};
class JsonStringParser: public JsonParser {
private:
std::stringstream stream;
public:
JsonStringParser() {}
void put(char c) { stream.put(c); }
std::ostream& get_ostream() { return stream; }
void start_parser() { stream.seekg(0); set_stream(&stream); }
std::string get_string() { return stream.str(); }
void reset() { stream.str(""); JsonParser::reset(); }
char peek_first_char() { stream >> ws; return stream.peek(); }
};
/****************************************************************
** Setting file handling
** class SettingsFileHeader, class StateFile, class PresetFile,
** class AbstractStateIO, class AbstractPresetIO
** class PresetBanks
** class GxSettingsBase
*/
class SettingsFileHeader {
int file_major;
int file_minor;
string file_gx_version;
public:
enum {
major = 1,
minor = 2
};
static const string gx_version; // = GX_VERSION
SettingsFileHeader()
: file_major(), file_minor(), file_gx_version() {}
void read(JsonParser&);
static void write(JsonWriter&);
void set_to_current() { file_major = major; file_minor = minor; file_gx_version = gx_version; }
int get_major() const { return file_major; }
int get_minor() const { return file_minor; }
string get_revision() const { return file_gx_version; }
bool is_major_diff() const { return major != file_major; }
bool is_minor_diff() const { return minor != file_minor; }
bool is_current() const { return !is_major_diff() && !is_minor_diff(); }
bool is_equal() const { return is_current() && gx_version == file_gx_version; }
string display() const { ostringstream s; s << file_major << "." << file_minor << "." << file_gx_version; return s.str(); }
Glib::ustring version_string() const { return Glib::ustring::compose("%1.%2", file_major, file_minor); }
inline Glib::ustring current_version_string() const { return Glib::ustring::compose("%1.%2", int(major), int(minor)); }
static bool make_empty_settingsfile(const string& name);
static void write_current_major_minor(JsonWriter& jw);
void write_major_minor(JsonWriter& jw);
void read_major_minor(JsonParser& jp);
};
class StateFile {
private:
string filename;
istream *is;
time_t mtime;
SettingsFileHeader header;
void open();
public:
StateFile()
: filename(), is(0), mtime(), header() {}
~StateFile() { delete is; }
void set_filename(const string& fn);
const SettingsFileHeader& get_header() const { return header; }
string get_filename() const { return filename; }
JsonParser *create_reader();
JsonWriter *create_writer(bool *preserve_preset);
void ensure_is_current();
};
class PresetTransformer: public JsonWriter {
private:
string filename;
string tmpfile;
ofstream os;
protected:
istream *is;
public:
JsonParser jp;
SettingsFileHeader header;
void close();
void close_nocheck();
void abort();
PresetTransformer(string filename, istream* is);
~PresetTransformer();
};
enum {
PRESET_FLAG_VERSIONDIFF = 1,
PRESET_FLAG_READONLY = 2,
PRESET_FLAG_INVALID = 4,
};
class PresetFileGui;
class PresetFile : boost::noncopyable {
public:
enum { PRESET_SEP = -1, PRESET_SCRATCH = 0, PRESET_FILE = 1, PRESET_FACTORY = 2 };
protected:
class Position {
public:
Glib::ustring name;
streampos pos;
Position(Glib::ustring n, streampos p): name(n), pos(p) {}
};
std::string filename;
ifstream *is;
time_t mtime;
SettingsFileHeader header;
std::vector<Position> entries;
Glib::ustring name;
int tp;
int flags;
friend class gx_engine::GxMachineRemote;
protected:
void open();
public:
typedef std::vector<Position>::iterator iterator;
PresetFile();
~PresetFile() { delete is; }
void readJSON_remote(JsonParser& jp);
void writeJSON_remote(JsonWriter& jw);
bool open_file(const Glib::ustring& name, const std::string& path, int tp, int flags);
bool create_file(const Glib::ustring& name, const std::string& path, int tp, int flags);
bool set_factory(const Glib::ustring& name_, const std::string& path);
bool readJSON(const std::string& dirpath, JsonParser &jp, bool *mtime_diff);
void writeJSON(JsonWriter& jw);
void reopen() { if (!is && !filename.empty()) open(); }
void open(const std::string& fname);
void close() { delete is; is = 0; }
bool fail();
bool ensure_is_current();
void check_flags();
const std::string& get_filename() const { return filename; }
const SettingsFileHeader& get_header();
int size();
void fill_names(vector<Glib::ustring>&);
const Glib::ustring& get_name(int n);
int get_index(const Glib::ustring& name);
JsonParser *create_reader(int n);
JsonParser *create_reader(const Glib::ustring& name) {
return create_reader(get_index(name)); }
JsonWriter *create_writer(int n) {
return create_writer(get_name(n)); }
JsonWriter *create_writer(const Glib::ustring& name);
JsonWriter *create_writer_at(const Glib::ustring& pos, const Glib::ustring& name);
PresetTransformer *create_transformer();
bool clear();
bool erase(const Glib::ustring& name);
bool rename(const Glib::ustring& name, Glib::ustring newname);
bool has_entry(const Glib::ustring& name) { return get_index(name) >= 0; }
void append(const Glib::ustring& name);
void insert_before(const Glib::ustring& nm, const Glib::ustring& newentry);
void insert_after(const Glib::ustring& nm, const Glib::ustring& newentry);
int get_flags() const { return flags; }
void set_flags(int f) { flags = f; }
void set_flag(int flag, bool v) { flags = (flags & ~flag) | (v ? flag : 0); }
int get_type() const { return tp; }
const Glib::ustring& get_name() const { return name; }
bool set_name(const Glib::ustring& n, const std::string& newfile);
bool remove_file();
iterator begin();
iterator end() { return entries.end(); }
bool is_mutable() const { return (tp == PRESET_SCRATCH || tp == PRESET_FILE) && !flags; }
PresetFileGui *get_guiwrapper();
};
class PresetFileGui: private PresetFile {
private:
PresetFileGui();
~PresetFileGui();
friend class PresetFile;
friend class gx_engine::GxMachine;
friend class gx_engine::GxMachineRemote;
public:
using PresetFile::get_header;
using PresetFile::size;
using PresetFile::fill_names;
using PresetFile::get_name;
using PresetFile::get_index;
using PresetFile::has_entry;
using PresetFile::get_flags;
using PresetFile::get_type;
using PresetFile::begin;
using PresetFile::end;
using PresetFile::is_mutable;
operator PresetFile*() { return this; }
};
inline PresetFileGui *PresetFile::get_guiwrapper() { return static_cast<PresetFileGui*>(this); }
class AbstractStateIO {
public:
virtual ~AbstractStateIO();
virtual void read_state(JsonParser&,const SettingsFileHeader&) = 0;
virtual void commit_state() = 0;
virtual void write_state(JsonWriter&, bool) = 0;
};
class AbstractPresetIO {
public:
virtual ~AbstractPresetIO();
virtual void read_preset(JsonParser&,const SettingsFileHeader&) = 0;
virtual void commit_preset() = 0;
virtual void write_preset(JsonWriter&) = 0;
virtual void copy_preset(JsonParser&,const SettingsFileHeader&,JsonWriter&) = 0;
};
class PresetBanks {
private:
typedef std::list<PresetFile*> bl_type;
bl_type banklist;
std::string filepath;
time_t mtime;
std::string preset_dir;
void parse_factory_list(const std::string& path);
void parse_bank_list(bl_type::iterator pos);
void collect_lost_banks(const char* scratchpad_name, const char* scratchpad_file);
friend class gx_engine::GxMachineRemote;
public:
class iterator {
private:
bl_type::iterator it;
public:
iterator(bl_type::iterator i): it(i) {}
bool operator!=(const iterator& i) const { return it != i.it; }
iterator& operator++() { ++it; return *this; }
PresetFile* operator->() { return *it; }
PresetFile* operator*() { return *it; }
};
PresetBanks();
~PresetBanks();
void readJSON_remote(gx_system::JsonParser& jp);
bool check_reparse();
void parse(const std::string& bank_path, const std::string& preset_dir,
const std::string& factory_path, const char* scratchpad_name, const char* scratchpad_file);
PresetFile* get_file(const Glib::ustring& bank) const;
int get_index(const Glib::ustring& bank) const;
iterator begin() { return iterator(banklist.begin()); }
iterator end() { return iterator(banklist.end()); }
bool remove(const Glib::ustring& bank);
void save();
int size() { return banklist.size(); }
Glib::ustring get_name(int n);
void insert(PresetFile* f) { banklist.push_front(f); save(); }
bool has_entry(const Glib::ustring& bank) const { return get_file(bank) != 0; }
bool has_file(const std::string& file) const;
bool rename(const Glib::ustring& oldname, const Glib::ustring& newname, const std::string& newfile);
void reorder(const std::vector<Glib::ustring>& neworder);
static void make_valid_utf8(Glib::ustring& s);
static std::string add_preset_postfix(const std::string& filename);
static bool strip_preset_postfix(std::string& name);
void make_bank_unique(Glib::ustring& name, std::string *file = 0);
};
class GxSettingsBase {
protected:
AbstractStateIO* state_io;
AbstractPresetIO* preset_io;
StateFile statefile;
PresetBanks banks;
Glib::ustring current_bank;
Glib::ustring current_name;
gx_engine::EngineControl& seq;
sigc::signal<void> selection_changed;
sigc::signal<void> presetlist_changed;
bool loadsetting(PresetFile *p, const Glib::ustring& name);
protected:
void loadstate();
void set_io(AbstractStateIO* st, AbstractPresetIO* pr) { state_io = st; preset_io = pr; }
public:
inline sigc::signal<void>& signal_selection_changed() {
return selection_changed; }
inline sigc::signal<void>& signal_presetlist_changed() {
return presetlist_changed; }
GxSettingsBase(gx_engine::EngineControl& seq_);
~GxSettingsBase();
const Glib::ustring& get_current_bank() { return current_bank; }
PresetFile *get_current_bank_file() { return setting_is_preset() ? banks.get_file(current_bank) : 0; }
const Glib::ustring& get_current_name() { return current_name; }
void set_statefilename(const std::string& fn) { statefile.set_filename(fn); }
void save_to_state(bool preserve_preset=false);
void set_source_to_state();
void erase_preset(const Glib::ustring& name);
bool setting_is_preset() { return !current_bank.empty(); }
bool convert_preset(PresetFile& pf);
void reorder_preset(PresetFile& pf, const std::vector<Glib::ustring>& neworder);
void erase_preset(PresetFile& pf, const Glib::ustring& name);
void save(PresetFile& pf, const Glib::ustring& name);
void append(PresetFile& pf, const Glib::ustring& src, PresetFile& pftgt, const Glib::ustring& name);
void insert_before(PresetFile& pf, const Glib::ustring& src, PresetFile& pftgt, const Glib::ustring& pos, const Glib::ustring& name);
void insert_after(PresetFile& pf, const Glib::ustring& src, PresetFile& pftgt, const Glib::ustring& pos, const Glib::ustring& name);
void load_preset(PresetFile *pf, const Glib::ustring& name);
bool rename_bank(const Glib::ustring& oldname, const Glib::ustring& newname, const std::string& newfile);
bool remove_bank(const Glib::ustring& bank);
bool rename_preset(PresetFile& pf, const Glib::ustring& oldname, const Glib::ustring& newname);
};
} /* end of gx_system namespace */
#endif // SRC_HEADERS_GX_JSON_H_