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.

XMLwrapper.h 9.4KB

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