environment.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright (c) 2011 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BUTIL_ENVIRONMENT_H_
  5. #define BUTIL_ENVIRONMENT_H_
  6. #include <map>
  7. #include <string>
  8. #include "butil/base_export.h"
  9. #include "butil/memory/scoped_ptr.h"
  10. #include "butil/strings/string16.h"
  11. #include "butil/build_config.h"
  12. namespace butil {
  13. namespace env_vars {
  14. #if defined(OS_POSIX)
  15. BUTIL_EXPORT extern const char kHome[];
  16. #endif
  17. } // namespace env_vars
  18. class BUTIL_EXPORT Environment {
  19. public:
  20. virtual ~Environment();
  21. // Static factory method that returns the implementation that provide the
  22. // appropriate platform-specific instance.
  23. static Environment* Create();
  24. // Gets an environment variable's value and stores it in |result|.
  25. // Returns false if the key is unset.
  26. virtual bool GetVar(const char* variable_name, std::string* result) = 0;
  27. // Syntactic sugar for GetVar(variable_name, NULL);
  28. virtual bool HasVar(const char* variable_name);
  29. // Returns true on success, otherwise returns false.
  30. virtual bool SetVar(const char* variable_name,
  31. const std::string& new_value) = 0;
  32. // Returns true on success, otherwise returns false.
  33. virtual bool UnSetVar(const char* variable_name) = 0;
  34. };
  35. #if defined(OS_WIN)
  36. typedef string16 NativeEnvironmentString;
  37. typedef std::map<NativeEnvironmentString, NativeEnvironmentString>
  38. EnvironmentMap;
  39. // Returns a modified environment vector constructed from the given environment
  40. // and the list of changes given in |changes|. Each key in the environment is
  41. // matched against the first element of the pairs. In the event of a match, the
  42. // value is replaced by the second of the pair, unless the second is empty, in
  43. // which case the key-value is removed.
  44. //
  45. // This Windows version takes and returns a Windows-style environment block
  46. // which is a concatenated list of null-terminated 16-bit strings. The end is
  47. // marked by a double-null terminator. The size of the returned string will
  48. // include the terminators.
  49. BUTIL_EXPORT string16 AlterEnvironment(const wchar_t* env,
  50. const EnvironmentMap& changes);
  51. #elif defined(OS_POSIX)
  52. typedef std::string NativeEnvironmentString;
  53. typedef std::map<NativeEnvironmentString, NativeEnvironmentString>
  54. EnvironmentMap;
  55. // See general comments for the Windows version above.
  56. //
  57. // This Posix version takes and returns a Posix-style environment block, which
  58. // is a null-terminated list of pointers to null-terminated strings. The
  59. // returned array will have appended to it the storage for the array itself so
  60. // there is only one pointer to manage, but this means that you can't copy the
  61. // array without keeping the original around.
  62. BUTIL_EXPORT scoped_ptr<char*[]> AlterEnvironment(
  63. const char* const* env,
  64. const EnvironmentMap& changes);
  65. #endif
  66. } // namespace butil
  67. #endif // BUTIL_ENVIRONMENT_H_