environment.h 2.91 KB
Newer Older
gejun's avatar
gejun committed
1 2 3 4
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef BUTIL_ENVIRONMENT_H_
#define BUTIL_ENVIRONMENT_H_
gejun's avatar
gejun committed
7 8 9 10

#include <map>
#include <string>

11 12 13 14
#include "butil/base_export.h"
#include "butil/memory/scoped_ptr.h"
#include "butil/strings/string16.h"
#include "butil/build_config.h"
gejun's avatar
gejun committed
15

16
namespace butil {
gejun's avatar
gejun committed
17 18 19 20

namespace env_vars {

#if defined(OS_POSIX)
21
BUTIL_EXPORT extern const char kHome[];
gejun's avatar
gejun committed
22 23 24 25
#endif

}  // namespace env_vars

26
class BUTIL_EXPORT Environment {
gejun's avatar
gejun committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
 public:
  virtual ~Environment();

  // Static factory method that returns the implementation that provide the
  // appropriate platform-specific instance.
  static Environment* Create();

  // Gets an environment variable's value and stores it in |result|.
  // Returns false if the key is unset.
  virtual bool GetVar(const char* variable_name, std::string* result) = 0;

  // Syntactic sugar for GetVar(variable_name, NULL);
  virtual bool HasVar(const char* variable_name);

  // Returns true on success, otherwise returns false.
  virtual bool SetVar(const char* variable_name,
                      const std::string& new_value) = 0;

  // Returns true on success, otherwise returns false.
  virtual bool UnSetVar(const char* variable_name) = 0;
};


#if defined(OS_WIN)

typedef string16 NativeEnvironmentString;
typedef std::map<NativeEnvironmentString, NativeEnvironmentString>
    EnvironmentMap;

// Returns a modified environment vector constructed from the given environment
// and the list of changes given in |changes|. Each key in the environment is
// matched against the first element of the pairs. In the event of a match, the
// value is replaced by the second of the pair, unless the second is empty, in
// which case the key-value is removed.
//
// This Windows version takes and returns a Windows-style environment block
// which is a concatenated list of null-terminated 16-bit strings. The end is
// marked by a double-null terminator. The size of the returned string will
// include the terminators.
66
BUTIL_EXPORT string16 AlterEnvironment(const wchar_t* env,
gejun's avatar
gejun committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
                                      const EnvironmentMap& changes);

#elif defined(OS_POSIX)

typedef std::string NativeEnvironmentString;
typedef std::map<NativeEnvironmentString, NativeEnvironmentString>
    EnvironmentMap;

// See general comments for the Windows version above.
//
// This Posix version takes and returns a Posix-style environment block, which
// is a null-terminated list of pointers to null-terminated strings. The
// returned array will have appended to it the storage for the array itself so
// there is only one pointer to manage, but this means that you can't copy the
// array without keeping the original around.
82
BUTIL_EXPORT scoped_ptr<char*[]> AlterEnvironment(
gejun's avatar
gejun committed
83 84 85 86 87
    const char* const* env,
    const EnvironmentMap& changes);

#endif

88
}  // namespace butil
gejun's avatar
gejun committed
89

90
#endif  // BUTIL_ENVIRONMENT_H_