OrocosComponentLibrary  2.9.0
OSService.cpp
1 
2 #include <rtt/Service.hpp>
3 #include <rtt/plugin/ServicePlugin.hpp>
4 #include <cstdlib>
5 
6 
7 // setEnvString and isEnv needed to be implemented because we
8 // can't provide a getenv() implementation on windows without leaking
9 // memory.
10 #ifdef WIN32
11 std::string getEnvString(const char *name)
12 {
13  char c[32767];
14  if (GetEnvironmentVariable(name, c, sizeof (c)) > 0) {
15  return string(c);
16  }
17  return 0;
18 }
19 
20 bool isEnv(const char* name)
21 {
22  char c;
23  // returns zero if not found and non-zero for needed buffer length.
24  if (GetEnvironmentVariable(name, &c, sizeof (c)) > 0) return true;
25  return false;
26 }
27 
28 int setenv(const char *name, const char *value, int overwrite)
29 {
30  if (overwrite == 0)
31  {
32  char c;
33  if (GetEnvironmentVariable(name, &c, sizeof (c)) > 0) return 0;
34  }
35 
36  if (SetEnvironmentVariable(name, value) != 0) return 0;
37  return -1;
38 }
39 #else
40 std::string getEnvString(const char *name)
41 {
42  std::string ret;
43  char* c = getenv(name);
44  if (c)
45  return std::string(c);
46  return ret;
47 }
48 bool isEnv(const char* name)
49 {
50  if( getenv(name) ) return true;
51  return false;
52 }
53 #endif
54 
55 #include <rtt/os/startstop.h>
56 
57 namespace OCL
58 {
59 
64  class OSService: public RTT::Service
65  {
66  public:
67  OSService(RTT::TaskContext* parent) :
68  RTT::Service("os", parent)
69  {
70  doc("A service that provides access to some useful Operating System functions.");
71  // add the operations
72  addOperation("getenv", &OSService::getenv, this).doc(
73  "Returns the value of an environment variable. If it is not set, returns the empty string.").arg("name",
74  "The name of the environment variable to read.");
75  addOperation("isenv", &OSService::isenv, this).doc(
76  "Checks if an environment variable exists").arg("name",
77  "The name of the environment variable to check.");
78  addOperation("setenv", &OSService::setenv, this).doc(
79  "Sets an environment variable.").arg("name",
80  "The name of the environment variable to write.").arg("value","The text to set.");
81  addOperation("argc", &OSService::argc, this).doc("Returns the number of arguments, given to this application.");
82  addOperation("argv", &OSService::argv, this).doc("Returns the arguments as a sequence of strings, given to this application.");
83  }
84 
85  int argc(void)
86  {
87  return __os_main_argc();
88  }
89 
90  std::vector<std::string> argv(void)
91  {
92  int a = argc();
93  char** args = __os_main_argv();
94  std::vector<std::string> ret(a);
95  for (int i = 0; i != a; ++i)
96  ret[i] = std::string(args[i]);
97  return ret;
98  }
99 
100  std::string getenv(const std::string& arg)
101  {
102  return getEnvString(arg.c_str());
103  }
104  bool isenv(const std::string& arg)
105  {
106  return isEnv(arg.c_str());
107  }
108  bool setenv(const std::string& arg, const std::string& value)
109  {
110  return ::setenv(arg.c_str(), value.c_str(), 1) == 0;
111  }
112  };
113 }
114 
115 ORO_SERVICE_NAMED_PLUGIN( OCL::OSService, "os")
116 
The Orocos Component Library.
Definition: Component.hpp:43
A service that provides access to some useful Operating System functions.
Definition: OSService.cpp:64