zynaddsubfx

ZynAddSubFX open source synthesizer
Log | Files | Refs | Submodules | LICENSE

commit c69f6ad8a5d2374600a1bbdb4066c95017de8a64
parent ddc646792d0f0f4557d08db0a8055c968c6b1c12
Author: fundamental <mark.d.mccurry@gmail.com>
Date:   Tue, 11 May 2010 11:28:37 -0400

Presets: Fixing loading/saving presets bug

Note: The likely source of this issue was the concatenation of an 'n' onto the
      filenames prior to the extension.
      If there are issues using old presets, removing that 'n' should resolve
      the issues.

Diffstat:
Msrc/Params/Presets.cpp | 6++----
Msrc/Params/PresetsStore.cpp | 103++++++++++++++++++++++++++++++-------------------------------------------------
Msrc/Params/PresetsStore.h | 14+++++++++-----
Msrc/UI/PresetsUI.fl | 17+++++++++--------
4 files changed, 59 insertions(+), 81 deletions(-)

diff --git a/src/Params/Presets.cpp b/src/Params/Presets.cpp @@ -47,11 +47,10 @@ void Presets::copy(const char *name) char type[MAX_PRESETTYPE_SIZE]; strcpy(type, this->type); - strcat(type, "n"); + //strcat(type, "n"); if(name == NULL) if(strstr(type, "Plfo") != NULL) strcpy(type, "Plfo"); - ; xml->beginbranch(type); add2XML(xml); @@ -69,12 +68,11 @@ void Presets::paste(int npreset) { char type[MAX_PRESETTYPE_SIZE]; strcpy(type, this->type); - strcat(type, "n"); + //strcat(type, "n"); if(npreset == 0) if(strstr(type, "Plfo") != NULL) strcpy(type, "Plfo"); - ; XMLwrapper *xml = new XMLwrapper(); if(npreset == 0) { diff --git a/src/Params/PresetsStore.cpp b/src/Params/PresetsStore.cpp @@ -19,6 +19,8 @@ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include <iostream> +#include <algorithm> #include <stdlib.h> #include <string.h> #include <dirent.h> @@ -27,17 +29,14 @@ #include "PresetsStore.h" #include "../Misc/Util.h" +using namespace std; + PresetsStore presetsstore; PresetsStore::PresetsStore() { clipboard.data = NULL; clipboard.type[0] = 0; - - for(int i = 0; i < MAX_PRESETS; i++) { - presets[i].file = NULL; - presets[i].name = NULL; - } } PresetsStore::~PresetsStore() @@ -78,90 +77,66 @@ bool PresetsStore::checkclipboardtype(char *type) //Presets management void PresetsStore::clearpresets() { - for(int i = 0; i < MAX_PRESETS; i++) { - if(presets[i].file != NULL) { - delete (presets[i].file); - presets[i].file = NULL; - } - if(presets[i].name != NULL) { - delete (presets[i].name); - presets[i].name = NULL; - } - } + presets.clear(); } //a helper function that compares 2 presets[] -int Presets_compar(const void *a, const void *b) +bool PresetsStore::presetstruct::operator<(const presetstruct &b) const { - struct PresetsStore::presetstruct *p1 = (PresetsStore::presetstruct *)a; - struct PresetsStore::presetstruct *p2 = (PresetsStore::presetstruct *)b; - if(((p1->name) == NULL) || ((p2->name) == NULL)) - return 0; - - return strcasecmp(p1->name, p2->name) < 0; + return name < b.name; } -void PresetsStore::rescanforpresets(char *type) +void PresetsStore::rescanforpresets(string type) { + //std::cout << "Scanning For Presets" << std::endl; + //std::cout << "Of Type: " << type << std::endl; + clearpresets(); - int presetk = 0; - char ftype[MAX_STRING_SIZE]; - snprintf(ftype, MAX_STRING_SIZE, ".%s.xpz", type); + string ftype = "." + type + ".xpz"; for(int i = 0; i < MAX_BANK_ROOT_DIRS; i++) { if(config.cfg.presetsDirList[i] == NULL) continue; - char *dirname = config.cfg.presetsDirList[i]; - DIR *dir = opendir(dirname); + + //open directory + string dirname = config.cfg.presetsDirList[i]; + DIR *dir = opendir(dirname.c_str()); if(dir == NULL) continue; struct dirent *fn; + + //check all files in directory while((fn = readdir(dir))) { - const char *filename = fn->d_name; - if(strstr(filename, ftype) == NULL) + string filename = fn->d_name; + if(filename.find(ftype) == string::npos) continue; - - presets[presetk].file = new char [MAX_STRING_SIZE]; - presets[presetk].name = new char [MAX_STRING_SIZE]; - char tmpc = dirname[strlen(dirname) - 1]; + //ensure proper path is formed + char tmpc = dirname[dirname.size() - 1]; const char *tmps; if((tmpc == '/') || (tmpc == '\\')) tmps = ""; else tmps = "/"; - snprintf(presets[presetk].file, - MAX_STRING_SIZE, - "%s%s%s", - dirname, - tmps, - filename); - snprintf(presets[presetk].name, MAX_STRING_SIZE, "%s", filename); - - char *tmp = strstr(presets[presetk].name, ftype); - if(tmp != NULL) - tmp[0] = '\0'; - presetk++; - if(presetk >= MAX_PRESETS) - return; + + string location = "" + dirname + tmps + filename; + + //trim file type off of name + string name = filename.substr(0, filename.find(ftype)); + + //put on list + presets.push_back(presetstruct(location, name)); } closedir(dir); } //sort the presets - for(int j = 0; j < MAX_PRESETS - 1; j++) { - for(int i = j + 1; i < MAX_PRESETS; i++) { - if(Presets_compar(&presets[i], &presets[j])) { - presetstruct tmp = presets[i]; - presets[i] = presets[j]; - presets[j] = tmp; - } - } - } + sort(presets.begin(), presets.end()); } + void PresetsStore::copypreset(XMLwrapper *xml, char *type, const char *name) { char filename[MAX_STRING_SIZE], tmpfilename[MAX_STRING_SIZE]; @@ -207,10 +182,10 @@ void PresetsStore::copypreset(XMLwrapper *xml, char *type, const char *name) bool PresetsStore::pastepreset(XMLwrapper *xml, int npreset) { npreset--; - if(npreset >= MAX_PRESETS) + if(npreset >= presets.size()) return false; - char *filename = presets[npreset].file; - if(filename == NULL) + string filename = presets[npreset].file; + if(filename.empty()) return false; bool result = (xml->loadXMLfile(filename) >= 0); return result; @@ -219,11 +194,11 @@ bool PresetsStore::pastepreset(XMLwrapper *xml, int npreset) void PresetsStore::deletepreset(int npreset) { npreset--; - if(npreset >= MAX_PRESETS) + if(npreset >= presets.size()) return; - char *filename = presets[npreset].file; - if(filename == NULL) + string filename = presets[npreset].file; + if(filename.empty()) return; - remove(filename); + remove(filename.c_str()); } diff --git a/src/Params/PresetsStore.h b/src/Params/PresetsStore.h @@ -20,11 +20,12 @@ */ +#include <string> +#include <vector> #include "../Misc/XMLwrapper.h" #include "../Misc/Config.h" #define MAX_PRESETTYPE_SIZE 30 -#define MAX_PRESETS 1000 class PresetsStore { @@ -43,12 +44,15 @@ class PresetsStore void deletepreset(int npreset); struct presetstruct { - char *file; - char *name; + presetstruct(std::string _file, std::string _name) + :file(_file),name(_name){}; + bool operator<(const presetstruct &b) const; + std::string file; + std::string name; }; - presetstruct presets[MAX_PRESETS]; + std::vector<presetstruct> presets; - void rescanforpresets(char *type); + void rescanforpresets(const std::string type); private: struct { diff --git a/src/UI/PresetsUI.fl b/src/UI/PresetsUI.fl @@ -11,8 +11,7 @@ decl {\#include <stdio.h>} {public decl {\#include <stdlib.h>} {public } -decl {\#include "../Params/PresetsArray.h"} {selected -} +decl {\#include "../Params/PresetsArray.h"} {} decl {\#include "../Params/Presets.h"} {public } @@ -190,12 +189,14 @@ paste(p,pui);} {} pastebrowse->clear(); p->rescanforpresets(); -for (int i=0;i<MAX_PRESETS;i++){ - char *name=presetsstore.presets[i].name; - if (name==NULL) break; - copybrowse->add(name); - pastebrowse->add(name); -};} {} +for (int i=0;i<presetsstore.presets.size();i++){ + std::string name=presetsstore.presets[i].name; + if(name.empty()) + continue; + copybrowse->add(name.c_str()); + pastebrowse->add(name.c_str()); +};} {selected + } } decl {Presets *p;} {public }