Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PresetsStore.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. PresetsStore.cpp - Presets and Clipboard store
  4. Copyright (C) 2002-2005 Nasca Octavian Paul
  5. Author: Nasca Octavian Paul
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10. */
  11. #include <iostream>
  12. #include <algorithm>
  13. #include <cctype>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <dirent.h>
  17. #include <sys/stat.h>
  18. #include "PresetsStore.h"
  19. #include "../Misc/XMLwrapper.h"
  20. #include "../Misc/Util.h"
  21. #include "../Misc/Config.h"
  22. using namespace std;
  23. namespace zyncarla {
  24. //XXX to remove
  25. //PresetsStore presetsstore;
  26. PresetsStore::PresetsStore(const Config& config) : config(config)
  27. {
  28. }
  29. PresetsStore::~PresetsStore()
  30. {
  31. }
  32. //Clipboard management
  33. void PresetsStore::copyclipboard(XMLwrapper &xml, char *type)
  34. {
  35. clipboard.type = type;
  36. const char *tmp = xml.getXMLdata();
  37. clipboard.data = tmp;
  38. free((void*)tmp);
  39. }
  40. bool PresetsStore::pasteclipboard(XMLwrapper &xml)
  41. {
  42. if(clipboard.data.empty())
  43. return false;
  44. xml.putXMLdata(clipboard.data.c_str());
  45. return true;
  46. }
  47. bool PresetsStore::checkclipboardtype(const char *type)
  48. {
  49. //makes LFO's compatible
  50. if(strstr(type, "Plfo") && strstr(clipboard.type.c_str(), "Plfo"))
  51. return true;
  52. return type == clipboard.type;
  53. }
  54. //Presets management
  55. void PresetsStore::clearpresets()
  56. {
  57. presets.clear();
  58. }
  59. //a helper function that compares 2 presets[]
  60. bool PresetsStore::presetstruct::operator<(const presetstruct &b) const
  61. {
  62. return name < b.name;
  63. }
  64. void PresetsStore::scanforpresets()
  65. {
  66. clearpresets();
  67. string ftype = ".xpz";
  68. for(int i = 0; i < MAX_BANK_ROOT_DIRS; ++i) {
  69. if(config.cfg.presetsDirList[i].empty())
  70. continue;
  71. //open directory
  72. string dirname = config.cfg.presetsDirList[i];
  73. DIR *dir = opendir(dirname.c_str());
  74. if(dir == NULL)
  75. continue;
  76. struct dirent *fn;
  77. //check all files in directory
  78. while((fn = readdir(dir))) {
  79. string filename = fn->d_name;
  80. if(filename.find(ftype) == string::npos)
  81. continue;
  82. //ensure proper path is formed
  83. char tmpc = dirname[dirname.size() - 1];
  84. const char *tmps;
  85. if((tmpc == '/') || (tmpc == '\\'))
  86. tmps = "";
  87. else
  88. tmps = "/";
  89. string location = "" + dirname + tmps + filename;
  90. //trim file type off of name
  91. string name_type = filename.substr(0, filename.find(ftype));
  92. size_t tmp = name_type.find_last_of(".");
  93. if(tmp == string::npos)
  94. continue;
  95. string type = name_type.substr(tmp+1);
  96. string name = name_type.substr(0, tmp);
  97. //put on list
  98. presets.push_back(presetstruct{location, name, type});
  99. }
  100. closedir(dir);
  101. }
  102. //sort the presets
  103. sort(presets.begin(), presets.end());
  104. }
  105. void PresetsStore::copypreset(XMLwrapper &xml, char *type, string name)
  106. {
  107. if(config.cfg.presetsDirList[0].empty())
  108. return;
  109. //make the filenames legal
  110. name = legalizeFilename(name);
  111. //make path legal
  112. const string dirname = config.cfg.presetsDirList[0];
  113. char tmpc = dirname[dirname.size() - 1];
  114. const char *tmps;
  115. if((tmpc == '/') || (tmpc == '\\'))
  116. tmps = "";
  117. else
  118. tmps = "/";
  119. string filename("" + dirname + tmps + name + "." + &type[1] + ".xpz");
  120. xml.saveXMLfile(filename, config.cfg.GzipCompression);
  121. }
  122. bool PresetsStore::pastepreset(XMLwrapper &xml, unsigned int npreset)
  123. {
  124. npreset--;
  125. if(npreset >= presets.size())
  126. return false;
  127. string filename = presets[npreset].file;
  128. if(filename.empty())
  129. return false;
  130. return xml.loadXMLfile(filename) >= 0;
  131. }
  132. void PresetsStore::deletepreset(unsigned int npreset)
  133. {
  134. npreset--;
  135. if(npreset >= presets.size())
  136. return;
  137. string filename = presets[npreset].file;
  138. if(filename.empty())
  139. return;
  140. remove(filename.c_str());
  141. }
  142. void PresetsStore::deletepreset(std::string filename)
  143. {
  144. for(int i=0; i<(int)presets.size(); ++i) {
  145. if(presets[i].file == filename) {
  146. presets.erase(presets.begin()+i);
  147. remove(filename.c_str());
  148. return;
  149. }
  150. }
  151. }
  152. }