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.

93 lines
3.0 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: examples/cpp/inserters/example.cpp.inserter.b64/example.cpp.inserter.b64.cpp
  3. *
  4. * Purpose: C++ example program for Pantheios. Demonstrates:
  5. *
  6. * - use of Pantheios b64 inserter for blob types
  7. * - use of pantheios::logputs() in bail-out conditions
  8. *
  9. * Created: 25th 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/inserters/b64.hpp> // for pantheios::b64
  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.inserter.b64");
  35. /* ////////////////////////////////////////////////////////////////////// */
  36. #define PSTR(x) PANTHEIOS_LITERAL_STRING(x)
  37. /* ////////////////////////////////////////////////////////////////////// */
  38. int main()
  39. {
  40. try
  41. {
  42. // Make a blob with some arbitrary values
  43. pantheios::uint8_t bytes[20];
  44. { for(size_t i = 0; i < STLSOFT_NUM_ELEMENTS(bytes); ++i)
  45. {
  46. //bytes[i] = static_cast<pantheios::uint8_t>((i << 8) | (i & 0x0f));
  47. bytes[i] = static_cast<pantheios::uint8_t>(i);
  48. }}
  49. // Log the blob with default formatting; Output:
  50. //
  51. // "bytes: [AAECAwQFBgcICQoLDA0ODxAREhM=]"
  52. pantheios::log_NOTICE(PSTR("bytes: ["), pantheios::b64(bytes, sizeof(bytes)), PSTR("]"));
  53. // Log the blob with a maximum line length of 16; Output:
  54. // "bytes: [AAECAwQFBgcICQoL
  55. // DA0ODxAREhM=]"
  56. pantheios::log_NOTICE(PSTR("bytes: ["), pantheios::b64(bytes, sizeof(bytes), ::b64::B64_F_LINE_LEN_USE_PARAM, 16), PSTR("]"));
  57. return EXIT_SUCCESS;
  58. }
  59. catch(std::bad_alloc&)
  60. {
  61. pantheios::log(pantheios::alert, PSTR("out of memory"));
  62. }
  63. catch(std::exception& x)
  64. {
  65. pantheios::log_CRITICAL(PSTR("Exception: "), x);
  66. }
  67. catch(...)
  68. {
  69. pantheios::logputs(pantheios::emergency, PSTR("Unexpected unknown error"));
  70. }
  71. return EXIT_FAILURE;
  72. }
  73. /* ///////////////////////////// end of file //////////////////////////// */