ECConfig.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #include <kopano/platform.h>
  18. #include <new>
  19. #include "ECConfigImpl.h"
  20. #include <kopano/charset/convert.h>
  21. namespace KC {
  22. ECConfig *ECConfig::Create(const configsetting_t *lpDefaults,
  23. const char *const *lpszDirectives)
  24. {
  25. return new ECConfigImpl(lpDefaults, lpszDirectives);
  26. }
  27. ECConfig *ECConfig::Create(const std::nothrow_t &,
  28. const configsetting_t *dfl, const char *const *direc)
  29. {
  30. return new(std::nothrow) ECConfigImpl(dfl, direc);
  31. }
  32. bool ECConfig::LoadSettings(const wchar_t *szFilename)
  33. {
  34. return LoadSettings(convert_context().convert_to<char *>(szFilename));
  35. }
  36. /**
  37. * Get the default path for the configuration file specified with lpszBasename.
  38. * Usually this will return '/etc/kopano/<lpszBasename>'. However, the path to
  39. * the configuration files can be altered by setting the 'KOPANO_CONFIG_PATH'
  40. * environment variable.
  41. *
  42. * @param[in] lpszBasename
  43. * The basename of the requested configuration file. Passing
  44. * NULL or an empty string will result in the default path
  45. * to be returned.
  46. *
  47. * @returns The full path to the requested configuration file. Memory for
  48. * the returned data is allocated in this function and will be freed
  49. * at program termination.
  50. *
  51. * @warning This function is not thread safe!
  52. */
  53. const char* ECConfig::GetDefaultPath(const char* lpszBasename)
  54. {
  55. typedef map<string, string> stringmap_t;
  56. // @todo: Check how this behaves with dlopen,dlclose,dlopen,etc...
  57. // We use a static map here to store the strings we're going to return.
  58. // This could have been a global, but this way everything is kept together.
  59. static stringmap_t s_mapPaths;
  60. if (!lpszBasename)
  61. lpszBasename = "";
  62. auto result = s_mapPaths.insert(stringmap_t::value_type(lpszBasename, string()));
  63. if (result.second == true) { // New item added, so create the actual path
  64. const char *lpszDirname = getenv("KOPANO_CONFIG_PATH");
  65. if (!lpszDirname || lpszDirname[0] == '\0')
  66. lpszDirname = "/etc/kopano";
  67. result.first->second = string(lpszDirname) + "/" + lpszBasename;
  68. }
  69. return result.first->second.c_str();
  70. }
  71. } /* namespace */