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.

99 lines
2.8 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /***************************************************************************
  2. Copyright 2002 by Salvador Eduardo Tropea (SET)
  3. This file is covered by the GPL license.
  4. For more information read configfile.cc
  5. ***************************************************************************/
  6. #if defined(Uses_TVConfigFile) && !defined(TVConfigFile_Included)
  7. #define TVConfigFile_Included
  8. // Structure for a node
  9. struct TVConfigFileTreeNode
  10. {
  11. char type;
  12. char priority;
  13. union
  14. {
  15. long integer;
  16. char *string;
  17. TVConfigFileTreeNode *content;
  18. };
  19. char *name;
  20. TVConfigFileTreeNode *next;
  21. };
  22. class TVConfigFile
  23. {
  24. public:
  25. TVConfigFile();
  26. ~TVConfigFile() { FreeTree(); };
  27. int Load(const char *file) { ErrorStatus=Read(file); return ErrorStatus; };
  28. // Priorities 0 to 100
  29. enum Priority { justHint=25, fromFile=50, fromApplication=75 };
  30. int Search(const char *key, char *&p, long &n);
  31. int AddInt(const char *key, const char *name, long value, int priority);
  32. int AddString(const char *key, const char *name, const char *value, int priority);
  33. // Just for debug purposes
  34. void Print(FILE *f);
  35. int ErrorLine;
  36. int ErrorStatus;
  37. static const char *Errors[];
  38. protected:
  39. TVConfigFileTreeNode tree;
  40. FILE *f;
  41. char *s, *line;
  42. size_t sLine;
  43. void PrintBranch(TVConfigFileTreeNode *base, int indent, FILE *f);
  44. void PrintIndent(int indent, FILE *f);
  45. int EatSpaces();
  46. int GetLine();
  47. TVConfigFileTreeNode
  48. *NewBranch(const char *name, int len);
  49. char *GetString();
  50. long GetInteger();
  51. int ReadBranch(TVConfigFileTreeNode *&base);
  52. int ReadBase(TVConfigFileTreeNode *&base);
  53. int SearchInBranch(TVConfigFileTreeNode *b, char *key, char *&p, long &n);
  54. TVConfigFileTreeNode *
  55. SearchOnlyInBranch(TVConfigFileTreeNode *b, char *name, int len);
  56. TVConfigFileTreeNode *
  57. SearchOnlyInBranch(TVConfigFileTreeNode *b, char *name);
  58. int Read(const char *file);
  59. void FreeList(TVConfigFileTreeNode *p);
  60. void FreeTree();
  61. int Add(const char *key, TVConfigFileTreeNode *node);
  62. static int IsWordChar(int val);
  63. static char *newStrL(const char *start, int len);
  64. };
  65. // This is a special case to configure the library
  66. class TVMainConfigFile
  67. {
  68. public:
  69. TVMainConfigFile();
  70. ~TVMainConfigFile();
  71. static int Load();
  72. static Boolean Search(const char *key, long &val);
  73. static Boolean Search(const char *section, const char *variable, long &val);
  74. static char *Search(const char *key);
  75. static char *Search(const char *section, const char *variable);
  76. static int Add(const char *key, const char *name, long value);
  77. static int Add(const char *key, const char *name, const char *value);
  78. static void Print(FILE *f) { config->Print(f); };
  79. static void SetUserConfigFile(const char *name) { userConfigFile=name; };
  80. protected:
  81. static TVConfigFile *config;
  82. static char *TestForFileIn(const char *where);
  83. static const char *userConfigFile;
  84. };
  85. #endif