Function insertFormattedString

Synopsis

#include <lib/inc/drogon/HttpViewData.h>

void insertFormattedString(const std::string &key, const char *format,...)

Description

Insert a formated string identified by the key parameter.

Source

Lines 75-124 in lib/inc/drogon/HttpViewData.h.

void insertFormattedString(const std::string &key, const char *format, ...)
{
    std::string strBuffer;
    strBuffer.resize(128);
    va_list ap, backup_ap;
    va_start(ap, format);
    va_copy(backup_ap, ap);
    auto result = vsnprintf((char *)strBuffer.data(),
                            strBuffer.size(),
                            format,
                            backup_ap);
    va_end(backup_ap);
    if ((result >= 0) &&
        ((std::string::size_type)result < strBuffer.size()))
    {
        strBuffer.resize(result);
    }
    else
    {
        while (true)
        {
            if (result < 0)
            {
                // Older snprintf() behavior. Just try doubling the buffer
                // size
                strBuffer.resize(strBuffer.size() * 2);
            }
            else
            {
                strBuffer.resize(result + 1);
            }
            va_copy(backup_ap, ap);
            auto result = vsnprintf((char *)strBuffer.data(),
                                    strBuffer.size(),
                                    format,
                                    backup_ap);
            va_end(backup_ap);
            if ((result >= 0) &&
                ((std::string::size_type)result < strBuffer.size()))
            {
                strBuffer.resize(result);
                break;
            }
        }
    }
    va_end(ap);
    viewData_[key] = std::move(strBuffer);
}





Add Discussion as Guest

Log in to DocsForge