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.

92 lines
2.9 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: examples/cpp/misc/example.cpp.misc.strings/example.cpp.misc.strings.cpp
  3. *
  4. * Purpose: C++ example program for Pantheios. Demonstrates:
  5. *
  6. * - use of Pantheios logging statements for string types
  7. * - use of pantheios::logputs() in bail-out conditions
  8. *
  9. * Created: 17th May 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. #define PANTHEIOS_NO_INCLUDE_OS_AND_3PTYLIB_STRING_ACCESS // Faster compilation
  25. #include <pantheios/pantheios.hpp> // Pantheios C++ main header
  26. /* Standard C/C++ Header Files */
  27. #include <exception> // for std::exception
  28. #include <new> // for std::bad_alloc
  29. #include <string> // for std::string
  30. #include <stdlib.h> // for exit codes
  31. /* ////////////////////////////////////////////////////////////////////// */
  32. /* Define the stock front-end process identity, so that it links when using
  33. * fe.N, fe.simple, etc. */
  34. PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("example.cpp.misc.strings");
  35. /* ////////////////////////////////////////////////////////////////////// */
  36. #define PSTR(x) PANTHEIOS_LITERAL_STRING(x)
  37. /* ////////////////////////////////////////////////////////////////////// */
  38. typedef std::basic_string<PAN_CHAR_T> string_t;
  39. /* ////////////////////////////////////////////////////////////////////// */
  40. static string_t concat(string_t const& s1, string_t const& s2)
  41. {
  42. return s1 + s2;
  43. }
  44. int main()
  45. {
  46. try
  47. {
  48. PAN_CHAR_T s1[] = PSTR("abc");
  49. PAN_CHAR_T const* s2 = PSTR("def");
  50. // Log two C-style strings
  51. pantheios::log_NOTICE(PSTR("Concatenating '"), s1, PSTR("' and '"), s2, PSTR("'"));
  52. string_t result = concat(s1, s2);
  53. // Log two C-style strings and a std::string
  54. pantheios::log_NOTICE(PSTR("Concatenation of '"), s1, PSTR("' and '"), s2, PSTR("' succeeded; result="), result);
  55. return EXIT_SUCCESS;
  56. }
  57. catch(std::bad_alloc&)
  58. {
  59. pantheios::log(pantheios::alert, PSTR("out of memory"));
  60. }
  61. catch(std::exception& x)
  62. {
  63. pantheios::log_CRITICAL(PSTR("Exception: "), x);
  64. }
  65. catch(...)
  66. {
  67. pantheios::logputs(pantheios::emergency, PSTR("Unexpected unknown error"));
  68. }
  69. return EXIT_FAILURE;
  70. }
  71. /* ///////////////////////////// end of file //////////////////////////// */