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.0KB

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