The Gaudi Framework  v40r0 (475e45c1)
SystemWin32.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 
12 #include "SystemWin32.h"
13 #include <cstring>
14 #include <sstream>
15 #include <windows.h>
16 
17 // Disable warning C4996 triggered by C standard library calls
18 #pragma warning( disable : 4996 )
19 
20 namespace System {
21  namespace Win32 {
22 
23  std::vector<std::string> cmdLineArgs() {
24 
25  // The result object:
26  std::vector<std::string> result;
27 
28  // For compatibility with UNIX we CANNOT use strtok!
29  // If we would use strtok, options like -g="My world" at
30  // the command line level would result on NT in TWO options
31  // instead in one as in UNIX.
32  char exe[1024];
33  char *next, *tmp1, *tmp2;
34  for ( LPTSTR cmd = ::GetCommandLine(); *cmd; cmd = next ) {
35  ::memset( exe, 0, sizeof( exe ) );
36  while ( *cmd == ' ' ) cmd++;
37  next = ::strchr( cmd, ' ' );
38  if ( !next ) next = cmd + ::strlen( cmd );
39  if ( ( tmp1 = ::strchr( cmd, '\"' ) ) > 0 && tmp1 < next ) {
40  tmp2 = ::strchr( ++tmp1, '\"' );
41  if ( tmp2 > 0 ) {
42  next = ++tmp2;
43  if ( cmd < tmp1 ) ::strncpy( exe, cmd, tmp1 - cmd - 1 );
44  ::strncpy( &exe[strlen( exe )], tmp1, tmp2 - tmp1 - 1 );
45  } else {
46  std::cout << "Mismatched \" in command line arguments" << std::endl;
47  return std::vector<std::string>();
48  }
49  } else {
50  ::strncpy( exe, cmd, next - cmd );
51  }
52  result.push_back( exe );
53  }
54 
55  return result;
56  }
57 
58  std::string typeinfoName( const char* class_name ) {
59 
60  // The result variable:
61  std::string result;
62 
63  long off = 0;
64  if ( ::strncmp( class_name, "class ", 6 ) == 0 ) {
65  // The returned name is prefixed with "class "
66  off = 6;
67  }
68  if ( ::strncmp( class_name, "struct ", 7 ) == 0 ) {
69  // The returned name is prefixed with "struct "
70  off = 7;
71  }
72  if ( off > 0 ) {
73  std::string tmp = class_name + off;
74  long loc = 0;
75  while ( ( loc = tmp.find( "class " ) ) > 0 ) { tmp.erase( loc, 6 ); }
76  loc = 0;
77  while ( ( loc = tmp.find( "struct " ) ) > 0 ) { tmp.erase( loc, 7 ); }
78  result = tmp;
79  } else {
80  result = class_name;
81  }
82  // Change any " *" to "*"
83  while ( ( off = result.find( " *" ) ) != std::string::npos ) { result.replace( off, 2, "*" ); }
84  // Change any " &" to "&"
85  while ( ( off = result.find( " &" ) ) != std::string::npos ) { result.replace( off, 2, "&" ); }
86 
87  return result;
88  }
89 
90  std::string hostName() {
91 
92  static const size_t STRING_SIZE = 512;
93  char hname[STRING_SIZE];
94  size_t strlen = STRING_SIZE;
95  if ( !::GetComputerName( hname, &strlen ) ) { return "UNKNOWN"; }
96  return std::string( hname );
97  }
98 
99  std::string osName() { return "Windows"; }
100 
101  std::string osVersion() {
102 
103  OSVERSIONINFO ut;
104  ut.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
105  if ( !::GetVersionEx( &ut ) ) { return "UNKNOWN"; }
106  std::ostringstream ver;
107  ver << ut.dwMajorVersion << '.' << ut.dwMinorVersion;
108  return ver.str();
109  }
110 
111  std::string machineType() {
112 
113  SYSTEM_INFO ut;
114  ::GetSystemInfo( &ut );
115  std::ostringstream arch;
116  arch << ut.wProcessorArchitecture;
117  return arch.str();
118  }
119 
120  std::string accountName() {
121 
122  static const size_t STRING_SIZE = 512;
123  char uname[STRING_SIZE];
124  size_t strlen = STRING_SIZE;
125  if ( !::GetUserName( uname, &strlen ) ) { return "UNKNOWN"; }
126  return std::string( uname );
127  }
128 
129  } // namespace Win32
130 } // namespace System
System::Win32::hostName
std::string hostName()
Get the system's host name.
Definition: SystemWin32.cpp:90
System::Win32::machineType
std::string machineType()
Get the runner machine's type.
Definition: SystemWin32.cpp:111
SystemWin32.h
System::Win32::accountName
std::string accountName()
Get the account name of the current user.
Definition: SystemWin32.cpp:120
System::Win32::cmdLineArgs
std::vector< std::string > cmdLineArgs()
Get the command line arguments of the process.
Definition: SystemWin32.cpp:23
System
Note: OS specific details for environment resolution.
Definition: Debugger.h:15
System::Win32::osVersion
std::string osVersion()
Get the operating system's version.
Definition: SystemWin32.cpp:101
System::Win32::osName
std::string osName()
Get the operating system's name.
Definition: SystemWin32.cpp:99
System::Win32::typeinfoName
std::string typeinfoName(const char *class_name)
Get the human readable type name from a typeinfo name.
Definition: SystemWin32.cpp:58