Main.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // main.cpp
  3. //
  4. // Copyright (c) 2003, Electronic Arts Inc. All rights reserved.
  5. // Created by Paul Pedriana
  6. ///////////////////////////////////////////////////////////////////////////////
  7. #include <EABase/eabase.h>
  8. #include <EAMain/EAMain.h>
  9. #include <EAMain/EAMainInitFini.inl>
  10. #include <EAMain/EAEntryPointMain.inl>
  11. #include <string.h>
  12. #ifdef _MSC_VER
  13. #pragma warning(push, 0)
  14. #endif
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #if defined(EA_COMPILER_MSVC) && defined(EA_PLATFORM_MICROSOFT)
  18. #include <crtdbg.h>
  19. #endif
  20. #ifdef _MSC_VER
  21. #pragma warning(pop)
  22. #endif
  23. void* operator new[](size_t size, const char* /*pName*/, int /*flags*/, unsigned /*debugFlags*/, const char* /*file*/, int /*line*/)
  24. {
  25. return operator new[](size);
  26. }
  27. void* operator new[](size_t size, size_t /*alignment*/, size_t /*alignmentOffset*/, const char* /*pName*/,
  28. int /*flags*/, unsigned /*debugFlags*/, const char* /*file*/, int /*line*/)
  29. {
  30. return operator new[](size);
  31. }
  32. static int TestCommandLineArgs()
  33. {
  34. using namespace EA::EAMain;
  35. #define EAMAIN_TEST(x) if (!(x)) { Report("%s(%d): %s\n", __FILE__, __LINE__, #x); ++nErrorCount; } else (void)0
  36. #define EAMAIN_TEST_FATAL(x) if (!(x)) { Report("%s(%d): %s\n", __FILE__, __LINE__, #x); ++nErrorCount; return nErrorCount; } else (void)0
  37. int nErrorCount = 0;
  38. // This test is disabled on xenon because xenon has implicit command line
  39. // construction if argc is zero.
  40. {
  41. int argc = 0;
  42. char *argv[] = { NULL };
  43. CommandLine commandLine(argc, argv);
  44. EAMAIN_TEST(commandLine.Argc() == 0);
  45. EAMAIN_TEST(commandLine.Argv() != NULL);
  46. EAMAIN_TEST(commandLine.FindSwitch("-x") == -1);
  47. EAMAIN_TEST(commandLine.Arg(0) == nullptr);
  48. EAMAIN_TEST(commandLine.Argv()[0] == nullptr);
  49. }
  50. {
  51. int argc = 1;
  52. char arg[] = "program.elf";
  53. char *argv[] = { arg };
  54. CommandLine commandLine(argc, argv);
  55. EAMAIN_TEST(commandLine.Argc() == 1);
  56. EAMAIN_TEST_FATAL(commandLine.Argv() != NULL);
  57. EAMAIN_TEST(strcmp(commandLine.Argv()[0], "program.elf") == 0);
  58. EAMAIN_TEST(strcmp(commandLine.Arg(0), "program.elf") == 0);
  59. EAMAIN_TEST(commandLine.FindSwitch("-x") == -1);
  60. EAMAIN_TEST(commandLine.Arg(1) == nullptr);
  61. EAMAIN_TEST(commandLine.Argv()[1] == nullptr);
  62. }
  63. {
  64. const char *commandLineString = "program.elf";
  65. CommandLine commandLine(commandLineString);
  66. EAMAIN_TEST(commandLine.Argc() == 1);
  67. EAMAIN_TEST_FATAL(commandLine.Argv() != NULL);
  68. EAMAIN_TEST(strcmp(commandLine.Argv()[0], "program.elf") == 0);
  69. EAMAIN_TEST(strcmp(commandLine.Arg(0), "program.elf") == 0);
  70. EAMAIN_TEST(commandLine.FindSwitch("-x") == -1);
  71. EAMAIN_TEST(commandLine.Arg(1) == nullptr);
  72. EAMAIN_TEST(commandLine.Argv()[1] == nullptr);
  73. }
  74. {
  75. const char *commandLineString = "program.elf arg1 \"arg 2\"";
  76. CommandLine commandLine(commandLineString);
  77. EAMAIN_TEST(commandLine.Argc() == 3);
  78. EAMAIN_TEST_FATAL(commandLine.Argv() != NULL);
  79. EAMAIN_TEST(strcmp(commandLine.Argv()[0], "program.elf") == 0);
  80. EAMAIN_TEST(strcmp(commandLine.Arg(0), "program.elf") == 0);
  81. EAMAIN_TEST(strcmp(commandLine.Arg(1), "arg1") == 0);
  82. EAMAIN_TEST(strcmp(commandLine.Arg(2), "arg 2") == 0);
  83. EAMAIN_TEST(commandLine.FindSwitch("-x") == -1);
  84. EAMAIN_TEST(commandLine.Arg(3) == nullptr);
  85. EAMAIN_TEST(commandLine.Argv()[3] == nullptr);
  86. }
  87. {
  88. const char *commandLineString = "program.elf -x -y:1";
  89. const char *parameter = NULL;
  90. CommandLine commandLine(commandLineString);
  91. EAMAIN_TEST(commandLine.Argc() == 3);
  92. EAMAIN_TEST_FATAL(commandLine.Argv() != NULL);
  93. EAMAIN_TEST(strcmp(commandLine.Arg(0), "program.elf") == 0);
  94. EAMAIN_TEST(strcmp(commandLine.Arg(1), "-x") == 0);
  95. EAMAIN_TEST(commandLine.FindSwitch("-x") == 1);
  96. EAMAIN_TEST(commandLine.FindSwitch("-y", false, &parameter) == 2);
  97. EAMAIN_TEST_FATAL(parameter != NULL);
  98. EAMAIN_TEST(strcmp(parameter, "1") == 0);
  99. EAMAIN_TEST(commandLine.Arg(3) == nullptr);
  100. EAMAIN_TEST(commandLine.Argv()[3] == nullptr);
  101. }
  102. {
  103. const char *commandLineString = "program.elf -x \"-switch:this switch parameter has spaces\"";
  104. const char *parameter = NULL;
  105. CommandLine commandLine(commandLineString);
  106. EAMAIN_TEST(commandLine.Argc() == 3);
  107. EAMAIN_TEST_FATAL(commandLine.Argv() != NULL);
  108. EAMAIN_TEST(strcmp(commandLine.Arg(0), "program.elf") == 0);
  109. EAMAIN_TEST(strcmp(commandLine.Arg(1), "-x") == 0);
  110. EAMAIN_TEST(commandLine.FindSwitch("-x") == 1);
  111. EAMAIN_TEST(commandLine.FindSwitch("-switch", false, &parameter) == 2);
  112. EAMAIN_TEST_FATAL(parameter != NULL);
  113. EAMAIN_TEST(strcmp(parameter, "this switch parameter has spaces") == 0);
  114. EAMAIN_TEST(commandLine.Arg(3) == nullptr);
  115. EAMAIN_TEST(commandLine.Argv()[3] == nullptr);
  116. }
  117. {
  118. const char *commandLineString = "program.elf -x -switch:\"this switch parameter has spaces\" \"-switch2:as does this one\"";
  119. const char *parameter = NULL;
  120. CommandLine commandLine(commandLineString);
  121. EAMAIN_TEST(commandLine.Argc() == 4);
  122. EAMAIN_TEST_FATAL(commandLine.Argv() != NULL);
  123. EAMAIN_TEST(strcmp(commandLine.Arg(0), "program.elf") == 0);
  124. EAMAIN_TEST(strcmp(commandLine.Arg(1), "-x") == 0);
  125. EAMAIN_TEST(commandLine.FindSwitch("-x") == 1);
  126. EAMAIN_TEST(commandLine.FindSwitch("-switch", false, &parameter) == 2);
  127. EAMAIN_TEST_FATAL(parameter != NULL);
  128. EAMAIN_TEST(strcmp(parameter, "this switch parameter has spaces") == 0);
  129. EAMAIN_TEST(commandLine.Arg(4) == nullptr);
  130. EAMAIN_TEST(commandLine.Argv()[4] == nullptr);
  131. }
  132. {
  133. const char *commandLineString = "-x -switch:\"this switch parameter has spaces\" \"-switch2:as does this one\"";
  134. const char *parameter = NULL;
  135. CommandLine commandLine(commandLineString, CommandLine::FLAG_NO_PROGRAM_NAME);
  136. EAMAIN_TEST(commandLine.Argc() == 4);
  137. EAMAIN_TEST_FATAL(commandLine.Argv() != NULL);
  138. EAMAIN_TEST(strcmp(commandLine.Arg(0), "") == 0);
  139. EAMAIN_TEST(strcmp(commandLine.Arg(1), "-x") == 0);
  140. EAMAIN_TEST(commandLine.FindSwitch("-x") == 1);
  141. EAMAIN_TEST(commandLine.FindSwitch("-switch", false, &parameter) == 2);
  142. EAMAIN_TEST_FATAL(parameter != NULL);
  143. EAMAIN_TEST(strcmp(parameter, "this switch parameter has spaces") == 0);
  144. EAMAIN_TEST(commandLine.Arg(4) == nullptr);
  145. EAMAIN_TEST(commandLine.Argv()[4] == nullptr);
  146. }
  147. {
  148. int argc = 3;
  149. char arg0[] = "program.elf";
  150. char arg1[] = "-x";
  151. char arg2[] = "-switch:this switch parameter has spaces";
  152. char *argv[] = { arg0, arg1, arg2 };
  153. const char *parameter;
  154. CommandLine commandLine(argc, argv);
  155. EAMAIN_TEST(commandLine.Argc() == 3);
  156. EAMAIN_TEST_FATAL(commandLine.Argv() != NULL);
  157. EAMAIN_TEST(strcmp(commandLine.Arg(0), "program.elf") == 0);
  158. EAMAIN_TEST(strcmp(commandLine.Arg(1), "-x") == 0);
  159. EAMAIN_TEST(commandLine.FindSwitch("-x") == 1);
  160. EAMAIN_TEST(commandLine.FindSwitch("-switch", false, &parameter) == 2);
  161. EAMAIN_TEST_FATAL(parameter != NULL);
  162. EAMAIN_TEST(strcmp(parameter, "this switch parameter has spaces") == 0);
  163. EAMAIN_TEST(commandLine.Arg(3) == nullptr);
  164. EAMAIN_TEST(commandLine.Argv()[3] == nullptr);
  165. }
  166. return nErrorCount;
  167. #undef EAMAIN_TEST
  168. #undef EAMAIN_TEST_FATAL
  169. }
  170. static bool gEAMainInitCalled;
  171. void EAMainInit()
  172. {
  173. gEAMainInitCalled = true;
  174. }
  175. void EAMainFini()
  176. {
  177. }
  178. ///////////////////////////////////////////////////////////////////////////////
  179. // EAMain
  180. //
  181. int EAMain(int argc, char** argv)
  182. {
  183. using namespace EA::EAMain;
  184. int nErrorCount(0);
  185. Report("List of arguments passed:\n");
  186. for (int i = 0; i < argc; ++i)
  187. {
  188. Report("Arg %d: %s\n", i, argv[i]);
  189. }
  190. Report("\n");
  191. // Basic test of redirection of stdout/stderr on platforms that support
  192. // redirection of these streams.
  193. printf("printf(...)\n");
  194. fprintf(stdout, "fprintf(stdout, ...)\n");
  195. fflush(stdout);
  196. fprintf(stderr, "fprintf(stderr, ...)\n");
  197. fflush(stderr);
  198. Report("Test of %s.\n", "Report()");
  199. Report("Report()\n");
  200. bool bArgPassed = false;
  201. for (int i = 0; i < argc; ++i)
  202. {
  203. if (strcmp(argv[i], "-testargpassing") == 0)
  204. {
  205. bArgPassed = true;
  206. }
  207. }
  208. // Windows Phone does not support passing arguments to app bundles when
  209. // launching them.
  210. #if !defined(EA_PLATFORM_WINDOWS_PHONE)
  211. if (!bArgPassed)
  212. {
  213. Report("Arg not passed!\n");
  214. ++nErrorCount;
  215. }
  216. #endif
  217. if (TestCommandLineArgs() != 0)
  218. {
  219. Report("Error parsing command line arguments!\n");
  220. ++nErrorCount;
  221. }
  222. if (!gEAMainInitCalled)
  223. {
  224. Report("EAMainInit was not called!\n");
  225. ++nErrorCount;
  226. }
  227. Report("This report statement has \nno terminating newline");
  228. return nErrorCount;
  229. }