ECConfigImpl.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  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 Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #ifndef ECCONFIGIMPL_H
  18. #define ECCONFIGIMPL_H
  19. #include <kopano/zcdefs.h>
  20. using namespace std;
  21. #include <kopano/ECConfig.h>
  22. #include <map>
  23. #include <set>
  24. #include <list>
  25. #include <string>
  26. #include <cstring>
  27. #include <pthread.h>
  28. #include <iostream>
  29. #include <fstream>
  30. #include <kopano/lockhelper.hpp>
  31. namespace KC {
  32. struct settingkey_t {
  33. char s[256];
  34. unsigned short ulFlags;
  35. unsigned short ulGroup;
  36. };
  37. struct settingcompare {
  38. bool operator()(const settingkey_t &a, const settingkey_t &b) const
  39. {
  40. return strcmp(a.s, b.s) < 0;
  41. }
  42. };
  43. #define MAXLINELEN 4096
  44. class ECConfigImpl;
  45. /* Note: char* in map is allocated ONCE to 1024, and GetSetting will always return the same pointer to this buffer */
  46. typedef std::map<settingkey_t, char*, settingcompare> settingmap_t;
  47. typedef bool (ECConfigImpl::*directive_func_t)(const char *, unsigned int);
  48. struct directive_t {
  49. const char *lpszDirective;
  50. directive_func_t fExecute;
  51. };
  52. /*
  53. * Flags for the InitDefaults & InitConfigFile functions
  54. */
  55. #define LOADSETTING_INITIALIZING 0x0001 /* ECConfig is initializing, turns on extra debug information */
  56. #define LOADSETTING_UNKNOWN 0x0002 /* Allow adding new configuration options */
  57. #define LOADSETTING_OVERWRITE 0x0004 /* Allow overwriting predefined configuration options */
  58. #define LOADSETTING_OVERWRITE_GROUP 0x0008 /* Same as CONFIG_LOAD_OVERWRITE but only if options are in the same group */
  59. #define LOADSETTING_OVERWRITE_RELOAD 0x0010 /* Same as CONFIG_LOAD_OVERWRITE but only if option is marked reloadable */
  60. #define LOADSETTING_CMDLINE_PARAM 0x0020 /* This setting is being set from commandline parameters. Sets the option non-reloadable */
  61. class ECConfigImpl _kc_final : public ECConfig {
  62. public:
  63. ECConfigImpl(const configsetting_t *lpDefaults, const char *const *lpszDirectives);
  64. ~ECConfigImpl();
  65. bool LoadSettings(const char *file) _kc_override;
  66. virtual int ParseParams(int argc, char **argv) _kc_override;
  67. const char *GetSettingsPath(void) _kc_override { return m_szConfigFile; }
  68. bool ReloadSettings(void) _kc_override;
  69. bool AddSetting(const char *name, const char *value, const unsigned int group = 0) _kc_override;
  70. const char *GetSetting(const char *name) _kc_override;
  71. const char *GetSetting(const char *name, const char *equal, const char *other) _kc_override;
  72. const wchar_t *GetSettingW(const char *name) _kc_override;
  73. const wchar_t *GetSettingW(const char *name, const wchar_t *equal, const wchar_t *other) _kc_override;
  74. std::list<configsetting_t> GetSettingGroup(unsigned int group) _kc_override;
  75. std::list<configsetting_t> GetAllSettings(void) _kc_override;
  76. bool HasWarnings(void) _kc_override;
  77. const std::list<std::string> *GetWarnings(void) _kc_override { return &warnings; }
  78. bool HasErrors(void) _kc_override;
  79. const std::list<std::string> *GetErrors(void) _kc_override { return &errors; }
  80. private:
  81. bool InitDefaults(unsigned int ulFlags);
  82. bool InitConfigFile(unsigned int ulFlags);
  83. bool ReadConfigFile(const std::string &file, unsigned int ulFlags, unsigned int ulGroup = 0);
  84. bool HandleDirective(const std::string &strLine, unsigned int ulFlags);
  85. bool HandleInclude(const char *lpszArgs, unsigned int ulFlags);
  86. bool HandlePropMap(const char *lpszArgs, unsigned int ulFlags);
  87. size_t GetSize(const char *szValue);
  88. void InsertOrReplace(settingmap_t *lpMap, const settingkey_t &s, const char* szValue, bool bIsSize);
  89. const char *GetMapEntry(const settingmap_t *lpMap, const char *szName);
  90. const char *GetAlias(const char *szAlias);
  91. bool AddSetting(const configsetting_t *lpsConfig, unsigned int ulFlags);
  92. void AddAlias(const configsetting_t *lpsAlias);
  93. void CleanupMap(settingmap_t *lpMap);
  94. bool CopyConfigSetting(const configsetting_t *lpsSetting, settingkey_t *lpsKey);
  95. bool CopyConfigSetting(const settingkey_t *lpsKey, const char *szValue, configsetting_t *lpsSetting);
  96. const configsetting_t *m_lpDefaults;
  97. const char *m_szConfigFile = nullptr;
  98. std::list<std::string> m_lDirectives;
  99. /* m_mapSettings & m_mapAliases are protected by m_settingsLock */
  100. KC::shared_mutex m_settingsRWLock;
  101. settingmap_t m_mapSettings;
  102. settingmap_t m_mapAliases;
  103. std::list<std::string> warnings;
  104. std::list<std::string> errors;
  105. std::string m_currentFile;
  106. std::set<std::string> m_readFiles;
  107. typedef std::map<const char*, std::wstring> ConvertCache;
  108. ConvertCache m_convertCache;
  109. static const directive_t s_sDirectives[];
  110. };
  111. } /* namespace */
  112. #endif // ECCONFIGIMPL_H