environment.h
説明を見る。
1 //
2 // Copyright (c) 2003-2011, MIST Project, Nagoya University
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the name of the Nagoya University nor the names of its contributors
16 // may be used to endorse or promote products derived from this software
17 // without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 
33 #ifndef __INCLUDE_MIST_ENVIRONMENT__
34 #define __INCLUDE_MIST_ENVIRONMENT__
35 
36 
37 #ifndef __INCLUDE_MIST_CONF_H__
38 #include "config/mist_conf.h"
39 #endif
40 
41 #ifndef __INCLUDE_MIST_SINGLETON__
42 #include "singleton.h"
43 #endif
44 
45 #include <string>
46 
47 #if defined( __MIST_WINDOWS__ ) && __MIST_WINDOWS__ > 0
48 
49  #include <windows.h>
50  #include <shlwapi.h>
51  #pragma comment( lib, "shlwapi.lib" )
52 
53 #else
54 
55  //#include <pwd.h>
56  #include <iostream>
57  #include <fstream>
58 
59 #endif
60 
61 
62 // mist名前空間の始まり
64 
65 
66 namespace __environment__
67 {
68 #if defined( __MIST_WINDOWS__ ) && __MIST_WINDOWS__ > 0
69  static std::string cpu( size_t &num_processors )
70  {
71  SYSTEM_INFO info;
72  memset( &info, 0, sizeof( info ) );
73  GetSystemInfo( &info );
74 
75  char value[ 1024 ];
76  DWORD type = REG_SZ;
77  DWORD length = 1024;
78  std::string cpu = "unknown";
79  std::string machine = "unknown";
80 
81  if( SHGetValue( HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", "ProcessorNameString", &type, value, &length ) == ERROR_SUCCESS )
82  {
83  cpu = value;
84  size_t index = 0;
85 
86  for( size_t i = 0 ; i < cpu.size( ) ; i++ )
87  {
88  if( cpu[ i ] == ' ' )
89  {
90  index++;
91  }
92  else
93  {
94  break;
95  }
96  }
97 
98  cpu = cpu.substr( index );
99 
100  num_processors = info.dwNumberOfProcessors;
101  }
102 
103  return( cpu );
104  }
105 
106  static std::string user( )
107  {
108  char value[ 1024 ];
109  DWORD length = 1024;
110 
111  if( GetUserName( value, &length ) )
112  {
113  return ( std::string( value ) );
114  }
115  else
116  {
117  return ( "unknown" );
118  }
119  }
120 
121  static std::string machine( )
122  {
123  char value[ 1024 ];
124  DWORD length = 1024;
125 
126  if( GetComputerName( value, &length ) )
127  {
128  return ( std::string( value ) );
129  }
130  else
131  {
132  return ( "unknown" );
133  }
134  }
135 
136  static std::string os( )
137  {
138  std::string os = "unknown";
139  OSVERSIONINFO osinfo;
140  memset( &osinfo, 0, sizeof( osinfo ) );
141  osinfo.dwOSVersionInfoSize = sizeof( osinfo );
142 
143  if( GetVersionEx( &osinfo ) )
144  {
145  switch( osinfo.dwPlatformId )
146  {
147  case VER_PLATFORM_WIN32s:
148  os = "Win32s on Windows 3.1";
149  break;
150 
151  case VER_PLATFORM_WIN32_WINDOWS:
152  switch( osinfo.dwMinorVersion )
153  {
154  case 0:
155  os = "Windows 95";
156  break;
157 
158  case 10:
159  os = "Windows 98";
160  break;
161 
162  case 90:
163  os = "Windows Me";
164  break;
165  }
166  break;
167 
168  case VER_PLATFORM_WIN32_NT:
169  if( osinfo.dwMajorVersion == 5 )
170  {
171  switch( osinfo.dwMinorVersion )
172  {
173  case 0:
174  os = "Windows 2000";
175  break;
176 
177  case 1:
178  os = "Windows XP";
179  break;
180 
181  case 2:
182  os = "Windows Server 2003 family";
183  break;
184  }
185  }
186  else if( osinfo.dwMajorVersion == 3 )
187  {
188  os = "Windows NT 3.51";
189  }
190  else if( osinfo.dwMajorVersion == 4 )
191  {
192  os = "Windows NT 4.0";
193  }
194  break;
195 
196  default:
197  break;
198  }
199  }
200 
201  return( os );
202  }
203 
204 #else
205 
206  // LINUX 専用
207 
208  static std::string cpu( size_t &num_processors )
209  {
210  typedef std::string::size_type size_type;
211  std::ifstream fi;
212 
213  fi.open( "/proc/cpuinfo" );
214 
215  if( !fi )
216  {
217  return( "unknown" );
218  }
219 
220  std::string cpuinfo = "";
221 
222  char buff[ 4096 ];
223  while( fi )
224  {
225  fi.getline( buff, 4094 );
226  cpuinfo += buff;
227  cpuinfo += "\n";
228  }
229 
230  fi.close( );
231 
232  // CPU数を取得する
233  size_type indx = cpuinfo.find( "processor", 0 );
234  size_type num_cpu = 0;
235  while( indx != cpuinfo.npos )
236  {
237  num_cpu++;
238  indx = cpuinfo.find( "processor", indx + 1 );
239  }
240 
241  num_processors = num_cpu;
242 
243  // CPUの種類を取得する
244  std::string cpuname = "";
245  indx = cpuinfo.find( "model name", 0 );
246  if( indx != cpuinfo.npos )
247  {
248  size_type sindx = cpuinfo.find( ':', indx );
249  size_type eindx = cpuinfo.find( '\n', indx );
250  if( sindx != cpuinfo.npos && eindx != cpuinfo.npos )
251  {
252  cpuname = cpuinfo.substr( sindx + 2, eindx - sindx - 2 );
253  return( cpuname );
254  }
255  }
256 
257  return( "unknown" );
258  }
259 
260  static std::string chomp( const std::string &str )
261  {
262  if( str.empty( ) )
263  {
264  return( "" );
265  }
266  else if( str.length( ) > 1 )
267  {
268  if( str[ str.length( ) - 1 ] == '\n' )
269  {
270  if( str[ str.length( ) - 2 ] == '\r' )
271  {
272  return( str.substr( 0, str.length( ) - 2 ) );
273  }
274  else
275  {
276  return( str.substr( 0, str.length( ) - 1 ) );
277  }
278  }
279  }
280  else if( str[ 0 ] == '\n' )
281  {
282  return( "" );
283  }
284 
285  return( str );
286  }
287 
288  static std::string system_command( const std::string &command )
289  {
290  FILE *gid = popen( command.c_str( ), "r" );
291 
292  if( gid == NULL )
293  {
294  return( "unknown" );
295  }
296 
297  char buff[ 4096 ];
298 
299  fgets( buff, 4096, gid );
300 
301  pclose( gid );
302 
303  return( chomp( std::string( buff ) ) );
304  }
305 
306  static std::string user( )
307  {
308  return( system_command( "whoami" ) );
309  }
310 
311  static std::string machine( )
312  {
313  return( system_command( "uname -n" ) );
314  }
315 
316  static std::string os( )
317  {
318  return( system_command( "uname -sr" ) );
319  }
320 
321 #endif
322 
323  struct environment_info
324  {
325  std::string os_name;
326  std::string cpu_name;
327  size_t cpu_num;
328  std::string machine_name;
329  std::string user_name;
330 
331  environment_info( ) :
332  os_name( os( ) ),
333  machine_name( machine( ) ),
334  user_name( user( ) )
335  {
336  cpu_name = cpu( cpu_num );
337  }
338  };
339 }
340 
341 
349 
354 inline std::string os( ){ return( singleton< __environment__::environment_info >::get_instance( ).os_name ); }
355 
360 inline std::string cpu( ){ return( singleton< __environment__::environment_info >::get_instance( ).cpu_name ); }
361 
367 
372 inline std::string machine( ){ return( singleton< __environment__::environment_info >::get_instance( ).machine_name ); }
373 
378 inline std::string user( ){ return( singleton< __environment__::environment_info >::get_instance( ).user_name ); }
379 
380 
382 // 計算機環境情報グループの終わり
383 
384 
385 
386 // mist名前空間の終わり
387 _MIST_END
388 
389 
390 #endif // __INCLUDE_MIST_ENVIRONMENT__
391 

Generated on Wed Nov 12 2014 19:44:14 for MIST by doxygen 1.8.1.2