From cbc0145c209bb775a1070bee6936dbc3e832450e Mon Sep 17 00:00:00 2001 From: aj_genius Date: Sat, 13 Mar 2004 01:16:29 +0000 Subject: [PATCH] Rewrite config file reading/writing to safely handle invalid sections/entries and safely handle differing order sections/idents and missing etc. This is so that it can handle driver specific settings in the future(eg. won't die on ALSA settings if alsa driver doesn't exist/missing entry etc). --- SpiralSound/SpiralInfo.C | 404 ++++++++++++++++++++++++++++++++------- SpiralSound/SpiralInfo.h | 10 + 2 files changed, 345 insertions(+), 69 deletions(-) diff --git a/SpiralSound/SpiralInfo.C b/SpiralSound/SpiralInfo.C index c7a68db..17bb359 100644 --- a/SpiralSound/SpiralInfo.C +++ b/SpiralSound/SpiralInfo.C @@ -77,7 +77,7 @@ SpiralInfo* SpiralInfo::Get() { } SpiralInfo::SpiralInfo(): -m_Version(1) +m_Version(2) { m_ResFileName = getenv("HOME"); m_ResFileName += "/.spiralmodular"; @@ -99,47 +99,316 @@ void SpiralInfo::SavePrefs() { StreamOutPrefs (o); } -void SpiralInfo::StreamInPrefs (istream &s) { +void SpiralInfo::StreamOutMainColourPrefs (ostream &s) +{ + s << "[MAIN COLOURS]" << endl; + s << "ToolBoxColour = " << GUICOL_Tool << endl; + s << "ButtonColour = " << GUICOL_Button << endl; + s << "CanvasColour = " << GUICOL_Canvas << endl; + s << "DeviceColour = " << GUICOL_Device << endl; + s << "DeviceBoxType = " << GUIDEVICE_Box << endl; + + s << endl; //section spacer +} + +bool SpiralInfo::StreamInMainColourPrefs (istream &s, string §ion, string &ident) +{ + char temp[256]; + bool result = false; + + /* just ignore if this isn't calling us */ + if (section == "MAIN COLOURS") + { + if (ident == "ToolBoxColour") + s >> GUICOL_Tool; + + else if (ident == "ButtonColour") + s >> GUICOL_Button; + + else if (ident == "CanvasColour") + s >> GUICOL_Canvas; + + else if (ident == "DeviceColour") + s >> GUICOL_Device; + + else if (ident == "DeviceBoxType") + s >> GUIDEVICE_Box; + + else + /* ignore idents that don't belong here/we don't know about */ + s >> temp; + + result = true; + } + + return result; +} + +void SpiralInfo::StreamOutDeviceColourPrefs (ostream &s) +{ + s << "[DEVICE COLOURS]" << endl; + s << "GUIColour = " << GUI_COLOUR << endl; + s << "ScopeBGColour = " << SCOPE_BG_COLOUR << endl; + s << "ScopeFGColour = " << SCOPE_FG_COLOUR << endl; + s << "ScopeSelColour = " << SCOPE_SEL_COLOUR << endl; + s << "ScopeIndColour = " << SCOPE_IND_COLOUR << endl; + s << "ScopeMrkColour = " << SCOPE_MRK_COLOUR << endl; + + s << endl; //section spacer +} + +bool SpiralInfo::StreamInDeviceColourPrefs (istream &s, string §ion, string &ident) +{ + char temp[256]; + bool result = false; + + /* just ignore if this isn't calling us */ + if (section == "DEVICE COLOURS") + { + if (ident == "GUIColour") + s >> GUI_COLOUR; + + else if (ident == "ScopeBGColour") + s >> SCOPE_BG_COLOUR; + + else if (ident == "ScopeFGColour") + s >> SCOPE_FG_COLOUR; + + else if (ident == "ScopeSelColour") + s >> SCOPE_SEL_COLOUR; + + else if (ident == "ScopeIndColour") + s >> SCOPE_IND_COLOUR; + + else if (ident == "ScopeMrkColour") + s >> SCOPE_MRK_COLOUR; + + else + /* ignore idents that don't belong here/we don't know about */ + s >> temp; + + result = true; + } + + return result; +} + +void SpiralInfo::StreamOutAudioSettingsPrefs (ostream &s) +{ + s << "[AUDIO SETTINGS]" << endl; + + s << "BufferSize = " << BUFSIZE << endl; + s << "FragmentSize = " << FRAGSIZE << endl; + s << "FragmentCount = " << FRAGCOUNT << endl; + s << "Samplerate = " << SAMPLERATE << endl; + s << "WantMidi = " << WANTMIDI << endl; + s << "FilterGranularity = " << FILTERGRAN << endl; + s << "Output = " << OUTPUTFILE << endl; + s << "Midi = " << MIDIFILE << endl; + s << "UsePluginList = " << USEPLUGINLIST << endl; + s << "Polyphony = " << POLY << endl; + + s << endl; //section spacer +} + +bool SpiralInfo::StreamInAudioSettingsPrefs (istream &s, string §ion, string &ident) +{ + char temp[256]; + bool result = false; + + /* just ignore if this isn't calling us */ + if (section == "AUDIO SETTINGS") + { + if (ident == "BufferSize") + s >> BUFSIZE; + + else if (ident == "FragmentSize") + s >> FRAGSIZE; + + else if (ident == "FragmentCount") + s >> FRAGCOUNT; + + else if (ident == "Samplerate") + s >> SAMPLERATE; + + else if (ident == "WantMidi") + s >> WANTMIDI; + + else if (ident == "FilterGranularity") + s >> FILTERGRAN; + + else if (ident == "Output") + s >> OUTPUTFILE; + + else if (ident == "Midi") + s >> MIDIFILE; + + else if (ident == "UsePluginList") + s >> USEPLUGINLIST; + + else if (ident == "Polyphony") + s >> POLY; + + else + /* ignore idents that don't belong here/we don't know about */ + s >> temp; + + result = true; + } + + return result; +} + +void SpiralInfo::StreamInPrefs (istream &s) +{ // These lines are from SpiralInfo char temp[256]; + s >> temp >> temp >> temp; s >> temp >> temp >> m_Version; s >> temp >> temp >> LOCALE; - s >> temp >> temp >> BUFSIZE; - s >> temp >> temp >> FRAGSIZE; - s >> temp >> temp >> FRAGCOUNT; - s >> temp >> temp >> SAMPLERATE; - s >> temp >> temp >> WANTMIDI; - s >> temp >> temp >> FILTERGRAN; - s >> temp >> temp >> OUTPUTFILE; - s >> temp >> temp >> MIDIFILE; - s >> temp >> temp >> USEPLUGINLIST; - s >> temp >> temp >> POLY; - if (m_Version>0) { - s >> temp >> temp >> GUI_COLOUR; - s >> temp >> temp >> SCOPE_BG_COLOUR; - s >> temp >> temp >> SCOPE_FG_COLOUR; - s >> temp >> temp >> SCOPE_SEL_COLOUR; - s >> temp >> temp >> SCOPE_IND_COLOUR; - s >> temp >> temp >> SCOPE_MRK_COLOUR; - } - s >> temp >> temp; - s >> temp >> temp >> GUICOL_Tool; - s >> temp >> temp >> GUICOL_Button; - s >> temp >> temp >> GUICOL_Canvas; - s >> temp >> temp >> GUICOL_Device; - s >> temp >> temp >> GUIDEVICE_Box; - s >> temp >> temp >> PLUGIN_PATH; - s >> temp >> temp; - string st; - PLUGINVEC.clear(); - if (USEPLUGINLIST) { - while (st!="end" && !s.eof()) { - s >> st; - if (st!="end") PLUGINVEC.push_back (st); - } - } + switch (m_Version) + { + case 2: + { + char test; + string section; + + /* skip next line, since this is just a spacer between file header + and the rest of the file config + */ + s.seekg (2, ios::cur ); + + while (! s.eof ()) + { + /* peek first char to find out if this is a delimiter line + a header line, or a regular section + */ + test = s.peek(); + + switch (test) + { + /* if the line starts with an open bracket just presume its a header */ + case '[': + { + /* skip open bracket */ + s.ignore(1); + + /* read header line up to close bracket or end of line */ + s.getline (temp, 256, ']'); + + /* move to next line */ + s.seekg (1, ios::cur ); + + /* assign current section string for easy testing */ + section = temp; + cout << "at section " + section << endl; + + /* the plugins format is a bit odd so we + do it all here at once mostly the same + as the old way + */ + if (section == "PLUGINS") + { + s >> temp >> temp >> PLUGIN_PATH; + s >> temp >> temp; + + string st; + PLUGINVEC.clear(); + while (st!="end" && !s.eof()) + { + s >> st; + if (st!="end" && USEPLUGINLIST) PLUGINVEC.push_back (st); + } + + /* move to next line */ + s.seekg (1, ios::cur ); + } + } + break; + + case '\n': + { + /* skip this line, its just a spacer between section */ + s.seekg (1, ios::cur ); + } + break; + + default: + { + string ident; + s >> ident;//read in identifier name + s >> temp;//skip "=" + + /* Check to see if this a Audio Settings section */ + if (StreamInAudioSettingsPrefs(s, section, ident)) { cout << "successfully read " + section + ", " + ident << endl; } + + else + /* Check next to see if this a Main Colour section */ + if (StreamInMainColourPrefs(s, section, ident)) { cout << "successfully read " + section + ", " + ident << endl; } + + else + /* Check next to see if this a Device Colour section */ + if (StreamInDeviceColourPrefs(s, section, ident)) { cout << "successfully read " + section + ", " + ident << endl; } + + + else + /* lastly just ignore idents for sections that are invalid or we don't know about */ + s >> temp; + + s.seekg (1, ios::cur ); //move to next line + } + } + } + } + break; + + default: //old file fallback + { + s >> temp >> temp >> BUFSIZE; + s >> temp >> temp >> FRAGSIZE; + s >> temp >> temp >> FRAGCOUNT; + s >> temp >> temp >> SAMPLERATE; + + s >> temp >> temp >> WANTMIDI; + s >> temp >> temp >> FILTERGRAN; + + s >> temp >> temp >> OUTPUTFILE; + s >> temp >> temp >> MIDIFILE; + + s >> temp >> temp >> USEPLUGINLIST; + + s >> temp >> temp >> POLY; + if (m_Version>0) { + s >> temp >> temp >> GUI_COLOUR; + s >> temp >> temp >> SCOPE_BG_COLOUR; + s >> temp >> temp >> SCOPE_FG_COLOUR; + s >> temp >> temp >> SCOPE_SEL_COLOUR; + s >> temp >> temp >> SCOPE_IND_COLOUR; + s >> temp >> temp >> SCOPE_MRK_COLOUR; + } + s >> temp >> temp; + s >> temp >> temp >> GUICOL_Tool; + s >> temp >> temp >> GUICOL_Button; + s >> temp >> temp >> GUICOL_Canvas; + s >> temp >> temp >> GUICOL_Device; + s >> temp >> temp >> GUIDEVICE_Box; + s >> temp >> temp >> PLUGIN_PATH; + s >> temp >> temp; + + string st; + PLUGINVEC.clear(); + if (USEPLUGINLIST) + { + while (st!="end" && !s.eof()) + { + s >> st; + if (st!="end") PLUGINVEC.push_back (st); + } + } + } + } #if __APPLE__ // ignore custom paths, plugins are encapsulated in the app anyway // this prevents the program to fail if the user move the application icon @@ -147,39 +416,36 @@ void SpiralInfo::StreamInPrefs (istream &s) { #endif } -void SpiralInfo::StreamOutPrefs (ostream &s) { - // These lines are from SpiralInfo - s << "SpiralSound resource file" << endl << endl; - s << "Version = " << m_Version << endl; - s << "Locale = " << LOCALE << endl; - s << "BufferSize = " << BUFSIZE << endl; - s << "FragmentSize = " << FRAGSIZE << endl; - s << "FragmentCount = " << FRAGCOUNT << endl; - s << "Samplerate = " << SAMPLERATE << endl; - s << "WantMidi = " << WANTMIDI << endl; - s << "FilterGranularity = " << FILTERGRAN << endl; - s << "Output = " << OUTPUTFILE << endl; - s << "Midi = " << MIDIFILE << endl; - s << "UsePluginList = " << USEPLUGINLIST << endl; - s << "Polyphony = " << POLY << endl; - s << "GUIColour = " << GUI_COLOUR << endl; - s << "ScopeBGColour = " << SCOPE_BG_COLOUR << endl; - s << "ScopeFGColour = " << SCOPE_FG_COLOUR << endl; - s << "ScopeSelColour = " << SCOPE_SEL_COLOUR << endl; - s << "ScopeIndColour = " << SCOPE_IND_COLOUR << endl; - s << "ScopeMrkColour = " << SCOPE_MRK_COLOUR << endl; - s << endl << "SpiralSynthModular Info:" << endl << endl; - s << "ToolBoxColour = " << GUICOL_Tool << endl; - s << "ButtonColour = " << GUICOL_Button << endl; - s << "CanvasColour = " << GUICOL_Canvas << endl; - s << "DeviceColour = " << GUICOL_Device << endl; - s << "DeviceBoxType = " << GUIDEVICE_Box << endl << endl; - s << "PluginPath = " << PLUGIN_PATH << endl << endl; - s << "PluginList = " << endl; - for (vector::iterator i=PLUGINVEC.begin(); i!=PLUGINVEC.end(); i++) { - s << *i << endl; - } - s << "end" << endl; +void SpiralInfo::StreamOutPrefs (ostream &s) +{ + s << "SpiralSound resource file" << endl << endl; + s << "Version = " << m_Version << endl; + s << "Locale = " << LOCALE << endl; + + s << endl; //section spacer + + /* Write Audio Settings section */ + StreamOutAudioSettingsPrefs(s); + + /* Write Main Colour section */ + StreamOutMainColourPrefs(s); + + /* Write Device Colour section */ + StreamOutDeviceColourPrefs(s); + + /* Write PLUGINS section + + because of the plugin path output format it must always be appended with an "end" line, + and currently this MUST be the last thing in the file + */ + s << "[PLUGINS]" << endl; + s << "PluginPath = " << PLUGIN_PATH << endl; + s << "PluginList = " << endl; + for (vector::iterator i=PLUGINVEC.begin(); i!=PLUGINVEC.end(); i++) + { + s << *i << endl; + } + s << "end" << endl; } void SpiralInfo::Alert (string Text) { diff --git a/SpiralSound/SpiralInfo.h b/SpiralSound/SpiralInfo.h index 297a681..3ced283 100644 --- a/SpiralSound/SpiralInfo.h +++ b/SpiralSound/SpiralInfo.h @@ -72,6 +72,16 @@ class SpiralInfo { string m_ResFileName; int m_Version; static SpiralInfo *m_SpiralInfo; + + void SpiralInfo::StreamOutMainColourPrefs (ostream &s); + bool SpiralInfo::StreamInMainColourPrefs (istream &s, string §ion, string &ident); + + void SpiralInfo::StreamOutDeviceColourPrefs (ostream &s); + bool SpiralInfo::StreamInDeviceColourPrefs (istream &s, string §ion, string &ident); + + void SpiralInfo::StreamOutAudioSettingsPrefs (ostream &s); + bool SpiralInfo::StreamInAudioSettingsPrefs (istream &s, string §ion, string &ident); + void StreamInPrefs (istream &s); void StreamOutPrefs (ostream &s); };