/* ///////////////////////////////////////////////////////////////////////// * File: examples/cpp/util/example.cpp.util.strdup/example.cpp.util.strdup.cpp * * Purpose: C++ example program for Pantheios. Demonstrates: * * - use of pantheios::util::strdup_throw() and * pantheios::util::strdup_nothrow() for creating C-style * strings * * Created: 27th December 2010 * Updated: 4th January 2011 * * www: http://www.pantheios.org/ * * License: This source code is placed into the public domain 2006 * by Synesis Software Pty Ltd. There are no restrictions * whatsoever to your use of the software. * * This software is provided "as is", and any warranties, * express or implied, of any kind and for any purpose, are * disclaimed. * * ////////////////////////////////////////////////////////////////////// */ #include /* Pantheios Header Files */ #include // for pantheios::util::strdup_throw/strdup_nothrow() /* STLSoft Header Files */ #include // for STLSOFT_CF_THROW_BAD_ALLOC /* Standard C/C++ Header Files */ #include // for std::bad_alloc #include // for exit codes #include /* ////////////////////////////////////////////////////////////////////// */ #define PSTR(x) PANTHEIOS_LITERAL_STRING(x) /* ////////////////////////////////////////////////////////////////////// */ int main(int /* argc */, char** /* argv */) { { // nothrow form PAN_CHAR_T* s = pantheios::util::strdup_nothrow(PSTR("abc")); if(NULL == s) { // ... failed to allocate } else { // ... allocated successfully // free string after using it pantheios::util::strfree(s); } } #ifdef STLSOFT_CF_THROW_BAD_ALLOC { // throw form try { PAN_CHAR_T* s = pantheios::util::strdup_throw(PSTR("abc")); // ... allocated successfully // free string after using it pantheios::util::strfree(s); } catch(std::bad_alloc&) { // ... failed to allocate } } #endif /* STLSOFT_CF_THROW_BAD_ALLOC */ return EXIT_SUCCESS; } /* ///////////////////////////// end of file //////////////////////////// */