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.

284 lines
9.0KB

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