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
2.5 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: examples/cpp/util/example.cpp.util.strdup/example.cpp.util.strdup.cpp
  3. *
  4. * Purpose: C++ example program for Pantheios. Demonstrates:
  5. *
  6. * - use of pantheios::util::strdup_throw() and
  7. * pantheios::util::strdup_nothrow() for creating C-style
  8. * strings
  9. *
  10. * Created: 27th December 2010
  11. * Updated: 4th January 2011
  12. *
  13. * www: http://www.pantheios.org/
  14. *
  15. * License: This source code is placed into the public domain 2006
  16. * by Synesis Software Pty Ltd. There are no restrictions
  17. * whatsoever to your use of the software.
  18. *
  19. * This software is provided "as is", and any warranties,
  20. * express or implied, of any kind and for any purpose, are
  21. * disclaimed.
  22. *
  23. * ////////////////////////////////////////////////////////////////////// */
  24. #include <pantheios/util/test/compiler_warnings_suppression.first_include.h>
  25. /* Pantheios Header Files */
  26. #include <pantheios/util/string/strdup.h> // for pantheios::util::strdup_throw/strdup_nothrow()
  27. /* STLSoft Header Files */
  28. #include <stlsoft/stlsoft.h> // for STLSOFT_CF_THROW_BAD_ALLOC
  29. /* Standard C/C++ Header Files */
  30. #include <new> // for std::bad_alloc
  31. #include <stdlib.h> // for exit codes
  32. #include <pantheios/util/test/compiler_warnings_suppression.last_include.h>
  33. /* ////////////////////////////////////////////////////////////////////// */
  34. #define PSTR(x) PANTHEIOS_LITERAL_STRING(x)
  35. /* ////////////////////////////////////////////////////////////////////// */
  36. int main(int /* argc */, char** /* argv */)
  37. {
  38. { // nothrow form
  39. PAN_CHAR_T* s = pantheios::util::strdup_nothrow(PSTR("abc"));
  40. if(NULL == s)
  41. {
  42. // ... failed to allocate
  43. }
  44. else
  45. {
  46. // ... allocated successfully
  47. // free string after using it
  48. pantheios::util::strfree(s);
  49. }
  50. }
  51. #ifdef STLSOFT_CF_THROW_BAD_ALLOC
  52. { // throw form
  53. try
  54. {
  55. PAN_CHAR_T* s = pantheios::util::strdup_throw(PSTR("abc"));
  56. // ... allocated successfully
  57. // free string after using it
  58. pantheios::util::strfree(s);
  59. }
  60. catch(std::bad_alloc&)
  61. {
  62. // ... failed to allocate
  63. }
  64. }
  65. #endif /* STLSOFT_CF_THROW_BAD_ALLOC */
  66. return EXIT_SUCCESS;
  67. }
  68. /* ///////////////////////////// end of file //////////////////////////// */