25 #ifndef SRC_HEADERS_GX_JSON_H_ 26 #define SRC_HEADERS_GX_JSON_H_ 30 class GxMachineRemote;
46 virtual const char*
what()
const throw() {
return what_str.c_str(); }
61 void snl(
bool v) {
if (deferred_nl >= 0) deferred_nl = (v ? 1 : 0); }
67 JsonWriter(ostream* o = 0,
bool enable_newlines =
true);
71 bool good() {
return os->good(); }
75 void write(
float v,
bool nl =
false);
76 void write(
double v,
bool nl =
false);
77 void write(
int i,
bool nl =
false);
78 void write(
unsigned int i,
bool nl =
false);
79 void write(
const char* p,
bool nl =
false);
80 void write(
const string& s,
bool nl =
false) {
write(s.c_str(), nl); }
81 void write_kv(
const char *key,
float v) { write_key(key); write(v,
true); }
82 void write_kv(
const char *key,
double v) { write_key(key); write(v,
true); }
83 void write_kv(
const char *key,
int i) { write_key(key); write(i,
true); }
84 void write_kv(
const char *key,
unsigned int i) { write_key(key); write(i,
true); }
85 void write_kv(
const char *key,
const char* p) { write_key(key); write(p,
true); }
86 void write_kv(
const char *key,
const std::string& s) { write_key(key); write(s,
true); }
87 void write_lit(
const string& s,
bool nl =
false);
88 void begin_object(
bool nl =
false);
89 void end_object(
bool nl =
false);
90 void begin_array(
bool nl =
false);
91 void end_array(
bool nl =
false);
92 void write_key(
const char* p,
bool nl =
false);
93 void write_key(
const string& p,
bool nl =
false);
94 void write_null(
bool nl =
false) { write_lit(
"null", nl); }
101 ostringstream stream;
104 void send_notify_begin(
const char *method);
105 void send_notify_end();
116 virtual void close();
124 begin_object = 0x002,
126 begin_array = 0x0008,
128 value_string = 0x0020,
129 value_number = 0x0040,
132 value_false = 0x0200,
136 const char* get_token_name(
token tok);
137 bool good() {
return is->good(); }
141 void set_streampos(streampos pos);
147 istringstream b(str);
153 istringstream b(str);
158 bool read_kv(
const char *key,
float& v);
159 bool read_kv(
const char *key,
double& v);
160 bool read_kv(
const char *key,
int& i);
161 bool read_kv(
const char *key,
unsigned int& i);
162 bool read_kv(
const char *key, std::string& s);
163 bool read_kv(
const char *key, Glib::ustring& s);
164 template<
class T>
inline bool read_kv(
const char *key, T& v) {
166 if (read_kv(key, i)) {
167 v =
static_cast<T
>(i);
174 void throw_unexpected(
token expect);
186 const char* readcode();
188 token read_value_token(
char c);
189 string readnumber(
char c);
196 std::streampos position;
204 std::stringstream stream;
207 void put(
char c) { stream.put(c); }
211 void reset() { stream.str(
""); JsonParser::reset(); }
227 string file_gx_version;
235 : file_major(), file_minor(), file_gx_version() {}
238 void set_to_current() { file_major = major; file_minor = minor; file_gx_version = gx_version; }
244 bool is_current()
const {
return !is_major_diff() && !is_minor_diff(); }
245 bool is_equal()
const {
return is_current() && gx_version == file_gx_version; }
246 string display()
const { ostringstream s; s << file_major <<
"." << file_minor <<
"." << file_gx_version;
return s.str(); }
247 Glib::ustring
version_string()
const {
return Glib::ustring::compose(
"%1.%2", file_major, file_minor); }
249 static bool make_empty_settingsfile(
const string& name);
250 static void write_current_major_minor(
JsonWriter& jw);
264 : filename(), is(0), mtime(), header() {}
266 void set_filename(
const string& fn);
270 JsonWriter *create_writer(
bool *preserve_preset);
271 void ensure_is_current();
285 void close_nocheck();
301 enum { PRESET_SEP = -1, PRESET_SCRATCH = 0, PRESET_FILE = 1, PRESET_FACTORY = 2 };
307 Position(Glib::ustring n, streampos p): name(n), pos(p) {}
326 bool open_file(
const Glib::ustring& name,
const std::string& path,
int tp,
int flags);
327 bool create_file(
const Glib::ustring& name,
const std::string& path,
int tp,
int flags);
328 bool set_factory(
const Glib::ustring& name_,
const std::string& path);
329 bool readJSON(
const std::string& dirpath,
JsonParser &jp,
bool *mtime_diff);
331 void reopen() {
if (!is && !filename.empty()) open(); }
332 void open(
const std::string& fname);
335 bool ensure_is_current();
340 void fill_names(vector<Glib::ustring>&);
341 const Glib::ustring& get_name(
int n);
342 int get_index(
const Glib::ustring& name);
345 return create_reader(get_index(name)); }
347 return create_writer(get_name(n)); }
348 JsonWriter *create_writer(
const Glib::ustring& name);
349 JsonWriter *create_writer_at(
const Glib::ustring& pos,
const Glib::ustring& name);
352 bool erase(
const Glib::ustring& name);
353 bool rename(
const Glib::ustring& name, Glib::ustring newname);
354 bool has_entry(
const Glib::ustring& name) {
return get_index(name) >= 0; }
355 void append(
const Glib::ustring& name);
356 void insert_before(
const Glib::ustring& nm,
const Glib::ustring& newentry);
357 void insert_after(
const Glib::ustring& nm,
const Glib::ustring& newentry);
360 void set_flag(
int flag,
bool v) { flags = (flags & ~flag) | (v ? flag : 0); }
362 const Glib::ustring&
get_name()
const {
return name; }
363 bool set_name(
const Glib::ustring& n,
const std::string& newfile);
366 iterator
end() {
return entries.end(); }
367 bool is_mutable()
const {
return (tp == PRESET_SCRATCH || tp == PRESET_FILE) && !flags; }
379 using PresetFile::get_header;
380 using PresetFile::size;
381 using PresetFile::fill_names;
382 using PresetFile::get_name;
383 using PresetFile::get_index;
384 using PresetFile::has_entry;
385 using PresetFile::get_flags;
386 using PresetFile::get_type;
387 using PresetFile::begin;
389 using PresetFile::is_mutable;
399 virtual void commit_state() = 0;
400 virtual void write_state(
JsonWriter&,
bool) = 0;
407 virtual void commit_preset() = 0;
414 typedef std::list<PresetFile*> bl_type;
416 std::string filepath;
418 std::string preset_dir;
419 void parse_factory_list(
const std::string& path);
420 void parse_bank_list(bl_type::iterator pos);
421 void collect_lost_banks(
const char* scratchpad_name,
const char* scratchpad_file);
426 bl_type::iterator it;
437 bool check_reparse();
438 void parse(
const std::string& bank_path,
const std::string& preset_dir,
439 const std::string& factory_path,
const char* scratchpad_name,
const char* scratchpad_file);
440 PresetFile* get_file(
const Glib::ustring& bank)
const;
441 int get_index(
const Glib::ustring& bank)
const;
444 bool remove(
const Glib::ustring& bank);
446 int size() {
return banklist.size(); }
447 Glib::ustring get_name(
int n);
449 bool has_entry(
const Glib::ustring& bank)
const {
return get_file(bank) != 0; }
450 bool has_file(
const std::string& file)
const;
451 bool rename(
const Glib::ustring& oldname,
const Glib::ustring& newname,
const std::string& newfile);
452 void reorder(
const std::vector<Glib::ustring>& neworder);
453 static void make_valid_utf8(Glib::ustring& s);
454 static std::string add_preset_postfix(
const std::string& filename);
455 static bool strip_preset_postfix(std::string& name);
456 void make_bank_unique(Glib::ustring& name, std::string *file = 0);
470 bool loadsetting(
PresetFile *p,
const Glib::ustring& name);
476 return selection_changed; }
478 return presetlist_changed; }
485 void save_to_state(
bool preserve_preset=
false);
486 void set_source_to_state();
487 void erase_preset(
const Glib::ustring& name);
490 void reorder_preset(
PresetFile& pf,
const std::vector<Glib::ustring>& neworder);
491 void erase_preset(
PresetFile& pf,
const Glib::ustring& name);
492 void save(
PresetFile& pf,
const Glib::ustring& name);
493 void append(
PresetFile& pf,
const Glib::ustring& src,
PresetFile& pftgt,
const Glib::ustring& name);
494 void insert_before(
PresetFile& pf,
const Glib::ustring& src,
PresetFile& pftgt,
const Glib::ustring& pos,
const Glib::ustring& name);
495 void insert_after(
PresetFile& pf,
const Glib::ustring& src,
PresetFile& pftgt,
const Glib::ustring& pos,
const Glib::ustring& name);
496 void load_preset(
PresetFile *pf,
const Glib::ustring& name);
497 bool rename_bank(
const Glib::ustring& oldname,
const Glib::ustring& newname,
const std::string& newfile);
498 bool remove_bank(
const Glib::ustring& bank);
499 bool rename_preset(
PresetFile& pf,
const Glib::ustring& oldname,
const Glib::ustring& newname);
503 #endif // SRC_HEADERS_GX_JSON_H_
gx_engine::EngineControl & seq
CmdConnection::msg_type end
void write_kv(const char *key, float v)
void set_io(AbstractStateIO *st, AbstractPresetIO *pr)
bool has_entry(const Glib::ustring &name)
bool operator!=(const iterator &i) const
PresetFile * get_current_bank_file()
void set_stream(ostream *o)
Position(Glib::ustring n, streampos p)
std::ostream & get_ostream()
unsigned int current_value_uint()
void insert(PresetFile *f)
sigc::signal< void > selection_changed
PresetFile * get_file(const Glib::ustring &bank) const
const Glib::ustring & get_name() const
void write_kv(const char *key, double v)
void set_flag(int flag, bool v)
void write_kv(const char *key, unsigned int i)
std::vector< Position >::iterator iterator
sigc::signal< void > presetlist_changed
std::vector< Position > entries
PresetFile * operator->()
sigc::signal< void > & signal_selection_changed()
void write_kv(const char *key, const std::string &s)
SettingsFileHeader header
void check_expect(token expect)
void set_filename(const string &fn)
void write(const string &s, bool nl=false)
string get_filename() const
sigc::signal< void > & signal_presetlist_changed()
void write_null(bool nl=false)
void set_statefilename(const std::string &fn)
AbstractPresetIO * preset_io
void set_stream(istream *i)
bool read_kv(const char *key, T &v)
virtual const char * what() const
AbstractStateIO * state_io
streampos get_streampos()
const Glib::ustring & get_current_name()
const SettingsFileHeader & get_header() const
Glib::ustring current_name
string current_value() const
JsonWriter * create_writer(int n)
Glib::ustring current_bank
bool has_entry(const Glib::ustring &bank) const
void write_kv(const char *key, int i)
JsonParser * create_reader(const Glib::ustring &name)
JsonExceptionEOF(const char *desc)
float current_value_float()
const std::string & get_filename() const
double current_value_double()
void write_kv(const char *key, const char *p)
iterator(bl_type::iterator i)
const Glib::ustring & get_current_bank()