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.

168 lines
4.6KB

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