OS.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00019 #ifndef COIL_OS_H
00020 #define COIL_OS_H
00021 
00022 
00023 #ifdef WINVER
00024 #undef WINVER
00025 #endif
00026 #define WINVER 0x0500
00027 
00028 #ifdef _WIN32_WINNT
00029 #undef _WIN32_WINNT
00030 #endif
00031 #define _WIN32_WINNT 0x0500
00032 
00033 #include <windows.h>
00034 #include <string.h>
00035 #include <stdio.h>
00036 #include <process.h>
00037 #include <stdlib.h>
00038 #include <winbase.h>
00039 
00040 extern "C"
00041 {
00042   extern char *optarg;
00043 };
00044 
00045 namespace coil
00046 {
00047 
00048 #define COIL_UTSNAME_LENGTH 65
00049   struct utsname
00050   {
00051     char sysname[COIL_UTSNAME_LENGTH];
00052     char nodename[COIL_UTSNAME_LENGTH];
00053     char release[COIL_UTSNAME_LENGTH];
00054     char version[COIL_UTSNAME_LENGTH];
00055     char machine[COIL_UTSNAME_LENGTH];
00056   };
00057 
00081   inline int uname(utsname* name)
00082   {
00083     if (name == NULL) return 0;
00084     int ret(0);
00085 
00086     // name.sysname
00087     ::strcpy(name->sysname, "Win32");
00088 
00089     // name.release, name.version
00090     OSVERSIONINFO version_info;
00091     version_info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
00092     if (::GetVersionEx(&version_info) == false)
00093       ret = -1;
00094 
00095     const char *os;
00096     if (version_info.dwPlatformId == VER_PLATFORM_WIN32_NT)
00097       {
00098         os = "Windows NT %d.%d";
00099       }
00100     else
00101       {
00102         os = "Windows CE %d.%d";
00103       }
00104     
00105     sprintf(name->release, os,
00106             (int) version_info.dwMajorVersion,
00107             (int) version_info.dwMinorVersion);
00108 
00109     sprintf(name->version, "Build %d %s",
00110             (int) version_info.dwBuildNumber,
00111             version_info.szCSDVersion);
00112 
00113     // name.machine
00114     SYSTEM_INFO sys_info;
00115     GetSystemInfo(&sys_info);
00116     WORD arch = sys_info.wProcessorArchitecture;
00117     char cputype[COIL_UTSNAME_LENGTH/2];
00118     char subtype[COIL_UTSNAME_LENGTH/2];
00119 
00120     switch (arch)
00121       {
00122         case PROCESSOR_ARCHITECTURE_INTEL:
00123           strcpy(cputype, "Intel");
00124           if (sys_info.wProcessorLevel == 3)
00125             strcpy(subtype, "80386");
00126           else if (sys_info.wProcessorLevel == 4)
00127             strcpy(subtype, "80486");
00128           else if (sys_info.wProcessorLevel == 5)
00129             strcpy(subtype, "Pentium");
00130           else if (sys_info.wProcessorLevel == 6)
00131             strcpy(subtype, "Pentium Pro");
00132           else if (sys_info.wProcessorLevel == 7)
00133             strcpy(subtype, "Pentium II");
00134           else
00135             strcpy(subtype, "Pentium Family");
00136           break;
00137       default:
00138         strcpy(cputype, "Unknown");
00139         strcpy(subtype, "Unknown");
00140       }
00141     sprintf(name->machine, "%s %s", cputype, subtype);
00142 
00143     // name.nodename
00144     DWORD len = COIL_UTSNAME_LENGTH;
00145     if (GetComputerNameExA(ComputerNameDnsHostname,
00146                                                         name->nodename,
00147                             &len) == false)
00148     ret = -1;
00149     return ret;
00150   }
00151 
00171   typedef int pid_t;
00172   inline pid_t getpid()
00173   {
00174     return ::_getpid();
00175   }
00176 
00196   inline pid_t getppid()
00197   {
00198     return 0;
00199   }
00200 
00224   inline char* getenv(const char* name)
00225   {
00226     return ::getenv(name);
00227   }
00228 
00229   static int    opterr = 1,             /* if error message should be printed */
00230                 optind = 1,             /* index into parent argv vector */
00231                 optopt,                 /* character checked for validity */
00232                 optreset;               /* reset getopt */
00233   static char   *optarg;                /* argument associated with option */
00234 
00235 #define BADCH   (int)'?'
00236 #define BADARG  (int)':'
00237 #define EMSG    ""
00238   
00258   static int getopt(int nargc, char * const *nargv, const char *ostr)
00259   {
00260     static char *place = EMSG;          /* option letter processing */
00261     const char *oli;                    /* option letter list index */
00262     char *p;
00263     
00264     if (optreset || !*place) {          /* update scanning pointer */
00265       optreset = 0;
00266       /* Check that the scanning reached the last element. */
00267       if (optind >= nargc)
00268       { 
00269         place = EMSG;
00270         return(-1);
00271       }
00272       for(;;)
00273       {
00274         /* Check that the scanning reached the last element. */
00275         if (optind >= nargc)
00276         {                
00277           place = EMSG;
00278           return(-1);
00279         }
00280         place = nargv[optind];
00281         /* Check that the first character of the element is '-'. */
00282         if (*place != '-')
00283         {                  
00284           /* When the head of the element is not '-',        */
00285           /* Scan the next element disregarding the element. */
00286           optind++;
00287                    
00288         }
00289         else
00290         {
00291           break;
00292         }
00293       }
00294       if (place[1] && *++place == '-') {        /* found "--" */
00295         ++optind;
00296         place = EMSG;
00297         return(-1);
00298       }
00299     }                                   /* option letter okay? */
00300     if ((optopt = (int)*place++) == (int)':' ||
00301         !(oli = strchr(ostr, optopt))) {
00302       /*
00303        * if the user didn't specify '-' as an option,
00304        * assume it means -1 (EOF).
00305        */
00306       if (optopt == (int)'-')
00307         return(-1);
00308       if (!*place)
00309         ++optind;
00310       if (opterr && *ostr != ':') {
00311         if (!(p = strrchr(*nargv, '/')))
00312           p = *nargv;
00313         else
00314           ++p;
00315         fprintf(stderr, "%s: illegal option -- %c\n",
00316                 p, optopt);
00317       }
00318       return(BADCH);
00319     }
00320     if (*++oli != ':') {                        /* don't need argument */
00321       optarg = NULL;
00322       if (!*place)
00323         ++optind;
00324     }
00325     else {                                      /* need an argument */
00326       if (*place)                       /* no white space */
00327         optarg = place;
00328       else if (nargc <= ++optind) {     /* no arg */
00329         place = EMSG;
00330         if (!(p = strrchr(*nargv, '/')))
00331           p = *nargv;
00332         else
00333           ++p;
00334         if (*ostr == ':')
00335           return(BADARG);
00336         if (opterr)
00337           fprintf(stderr,
00338                   "%s: option requires an argument -- %c\n",
00339                   p, optopt);
00340         return(BADCH);
00341       }
00342       else                              /* white space */
00343         optarg = nargv[optind];
00344       place = EMSG;
00345       ++optind;
00346     }
00347     return(optopt);                             /* dump back option letter */
00348   }
00349 
00363   class GetOpt
00364   {
00365   public:
00385     GetOpt(int argc, char* const argv[], const char* opt, int flag)
00386       : m_argc(argc), m_argv(argv), m_opt(opt), m_flag(flag), optind(1), opterr(1), optopt(0)
00387 
00388     {
00389       this->optarg = coil::optarg;
00390       coil::optind = 1;
00391     }
00392 
00408     ~GetOpt()
00409     {
00410       coil::optind = 1;
00411     }
00412 
00432     int operator()()
00433     {
00434       coil::opterr = opterr;
00435       coil::optind = optind;
00436 
00437       int result = getopt(m_argc, m_argv, m_opt);
00438 
00439       optarg = coil::optarg;
00440       optind = coil::optind;
00441       optopt = coil::optopt;
00442 
00443       return result;
00444     }
00445 
00446     char* optarg;     
00447     int optind;       
00448     int opterr;       
00449     int optopt;       
00450  
00451 
00452   private:
00453     int m_argc;
00454     char* const * m_argv;
00455     const char* m_opt;
00456     int m_flag;
00457 
00458   };
00459     
00460 };
00461 
00462 #endif // COIL_OS_H


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sun Mar 26 2017 03:37:16