Back to index

nux  3.0.0
Platform.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #include "NuxCore.h"
00024 
00025 #if defined(NUX_OS_LINUX)
00026 #include <pwd.h>
00027 #endif
00028 
00029 namespace nux
00030 {
00031 
00032   NUX_IMPLEMENT_GLOBAL_OBJECT (NGlobalData);
00033 
00034   void NuxCoreInitialize (const TCHAR *CommandLine)
00035   {
00036     static bool sInitialized = false;
00037 
00038     // Avoid initializing multiple times.
00039     if (sInitialized)
00040       return;
00041 
00042     sInitialized = true;
00043 
00044     NUX_GLOBAL_OBJECT_INSTANCE (NGlobalData).Initialize (CommandLine);
00045 
00046     NThreadLocalStorage::Initialize();
00047   }
00048 
00049 
00050   void ExitSystem()
00051   {
00052     //SystemShutdown();
00053   }
00054 
00055   void inlInitRandomGenerator()
00056   {
00057 #if _WIN32
00058     std::srand ( time (NULL) );
00059 #endif
00060 
00061   }
00062 
00063   static NString _GetProgramDirectory()
00064   {
00065 #if defined(NUX_OS_WINDOWS)
00066     TCHAR RootDirectory[NUX_MAX_FILEPATH_SIZE] = TEXT ("");
00067 
00068     if (!RootDirectory[0])
00069     {
00070       DWORD Result = ::GetModuleFileName (NULL, RootDirectory, NUX_MAX_FILEPATH_SIZE);
00071       nuxAssertMsg (Result, TEXT ("[GetProgramDirectory] Can't get program's directory path.") );
00072 
00073       if (Result == 0)
00074         NString (TEXT ("Unknown Program Directory") );
00075 
00076       unsigned int i;
00077 
00078       // Skip the program name
00079       for (i = (unsigned int) StringLength (RootDirectory) - 1; i > 0; i--)
00080       {
00081         if ( (RootDirectory[i - 1] == NUX_BACKSLASH_CHAR) || (RootDirectory[i-1] == TEXT ('/') ) )
00082           break;
00083       }
00084 
00085       RootDirectory[i] = 0;
00086     }
00087 
00088     return NString (RootDirectory);
00089 
00090 #elif defined(NUX_OS_LINUX)
00091 
00092     TCHAR RootDirectory[NUX_MAX_FILEPATH_SIZE] = TEXT ("");
00093 
00094     if (!RootDirectory[0])
00095     {
00096       char *Result = getcwd (RootDirectory, NUX_MAX_FILEPATH_SIZE);
00097       nuxAssertMsg (Result, TEXT ("[GetProgramDirectory] Can't get program's directory path.") );
00098 
00099       if (Result == 0)
00100         NString (TEXT ("Unknown Program Directory") );
00101 
00102     }
00103 
00104     nuxDebugMsg (TEXT ("[GetProgramDirectory] Program directory path: %s"), RootDirectory);
00105     return NString (RootDirectory);
00106 
00107 #else
00108     return NString (TEXT ("Unknown Program Directory") );
00109 #endif
00110   }
00111 
00112   static NString _GetComputerName()
00113   {
00114 #if defined(NUX_OS_WINDOWS)
00115     TCHAR ComputerName[NUX_MAX_FILEPATH_SIZE] = TEXT ("");
00116 
00117     if (!ComputerName[0])
00118     {
00119       DWORD Size = NUX_ARRAY_COUNT (ComputerName);
00120       ::GetComputerName (ComputerName, &Size);
00121 
00122       TCHAR *c, *d;
00123 
00124       for (c = ComputerName, d = ComputerName; *c != 0; c++)
00125       {
00126         if (IsAlphanumericChar (*c) )
00127           *d++ = *c;
00128       }
00129 
00130       *d++ = 0;
00131     }
00132 
00133     return NString (ComputerName);
00134 
00135 #elif defined(NUX_OS_LINUX)
00136     char Buffer[NUX_MAX_FILEPATH_SIZE];
00137     size_t BufferSize = NUX_ARRAY_COUNT (Buffer);
00138 
00139     if (gethostname (Buffer, BufferSize) != -1)
00140     {
00141       return NString (Buffer);
00142     }
00143 
00144     return NString (TEXT ("Unknown Computer Name") );
00145 
00146 #else
00147     return NString (TEXT ("Unknown Computer Name") );
00148 #endif
00149   }
00150 
00151 // Get user name.  NOTE: Only one return value is valid at a time!
00152   static NString _GetUserName()
00153   {
00154 #if defined(NUX_OS_WINDOWS)
00155     TCHAR UserName[256] = TEXT ("");
00156 
00157     if ( !UserName[0] )
00158     {
00159       DWORD Size = NUX_ARRAY_COUNT (UserName);
00160       ::GetUserName (UserName, &Size);
00161       TCHAR *c, *d;
00162 
00163       for (c = UserName, d = UserName; *c != 0; c++)
00164         if (IsAlphanumericChar (*c) )
00165           *d++ = *c;
00166 
00167       *d++ = 0;
00168     }
00169 
00170     return NString (UserName);
00171 
00172 #elif defined(NUX_OS_LINUX)
00173     struct passwd *userinfo;
00174     userinfo = getpwuid (getuid() );
00175 
00176     if (userinfo == 0)
00177       return NString (TEXT ("Unknown User") );
00178 
00179     return NString (userinfo->pw_name);
00180 
00181 #else
00182     return return NString (TEXT ("Unknown User") );
00183 #endif
00184   }
00185 
00186   void NGlobalData::Initialize (const TCHAR *CommandLine)
00187   {
00188 
00189   }
00190 
00191   void NGlobalData::Constructor()
00192   {
00193     m_ComputerName      = _GetComputerName();
00194     m_ProgramDirectory  = _GetProgramDirectory();
00195     m_UserName          = _GetUserName();
00196     m_RandomSeed        = 0x5A7CF91E;   // arbitrary
00197 
00198     std::srand (m_RandomSeed);
00199   }
00200 
00201   void NGlobalData::Destructor()
00202   {
00203 
00204   }
00205 
00206   NString GetComputerName()
00207   {
00208     return NUX_GLOBAL_OBJECT_INSTANCE (NGlobalData).m_ComputerName;
00209   }
00210 
00211   NString GetProgramDirectory()
00212   {
00213     return NUX_GLOBAL_OBJECT_INSTANCE (NGlobalData).m_ProgramDirectory;
00214   }
00215 
00216   NString GetUserName()
00217   {
00218     return NUX_GLOBAL_OBJECT_INSTANCE (NGlobalData).m_UserName;
00219   }
00220 
00221   NString GetCmdLine()
00222   {
00223     return NUX_GLOBAL_OBJECT_INSTANCE (NGlobalData).m_CommandLine;
00224   }
00225 
00226   NString GetLogDirectory()
00227   {
00228     return TEXT ("Logs");
00229   }
00230 
00231 }