MainAr.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // MainAr.cpp
  2. #include "StdAfx.h"
  3. // #include <locale.h>
  4. #include "Windows/Error.h"
  5. #include "Common/StdOutStream.h"
  6. #include "Common/NewHandler.h"
  7. #include "Common/MyException.h"
  8. #include "Common/StringConvert.h"
  9. #include "../Common/ExitCode.h"
  10. #include "../Common/ArchiveCommandLine.h"
  11. #include "ConsoleClose.h"
  12. using namespace NWindows;
  13. CStdOutStream *g_StdStream = 0;
  14. #ifdef _WIN32
  15. #ifndef _UNICODE
  16. bool g_IsNT = false;
  17. #endif
  18. #if !defined(_UNICODE) || !defined(_WIN64)
  19. static inline bool IsItWindowsNT()
  20. {
  21. OSVERSIONINFO versionInfo;
  22. versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
  23. if (!::GetVersionEx(&versionInfo))
  24. return false;
  25. return (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT);
  26. }
  27. #endif
  28. #endif
  29. extern int Main2(
  30. #ifndef _WIN32
  31. int numArguments, const char *arguments[]
  32. #endif
  33. );
  34. static const char *kExceptionErrorMessage = "\n\nError:\n";
  35. static const char *kUserBreak = "\nBreak signaled\n";
  36. static const char *kMemoryExceptionMessage = "\n\nERROR: Can't allocate required memory!\n";
  37. static const char *kUnknownExceptionMessage = "\n\nUnknown Error\n";
  38. static const char *kInternalExceptionMessage = "\n\nInternal Error #";
  39. int
  40. #ifdef _MSC_VER
  41. __cdecl
  42. #endif
  43. main
  44. (
  45. #ifndef _WIN32
  46. int numArguments, const char *arguments[]
  47. #endif
  48. )
  49. {
  50. g_StdStream = &g_StdOut;
  51. #ifdef _WIN32
  52. #ifdef _UNICODE
  53. #ifndef _WIN64
  54. if (!IsItWindowsNT())
  55. {
  56. (*g_StdStream) << "This program requires Windows NT/2000/XP/2003/Vista";
  57. return NExitCode::kFatalError;
  58. }
  59. #endif
  60. #else
  61. g_IsNT = IsItWindowsNT();
  62. #endif
  63. #endif
  64. // setlocale(LC_COLLATE, ".OCP");
  65. NConsoleClose::CCtrlHandlerSetter ctrlHandlerSetter;
  66. int res = 0;
  67. try
  68. {
  69. res = Main2(
  70. #ifndef _WIN32
  71. numArguments, arguments
  72. #endif
  73. );
  74. }
  75. catch(const CNewException &)
  76. {
  77. (*g_StdStream) << kMemoryExceptionMessage;
  78. return (NExitCode::kMemoryError);
  79. }
  80. catch(const NConsoleClose::CCtrlBreakException &)
  81. {
  82. (*g_StdStream) << endl << kUserBreak;
  83. return (NExitCode::kUserBreak);
  84. }
  85. catch(const CArchiveCommandLineException &e)
  86. {
  87. (*g_StdStream) << kExceptionErrorMessage << e << endl;
  88. return (NExitCode::kUserError);
  89. }
  90. catch(const CSystemException &systemError)
  91. {
  92. if (systemError.ErrorCode == E_OUTOFMEMORY)
  93. {
  94. (*g_StdStream) << kMemoryExceptionMessage;
  95. return (NExitCode::kMemoryError);
  96. }
  97. if (systemError.ErrorCode == E_ABORT)
  98. {
  99. (*g_StdStream) << endl << kUserBreak;
  100. return (NExitCode::kUserBreak);
  101. }
  102. UString message;
  103. NError::MyFormatMessage(systemError.ErrorCode, message);
  104. (*g_StdStream) << endl << endl << "System error:" << endl <<
  105. message << endl;
  106. return (NExitCode::kFatalError);
  107. }
  108. catch(NExitCode::EEnum &exitCode)
  109. {
  110. (*g_StdStream) << kInternalExceptionMessage << exitCode << endl;
  111. return (exitCode);
  112. }
  113. /*
  114. catch(const NExitCode::CMultipleErrors &multipleErrors)
  115. {
  116. (*g_StdStream) << endl << multipleErrors.NumErrors << " errors" << endl;
  117. return (NExitCode::kFatalError);
  118. }
  119. */
  120. catch(const UString &s)
  121. {
  122. (*g_StdStream) << kExceptionErrorMessage << s << endl;
  123. return (NExitCode::kFatalError);
  124. }
  125. catch(const AString &s)
  126. {
  127. (*g_StdStream) << kExceptionErrorMessage << s << endl;
  128. return (NExitCode::kFatalError);
  129. }
  130. catch(const char *s)
  131. {
  132. (*g_StdStream) << kExceptionErrorMessage << s << endl;
  133. return (NExitCode::kFatalError);
  134. }
  135. catch(int t)
  136. {
  137. (*g_StdStream) << kInternalExceptionMessage << t << endl;
  138. return (NExitCode::kFatalError);
  139. }
  140. catch(...)
  141. {
  142. (*g_StdStream) << kUnknownExceptionMessage;
  143. return (NExitCode::kFatalError);
  144. }
  145. return res;
  146. }