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.

128 lines
3.9 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: examples/cpp/misc/example.cpp.misc.hetero1/example.cpp.misc.hetero1.cpp
  3. *
  4. * Purpose: C++ example program for Pantheios. Demonstrates:
  5. *
  6. * - use of implicit support for heterogeneous non-string types
  7. * - use of pantheios::logputs() in bail-out conditions
  8. *
  9. * Created: 31st August 2006
  10. * Updated: 6th December 2010
  11. *
  12. * www: http://www.pantheios.org/
  13. *
  14. * License: This source code is placed into the public domain 2006
  15. * by Synesis Software Pty Ltd. There are no restrictions
  16. * whatsoever to your use of the software.
  17. *
  18. * This software is provided "as is", and any warranties,
  19. * express or implied, of any kind and for any purpose, are
  20. * disclaimed.
  21. *
  22. * ////////////////////////////////////////////////////////////////////// */
  23. /* Pantheios Header Files */
  24. #include <pantheios/pantheios.hpp> // Pantheios C++ main header
  25. #include <pantheios/internal/safestr.h>
  26. /* STLSoft Header Files */
  27. #include <platformstl/platformstl.h> // PlatformSTL main C header
  28. /* Standard C/C++ Header Files */
  29. #include <exception> // for std::exception
  30. #include <new> // for std::bad_alloc
  31. #include <string> // for std::string
  32. #include <stdlib.h> // for exit codes
  33. #if defined(PLATFORMSTL_OS_IS_UNIX)
  34. # include <dirent.h>
  35. #endif /* OS */
  36. /* ////////////////////////////////////////////////////////////////////// */
  37. /* Define the stock front-end process identity, so that it links when using
  38. * fe.N, fe.simple, etc. */
  39. PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("example.cpp.misc.hetero1");
  40. /* ////////////////////////////////////////////////////////////////////// */
  41. #define PSTR(x) PANTHEIOS_LITERAL_STRING(x)
  42. /* ////////////////////////////////////////////////////////////////////// */
  43. int main()
  44. {
  45. try
  46. {
  47. #if defined(PLATFORMSTL_OS_IS_UNIX)
  48. // Demonstrates handling of heterogeneous types by using UNIX/std
  49. // types: struct tm, struct dirent
  50. //
  51. // Output:
  52. // "Heterogeneous values: tm=Aug 31 15:12:18 2006; de=."
  53. time_t t = ::time(NULL);
  54. # ifdef PANTHEIOS_USING_SAFE_STR_FUNCTIONS
  55. struct tm tm_;
  56. errno_t e = ::localtime_s(&tm_, &t);
  57. struct tm *tm = (0== e) ? &tm_ : NULL;
  58. # else /* ? PANTHEIOS_USING_SAFE_STR_FUNCTIONS */
  59. struct tm *tm = ::localtime(&t);
  60. # endif /* PANTHEIOS_USING_SAFE_STR_FUNCTIONS */
  61. DIR *d = ::opendir(".");
  62. struct dirent *de = (NULL != d) ? readdir(d) : NULL;
  63. pantheios::log_NOTICE("Heterogeneous values: tm=", tm, "; de=", de);
  64. if(NULL != d)
  65. {
  66. ::closedir(d);
  67. }
  68. #elif defined(PLATFORMSTL_OS_IS_WINDOWS)
  69. // Demonstrates handling of heterogeneous types by using Win32
  70. // types: GUID, SYSTEMTIME, and VARIANT
  71. //
  72. // Output:
  73. // "Heterogeneous values: g={00000000-0000-0000-0000-000000000000}; s=31/08/2006 2:48:41 pm; v=123456789"
  74. GUID g = GUID_NULL;
  75. SYSTEMTIME s;
  76. VARIANT v;
  77. ::GetLocalTime(&s);
  78. ::VariantInit(&v);
  79. v.vt = VT_I4;
  80. v.lVal = 123456789;
  81. pantheios::log_NOTICE(PSTR("Heterogeneous values: g="), g, PSTR("; s="), s, PSTR("; v="), v);
  82. #else /* ? OS */
  83. # error Operating system not discriminated
  84. #endif /* OS */
  85. return EXIT_SUCCESS;
  86. }
  87. catch(std::bad_alloc&)
  88. {
  89. pantheios::logputs(pantheios::alert, PSTR("out of memory"));
  90. }
  91. catch(std::exception& x)
  92. {
  93. pantheios::log_CRITICAL(PSTR("Exception: "), x);
  94. }
  95. catch(...)
  96. {
  97. pantheios::logputs(pantheios::emergency, PSTR("Unexpected unknown error"));
  98. }
  99. return EXIT_FAILURE;
  100. }
  101. /* ///////////////////////////// end of file //////////////////////////// */