win32/coil/OS.h
Go to the documentation of this file.
1 // -*- C++ -*-
19 #ifndef COIL_OS_H
20 #define COIL_OS_H
21 
22 
23 #ifdef WINVER
24 #undef WINVER
25 #endif
26 #define WINVER 0x0500
27 
28 #ifdef _WIN32_WINNT
29 #undef _WIN32_WINNT
30 #endif
31 #define _WIN32_WINNT 0x0500
32 
33 #include <windows.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <process.h>
37 #include <stdlib.h>
38 #include <winbase.h>
39 
40 extern "C"
41 {
42  extern char *optarg;
43 };
44 
45 namespace coil
46 {
47 
48 #define COIL_UTSNAME_LENGTH 65
49  struct utsname
50  {
56  };
57 
81  inline int uname(utsname* name)
82  {
83  if (name == NULL) return 0;
84  int ret(0);
85 
86  // name.sysname
87  ::strcpy(name->sysname, "Win32");
88 
89  // name.release, name.version
90  OSVERSIONINFO version_info;
91  version_info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
92  if (::GetVersionEx(&version_info) == false)
93  ret = -1;
94 
95  const char *os;
96  if (version_info.dwPlatformId == VER_PLATFORM_WIN32_NT)
97  {
98  os = "Windows NT %d.%d";
99  }
100  else
101  {
102  os = "Windows CE %d.%d";
103  }
104 
105  sprintf(name->release, os,
106  (int) version_info.dwMajorVersion,
107  (int) version_info.dwMinorVersion);
108 
109  sprintf(name->version, "Build %d %s",
110  (int) version_info.dwBuildNumber,
111  version_info.szCSDVersion);
112 
113  // name.machine
114  SYSTEM_INFO sys_info;
115  GetSystemInfo(&sys_info);
116  WORD arch = sys_info.wProcessorArchitecture;
117  char cputype[COIL_UTSNAME_LENGTH/2];
118  char subtype[COIL_UTSNAME_LENGTH/2];
119 
120  switch (arch)
121  {
122  case PROCESSOR_ARCHITECTURE_INTEL:
123  strcpy(cputype, "Intel");
124  if (sys_info.wProcessorLevel == 3)
125  strcpy(subtype, "80386");
126  else if (sys_info.wProcessorLevel == 4)
127  strcpy(subtype, "80486");
128  else if (sys_info.wProcessorLevel == 5)
129  strcpy(subtype, "Pentium");
130  else if (sys_info.wProcessorLevel == 6)
131  strcpy(subtype, "Pentium Pro");
132  else if (sys_info.wProcessorLevel == 7)
133  strcpy(subtype, "Pentium II");
134  else
135  strcpy(subtype, "Pentium Family");
136  break;
137  default:
138  strcpy(cputype, "Unknown");
139  strcpy(subtype, "Unknown");
140  }
141  sprintf(name->machine, "%s %s", cputype, subtype);
142 
143  // name.nodename
144  DWORD len = COIL_UTSNAME_LENGTH;
145  if (GetComputerNameExA(ComputerNameDnsHostname,
146  name->nodename,
147  &len) == false)
148  ret = -1;
149  return ret;
150  }
151 
171  typedef int pid_t;
172  inline pid_t getpid()
173  {
174  return ::_getpid();
175  }
176 
196  inline pid_t getppid()
197  {
198  return 0;
199  }
200 
224  inline char* getenv(const char* name)
225  {
226  return ::getenv(name);
227  }
228 
229  static int opterr = 1, /* if error message should be printed */
230  optind = 1, /* index into parent argv vector */
231  optopt, /* character checked for validity */
232  optreset; /* reset getopt */
233  static char *optarg; /* argument associated with option */
234 
235 #define BADCH (int)'?'
236 #define BADARG (int)':'
237 #define EMSG ""
238 
258  static int getopt(int nargc, char * const *nargv, const char *ostr)
259  {
260  static char *place = EMSG; /* option letter processing */
261  const char *oli; /* option letter list index */
262  char *p;
263 
264  if (optreset || !*place) { /* update scanning pointer */
265  optreset = 0;
266  /* Check that the scanning reached the last element. */
267  if (optind >= nargc)
268  {
269  place = EMSG;
270  return(-1);
271  }
272  for(;;)
273  {
274  /* Check that the scanning reached the last element. */
275  if (optind >= nargc)
276  {
277  place = EMSG;
278  return(-1);
279  }
280  place = nargv[optind];
281  /* Check that the first character of the element is '-'. */
282  if (*place != '-')
283  {
284  /* When the head of the element is not '-', */
285  /* Scan the next element disregarding the element. */
286  optind++;
287 
288  }
289  else
290  {
291  break;
292  }
293  }
294  if (place[1] && *++place == '-') { /* found "--" */
295  ++optind;
296  place = EMSG;
297  return(-1);
298  }
299  } /* option letter okay? */
300  if ((optopt = (int)*place++) == (int)':' ||
301  !(oli = strchr(ostr, optopt))) {
302  /*
303  * if the user didn't specify '-' as an option,
304  * assume it means -1 (EOF).
305  */
306  if (optopt == (int)'-')
307  return(-1);
308  if (!*place)
309  ++optind;
310  if (opterr && *ostr != ':') {
311  if (!(p = strrchr(*nargv, '/')))
312  p = *nargv;
313  else
314  ++p;
315  fprintf(stderr, "%s: illegal option -- %c\n",
316  p, optopt);
317  }
318  return(BADCH);
319  }
320  if (*++oli != ':') { /* don't need argument */
321  optarg = NULL;
322  if (!*place)
323  ++optind;
324  }
325  else { /* need an argument */
326  if (*place) /* no white space */
327  optarg = place;
328  else if (nargc <= ++optind) { /* no arg */
329  place = EMSG;
330  if (!(p = strrchr(*nargv, '/')))
331  p = *nargv;
332  else
333  ++p;
334  if (*ostr == ':')
335  return(BADARG);
336  if (opterr)
337  fprintf(stderr,
338  "%s: option requires an argument -- %c\n",
339  p, optopt);
340  return(BADCH);
341  }
342  else /* white space */
343  optarg = nargv[optind];
344  place = EMSG;
345  ++optind;
346  }
347  return(optopt); /* dump back option letter */
348  }
349 
363  class GetOpt
364  {
365  public:
385  GetOpt(int argc, char* const argv[], const char* opt, int flag)
386  : m_argc(argc), m_argv(argv), m_opt(opt), m_flag(flag), optind(1), opterr(1), optopt(0)
387 
388  {
389  this->optarg = coil::optarg;
390  coil::optind = 1;
391  }
392 
409  {
410  coil::optind = 1;
411  }
412 
433  {
436 
437  int result = getopt(m_argc, m_argv, m_opt);
438 
439  optarg = coil::optarg;
442 
443  return result;
444  }
445 
446  char* optarg;
447  int optind;
448  int opterr;
449  int optopt;
450 
451 
452  private:
453  int m_argc;
454  char* const * m_argv;
455  const char* m_opt;
456  int m_flag;
457 
458  };
459 
460 };
461 
462 #endif // COIL_OS_H
char version[COIL_UTSNAME_LENGTH]
Definition: win32/coil/OS.h:54
#define COIL_UTSNAME_LENGTH
Definition: win32/coil/OS.h:48
static int optopt
static int opterr
int operator()()
Parses the command line arguments.
GetOpt class.
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
char * optarg
#define EMSG
char sysname[COIL_UTSNAME_LENGTH]
Definition: win32/coil/OS.h:51
pid_t getppid()
Get process ID of the parent process.
Definition: ace/coil/OS.h:43
GetOpt(int argc, char *const argv[], const char *opt, int flag)
Constructor.
char nodename[COIL_UTSNAME_LENGTH]
Definition: win32/coil/OS.h:52
static int optind
::pid_t pid_t
Get process ID of the caller process.
Definition: ace/coil/OS.h:38
#define BADARG
static char * optarg
int uname(utsname *name)
Get System information.
Definition: ace/coil/OS.h:33
static int getopt(int nargc, char *const *nargv, const char *ostr)
Function of parses the command line arguments.
pid_t getpid()
Definition: ace/coil/OS.h:39
#define BADCH
char * getenv(const char *name)
Get environment variable.
Definition: ace/coil/OS.h:48
~GetOpt()
Destructor.
std::string sprintf(char const *__restrict fmt,...)
Convert it into a format given with an argumen.
Definition: stringutil.cpp:593
static int optreset
char machine[COIL_UTSNAME_LENGTH]
Definition: win32/coil/OS.h:55
char release[COIL_UTSNAME_LENGTH]
Definition: win32/coil/OS.h:53
Common Object Interface Layer.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Jun 6 2019 19:25:59