Пример #1
0
/* On Mac OS-X (10.5, at least) errno is not set to ENAMETOOLONG in
 * the case where the buffer is smaller than the hostname length.
 *
 * So, we must establish this for ourselves, as follows:
 */
int pantheios_gethostname_with_errno_fix_(pan_char_t* buffer, size_t cchBuffer)
{
    int res = gethostname(&buffer[0], cchBuffer);

    if(0 != cchBuffer)
    {
        size_t n2 = pan_strlen_(buffer);

        if(n2 + 1 < cchBuffer)
        {
            errno = 0;
        }
        else
        {
            errno = ENAMETOOLONG;
            res = -1;
        }
    }

    return res;
}
Пример #2
0
static size_t pantheios_getHostName_body_(pan_char_t* buffer, size_t cchBuffer)
{
#if defined(PLATFORMSTL_OS_IS_UNIX)

    if(0 == cchBuffer)
    {
        errno = ENAMETOOLONG;

        return 0;
    }
    else
    {
        int res;

        PANTHEIOS_CONTRACT_ENFORCE_ASSUMPTION(NULL != buffer);

        /* First, we mark the last available character in the buffer with
         * the nul terminator, to test later
         */
        buffer[cchBuffer - 1] = '\0';

        errno = 0;

        res = gethostname(&buffer[0], cchBuffer);

        /* Test for ENAMETOOLONG, to avoid any implementation-dependent
         * behaviour wrt whether this it is set on a 0 or a -1 return
         */
        if(ENAMETOOLONG == errno)
        {
            /* To homogenise platform behaviour, we ensure that no fragment is filled out */
            buffer[0] = '\0';

            return cchBuffer;
        }
        else
        {
            if(0 != res)
            {
                /* Was a failure, so return 0 */
                return 0;
            }
            else if('\0' != buffer[cchBuffer - 1])
            {
                /* Was insufficient buffer, so we return the given size (which is the
                 * failure indicator for that condition
                 *
                 * Also, to homogenise platform behaviour, we ensure that no fragment
                 * of the buffer is returned as filled out.
                 */
                buffer[0] = '\0';

                return cchBuffer;
            }
            else
            {
                /* Buffer was sufficient, and the value has been written. The only
                 * way to return the length is to do strlen on it
                 */

                return pan_strlen_(buffer);
            }
        }
    }

#elif defined(PLATFORMSTL_OS_IS_WINDOWS)

    DWORD cchSize = stlsoft_static_cast(DWORD, cchBuffer);

    if(!pan_GetComputerName_(&buffer[0], &cchSize))
    {
        DWORD   err = GetLastError();

        if(ERROR_BUFFER_OVERFLOW == err)
        {
            return cchBuffer;
        }
        else
        {
            return 0;
        }
    }
    else
    {
        return cchSize;
    }

#else /* ? PLATFORMSTL_OS_IS_???? */
# error Platform not discriminated
#endif /* PLATFORMSTL_OS_IS_???? */
}
Пример #3
0
size_t pantheios_getHostName(pan_char_t* buffer, size_t cchBuffer)
{
    size_t  res;

    /* Preconditions */
    PANTHEIOS_CONTRACT_ENFORCE_PRECONDITION_PARAMS_API((0 == cchBuffer || NULL != buffer), "character buffer pointer may only be null if the indicated size is 0");

    /* Body */
    res = pantheios_getHostName_body_(buffer, cchBuffer);

    /* Postconditions */
    PANTHEIOS_CONTRACT_ENFORCE_POSTCONDITION_STATE_API(res <= cchBuffer, "the result must not exceed the buffer length");
    PANTHEIOS_CONTRACT_ENFORCE_POSTCONDITION_STATE_API((0 == res || cchBuffer == res || pan_strlen_(buffer) < cchBuffer), "the result must be 0, or the buffer must be zero, or the length of the written string is less than the buffer length");

    return res;
}
Пример #4
0
void interval::construct_()
{
    m_result.reserve(20);

    interval::units units       =   m_units;
    pan_sint64_t    interval    =   m_interval;

    if(0 == interval)
    {
        static pan_char_t const* const s_zeroes[] =
        {
                NULL
            ,   PANTHEIOS_LITERAL_STRING("0 us")
            ,   PANTHEIOS_LITERAL_STRING("0 ms")
            ,   PANTHEIOS_LITERAL_STRING("0 s")
            ,   PANTHEIOS_LITERAL_STRING("0 mins")
            ,   PANTHEIOS_LITERAL_STRING("0 hrs")
            ,   PANTHEIOS_LITERAL_STRING("0 days")
        };

        pan_char_t const* const ptr =   s_zeroes[units];
        size_t const            len =   pan_strlen_(ptr);

        m_result.assign(ptr, len);
    }
    else
    {
        pan_char_t const* u = NULL;

        switch(units)
        {
            case    microseconds:
                if(0 != (interval % 1000))
                {
                    u = PANTHEIOS_LITERAL_STRING("us");
                }
                else
                {
                    interval /= 1000;

            case    milliseconds:
                    if(0 != (interval % 1000))
                    {
                        u = PANTHEIOS_LITERAL_STRING("ms");
                    }
                    else
                    {
                        interval /= 1000;

            case    seconds:
                        if(0 != (interval % 60))
                        {
                            u = PANTHEIOS_LITERAL_STRING("s");
                        }
                        else
                        {
                            interval /= 60;

            case    minutes:
                            if(0 != (interval % 60))
                            {
                                u = (1 == interval) ? PANTHEIOS_LITERAL_STRING("min") : PANTHEIOS_LITERAL_STRING("mins");
                            }
                            else
                            {
                                interval /= 60;

            case    hours:
                                if(0 != (interval % 24))
                                {
                                    u = (1 == interval) ? PANTHEIOS_LITERAL_STRING("hr") : PANTHEIOS_LITERAL_STRING("hrs");
                                }
                                else
                                {
                                    interval /= 24;

            case    days:
                                    u = (1 == interval) ? PANTHEIOS_LITERAL_STRING("day") : PANTHEIOS_LITERAL_STRING("days");
                                }
                            }
                        }
                    }
                break;
                }
        }

        STLSOFT_ASSERT(NULL != u);

        pan_char_t              buf[21];
        size_t                  cch;
#ifdef PANTHEIOS_STLSOFT_1_12_OR_LATER
        pan_char_t const* const num =   stlsoft::integer_to_decimal_string(buf, STLSOFT_NUM_ELEMENTS(buf), interval, &cch);
#else /* ? PANTHEIOS_STLSOFT_1_12_OR_LATER */
        pan_char_t const* const num =   stlsoft::integer_to_string(buf, STLSOFT_NUM_ELEMENTS(buf), interval, cch);
#endif /* PANTHEIOS_STLSOFT_1_12_OR_LATER */

        m_result.assign(num, cch);
        m_result.append(1, ' ');
        m_result.append(u);
    }
}