genericparser2.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Filename:- genericparser2.h
  2. #ifndef GENERICPARSER2_H
  3. #define GENERICPARSER2_H
  4. // conditional expression is constant
  5. // conversion from int to char, possible loss of data
  6. // unreferenced inline funciton has been removed
  7. #pragma warning( disable : 4127 4244 4514 )
  8. #ifdef DEBUG_LINKING
  9. #pragma message("...including GenericParser2.h")
  10. #endif
  11. //#include "disablewarnings.h"
  12. #ifdef _JK2EXE
  13. #define trap_Z_Malloc(x, y) Z_Malloc(x,y,qtrue)
  14. #define trap_Z_Free(x) Z_Free(x)
  15. #else
  16. #define trap_Z_Malloc(x, y) gi.Malloc(x,y,qtrue)
  17. #define trap_Z_Free(x) gi.Free(x)
  18. #endif
  19. class CTextPool;
  20. class CGPObject;
  21. class CTextPool
  22. {
  23. private:
  24. char *mPool;
  25. CTextPool *mNext;
  26. int mSize, mUsed;
  27. public:
  28. CTextPool(int initSize = 10240);
  29. ~CTextPool(void);
  30. CTextPool *GetNext(void) { return mNext; }
  31. void SetNext(CTextPool *which) { mNext = which; }
  32. char *GetPool(void) { return mPool; }
  33. int GetUsed(void) { return mUsed; }
  34. char *AllocText(char *text, bool addNULL = true, CTextPool **poolPtr = 0);
  35. };
  36. void CleanTextPool(CTextPool *pool);
  37. class CGPObject
  38. {
  39. protected:
  40. const char *mName;
  41. CGPObject *mNext, *mInOrderNext, *mInOrderPrevious;
  42. public:
  43. CGPObject(const char *initName);
  44. const char *GetName(void) { return mName; }
  45. CGPObject *GetNext(void) { return mNext; }
  46. void SetNext(CGPObject *which) { mNext = which; }
  47. CGPObject *GetInOrderNext(void) { return mInOrderNext; }
  48. void SetInOrderNext(CGPObject *which) { mInOrderNext = which; }
  49. CGPObject *GetInOrderPrevious(void) { return mInOrderPrevious; }
  50. void SetInOrderPrevious(CGPObject *which) { mInOrderPrevious = which; }
  51. bool WriteText(CTextPool **textPool, const char *text);
  52. };
  53. class CGPValue : public CGPObject
  54. {
  55. private:
  56. CGPObject *mList;
  57. public:
  58. CGPValue(const char *initName, const char *initValue = 0);
  59. ~CGPValue(void);
  60. CGPValue *GetNext(void) { return (CGPValue *)mNext; }
  61. CGPValue *Duplicate(CTextPool **textPool = 0);
  62. bool IsList(void);
  63. const char *GetTopValue(void);
  64. CGPObject *GetList(void) { return mList; }
  65. void AddValue(const char *newValue, CTextPool **textPool = 0);
  66. bool Parse(char **dataPtr, CTextPool **textPool);
  67. bool Write(CTextPool **textPool, int depth);
  68. };
  69. class CGPGroup : public CGPObject
  70. {
  71. private:
  72. CGPValue *mPairs, *mInOrderPairs;
  73. CGPValue *mCurrentPair;
  74. CGPGroup *mSubGroups, *mInOrderSubGroups;
  75. CGPGroup *mCurrentSubGroup;
  76. CGPGroup *mParent;
  77. bool mWriteable;
  78. void SortObject(CGPObject *object, CGPObject **unsortedList, CGPObject **sortedList,
  79. CGPObject **lastObject);
  80. public:
  81. CGPGroup(const char *initName = "Top Level", CGPGroup *initParent = 0);
  82. ~CGPGroup(void);
  83. CGPGroup *GetParent(void) { return mParent; }
  84. CGPGroup *GetNext(void) { return (CGPGroup *)mNext; }
  85. int GetNumSubGroups(void);
  86. int GetNumPairs(void);
  87. void Clean(void);
  88. CGPGroup *Duplicate(CTextPool **textPool = 0, CGPGroup *initParent = 0);
  89. void SetWriteable(const bool writeable) { mWriteable = writeable; }
  90. CGPValue *GetPairs(void) { return mPairs; }
  91. CGPValue *GetInOrderPairs(void) { return mInOrderPairs; }
  92. CGPGroup *GetSubGroups(void) { return mSubGroups; }
  93. CGPGroup *GetInOrderSubGroups(void) { return mInOrderSubGroups; }
  94. CGPValue *AddPair(const char *name, const char *value, CTextPool **textPool = 0);
  95. void AddPair(CGPValue *NewPair);
  96. CGPGroup *AddGroup(const char *name, CTextPool **textPool = 0);
  97. void AddGroup(CGPGroup *NewGroup);
  98. CGPGroup *FindSubGroup(const char *name);
  99. bool Parse(char **dataPtr, CTextPool **textPool);
  100. bool Write(CTextPool **textPool, int depth);
  101. CGPValue *FindPair(const char *key);
  102. const char *FindPairValue(const char *key, const char *defaultVal = 0);
  103. };
  104. class CGenericParser2
  105. {
  106. private:
  107. CGPGroup mTopLevel;
  108. CTextPool *mTextPool;
  109. bool mWriteable;
  110. public:
  111. CGenericParser2(void);
  112. ~CGenericParser2(void);
  113. void SetWriteable(const bool writeable) { mWriteable = writeable; }
  114. CGPGroup *GetBaseParseGroup(void) { return &mTopLevel; }
  115. bool Parse(char **dataPtr, bool cleanFirst = true, bool writeable = false);
  116. bool Parse(char *dataPtr, bool cleanFirst = true, bool writeable = false)
  117. {
  118. return Parse(&dataPtr, cleanFirst, writeable);
  119. }
  120. void Clean(void);
  121. bool Write(CTextPool *textPool);
  122. };
  123. // The following groups of routines are used for a C interface into GP2.
  124. // C++ users should just use the objects as normally and not call these routines below
  125. //
  126. typedef void *TGenericParser2;
  127. typedef void *TGPGroup;
  128. typedef void *TGPValue;
  129. // CGenericParser2 (void *) routines
  130. TGenericParser2 GP_Parse(char **dataPtr, bool cleanFirst, bool writeable);
  131. void GP_Clean(TGenericParser2 GP2);
  132. void GP_Delete(TGenericParser2 *GP2);
  133. TGPGroup GP_GetBaseParseGroup(TGenericParser2 GP2);
  134. // CGPGroup (void *) routines
  135. const char *GPG_GetName(TGPGroup GPG);
  136. TGPGroup GPG_GetNext(TGPGroup GPG);
  137. TGPGroup GPG_GetInOrderNext(TGPGroup GPG);
  138. TGPGroup GPG_GetInOrderPrevious(TGPGroup GPG);
  139. TGPGroup GPG_GetPairs(TGPGroup GPG);
  140. TGPGroup GPG_GetInOrderPairs(TGPGroup GPG);
  141. TGPGroup GPG_GetSubGroups(TGPGroup GPG);
  142. TGPGroup GPG_GetInOrderSubGroups(TGPGroup GPG);
  143. TGPGroup GPG_FindSubGroup(TGPGroup GPG, const char *name);
  144. TGPValue GPG_FindPair(TGPGroup GPG, const char *key);
  145. const char *GPG_FindPairValue(TGPGroup GPG, const char *key, const char *defaultVal);
  146. // CGPValue (void *) routines
  147. const char *GPV_GetName(TGPValue GPV);
  148. TGPValue GPV_GetNext(TGPValue GPV);
  149. TGPValue GPV_GetInOrderNext(TGPValue GPV);
  150. TGPValue GPV_GetInOrderPrevious(TGPValue GPV);
  151. bool GPV_IsList(TGPValue GPV);
  152. const char *GPV_GetTopValue(TGPValue GPV);
  153. TGPValue GPV_GetList(TGPValue GPV);
  154. #endif // #ifndef GENERICPARSER2_H
  155. //////////////////// eof /////////////////////