OSService.cpp
Go to the documentation of this file.
00001 
00002 #include <rtt/Service.hpp>
00003 #include <rtt/plugin/ServicePlugin.hpp>
00004 #include <cstdlib>
00005 
00006 
00007 // setEnvString and isEnv needed to be implemented because we
00008 // can't provide a getenv() implementation on windows without leaking
00009 // memory.
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     // returns zero if not found and non-zero for needed buffer length.
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     std::string ret;
00043     char* c = getenv(name);
00044     if (c)
00045         return std::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 #include <rtt/os/startstop.h>
00056 
00057 namespace OCL
00058 {
00059 
00064     class OSService: public RTT::Service
00065     {
00066     public:
00067         OSService(RTT::TaskContext* parent) :
00068             RTT::Service("os", parent)
00069         {
00070             doc("A service that provides access to some useful Operating System functions.");
00071             // add the operations
00072             addOperation("getenv", &OSService::getenv, this).doc(
00073                     "Returns the value of an environment variable. If it is not set, returns the empty string.").arg("name",
00074                     "The name of the environment variable to read.");
00075             addOperation("isenv", &OSService::isenv, this).doc(
00076                     "Checks if an environment variable exists").arg("name",
00077                             "The name of the environment variable to check.");
00078             addOperation("setenv", &OSService::setenv, this).doc(
00079                     "Sets an environment variable.").arg("name",
00080                             "The name of the environment variable to write.").arg("value","The text to set.");
00081             addOperation("argc", &OSService::argc, this).doc("Returns the number of arguments, given to this application.");
00082             addOperation("argv", &OSService::argv, this).doc("Returns the arguments as a sequence of strings, given to this application.");
00083         }
00084 
00085         int argc(void)
00086         {
00087             return __os_main_argc();
00088         }
00089 
00090         std::vector<std::string> argv(void)
00091         {
00092             int a = argc();
00093             char** args = __os_main_argv();
00094             std::vector<std::string> ret(a);
00095             for (int i = 0; i != a; ++i)
00096                 ret[i] = std::string(args[i]);
00097             return ret;
00098         }
00099 
00100         std::string getenv(const std::string& arg)
00101         {
00102             return getEnvString(arg.c_str());
00103         }
00104         bool isenv(const std::string& arg)
00105         {
00106             return isEnv(arg.c_str());
00107         }
00108         bool setenv(const std::string& arg, const std::string& value)
00109         {
00110             return ::setenv(arg.c_str(), value.c_str(), 1) == 0;
00111         }
00112     };
00113 }
00114 
00115 ORO_SERVICE_NAMED_PLUGIN( OCL::OSService, "os")
00116 


ocl
Author(s): OCL Development Team
autogenerated on Sun Nov 27 2016 03:45:31