00001
00002 #include <rtt/Service.hpp>
00003 #include <rtt/plugin/ServicePlugin.hpp>
00004 #include <cstdlib>
00005
00006
00007
00008
00009
00010 #ifdef WIN32
00011 std::string getEnvString(const char *name)
00012 {
00013 char c[32767];
00014 if (GetEnvironmentVariable(name, c, sizeof (c)) > 0) {
00015 return string(c);
00016 }
00017 return 0;
00018 }
00019
00020 bool isEnv(const char* name)
00021 {
00022 char c;
00023
00024 if (GetEnvironmentVariable(name, &c, sizeof (c)) > 0) return true;
00025 return false;
00026 }
00027
00028 int setenv(const char *name, const char *value, int overwrite)
00029 {
00030 if (overwrite == 0)
00031 {
00032 char c;
00033 if (GetEnvironmentVariable(name, &c, sizeof (c)) > 0) return 0;
00034 }
00035
00036 if (SetEnvironmentVariable(name, value) != 0) return 0;
00037 return -1;
00038 }
00039 #else
00040 std::string getEnvString(const char *name)
00041 {
00042 string ret;
00043 char* c = getenv(name);
00044 if (c)
00045 return string(c);
00046 return ret;
00047 }
00048 bool isEnv(const char* name)
00049 {
00050 if( getenv(name) ) return true;
00051 return false;
00052 }
00053 #endif
00054
00055 namespace OCL
00056 {
00057
00062 class OSService: public RTT::Service
00063 {
00064 public:
00065 OSService(RTT::TaskContext* parent) :
00066 RTT::Service("os", parent)
00067 {
00068 doc("A service that provides access to some useful Operating System functions.");
00069
00070 addOperation("getenv", &OSService::getenv, this).doc(
00071 "Returns the value of an environment variable. If it is not set, returns the empty string.").arg("name",
00072 "The name of the environment variable to read.");
00073 addOperation("isenv", &OSService::isenv, this).doc(
00074 "Checks if an environment variable exists").arg("name",
00075 "The name of the environment variable to check.");
00076 addOperation("setenv", &OSService::setenv, this).doc(
00077 "Sets an environment variable.").arg("name",
00078 "The name of the environment variable to write.").arg("value","The text to set.");
00079 }
00080
00081 std::string getenv(const std::string& arg)
00082 {
00083 return getEnvString(arg.c_str());
00084 }
00085 bool isenv(const std::string& arg)
00086 {
00087 return isEnv(arg.c_str());
00088 }
00089 bool setenv(const std::string& arg, const std::string& value)
00090 {
00091 return ::setenv(arg.c_str(), value.c_str(), 1) == 0;
00092 }
00093 };
00094 }
00095
00096 ORO_SERVICE_NAMED_PLUGIN( OCL::OSService, "os")
00097