zynaddsubfx

ZynAddSubFX open source synthesizer
Log | Files | Refs | Submodules | LICENSE

XMLwrapper.h (9606B)


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