You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

569 lines
19 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: test/scratch/test.scratch.api/test.scratch.api.cpp
  3. *
  4. * Purpose: Implementation file for the test.scratch.api project.
  5. *
  6. * Created: 26th June 2005
  7. * Updated: 6th August 2012
  8. *
  9. * Status: Wizard-generated
  10. *
  11. * License: (Licensed under the Synesis Software Open License)
  12. *
  13. * www: http://www.synesis.com.au/software
  14. *
  15. * This source code is placed into the public domain 2005
  16. * by Synesis Software Pty Ltd. There are no restrictions
  17. * whatsoever to your use of the software.
  18. *
  19. * This source code is provided by Synesis Software Pty Ltd "as is"
  20. * and any warranties, whether expressed or implied, including, but
  21. * not limited to, the implied warranties of merchantability and
  22. * fitness for a particular purpose are disclaimed. In no event
  23. * shall the Synesis Software Pty Ltd be liable for any direct,
  24. * indirect, incidental, special, exemplary, or consequential
  25. * damages (including, but not limited to, procurement of
  26. * substitute goods or services; loss of use, data, or profits; or
  27. * business interruption) however caused and on any theory of
  28. * liability, whether in contract, strict liability, or tort
  29. * (including negligence or otherwise) arising in any way out of
  30. * the use of this software, even if advised of the possibility of
  31. * such damage.
  32. *
  33. * Neither the name of Synesis Software Pty Ltd nor the names of
  34. * any subdivisions, employees or agents of Synesis Software Pty
  35. * Ltd, nor the names of any other contributors to this software
  36. * may be used to endorse or promote products derived from this
  37. * software without specific prior written permission.
  38. *
  39. * ////////////////////////////////////////////////////////////////////// */
  40. #define PANTHEIOS_NO_INCLUDE_OS_AND_3PTYLIB_STRING_ACCESS
  41. /* This inclusion required for suppressing warnings during NoX (No eXception-support) configurations. */
  42. #include <pantheios/util/test/compiler_warnings_suppression.first_include.h>
  43. /* Pantheios Header Files */
  44. #include <pantheios/pantheios.hpp>
  45. #include <pantheios/inserters/blob.hpp>
  46. #include <pantheios/inserters/integer.hpp>
  47. #include <pantheios/inserters/pointer.hpp>
  48. #include <pantheios/inserters/real.hpp>
  49. #include <pantheios/backends/be.lrsplit.h>
  50. #include <pantheios/frontend.h>
  51. /* STLSoft Header Files */
  52. #include <stlsoft/memory/malloc_allocator.hpp>
  53. #include <stlsoft/memory/new_allocator.hpp>
  54. #include <stlsoft/string/shim_string.hpp>
  55. #include <stlsoft/string/simple_string.hpp>
  56. /* PlatformSTL Header Files */
  57. #include <platformstl/platformstl.hpp>
  58. #if defined(PLATFORMSTL_OS_IS_WINDOWS)
  59. /* WinSTL Header Files */
  60. # include <winstl/error/error_desc.hpp>
  61. # include <winstl/filesystem/findfile_sequence.hpp>
  62. # include <winstl/memory/processheap_allocator.hpp>
  63. # include <winstl/shims/access/string/time.hpp>
  64. #else /* ? platform */
  65. # include <stlsoft/shims/access/string/std/time.hpp>
  66. #endif /* platform */
  67. /* Standard C++ Header Files */
  68. #include <exception>
  69. #include <iostream>
  70. #include <string>
  71. using std::cerr;
  72. using std::cin;
  73. using std::cout;
  74. using std::endl;
  75. /* Standard C Header Files */
  76. #include <time.h>
  77. #include <pantheios/util/test/compiler_warnings_suppression.last_include.h>
  78. /* ////////////////////////////////////////////////////////////////////// */
  79. #ifdef PANTHEIOS_USE_WIDE_STRINGS
  80. PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("test.scratch.api");
  81. /* The following allows this to be used with the Windows
  82. * EventLog back-end.
  83. */
  84. PANTHEIOS_CALL(void) pantheios_be_WindowsEventLog_calcCategoryAndEventId(
  85. int /* backEndId */
  86. , int /* severity */
  87. , pantheios::uint16_t* category
  88. , pantheios::uint32_t* eventId
  89. ) /* throw() */;
  90. PANTHEIOS_CALL(void) pantheios_be_WindowsEventLog_calcCategoryAndEventId(
  91. int /* backEndId */
  92. , int /* severity */
  93. , pantheios::uint16_t* category
  94. , pantheios::uint32_t* eventId
  95. ) /* throw() */
  96. {
  97. // NOTE: A proper implementation would specify non-0 values here that
  98. // identify the requisite event identifier and category within the
  99. // associated message file(s)
  100. *eventId = 0;
  101. *category = 0;
  102. }
  103. int main()
  104. {
  105. return 0;
  106. }
  107. #else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  108. /* /////////////////////////////////////////////////////////////////////////
  109. * Forward declarations
  110. */
  111. static int main_(int /* argc */, char ** /*argv*/);
  112. static void some_logging_with_using_namespace_directive();
  113. static void some_logging_with_using_declarations();
  114. static void some_logging_with_explicit_qualification();
  115. /* /////////////////////////////////////////////////////////////////////////
  116. * Macros
  117. */
  118. #define P_STR PANTHEIOS_LITERAL_STRING
  119. /* /////////////////////////////////////////////////////////////////////////
  120. * String encoding compatibility
  121. */
  122. #ifdef PANTHEIOS_USE_WIDE_STRINGS
  123. # define pan_sprintf_s_ swprintf_s
  124. # define pan_sprintf_ swprintf
  125. # define c_str_data_t_ c_str_data_w
  126. # define c_str_len_t_ c_str_len_w
  127. #else /* ? PANTHEIOS_USE_WIDE_STRINGS */
  128. # define pan_sprintf_s_ sprintf_s
  129. # define pan_sprintf_ sprintf
  130. # define c_str_data_t_ c_str_data_a
  131. # define c_str_len_t_ c_str_len_a
  132. #endif /* PANTHEIOS_USE_WIDE_STRINGS */
  133. /* /////////////////////////////////////////////////////////////////////////
  134. * Classes
  135. */
  136. class Thingy
  137. {
  138. };
  139. namespace stlsoft
  140. {
  141. inline stlsoft::basic_shim_string<pantheios::pan_char_t>
  142. c_str_data_t_(Thingy const &thingy)
  143. {
  144. stlsoft::basic_shim_string<pantheios::pan_char_t> s(50);
  145. int cch = pan_sprintf_(s.data(), P_STR("0x%p"), static_cast<void const*>(&thingy));
  146. s.truncate(static_cast<size_t>(cch));
  147. return s;
  148. }
  149. inline size_t
  150. c_str_len_t_(Thingy const &/* thingy */)
  151. {
  152. const size_t minLen = 4;
  153. const size_t maxLen = 2 + sizeof(void*) * 2;
  154. return pantheios::pan_slice_t::get_lazy_length(minLen, maxLen);
  155. }
  156. } // namespace stlsoft
  157. std::string Thingy_to_string(Thingy const *thingy);
  158. std::string Thingy_to_string(Thingy const *thingy)
  159. {
  160. char sz[21];
  161. void const* pv = thingy;
  162. return std::string(sz, static_cast<size_t>(::sprintf(sz, "0x%p", pv)));
  163. }
  164. /* /////////////////////////////////////////////////////////////////////////
  165. * Process Identity
  166. *
  167. * This is defined for when we link with the pantheios.fe.simple front-end
  168. * library, which implements pantheios_fe_getProcessIdentity() in terms of this
  169. * externally defined array
  170. */
  171. PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("test.scratch.api");
  172. /* The following allows this to be used with the Windows
  173. * EventLog back-end.
  174. */
  175. PANTHEIOS_CALL(void) pantheios_be_WindowsEventLog_calcCategoryAndEventId(
  176. int /* backEndId */
  177. , int /* severity */
  178. , pantheios::uint16_t* category
  179. , pantheios::uint32_t* eventId
  180. ) /* throw() */;
  181. PANTHEIOS_CALL(void) pantheios_be_WindowsEventLog_calcCategoryAndEventId(
  182. int /* backEndId */
  183. , int /* severity */
  184. , pantheios::uint16_t* category
  185. , pantheios::uint32_t* eventId
  186. ) /* throw() */
  187. {
  188. // NOTE: A proper implementation would specify non-0 values here that
  189. // identify the requisite event identifier and category within the
  190. // associated message file(s)
  191. *eventId = 0;
  192. *category = 0;
  193. }
  194. /* ////////////////////////////////////////////////////////////////////// */
  195. static int main_(int /* argc */, char ** /*argv*/)
  196. {
  197. #if defined(_DEBUG) || \
  198. defined(__SYNSOFT_DBS_DEBUG)
  199. cout << "test.scratch.api: " << STLSOFT_COMPILER_LABEL_STRING << endl;
  200. #endif /* debug */
  201. some_logging_with_using_declarations();
  202. some_logging_with_explicit_qualification();
  203. some_logging_with_using_namespace_directive();
  204. return EXIT_SUCCESS;
  205. }
  206. int main(int argc, char *argv[])
  207. {
  208. #if 0
  209. { for(size_t i = 0;0 != ++i;) {} }
  210. #endif /* 0 */
  211. pantheios::log_DEBUG(P_STR("debug"));
  212. pantheios::log_INFORMATIONAL(P_STR("informational"));
  213. pantheios::log_NOTICE(P_STR("notice"));
  214. pantheios::log_WARNING(P_STR("warning"));
  215. pantheios::log_ERROR(P_STR("error"));
  216. pantheios::log_CRITICAL(P_STR("critical"));
  217. pantheios::log_ALERT(P_STR("alert"));
  218. pantheios::log_EMERGENCY(P_STR("emergency"));
  219. pantheios::log(pantheios::debug, P_STR("debug"));
  220. pantheios::log(pantheios::informational, P_STR("informational"));
  221. pantheios::log(pantheios::notice, P_STR("notice"));
  222. pantheios::log(pantheios::warning, P_STR("warning"));
  223. pantheios::log(pantheios::error, P_STR("error"));
  224. pantheios::log(pantheios::critical, P_STR("critical"));
  225. pantheios::log(pantheios::alert, P_STR("alert"));
  226. pantheios::log(pantheios::emergency, P_STR("emergency"));
  227. pantheios::log(pantheios::debug(10), P_STR("debug"));
  228. pantheios::log(pantheios::informational(10), P_STR("informational"));
  229. pantheios::log(pantheios::notice(10), P_STR("notice"));
  230. pantheios::log(pantheios::warning(10), P_STR("warning"));
  231. pantheios::log(pantheios::error(10), P_STR("error"));
  232. pantheios::log(pantheios::critical(10), P_STR("critical"));
  233. pantheios::log(pantheios::alert(10), P_STR("alert"));
  234. pantheios::log(pantheios::emergency(10), P_STR("emergency"));
  235. #ifdef _DEBUGx
  236. { for(size_t i = 0; i != 200; ++i)
  237. {
  238. std::string s(i, '~');
  239. pantheios::log_DEBUG(s);
  240. pantheios::log_INFORMATIONAL(s);
  241. pantheios::log_NOTICE(s);
  242. pantheios::log_WARNING(s);
  243. pantheios::log_ERROR(s);
  244. pantheios::log_CRITICAL(s);
  245. pantheios::log_ALERT(s);
  246. pantheios::log_EMERGENCY(s);
  247. ::Sleep(5);
  248. }}
  249. #endif /* _DEBUG */
  250. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  251. try
  252. {
  253. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  254. #if !defined(STLSOFT_COMPILER_IS_GCC)
  255. if(1)
  256. {
  257. Thingy thingy;
  258. pantheios::log_NOTICE(P_STR("Testing use of string min/max, with a Thingy instance: ["), thingy, P_STR("]"));
  259. }
  260. #endif /* compiler */
  261. #if 0
  262. if(1)
  263. {
  264. Thingy thingy;
  265. pantheios::log_DEBUG("Testing custom type adaptation, with a Thingy instance: ", pantheios::insert(&thingy, Thingy_to_string));
  266. }
  267. #endif /* 0 */
  268. if(1)
  269. {
  270. short s = 123;
  271. int i = 456;
  272. long l = 789;
  273. float f = static_cast<float>(0.123);
  274. double d = 0.456;
  275. long double ld = 0.789;
  276. void *p = reinterpret_cast<void*>(0x01230123);
  277. int ar[2] = { 0x00112233, 0x44556677 };
  278. pantheios::log_INFORMATIONAL( P_STR("This string contains: ")
  279. , P_STR("some integers ("), pantheios::integer(s), P_STR(", "), pantheios::integer(i, 4 | pantheios::fmt::zeroPad), P_STR(", "), pantheios::integer(l), P_STR("); ")
  280. , P_STR("some real numbers ("), pantheios::real(f), P_STR(", "), pantheios::real(d), P_STR(", "), pantheios::real(ld), P_STR("); ")
  281. , P_STR("a pointer ("), pantheios::pointer(p, pantheios::fmt::fullHex), P_STR("), ")
  282. , P_STR("and a blob ("), pantheios::blob(ar, sizeof(ar), 2, P_STR("-"), 3, P_STR("\n\t")), P_STR("); "));
  283. }
  284. if(1)
  285. {
  286. using namespace pantheios;
  287. short s = 123;
  288. int i = 456;
  289. long l = 789;
  290. float f = static_cast<float>(0.123);
  291. double d = 0.456;
  292. long double ld = 0.789;
  293. void *p = reinterpret_cast<void*>(0x01230123);
  294. log_INFORMATIONAL( "This string contains: "
  295. , "some integers (", integer(s), P_STR(", "), integer(i, 4 | fmt::zeroPad), P_STR(", "), integer(l), "); "
  296. , "some real numbers (", real(f), P_STR(", "), real(d), P_STR(", "), real(ld), "); "
  297. , "and a pointer (", pointer(p, fmt::fullHex), "); "
  298. );
  299. }
  300. if(1)
  301. {
  302. using pantheios::log_INFORMATIONAL;
  303. using pantheios::integer;
  304. using pantheios::real;
  305. using pantheios::pointer;
  306. using pantheios::fmt;
  307. short s = 123;
  308. int i = 456;
  309. long l = 789;
  310. float f = static_cast<float>(0.123);
  311. double d = 0.456;
  312. long double ld = 0.789;
  313. void *p = reinterpret_cast<void*>(0x01230123);
  314. log_INFORMATIONAL( "This string contains: "
  315. , "some integers (", integer(s), P_STR(", "), integer(i, 4 | fmt::zeroPad), P_STR(", "), integer(l), "); "
  316. , "some real numbers (", real(f), P_STR(", "), real(d), P_STR(", "), real(ld), "); "
  317. , "and a pointer (", pointer(p, fmt::fullHex), "); "
  318. );
  319. }
  320. if(1)
  321. {
  322. }
  323. if(1)
  324. {
  325. void *p = reinterpret_cast<void*>(0x01234567);
  326. char s[] = "abc";
  327. std::string str("def");
  328. pantheios::log(pantheios::notice, "s=", s, ", p=", pantheios::pointer(p, 8 | pantheios::fmt::fullHex), ", str=", str);
  329. }
  330. if(1)
  331. {
  332. double d = 123.456;
  333. char s[] = "abc";
  334. std::string str("def");
  335. pantheios::log(pantheios::notice, "s=", s, ", d=", pantheios::real(d), ", str=", str);
  336. }
  337. if(1)
  338. {
  339. int i = 123;
  340. char s[] = "abc";
  341. std::string str("def");
  342. pantheios::log(pantheios::notice, "s=", s, ", i=", pantheios::integer(i), ", str=", str);
  343. }
  344. return main_(argc, argv);
  345. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  346. }
  347. catch(std::exception &x)
  348. {
  349. cerr << "Unhandled error: " << x.what() << endl;
  350. }
  351. catch(...)
  352. {
  353. pantheios::pantheios_puts(pantheios::emergency, P_STR("Unhandled unknown error"));
  354. }
  355. return EXIT_FAILURE;
  356. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  357. }
  358. /* ////////////////////////////////////////////////////////////////////// */
  359. static void some_logging_with_using_namespace_directive()
  360. {
  361. using namespace pantheios;
  362. log(PANTHEIOS_SEV_DEBUG, "some_logging_with_using_namespace_directive()");
  363. short s = 123;
  364. int i = 456;
  365. long l = 789;
  366. float f = static_cast<float>(0.123);
  367. double d = 0.456;
  368. long double ld = 0.789;
  369. void *p = &l;
  370. char const *lstr = "{a pointer to a C-style string}";
  371. std::string str = "{an instance of std::string}";
  372. #if 1
  373. # if 0
  374. stlsoft::simple_string sstr = "{an instance of stlsoft::simple_string}";
  375. # else /* ? 0 */
  376. std::basic_string< char
  377. , std::char_traits<char>
  378. , stlsoft::new_allocator<char>
  379. > sstr = "{an instance of stlsoft::simple_string}";
  380. # endif /* 0 */
  381. #else /* ? 0 */
  382. stlsoft::basic_simple_string< char
  383. # if 1
  384. , stlsoft::char_traits<char>
  385. , winstl::processheap_allocator<char>
  386. # else /* ? 0 */
  387. , std::char_traits<char>
  388. , stlsoft::new_allocator<char>
  389. # endif /* 0 */
  390. > sstr = "{an instance of stlsoft::simple_string}";
  391. #endif /* 0 */
  392. #if defined(PLATFORMSTL_OS_IS_WINDOWS)
  393. SYSTEMTIME tm;
  394. ::GetLocalTime(&tm);
  395. #else /* ? PLATFORMSTL_OS_IS_???? */
  396. time_t t = ::time(NULL);
  397. struct tm const *tm = ::localtime(&t);
  398. #endif /* PLATFORMSTL_OS_IS_???? */
  399. log_INFORMATIONAL( "This is a (hopefully) typical error string, containing: "
  400. , "some integers (", integer(s), P_STR(", "), integer(i), P_STR(", "), integer(l), "); "
  401. , "some real numbers (", real(f), P_STR(", "), real(d), P_STR(", "), real(ld), "); "
  402. , "a pointer (", pointer(p, fmt::hex), "); "
  403. , "some strings (", lstr, P_STR(", "), str, P_STR(", "), sstr, "); "
  404. , "and a converted time value (", tm, ")"
  405. );
  406. }
  407. static void some_logging_with_using_declarations()
  408. {
  409. using pantheios::log_CRITICAL;
  410. using pantheios::log;
  411. log(PANTHEIOS_SEV_DEBUG, "some_logging_with_using_declarations()");
  412. #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
  413. try
  414. {
  415. throw std::out_of_range("Eeep!");
  416. }
  417. catch(std::exception &x)
  418. {
  419. log_CRITICAL("Something really bad has happened! Details: \"", x, "\"");
  420. }
  421. #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
  422. }
  423. static void some_logging_with_explicit_qualification()
  424. {
  425. pantheios::log(PANTHEIOS_SEV_DEBUG, "some_logging_with_explicit_qualification()");
  426. pantheios::log_WARNING( "We're sure there're likely to be >"
  427. , pantheios::integer(1000000, 20 | pantheios::fmt::zeroPad)
  428. , " satisfied users of Pantheios"
  429. );
  430. }
  431. /* ////////////////////////////////////////////////////////////////////// */
  432. #ifdef WINSTL_FINDVOLUME_API_NOT_DECLARED
  433. # include <winstl/dl/dl_call.hpp>
  434. HANDLE WINAPI FindFirstVolumeA(
  435. LPSTR lpszVolumeName, // output buffer
  436. DWORD cchBufferLength // size of output buffer
  437. )
  438. {
  439. return winstl::dl_call<HANDLE>("KERNEL32", "S:FindFirstVolumeA", lpszVolumeName, cchBufferLength);
  440. }
  441. HANDLE WINAPI FindFirstVolumeW(
  442. LPWSTR lpszVolumeName, // output buffer
  443. DWORD cchBufferLength // size of output buffer
  444. );
  445. BOOL WINAPI FindNextVolumeA(
  446. HANDLE hFindVolume, // volume search handle
  447. LPSTR lpszVolumeName, // output buffer
  448. DWORD cchBufferLength // size of output buffer
  449. )
  450. {
  451. return winstl::dl_call<BOOL>("KERNEL32", "S:FindNextVolumeA", hFindVolume, lpszVolumeName, cchBufferLength);
  452. }
  453. BOOL WINAPI FindNextVolumeW(
  454. HANDLE hFindVolume, // volume search handle
  455. LPWSTR lpszVolumeName, // output buffer
  456. DWORD cchBufferLength // size of output buffer
  457. );
  458. BOOL WINAPI FindVolumeClose(
  459. HANDLE hFindVolume
  460. )
  461. {
  462. return winstl::dl_call<BOOL>("KERNEL32", "S:FindVolumeClose", hFindVolume);
  463. }
  464. #endif /* WINSTL_FINDVOLUME_API_NOT_DECLARED */
  465. /* ////////////////////////////////////////////////////////////////////// */
  466. #endif /* PANTHEIOS_USE_WIDE_STRINGS */
  467. /* ///////////////////////////// end of file //////////////////////////// */