Header And Logo

PostgreSQL
| The world's most advanced open source database.

Functions

common.c File Reference

#include "postgres_fe.h"
#include "extern.h"
Include dependency graph for common.c:

Go to the source code of this file.

Functions

char * pgtypes_alloc (long size)
char * pgtypes_strdup (const char *str)
int pgtypes_fmt_replace (union un_fmt_comb replace_val, int replace_type, char **output, int *pstr_len)

Function Documentation

char* pgtypes_alloc ( long  size  ) 
int pgtypes_fmt_replace ( union un_fmt_comb  replace_val,
int  replace_type,
char **  output,
int *  pstr_len 
)

Definition at line 29 of file common.c.

References un_fmt_comb::char_val, un_fmt_comb::double_val, free, i, un_fmt_comb::int64_val, pgtypes_alloc(), PGTYPES_FMT_NUM_MAX_DIGITS, PGTYPES_TYPE_CHAR, PGTYPES_TYPE_DOUBLE_NF, PGTYPES_TYPE_INT64, PGTYPES_TYPE_NOTHING, PGTYPES_TYPE_STRING_CONSTANT, PGTYPES_TYPE_STRING_MALLOCED, PGTYPES_TYPE_UINT, PGTYPES_TYPE_UINT_2_LS, PGTYPES_TYPE_UINT_2_LZ, PGTYPES_TYPE_UINT_3_LZ, PGTYPES_TYPE_UINT_4_LZ, snprintf(), un_fmt_comb::str_val, and un_fmt_comb::uint_val.

Referenced by dttofmtasc_replace().

{
    /*
     * general purpose variable, set to 0 in order to fix compiler warning
     */
    int         i = 0;

    switch (replace_type)
    {
        case PGTYPES_TYPE_NOTHING:
            break;
        case PGTYPES_TYPE_STRING_CONSTANT:
        case PGTYPES_TYPE_STRING_MALLOCED:
            i = strlen(replace_val.str_val);
            if (i + 1 <= *pstr_len)
            {
                /*
                 * copy over i + 1 bytes, that includes the tailing terminator
                 */
                strncpy(*output, replace_val.str_val, i + 1);
                *pstr_len -= i;
                *output += i;
                if (replace_type == PGTYPES_TYPE_STRING_MALLOCED)
                    free(replace_val.str_val);
                return 0;
            }
            else
                return -1;
            break;
        case PGTYPES_TYPE_CHAR:
            if (*pstr_len >= 2)
            {
                (*output)[0] = replace_val.char_val;
                (*output)[1] = '\0';
                (*pstr_len)--;
                (*output)++;
                return 0;
            }
            else
                return -1;
            break;
        case PGTYPES_TYPE_DOUBLE_NF:
        case PGTYPES_TYPE_INT64:
        case PGTYPES_TYPE_UINT:
        case PGTYPES_TYPE_UINT_2_LZ:
        case PGTYPES_TYPE_UINT_2_LS:
        case PGTYPES_TYPE_UINT_3_LZ:
        case PGTYPES_TYPE_UINT_4_LZ:
            {
                char       *t = pgtypes_alloc(PGTYPES_FMT_NUM_MAX_DIGITS);

                if (!t)
                    return ENOMEM;
                switch (replace_type)
                {
                    case PGTYPES_TYPE_DOUBLE_NF:
                        i = snprintf(t, PGTYPES_FMT_NUM_MAX_DIGITS,
                                     "%0.0g", replace_val.double_val);
                        break;
                    case PGTYPES_TYPE_INT64:
                        i = snprintf(t, PGTYPES_FMT_NUM_MAX_DIGITS,
                                     INT64_FORMAT, replace_val.int64_val);
                        break;
                    case PGTYPES_TYPE_UINT:
                        i = snprintf(t, PGTYPES_FMT_NUM_MAX_DIGITS,
                                     "%u", replace_val.uint_val);
                        break;
                    case PGTYPES_TYPE_UINT_2_LZ:
                        i = snprintf(t, PGTYPES_FMT_NUM_MAX_DIGITS,
                                     "%02u", replace_val.uint_val);
                        break;
                    case PGTYPES_TYPE_UINT_2_LS:
                        i = snprintf(t, PGTYPES_FMT_NUM_MAX_DIGITS,
                                     "%2u", replace_val.uint_val);
                        break;
                    case PGTYPES_TYPE_UINT_3_LZ:
                        i = snprintf(t, PGTYPES_FMT_NUM_MAX_DIGITS,
                                     "%03u", replace_val.uint_val);
                        break;
                    case PGTYPES_TYPE_UINT_4_LZ:
                        i = snprintf(t, PGTYPES_FMT_NUM_MAX_DIGITS,
                                     "%04u", replace_val.uint_val);
                        break;
                }

                if (i < 0)
                {
                    free(t);
                    return -1;
                }
                i = strlen(t);
                *pstr_len -= i;

                /*
                 * if *pstr_len == 0, we don't have enough space for the
                 * terminator and the conversion fails
                 */
                if (*pstr_len <= 0)
                {
                    free(t);
                    return -1;
                }
                strcpy(*output, t);
                *output += i;
                free(t);
            }
            break;
        default:
            break;
    }
    return 0;
}

char* pgtypes_strdup ( const char *  str  ) 

Definition at line 19 of file common.c.

Referenced by pgtypes_defmt_scan(), PGTYPESdate_defmt_asc(), PGTYPESdate_to_asc(), PGTYPESinterval_to_asc(), PGTYPEStimestamp_defmt_asc(), and PGTYPEStimestamp_to_asc().

{
    char       *new = (char *) strdup(str);

    if (!new)
        errno = ENOMEM;
    return (new);
}