00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #pragma once
00012
00013
00014 #include <iostream>
00015 #include <fstream>
00016 #include <sstream>
00017 #include <assert.h>
00018
00019 #include "TestToolBox\Environment.h"
00020
00021 #include <Lmcons.h>
00022
00023 #include <cctype>
00024 #include <algorithm>
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #define CATCH_ALL(M_funcName,M_successFlag) \
00035 catch(std::exception& exc) \
00036 { \
00037 exc; \
00038 M_successFlag = false; \
00039 TRC(TL_ERROR, "[%s]: EXCEPTION (std::Exception): %s", \
00040 #M_funcName, exc.what()); \
00041 } \
00042 catch(...) \
00043 { \
00044 M_successFlag = false; \
00045 TRC(TL_ERROR, "[%s]: EXCEPTION of unknown type detected!",#M_funcName); \
00046 } \
00047 if (!M_successFlag) \
00048 { \
00049 TRC(TL_ERROR, "[%s]: returned false!",#M_funcName); \
00050 }
00051
00052
00053
00054
00055
00056
00057 namespace TestToolBox
00058 {
00059
00060 TTB_INLINE Environment::Environment()
00061 : m_exeFileFullPath()
00062 , m_exeDir ()
00063 , m_exeDrive ()
00064 , m_outputDir ()
00065 , m_referenceDir ()
00066 {
00067 TRCF("Environment::Environment");
00068 TRC(TL_DEBUG, "Constructor");
00069
00070
00071 {
00072
00073 char appName [MAX_PATH+1];
00074 ::GetModuleFileNameA (0, appName, MAX_PATH+1);
00075 m_exeFileFullPath = appName;
00076
00077
00078 std::string::size_type pos = m_exeFileFullPath.find_last_of("\\");
00079 if (pos!=std::string::npos)
00080 {
00081 m_exeDir = m_exeFileFullPath.substr(0, pos);
00082 std::string::size_type posExt = m_exeFileFullPath.find_last_of(".");
00083 if (posExt!=std::string::npos)
00084 {
00085 m_appName = m_exeFileFullPath.substr(pos+1,posExt-pos-1);
00086 }
00087 }
00088
00089
00090 pos = m_exeDir.find_first_of(":");
00091 if (pos!=std::string::npos)
00092 {
00093 m_exeDrive = m_exeFileFullPath.substr(0, pos+1);
00094 }
00095 }
00096
00097
00098 {
00099
00100 m_sourceDir = __FILE__;
00101 std::string::size_type pos = m_sourceDir.find_last_of("\\");
00102 if (pos!=std::string::npos)
00103 {
00104 m_sourceDir = m_sourceDir.substr(0, pos);
00105 }
00106 CalculateSourceDrive();
00107 }
00108
00109
00110 {
00111
00112 m_outputDir = m_exeDir;
00113 m_referenceDir = m_exeDir;
00114 }
00115
00116 ParseCommandLine();
00117
00118 }
00119
00120
00121
00122
00123
00124
00125 TTB_INLINE Environment::~Environment()
00126 {
00127 TRCF("Environment::~Environment");
00128 TRC(TL_DEBUG, "Destructor");
00129
00130 }
00131
00132
00133 TTB_INLINE void Environment::CutSourceDirAfter (std::string const & in_findStr)
00134 {
00135
00136 std::string sourceDirLowerCase = m_sourceDir;
00137 std::transform(sourceDirLowerCase.begin(), sourceDirLowerCase.end(),
00138 sourceDirLowerCase.begin(), tolower);
00139 std::string findStrLowerCase = in_findStr;
00140 std::transform(findStrLowerCase.begin(), findStrLowerCase.end(),
00141 findStrLowerCase.begin(), tolower);
00142
00143 std::string::size_type pos = sourceDirLowerCase.find(findStrLowerCase);
00144 if (pos!=std::string::npos)
00145 {
00146 m_sourceDir = m_sourceDir.substr(0, pos + in_findStr.length());
00147 }
00148
00149
00150 if ( (m_sourceDir.length()>0)
00151 && (m_sourceDir[m_sourceDir.length()-1]=='\\'))
00152 {
00153 m_sourceDir.erase(m_sourceDir.length()-1,1);
00154 }
00155
00156 }
00157
00158
00159
00160
00161
00162
00163 TTB_INLINE void Environment::CalculateSourceDrive (void)
00164 {
00165
00166 std::string::size_type pos = m_sourceDir.find_first_of(":");
00167 if (pos!=std::string::npos)
00168 {
00169 m_sourceDrive = m_sourceDir.substr(0, pos+1);
00170 }
00171
00172 }
00173
00174
00175
00176
00177
00178
00179 TTB_INLINE std::string Environment::GetNameOfComputer (void)
00180 {
00181 char computerName [MAX_COMPUTERNAME_LENGTH + 1];
00182 DWORD lenComp = MAX_COMPUTERNAME_LENGTH + 1;
00183 ::GetComputerNameA (computerName, &lenComp);
00184 return std::string(computerName);
00185
00186 }
00187
00188
00189
00190
00191
00192
00193 TTB_INLINE std::string Environment::GetNameOfUser (void)
00194 {
00195 char userName [UNLEN + 1];
00196 DWORD lenUser = UNLEN + 1;
00197 ::GetUserNameA (userName, &lenUser);
00198 return std::string(userName);
00199
00200 }
00201
00202
00203
00204
00205
00206
00207 TTB_INLINE std::string Environment::GetCommandLineStr (void)
00208 {
00209 return std::string(::GetCommandLineA());
00210
00211 }
00212
00213
00214
00215
00216
00217
00218 TTB_INLINE std::string Environment::GetArgsStr (void)
00219 {
00220 std::string retVal;
00221 std::string cmdLineArgs(::GetCommandLineA());
00222 TrimStr(cmdLineArgs);
00223 assert (!cmdLineArgs.empty());
00224
00225
00226 std::string::size_type tokenEndPos;
00227 if (cmdLineArgs[0]=='\"')
00228 {
00229 tokenEndPos = cmdLineArgs.find_first_of ("\"", 1);
00230 }
00231 else
00232 {
00233 tokenEndPos = cmdLineArgs.find_first_of (" ", 1);
00234 }
00235
00236 if (tokenEndPos == std::string::npos)
00237 {
00238 retVal="";
00239 }
00240 else
00241 {
00242 retVal = cmdLineArgs.substr(tokenEndPos+1, cmdLineArgs.length() - tokenEndPos);
00243 TrimStr (retVal);
00244 }
00245
00246 return retVal;
00247
00248 }
00249
00250
00251
00252
00253
00254
00255 TTB_INLINE std::string Environment::GetCommandLineOption (
00256 std::string const & in_nameOption)
00257 {
00258 CmdLineOptions::iterator it = m_cmdLineOptions.find(in_nameOption);
00259 return it != m_cmdLineOptions.end()
00260 ? it->second : std::string();
00261
00262 }
00263
00264
00265
00266
00267
00268
00269 TTB_INLINE bool Environment::GetSystemEnvVariable (
00270 std::string const & in_envVarName,
00271 std::string& out_envVarVal)
00272 {
00273 const int MYBUFSIZE = 4096;
00274 bool varExists = false;
00275
00276 TRCF("Environment::GetSystemEnvVariable");
00277
00278 DWORD dwRet;
00279 DWORD dwErr;
00280
00281 TCHAR envVarValue [MYBUFSIZE];
00282
00283 dwRet = ::GetEnvironmentVariable(TEXT(in_envVarName.c_str()), envVarValue,
00284 MYBUFSIZE);
00285 if(0 == dwRet)
00286 {
00287 dwErr = GetLastError();
00288 if( ERROR_ENVVAR_NOT_FOUND == dwErr )
00289 {
00290 TRC(TL_ERROR, "variable >%s< is not existing", in_envVarName.c_str());
00291 }
00292 else
00293 {
00294 std::ostringstream oss;
00295 oss << "ERROR: Environment variable " << in_envVarName.c_str() << " could not be accessed (LastError="
00296 << dwErr << "/" << Environment::GetErrorDescription(dwErr).c_str() << ")" << std::endl;
00297 TRC(TL_ERROR, "throwing exception: %s", oss.str().c_str());
00298 throw std::exception(oss.str().c_str());
00299 }
00300 }
00301 else
00302 {
00303 if(MYBUFSIZE < dwRet)
00304 {
00305 TRC(TL_ERROR, "WARNING: Environment variable >%s< too long! Value is ignored!", in_envVarName.c_str());
00306 }
00307 else
00308 {
00309 out_envVarVal = envVarValue;
00310 varExists = true;
00311 }
00312 }
00313 TRC(TL_DEBUG, "variable >%s< returns value >%s<", in_envVarName.c_str(), out_envVarVal.c_str());
00314 return varExists;
00315
00316 }
00317
00318
00319
00320
00321
00322
00323 TTB_INLINE std::string Environment::GetSystemEnvVariable (
00324 std::string const & in_envVarName)
00325 {
00326 std::string retVal;
00327 GetSystemEnvVariable (in_envVarName, retVal);
00328 return retVal;
00329
00330 }
00331
00332
00333
00334
00335
00336
00337 TTB_INLINE void Environment::SetSystemEnvVariable (
00338 std::string const & in_envVarName,
00339 std::string CONST & in_envVarValue)
00340 {
00341 DWORD dwErr;
00342 TRCF("Environment::SetSystemEnvVariable");
00343 TRC(TL_DEBUG, "set variable >%s< to >%s<", in_envVarName.c_str(), in_envVarValue.c_str());
00344
00345 if(!::SetEnvironmentVariable(TEXT(in_envVarName.c_str()),
00346 TEXT(in_envVarValue.c_str())))
00347 {
00348 dwErr = ::GetLastError();
00349 {
00350 std::ostringstream oss;
00351 oss << "ERROR: Environment variable " << in_envVarName.c_str()
00352 << " could not be set (LastError="
00353 << dwErr << "/"
00354 << Environment::GetErrorDescription(dwErr).c_str() << ")" << std::endl;
00355 TRC(TL_ERROR, "throwing exception: %s", oss.str().c_str());
00356 throw std::exception(oss.str().c_str());
00357 }
00358 }
00359
00360 }
00361
00362
00363
00364
00365
00366
00367 TTB_INLINE std::string Environment::GetErrorDescription (int in_systemError)
00368 {
00369 static char charBuf [4096];
00370 TCHAR* msgBufPtr = 0;
00371
00372 if (::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
00373 FORMAT_MESSAGE_FROM_SYSTEM,
00374 0,
00375 static_cast<DWORD> (in_systemError),
00376 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00377 (LPTSTR)&msgBufPtr,
00378 0,
00379 0))
00380 {
00381 char* chBufPtr = static_cast<char*> (msgBufPtr);
00382 if (chBufPtr)
00383 {
00384 char* chPtr;
00385
00386
00387 if (chPtr = strchr(chBufPtr,13))
00388 {
00389 size_t len = chPtr - chBufPtr;
00390 strncpy_s (charBuf, chBufPtr, len);
00391 charBuf[len] = '\0';
00392 }
00393 else
00394 {
00395 strcpy_s (charBuf, chBufPtr);
00396 }
00397 }
00398 else
00399 strcpy_s (charBuf, "GetErrorDescription: ERROR 1");
00400 }
00401 else
00402 strcpy_s (charBuf, "GetErrorDescription: ERROR 2");
00403
00404 if (msgBufPtr)
00405 LocalFree (msgBufPtr);
00406
00407 return std::string(charBuf);
00408
00409 }
00410
00411
00412
00413
00414
00415
00416 TTB_INLINE bool Environment::StartExeFile (
00417 std::string const & in_executableToStart,
00418 std::string const & in_startArguments)
00419 {
00420 TRCF("Environment::StartExeFile");
00421
00422 bool succeeded = false;
00423
00424 std::string executableToStart = in_executableToStart;
00425
00426 if ( (executableToStart.length() > 0)
00427 && (executableToStart.at(0) != '\"'))
00428 {
00429 executableToStart = "\"" + executableToStart + "\"";
00430 }
00431
00432 try
00433 {
00434
00435 STARTUPINFO startInfo;
00436 startInfo. cb = sizeof (STARTUPINFO);
00437 startInfo. lpReserved = 0;
00438 startInfo. lpDesktop = 0;
00439 startInfo. lpTitle = 0;
00440 startInfo. dwX = 0;
00441 startInfo. dwY = 0;
00442 startInfo. dwXSize = 0;
00443 startInfo. dwYSize = 0;
00444 startInfo. dwXCountChars = 0;
00445 startInfo. dwYCountChars = 0;
00446 startInfo. dwFillAttribute = 0;
00447 startInfo. dwFlags = STARTF_USEPOSITION |
00448 STARTF_USESHOWWINDOW;
00449 startInfo. wShowWindow = SW_SHOW;
00450 startInfo. cbReserved2 = 0;
00451 startInfo. lpReserved2 = 0;
00452 startInfo. lpReserved = 0;
00453 startInfo. hStdInput = 0;
00454 startInfo. hStdOutput = 0;
00455 startInfo. hStdError = 0;
00456
00457
00458 std::string cmdStr = executableToStart;
00459 if (in_startArguments.length()>0)
00460 {
00461 cmdStr += in_startArguments;
00462 }
00463
00464
00465 PROCESS_INFORMATION procInfo;
00466
00467 if (CreateProcess (
00468 NULL,
00469 LPTSTR (cmdStr. c_str()),
00470 NULL,
00471 NULL,
00472 FALSE,
00473 0,
00474 NULL,
00475 NULL,
00476 &startInfo,
00477 &procInfo))
00478 {
00479 TRC(TL_DEBUG, "Successfully started %s", cmdStr.c_str());
00480 CloseHandle (procInfo. hThread);
00481 CloseHandle (procInfo. hProcess);
00482 succeeded = true;
00483 }
00484 else
00485 {
00486 int err = GetLastError();
00487 TRC(TL_ERROR, "ERROR: File %s could not be started (LastError=%d/%s)",
00488 executableToStart.c_str(),err,GetErrorDescription(err).c_str());
00489 }
00490 }
00491 CATCH_ALL(Environment::StartExeFile,succeeded)
00492 return succeeded;
00493
00494 }
00495
00496
00497
00498
00499
00500
00501 TTB_INLINE void Environment::ParseCommandLine (void)
00502 {
00503 TRCF("Environment::ParseCommandLine");
00504
00505 std::string cmdLine = Environment::GetCommandLineStr();
00506
00507 std::string opt;
00508 std::string par;
00509
00510 while (GetNextOption (cmdLine, opt, par))
00511 {
00512 TRC(TL_DEBUG, "command line option >%s< with param >%s<", opt.c_str(),par.c_str());
00513 m_cmdLineOptions[opt] = par;
00514 }
00515
00516 }
00517
00518
00519
00520
00521
00522
00523 TTB_INLINE bool Environment::GetNextOption (
00524 std::string& inOut_rCmdLine,
00525 std::string& out_rOpt,
00526 std::string& out_rPar)
00527 {
00528 TRCF("Environment::GetNextOption");
00529
00530
00531
00532
00533 out_rOpt = "";
00534 out_rPar = "";
00535 bool optionFound = false;
00536 bool paramFound = false;
00537 std::string option;
00538 std::string param;
00539 std::string::size_type endPos;
00540
00541
00542
00543
00544 while (!optionFound)
00545 {
00546 if (!GetNextToken(inOut_rCmdLine, option, endPos))
00547 break;
00548 if ( (option.length()>=2)
00549 && (option.at(0) == '-')
00550 && (option.at(1) != '-'))
00551 {
00552 optionFound = true;
00553 }
00554 else
00555 {
00556 TRC(TL_DEBUG, "token is skipped");
00557 }
00558
00559
00560 inOut_rCmdLine.erase (0, endPos);
00561 }
00562
00563
00564 if (optionFound)
00565 {
00566 if (GetNextToken(inOut_rCmdLine, param, endPos))
00567 {
00568 if ( (param.length()>=1)
00569 && (param.at(0) != '-'))
00570 {
00571 paramFound = true;
00572
00573 inOut_rCmdLine.erase (0, endPos);
00574 }
00575 else
00576 {
00577
00578
00579
00580 TRC(TL_DEBUG, "token belongs to next option and will be read in again");
00581 }
00582
00583 }
00584 }
00585
00586 if (optionFound) out_rOpt = option;
00587 if (paramFound) out_rPar = param;
00588
00589 return optionFound;
00590
00591 }
00592
00593
00594
00595
00596
00597
00598 TTB_INLINE bool Environment::GetNextToken (
00599 std::string& inOut_rCmdLine,
00600 std::string& out_token,
00601 std::string::size_type& out_tokenEndPos)
00602 {
00603 TRCF("Environment::GetNextToken");
00604
00605 std::string token;
00606 std::string::size_type tokenEndPos(inOut_rCmdLine.length());
00607
00608 std::string whiteSpace(" \t\n");
00609
00610 std::string::size_type tokenStartPos = inOut_rCmdLine.find_first_not_of (
00611 whiteSpace);
00612 if (tokenStartPos != std::string::npos)
00613 {
00614
00615 if (inOut_rCmdLine.at(tokenStartPos)== '\"')
00616 {
00617 tokenEndPos = inOut_rCmdLine.find_first_of ("\"", tokenStartPos+1);
00618 if (tokenEndPos == std::string::npos)
00619 {
00620 tokenEndPos = inOut_rCmdLine.length();
00621 }
00622 else
00623 {
00624 ++tokenEndPos;
00625 }
00626 }
00627 else
00628 {
00629 tokenEndPos = inOut_rCmdLine.find_first_of (whiteSpace, tokenStartPos+1);
00630 if (tokenEndPos == std::string::npos)
00631 {
00632 tokenEndPos = inOut_rCmdLine.length();
00633 }
00634 }
00635
00636
00637 token = inOut_rCmdLine.substr(
00638 tokenStartPos, tokenEndPos - tokenStartPos);
00639
00640
00641 if ( (token.length() > 0)
00642 && (token.at(0) =='"'))
00643 {
00644 token.erase(0,1);
00645 }
00646
00647 if ( (token.length() > 0)
00648 && (token.at(token.length()-1) =='"'))
00649 {
00650 token.erase(token.length()-1,1);
00651 }
00652 }
00653
00654 out_token = token;
00655 out_tokenEndPos = tokenEndPos;
00656 TRC(TL_DEBUG, "next token = >%s<", out_token.c_str());
00657
00658 return token.length()>0;
00659
00660 }
00661
00662
00663
00664
00665
00666
00667
00668 }
00669
00670