home *** CD-ROM | disk | FTP | other *** search
/ Enter 2007 January / EnterDVD_01_2007.iso / Multimedia / ZynAddSubFX 2.2.0 / Setup_ZynAddSubFX-2.2.0.exe / source code / Params / PresetsStore.C < prev    next >
Encoding:
C/C++ Source or Header  |  2005-03-14  |  5.0 KB  |  182 lines

  1. /*
  2.   ZynAddSubFX - a software synthesizer
  3.  
  4.   PresetsStore.C - Presets and Clipboard store
  5.   Copyright (C) 2002-2005 Nasca Octavian Paul
  6.   Author: Nasca Octavian Paul
  7.  
  8.   This program is free software; you can redistribute it and/or modify
  9.   it under the terms of version 2 of the GNU General Public License 
  10.   as published by the Free Software Foundation.
  11.  
  12.   This program is distributed in the hope that it will be useful,
  13.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.   GNU General Public License (version 2) for more details.
  16.  
  17.   You should have received a copy of the GNU General Public License (version 2)
  18.   along with this program; if not, write to the Free Software Foundation,
  19.   Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  20.  
  21. */
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <dirent.h>
  25. #include <sys/stat.h>
  26.  
  27. #include "PresetsStore.h"
  28. #include "../Misc/Util.h"
  29.  
  30. PresetsStore presetsstore;
  31.  
  32. PresetsStore::PresetsStore(){
  33.     clipboard.data=NULL;
  34.     clipboard.type[0]=0;
  35.     
  36.     for (int i=0;i<MAX_PRESETS;i++){
  37.     presets[i].file=NULL;
  38.     presets[i].name=NULL;
  39.     };
  40.     
  41. };
  42.  
  43. PresetsStore::~PresetsStore(){
  44.     if (clipboard.data!=NULL) delete (clipboard.data);
  45.     clearpresets();
  46. };
  47.  
  48. //Clipboard management
  49.  
  50. void PresetsStore::copyclipboard(XMLwrapper *xml,char *type){
  51.     strcpy(clipboard.type,type);
  52.     if (clipboard.data!=NULL) delete (clipboard.data);
  53.     clipboard.data=xml->getXMLdata();
  54. };
  55.  
  56. bool PresetsStore::pasteclipboard(XMLwrapper *xml){
  57.     if (clipboard.data!=NULL) xml->putXMLdata(clipboard.data);
  58.     else return(false);
  59.     return(true);    
  60. };
  61.  
  62. bool PresetsStore::checkclipboardtype(char *type){
  63.     //makes LFO's compatible
  64.     if ((strstr(type,"Plfo")!=NULL)&&(strstr(clipboard.type,"Plfo")!=NULL)) return(true);
  65.     return(strcmp(type,clipboard.type)==0);
  66. };
  67.  
  68. //Presets management
  69. void PresetsStore::clearpresets(){
  70.     for (int i=0;i<MAX_PRESETS;i++){
  71.     if (presets[i].file!=NULL) {
  72.         delete(presets[i].file);
  73.         presets[i].file=NULL;
  74.     };
  75.     if (presets[i].name!=NULL) {
  76.         delete(presets[i].name);
  77.         presets[i].name=NULL;
  78.     };
  79.     };
  80.     
  81. };
  82.  
  83. //a helper function that compares 2 presets[]
  84. int Presets_compar(const void *a,const void *b){
  85.     struct PresetsStore::presetstruct *p1= (PresetsStore::presetstruct *)a;
  86.     struct PresetsStore::presetstruct *p2= (PresetsStore::presetstruct *)b;
  87.     if (((p1->name)==NULL)||((p2->name)==NULL)) return(0);
  88.     
  89.     return(strcasecmp(p1->name,p2->name)<0);
  90. };
  91.  
  92.  
  93. void PresetsStore::rescanforpresets(char *type){
  94.     clearpresets();
  95.     int presetk=0;
  96.     char ftype[MAX_STRING_SIZE];
  97.     snprintf(ftype,MAX_STRING_SIZE,".%s.xpz",type);
  98.         
  99.     for (int i=0;i<MAX_BANK_ROOT_DIRS;i++){
  100.     if (config.cfg.presetsDirList[i]==NULL) continue;
  101.     char *dirname=config.cfg.presetsDirList[i];
  102.     DIR *dir=opendir(dirname);
  103.     if (dir==NULL) continue;
  104.     struct dirent *fn;
  105.     while((fn=readdir(dir))){
  106.         const char *filename=fn->d_name;
  107.         if (strstr(filename,ftype)==NULL) continue;
  108.         
  109.         
  110.         presets[presetk].file=new char [MAX_STRING_SIZE];
  111.         presets[presetk].name=new char [MAX_STRING_SIZE];
  112.         char tmpc=dirname[strlen(dirname)-1];
  113.         char *tmps="/";
  114.         if ((tmpc=='/')||(tmpc=='\\')) tmps="";
  115.         snprintf(presets[presetk].file,MAX_STRING_SIZE,"%s%s%s",dirname,tmps,filename);
  116.         snprintf(presets[presetk].name,MAX_STRING_SIZE,"%s",filename);
  117.  
  118.         char *tmp=strstr(presets[presetk].name,ftype);
  119.         if (tmp!=NULL) tmp[0]='\0';
  120.         presetk++; if (presetk>=MAX_PRESETS) return;
  121.     };    
  122.  
  123.     closedir(dir);
  124.     };
  125.  
  126.     //sort the presets
  127.     for (int j=0;j<MAX_PRESETS-1;j++){
  128.     for (int i=j+1;i<MAX_PRESETS;i++){
  129.         if (Presets_compar(&presets[i],&presets[j])) {
  130.         presetstruct tmp=presets[i];
  131.         presets[i]=presets[j];
  132.         presets[j]=tmp;
  133.         };
  134.     };
  135.     };
  136. };
  137.  
  138. void PresetsStore::copypreset(XMLwrapper *xml,char *type, const char *name){
  139.     char filename[MAX_STRING_SIZE],tmpfilename[MAX_STRING_SIZE];
  140.     
  141.     if (config.cfg.presetsDirList[0]==NULL) return;
  142.     
  143.     snprintf(tmpfilename,MAX_STRING_SIZE,"%s",name);
  144.  
  145.     //make the filenames legal
  146.     for (int i=0;i<(int) strlen(tmpfilename);i++) {
  147.     char c=tmpfilename[i];
  148.     if ((c>='0')&&(c<='9')) continue;
  149.     if ((c>='A')&&(c<='Z')) continue;
  150.     if ((c>='a')&&(c<='z')) continue;
  151.     if ((c=='-')||(c==' ')) continue;
  152.     tmpfilename[i]='_';
  153.     };
  154.     
  155.     char *dirname=config.cfg.presetsDirList[0];
  156.     char tmpc=dirname[strlen(dirname)-1];
  157.     char *tmps="/";
  158.     if ((tmpc=='/')||(tmpc=='\\')) tmps="";
  159.  
  160.     snprintf(filename,MAX_STRING_SIZE,"%s%s%s.%s.xpz",dirname,tmps,name,type);
  161.     
  162.     xml->saveXMLfile(filename);
  163. };
  164.  
  165. bool PresetsStore::pastepreset(XMLwrapper *xml, int npreset){
  166.     npreset--;
  167.     if (npreset>=MAX_PRESETS) return(false);
  168.     char *filename=presets[npreset].file;
  169.     if (filename==NULL) return(false);
  170.     bool result=(xml->loadXMLfile(filename)>=0);
  171.     return(result);
  172. };
  173.  
  174. void PresetsStore::deletepreset(int npreset){
  175.     npreset--;
  176.     if (npreset>=MAX_PRESETS) return;
  177.     char *filename=presets[npreset].file;
  178.     if (filename==NULL) return;
  179.     remove(filename);
  180. };
  181.  
  182.