2 * File: format_string.cpp
4 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5 * See included license file for license details.
8 #include "format_string.h"
14 //! Size of the temporary buffer to hold the formatted output string.
15 #define WIN32_FMT_BUF_LEN (512)
18 * \brief Simple template class to free a pointer.
24 //! \brief Constructor.
30 //! \brief Destructor.
40 T m_p; //!< The value to free.
43 //! The purpose of this function to provide a convenient way of generating formatted
44 //! STL strings inline. This is especially useful when throwing exceptions that take
45 //! a std::string for a message. The length of the formatted output string is limited
46 //! only by memory. Memory temporarily allocated for the output string is disposed of
51 //! throw std::runtime_error(format_string("error on line %d", line));
54 //! \param fmt Format string using printf-style format markers.
55 //! \return An STL string object of the formatted output.
56 std::string format_string(const char * fmt, ...)
63 buf = (char *)malloc(WIN32_FMT_BUF_LEN);
66 result = _vsnprintf(buf, WIN32_FMT_BUF_LEN, fmt, vargs);
69 result = vasprintf(&buf, fmt, vargs);
72 if (result != -1 && buf)
74 free_ptr<char *> freebuf(buf);
75 return std::string(buf);