paulstretch_cpp

PaulStretch
Log | Files | Refs | LICENSE

XMLwrapper.h (4705B)


      1 /*
      2   XML.h - XML wrapper
      3   Copyright (C) 2003-2011 Nasca Octavian Paul
      4   Author: Nasca Octavian Paul
      5 
      6   This program is free software; you can redistribute it and/or modify
      7   it under the terms of version 2 of the GNU General Public License 
      8   as published by the Free Software Foundation.
      9 
     10   This program is distributed in the hope that it will be useful,
     11   but WITHOUT ANY WARRANTY; without even the implied warranty of
     12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13   GNU General Public License (version 2 or later) for more details.
     14 
     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 */
     20 
     21 #include <mxml.h>
     22 #include "globals.h"
     23 
     24 #ifndef XML_WRAPPER_H
     25 #define XML_WRAPPER_H
     26 
     27 #define TMPSTR_SIZE 50
     28 
     29 //the maxim tree depth
     30 #define STACKSIZE 100
     31 
     32 class XMLwrapper{
     33     public:
     34 	XMLwrapper();
     35 	~XMLwrapper();
     36 	
     37 	/********************************/
     38 	/*         SAVE to XML          */
     39 	/********************************/
     40 
     41 	//returns 0 if ok or -1 if the file cannot be saved
     42 	int saveXMLfile(const char *filename);
     43 
     44 	//returns the new allocated string that contains the XML data (used for clipboard)
     45 	//the string is NULL terminated
     46 	char *getXMLdata();
     47 	
     48 	//add simple parameter (name and value)
     49 	void addpar(const char *name,int val);
     50 	void addparreal(const char *name,REALTYPE val);
     51 	
     52 	//add boolean parameter (name and boolean value)
     53 	//if the value is 0 => "yes", else "no"
     54 	void addparbool(const char *name,int val);
     55 
     56 	//add string parameter (name and string)
     57 	void addparstr(const char *name,const char *val);
     58 
     59 	//add a branch
     60 	void beginbranch(const char *name);
     61 	void beginbranch(const char *name, int id);
     62 
     63 	//this must be called after each branch (nodes that contains child nodes)
     64 	void endbranch();
     65 
     66 	/********************************/
     67 	/*        LOAD from XML         */
     68 	/********************************/
     69 	
     70 	//returns 0 if ok or -1 if the file cannot be loaded
     71 	int loadXMLfile(const char *filename);
     72 
     73 	//used by the clipboard
     74 	bool putXMLdata(char *xmldata);
     75     
     76 	//enter into the branch
     77 	//returns 1 if is ok, or 0 otherwise
     78 	int enterbranch(const char *name);
     79 
     80 		
     81 	//enter into the branch with id
     82 	//returns 1 if is ok, or 0 otherwise
     83 	int enterbranch(const char *name, int id);
     84 
     85 	//exits from a branch
     86 	void exitbranch();
     87 	
     88 	//get the the branch_id and limits it between the min and max
     89 	//if min==max==0, it will not limit it
     90 	//if there isn't any id, will return min
     91 	//this must be called only imediately after enterbranch()
     92 	int getbranchid(int min, int max);
     93 
     94 	//it returns the parameter and limits it between min and max
     95 	//if min==max==0, it will not limit it
     96 	//if no parameter will be here, the defaultpar will be returned
     97 	int getpar(const char *name,int defaultpar,int min,int max);
     98 
     99 	//the same as getpar, but the limits are 0 and 127
    100 	int getpar127(const char *name,int defaultpar);
    101 	
    102 	int getparbool(const char *name,int defaultpar);
    103 
    104 	void getparstr(const char *name,char *par,int maxstrlen);
    105 	REALTYPE getparreal(const char *name,REALTYPE defaultpar);
    106 	REALTYPE getparreal(const char *name,REALTYPE defaultpar,REALTYPE min,REALTYPE max);
    107 
    108 	bool minimal;//false if all parameters will be stored (used only for clipboard)
    109 	
    110 	//opens a file and parse only the "information" data on it
    111 	//returns "true" if all went ok or "false" on errors
    112 	bool checkfileinformation(char *filename);
    113 	
    114     private:
    115     
    116 	int dosavefile(char *filename,int compression,char *xmldata);
    117 	char *doloadfile(const char *filename);
    118 
    119     
    120 	mxml_node_t *tree;//all xml data
    121 	mxml_node_t *root;//xml data used
    122 	mxml_node_t *node;//current node
    123 	mxml_node_t *info;//this node is used to store the information about the data
    124 	
    125 	//adds params like this:
    126 	// <name>
    127 	//returns the node
    128 	mxml_node_t *addparams0(const char *name);
    129 
    130 	//adds params like this:
    131 	// <name par1="val1">
    132 	//returns the node
    133 	mxml_node_t *addparams1(const char *name,const char *par1,const char *val1);
    134 
    135 	//adds params like this:
    136 	// <name par1="val1" par2="val2">
    137 	//returns the node
    138 	mxml_node_t *addparams2(const char *name,const char *par1,const char *val1,const char *par2,const  char *val2);
    139 	
    140 	char *int2str(int x);
    141 	char *real2str(REALTYPE x);
    142 	
    143 	int str2int(const char *str);
    144 	REALTYPE str2real(const char *str);
    145 	
    146 	char tmpstr[TMPSTR_SIZE];	
    147 	
    148 	
    149 	//this is used to store the parents
    150 	mxml_node_t *parentstack[STACKSIZE];
    151 	int stackpos;
    152 
    153 	
    154 	void push(mxml_node_t *node);
    155 	mxml_node_t *pop();
    156 	mxml_node_t *peek();
    157 
    158 	//theese are used to store the values
    159 	struct{
    160 	    struct {
    161 		int major,minor;
    162 	    }xml_version;
    163 	}values;
    164 	
    165 };
    166 
    167 #endif