input.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/interactive/input.cpp
  3. // Purpose: Miscellaneous tests requiring user input
  4. // Author: Francesco Montorsi (extracted from console sample)
  5. // Created: 2010-06-21
  6. // Copyright: (c) 2010 wxWidgets team
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ============================================================================
  9. // declarations
  10. // ============================================================================
  11. // ----------------------------------------------------------------------------
  12. // headers
  13. // ----------------------------------------------------------------------------
  14. #include "testprec.h"
  15. #ifdef __BORLANDC__
  16. #pragma hdrstop
  17. #endif
  18. #include "wx/app.h"
  19. #include "wx/wxcrt.h" // for wxPuts
  20. #include "wx/wxcrtvararg.h" // for wxPrintf
  21. // ----------------------------------------------------------------------------
  22. // conditional compilation
  23. // ----------------------------------------------------------------------------
  24. #define TEST_SNGLINST
  25. #define TEST_FTP
  26. #define TEST_INFO_FUNCTIONS
  27. #if wxUSE_REGEX
  28. #define TEST_REGEX
  29. #endif
  30. #define TEST_DATETIME
  31. // ----------------------------------------------------------------------------
  32. // test class
  33. // ----------------------------------------------------------------------------
  34. class InteractiveInputTestCase : public CppUnit::TestCase
  35. {
  36. public:
  37. InteractiveInputTestCase() { }
  38. private:
  39. CPPUNIT_TEST_SUITE( InteractiveInputTestCase );
  40. CPPUNIT_TEST( TestSingleIstance );
  41. CPPUNIT_TEST( TestFtpInteractive );
  42. CPPUNIT_TEST( TestDiskInfo );
  43. CPPUNIT_TEST( TestRegExInteractive );
  44. CPPUNIT_TEST( TestDateTimeInteractive );
  45. CPPUNIT_TEST_SUITE_END();
  46. void TestSingleIstance();
  47. void TestFtpInteractive();
  48. void TestDiskInfo();
  49. void TestRegExInteractive();
  50. void TestDateTimeInteractive();
  51. wxDECLARE_NO_COPY_CLASS(InteractiveInputTestCase);
  52. };
  53. // ----------------------------------------------------------------------------
  54. // CppUnit macros
  55. // ----------------------------------------------------------------------------
  56. //CPPUNIT_TEST_SUITE_REGISTRATION( InteractiveInputTestCase );
  57. // do not run this test by default!
  58. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( InteractiveInputTestCase, "InteractiveInputTestCase" );
  59. // ============================================================================
  60. // implementation
  61. // ============================================================================
  62. // ----------------------------------------------------------------------------
  63. // misc information functions
  64. // ----------------------------------------------------------------------------
  65. #include "wx/utils.h"
  66. void InteractiveInputTestCase::TestDiskInfo()
  67. {
  68. #ifdef TEST_INFO_FUNCTIONS
  69. wxPuts(wxT("*** Testing wxGetDiskSpace() ***"));
  70. for ( ;; )
  71. {
  72. wxChar pathname[128];
  73. wxPrintf(wxT("Enter a directory name (press ENTER or type 'quit' to escape): "));
  74. if ( !wxFgets(pathname, WXSIZEOF(pathname), stdin) )
  75. break;
  76. // kill the last '\n'
  77. pathname[wxStrlen(pathname) - 1] = 0;
  78. if (pathname[0] == '\0' || wxStrcmp(pathname, "quit") == 0)
  79. break;
  80. wxLongLong total, free;
  81. if ( !wxGetDiskSpace(pathname, &total, &free) )
  82. {
  83. wxPuts(wxT("ERROR: wxGetDiskSpace failed."));
  84. }
  85. else
  86. {
  87. wxPrintf(wxT("%sKb total, %sKb free on '%s'.\n"),
  88. (total / 1024).ToString().c_str(),
  89. (free / 1024).ToString().c_str(),
  90. pathname);
  91. }
  92. wxPuts("\n");
  93. }
  94. wxPuts("\n");
  95. #endif // TEST_INFO_FUNCTIONS
  96. }
  97. // ----------------------------------------------------------------------------
  98. // regular expressions
  99. // ----------------------------------------------------------------------------
  100. #include "wx/regex.h"
  101. void InteractiveInputTestCase::TestRegExInteractive()
  102. {
  103. #ifdef TEST_REGEX
  104. wxPuts(wxT("*** Testing RE interactively ***"));
  105. for ( ;; )
  106. {
  107. wxChar pattern[128];
  108. wxPrintf(wxT("Enter a pattern (press ENTER or type 'quit' to escape): "));
  109. if ( !wxFgets(pattern, WXSIZEOF(pattern), stdin) )
  110. break;
  111. // kill the last '\n'
  112. pattern[wxStrlen(pattern) - 1] = 0;
  113. if (pattern[0] == '\0' || wxStrcmp(pattern, "quit") == 0)
  114. break;
  115. wxRegEx re;
  116. if ( !re.Compile(pattern) )
  117. {
  118. continue;
  119. }
  120. wxChar text[128];
  121. for ( ;; )
  122. {
  123. wxPrintf(wxT("Enter text to match: "));
  124. if ( !wxFgets(text, WXSIZEOF(text), stdin) )
  125. break;
  126. // kill the last '\n'
  127. text[wxStrlen(text) - 1] = 0;
  128. if ( !re.Matches(text) )
  129. {
  130. wxPrintf(wxT("No match.\n"));
  131. }
  132. else
  133. {
  134. wxPrintf(wxT("Pattern matches at '%s'\n"), re.GetMatch(text).c_str());
  135. size_t start, len;
  136. for ( size_t n = 1; ; n++ )
  137. {
  138. if ( !re.GetMatch(&start, &len, n) )
  139. {
  140. break;
  141. }
  142. wxPrintf(wxT("Subexpr %u matched '%s'\n"),
  143. n, wxString(text + start, len).c_str());
  144. }
  145. }
  146. }
  147. wxPuts("\n");
  148. }
  149. #endif // TEST_REGEX
  150. }
  151. // ----------------------------------------------------------------------------
  152. // FTP
  153. // ----------------------------------------------------------------------------
  154. #include "wx/protocol/ftp.h"
  155. #include "wx/protocol/log.h"
  156. #define FTP_ANONYMOUS
  157. #ifdef FTP_ANONYMOUS
  158. static const wxChar *hostname = wxT("ftp.wxwidgets.org");
  159. #else
  160. static const wxChar *hostname = "localhost";
  161. #endif
  162. void InteractiveInputTestCase::TestFtpInteractive()
  163. {
  164. #ifdef TEST_FTP
  165. wxFTP ftp;
  166. wxPuts(wxT("\n*** Interactive wxFTP test ***"));
  167. #ifdef FTP_ANONYMOUS
  168. wxPrintf(wxT("--- Attempting to connect to %s:21 anonymously...\n"), hostname);
  169. #else // !FTP_ANONYMOUS
  170. wxChar user[256];
  171. wxFgets(user, WXSIZEOF(user), stdin);
  172. user[wxStrlen(user) - 1] = '\0'; // chop off '\n'
  173. ftp.SetUser(user);
  174. wxChar password[256];
  175. wxPrintf(wxT("Password for %s: "), password);
  176. wxFgets(password, WXSIZEOF(password), stdin);
  177. password[wxStrlen(password) - 1] = '\0'; // chop off '\n'
  178. ftp.SetPassword(password);
  179. wxPrintf(wxT("--- Attempting to connect to %s:21 as %s...\n"), hostname, user);
  180. #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
  181. if ( !ftp.Connect(hostname) )
  182. {
  183. wxPrintf(wxT("ERROR: failed to connect to %s\n"), hostname);
  184. return;
  185. }
  186. else
  187. {
  188. wxPrintf(wxT("--- Connected to %s, current directory is '%s'\n"),
  189. hostname, ftp.Pwd().c_str());
  190. }
  191. wxChar buf[128];
  192. for ( ;; )
  193. {
  194. wxPrintf(wxT("Enter FTP command (press ENTER or type 'quit' to escape): "));
  195. if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
  196. break;
  197. // kill the last '\n'
  198. buf[wxStrlen(buf) - 1] = 0;
  199. if (buf[0] == '\0' || wxStrcmp(buf, "quit") == 0)
  200. break;
  201. // special handling of LIST and NLST as they require data connection
  202. wxString start(buf, 4);
  203. start.MakeUpper();
  204. if ( start == wxT("LIST") || start == wxT("NLST") )
  205. {
  206. wxString wildcard;
  207. if ( wxStrlen(buf) > 4 )
  208. wildcard = buf + 5;
  209. wxArrayString files;
  210. if ( !ftp.GetList(files, wildcard, start == wxT("LIST")) )
  211. {
  212. wxPrintf(wxT("ERROR: failed to get %s of files\n"), start.c_str());
  213. }
  214. else
  215. {
  216. wxPrintf(wxT("--- %s of '%s' under '%s':\n"),
  217. start.c_str(), wildcard.c_str(), ftp.Pwd().c_str());
  218. size_t count = files.GetCount();
  219. for ( size_t n = 0; n < count; n++ )
  220. {
  221. wxPrintf(wxT("\t%s\n"), files[n].c_str());
  222. }
  223. wxPuts(wxT("--- End of the file list"));
  224. }
  225. }
  226. else // !list
  227. {
  228. wxChar ch = ftp.SendCommand(buf);
  229. wxPrintf(wxT("Command %s"), ch ? wxT("succeeded") : wxT("failed"));
  230. if ( ch )
  231. {
  232. wxPrintf(wxT(" (return code %c)"), ch);
  233. }
  234. wxPrintf(wxT(", server reply:\n%s\n\n"), ftp.GetLastResult().c_str());
  235. }
  236. }
  237. wxPuts(wxT("\n"));
  238. #endif // TEST_FTP
  239. }
  240. // ----------------------------------------------------------------------------
  241. // date time
  242. // ----------------------------------------------------------------------------
  243. #include "wx/math.h"
  244. #include "wx/datetime.h"
  245. void InteractiveInputTestCase::TestDateTimeInteractive()
  246. {
  247. #ifdef TEST_DATETIME
  248. wxPuts(wxT("\n*** interactive wxDateTime tests ***"));
  249. wxChar buf[128];
  250. for ( ;; )
  251. {
  252. wxPrintf(wxT("Enter a date (press ENTER or type 'quit' to escape): "));
  253. if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
  254. break;
  255. // kill the last '\n'
  256. buf[wxStrlen(buf) - 1] = 0;
  257. if ( buf[0] == '\0' || wxStrcmp(buf, "quit") == 0 )
  258. break;
  259. wxDateTime dt;
  260. const wxChar *p = dt.ParseDate(buf);
  261. if ( !p )
  262. {
  263. wxPrintf(wxT("ERROR: failed to parse the date '%s'.\n"), buf);
  264. continue;
  265. }
  266. else if ( *p )
  267. {
  268. wxPrintf(wxT("WARNING: parsed only first %u characters.\n"), p - buf);
  269. }
  270. wxPrintf(wxT("%s: day %u, week of month %u/%u, week of year %u\n"),
  271. dt.Format(wxT("%b %d, %Y")).c_str(),
  272. dt.GetDayOfYear(),
  273. dt.GetWeekOfMonth(wxDateTime::Monday_First),
  274. dt.GetWeekOfMonth(wxDateTime::Sunday_First),
  275. dt.GetWeekOfYear(wxDateTime::Monday_First));
  276. }
  277. wxPuts("\n");
  278. #endif // TEST_DATETIME
  279. }
  280. // ----------------------------------------------------------------------------
  281. // single instance
  282. // ----------------------------------------------------------------------------
  283. #include "wx/snglinst.h"
  284. void InteractiveInputTestCase::TestSingleIstance()
  285. {
  286. #ifdef TEST_SNGLINST
  287. wxPuts(wxT("\n*** Testing wxSingleInstanceChecker ***"));
  288. wxSingleInstanceChecker checker;
  289. if ( checker.Create(wxT(".wxconsole.lock")) )
  290. {
  291. if ( checker.IsAnotherRunning() )
  292. {
  293. wxPrintf(wxT("Another instance of the program is running, exiting.\n"));
  294. return;
  295. }
  296. // wait some time to give time to launch another instance
  297. wxPuts(wxT("If you try to run another instance of this program now, it won't start."));
  298. wxPrintf(wxT("Press \"Enter\" to exit wxSingleInstanceChecker test and proceed..."));
  299. wxFgetc(stdin);
  300. }
  301. else // failed to create
  302. {
  303. wxPrintf(wxT("Failed to init wxSingleInstanceChecker.\n"));
  304. }
  305. wxPuts("\n");
  306. #endif // defined(TEST_SNGLINST)
  307. }