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.

280 lines
8.8KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. XMLwrapper.h - XML wrapper
  4. Copyright (C) 2003-2005 Nasca Octavian Paul
  5. Copyright (C) 2009-2009 Mark McCurry
  6. Author: Nasca Octavian Paul
  7. Mark McCurry
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12. */
  13. #include <mxml.h>
  14. #include <string>
  15. #include <vector>
  16. #include "../version.h"
  17. #ifndef XML_WRAPPER_H
  18. #define XML_WRAPPER_H
  19. class XmlAttr
  20. {
  21. public:
  22. std::string name;
  23. std::string value;
  24. };
  25. class XmlNode
  26. {
  27. public:
  28. XmlNode(std::string name_);
  29. std::string name;
  30. std::vector<XmlAttr> attrs;
  31. std::string &operator[](std::string name);
  32. bool has(std::string);
  33. };
  34. /**Mxml wrapper*/
  35. class XMLwrapper
  36. {
  37. public:
  38. /**
  39. * Constructor.
  40. * Will Construct the object and fill in top level branch
  41. * */
  42. XMLwrapper();
  43. /**Destructor*/
  44. ~XMLwrapper();
  45. /**
  46. * Saves the XML to a file.
  47. * @param filename the name of the destination file.
  48. * @returns 0 if ok or -1 if the file cannot be saved.
  49. */
  50. int saveXMLfile(const std::string &filename, int compression) const;
  51. /**
  52. * Return XML tree as a string.
  53. * Note: The string must be freed with free() to deallocate
  54. * @returns a newly allocated NULL terminated string of the XML data.
  55. */
  56. char *getXMLdata() const;
  57. /**
  58. * Add simple parameter.
  59. * @param name The name of the mXML node.
  60. * @param val The string value of the mXml node
  61. */
  62. void addpar(const std::string &name, int val);
  63. /**
  64. * Adds a realtype parameter.
  65. * @param name The name of the mXML node.
  66. * @param val The float value of the node.
  67. */
  68. void addparreal(const std::string &name, float val);
  69. /**
  70. * Add boolean parameter.
  71. * \todo Fix this reverse boolean logic.
  72. * @param name The name of the mXML node.
  73. * @param val The boolean value of the node (0->"yes";else->"no").
  74. */
  75. void addparbool(const std::string &name, int val);
  76. /**
  77. * Add string parameter.
  78. * @param name The name of the mXML node.
  79. * @param val The string value of the node.
  80. */
  81. void addparstr(const std::string &name, const std::string &val);
  82. /**
  83. * Create a new branch.
  84. * @param name Name of new branch
  85. * @see void endbranch()
  86. */
  87. void beginbranch(const std::string &name);
  88. /**
  89. * Create a new branch.
  90. * @param name Name of new branch
  91. * @param id "id" value of branch
  92. * @see void endbranch()
  93. */
  94. void beginbranch(const std::string &name, int id);
  95. /**Closes new branches.
  96. * This must be called to exit each branch created by beginbranch( ).
  97. * @see void beginbranch(const std::string &name)
  98. * @see void beginbranch(const std::string &name, int id)
  99. */
  100. void endbranch();
  101. /**
  102. * Loads file into XMLwrapper.
  103. * @param filename file to be loaded
  104. * @returns 0 if ok or -1 if the file cannot be loaded
  105. */
  106. int loadXMLfile(const std::string &filename);
  107. /**
  108. * Loads string into XMLwrapper.
  109. * @param xmldata NULL terminated string of XML data.
  110. * @returns true if successful.
  111. */
  112. bool putXMLdata(const char *xmldata);
  113. /**
  114. * Enters the branch.
  115. * @param name Name of branch.
  116. * @returns 1 if is ok, or 0 otherwise.
  117. */
  118. int enterbranch(const std::string &name);
  119. /**
  120. * Enter into the branch \c name with id \c id.
  121. * @param name Name of branch.
  122. * @param id Value of branch's "id".
  123. * @returns 1 if is ok, or 0 otherwise.
  124. */
  125. int enterbranch(const std::string &name, int id);
  126. /**Exits from a branch*/
  127. void exitbranch();
  128. /**Get the the branch_id and limits it between the min and max.
  129. * if min==max==0, it will not limit it
  130. * if there isn't any id, will return min
  131. * this must be called only imediately after enterbranch()
  132. */
  133. int getbranchid(int min, int max) const;
  134. /**
  135. * Returns the integer value stored in node name.
  136. * It returns the integer value between the limits min and max.
  137. * If min==max==0, then the value will not be limited.
  138. * If there is no location named name, then defaultpar will be returned.
  139. * @param name The parameter name.
  140. * @param defaultpar The default value if the real value is not found.
  141. * @param min The minimum return value.
  142. * @param max The maximum return value.
  143. */
  144. int getpar(const std::string &name, int defaultpar, int min,
  145. int max) const;
  146. /**
  147. * Returns the integer value stored in the node with range [0,127].
  148. * @param name The parameter name.
  149. * @param defaultpar The default value if the real value is not found.
  150. */
  151. int getpar127(const std::string &name, int defaultpar) const;
  152. /**
  153. * Returns the boolean value stored in the node.
  154. * @param name The parameter name.
  155. * @param defaultpar The default value if the real value is not found.
  156. */
  157. int getparbool(const std::string &name, int defaultpar) const;
  158. /**
  159. * Get the string value stored in the node.
  160. * @param name The parameter name.
  161. * @param par Pointer to destination string
  162. * @param maxstrlen Max string length for destination
  163. */
  164. void getparstr(const std::string &name, char *par, int maxstrlen) const;
  165. /**
  166. * Get the string value stored in the node.
  167. * @param name The parameter name.
  168. * @param defaultpar The default value if the real value is not found.
  169. */
  170. std::string getparstr(const std::string &name,
  171. const std::string &defaultpar) const;
  172. /**
  173. * Returns the real value stored in the node.
  174. * @param name The parameter name.
  175. * @param defaultpar The default value if the real value is not found.
  176. */
  177. float getparreal(const char *name, float defaultpar) const;
  178. /**
  179. * Returns the real value stored in the node.
  180. * @param name The parameter name.
  181. * @param defaultpar The default value if the real value is not found.
  182. * @param min The minimum value
  183. * @param max The maximum value
  184. */
  185. float getparreal(const char *name,
  186. float defaultpar,
  187. float min,
  188. float max) const;
  189. bool minimal; /**<false if all parameters will be stored (used only for clipboard)*/
  190. /**
  191. * Sets the current tree's PAD Synth usage
  192. */
  193. void setPadSynth(bool enabled);
  194. /**
  195. * Checks the current tree for PADsynth usage
  196. */
  197. bool hasPadSynth() const;
  198. void add(const XmlNode &node);
  199. std::vector<XmlNode> getBranch(void) const;
  200. private:
  201. /**
  202. * Save the file.
  203. * @param filename File to save to
  204. * @param compression Level of gzip compression
  205. * @param xmldata String to be saved
  206. */
  207. int dosavefile(const char *filename,
  208. int compression,
  209. const char *xmldata) const;
  210. /**
  211. * Loads specified file and returns data.
  212. *
  213. * Will load a gziped file or an uncompressed file.
  214. * @param filename the file
  215. * @return The decompressed data
  216. */
  217. char *doloadfile(const std::string &filename) const;
  218. mxml_node_t *tree; /**<all xml data*/
  219. mxml_node_t *root; /**<xml data used by zynaddsubfx*/
  220. mxml_node_t *node; /**<current subtree in parsing or writing */
  221. mxml_node_t *info; /**<Node used to store the information about the data*/
  222. /**
  223. * Create mxml_node_t with specified name and parameters
  224. *
  225. * Results should look like:
  226. * <name optionalParam1="value1" optionalParam2="value2" ...>
  227. *
  228. * @param name The name of the xml node
  229. * @param params The number of the attributes
  230. * @param ... const char * pairs that are in the format attribute_name,
  231. * attribute_value
  232. */
  233. mxml_node_t *addparams(const char *name, unsigned int params,
  234. ...) const;
  235. version_type fileversion;
  236. };
  237. #endif