Header And Logo

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

Data Structures | Functions

mbprint.h File Reference

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  lineptr

Functions

unsigned char * mbvalidate (unsigned char *pwcs, int encoding)
int pg_wcswidth (const char *pwcs, size_t len, int encoding)
void pg_wcsformat (const unsigned char *pwcs, size_t len, int encoding, struct lineptr *lines, int count)
void pg_wcssize (const unsigned char *pwcs, size_t len, int encoding, int *width, int *height, int *format_size)

Function Documentation

unsigned char* mbvalidate ( unsigned char *  pwcs,
int  encoding 
)

Definition at line 385 of file mbprint.c.

References mb_utf_validate(), and PG_UTF8.

Referenced by printTableAddCell(), and printTableAddHeader().

{
    if (encoding == PG_UTF8)
        mb_utf_validate(pwcs);
    else
    {
        /*
         * other encodings needing validation should add their own routines
         * here
         */
    }

    return pwcs;
}

void pg_wcsformat ( const unsigned char *  pwcs,
size_t  len,
int  encoding,
struct lineptr lines,
int  count 
)

Definition at line 293 of file mbprint.c.

References i, NULL, PG_UTF8, PQdsplen(), PQmblen(), lineptr::ptr, utf8_to_unicode(), and lineptr::width.

Referenced by print_aligned_text(), and print_aligned_vertical().

{
    int         w,
                chlen = 0;
    int         linewidth = 0;
    unsigned char *ptr = lines->ptr;    /* Pointer to data area */

    for (; *pwcs && len > 0; pwcs += chlen)
    {
        chlen = PQmblen((const char *) pwcs, encoding);
        if (len < (size_t) chlen)
            break;
        w = PQdsplen((const char *) pwcs, encoding);

        if (chlen == 1)         /* single-byte char */
        {
            if (*pwcs == '\n')  /* Newline */
            {
                *ptr++ = '\0';
                lines->width = linewidth;
                linewidth = 0;
                lines++;
                count--;
                if (count <= 0)
                    exit(1);    /* Screwup */

                /* make next line point to remaining memory */
                lines->ptr = ptr;
            }
            else if (*pwcs == '\r')     /* Linefeed */
            {
                strcpy((char *) ptr, "\\r");
                linewidth += 2;
                ptr += 2;
            }
            else if (*pwcs == '\t')     /* Tab */
            {
                do
                {
                    *ptr++ = ' ';
                    linewidth++;
                } while (linewidth % 8 != 0);
            }
            else if (w < 0)     /* Other control char */
            {
                sprintf((char *) ptr, "\\x%02X", *pwcs);
                linewidth += 4;
                ptr += 4;
            }
            else    /* Output it as-is */
            {
                linewidth += w;
                *ptr++ = *pwcs;
            }
        }
        else if (w < 0)         /* Non-ascii control char */
        {
            if (encoding == PG_UTF8)
                sprintf((char *) ptr, "\\u%04X", utf8_to_unicode(pwcs));
            else
            {
                /*
                 * This case cannot happen in the current code because only
                 * UTF-8 signals multibyte control characters. But we may need
                 * to support it at some stage
                 */
                sprintf((char *) ptr, "\\u????");
            }
            ptr += 6;
            linewidth += 6;
        }
        else    /* All other chars */
        {
            int         i;

            for (i = 0; i < chlen; i++)
                *ptr++ = pwcs[i];
            linewidth += w;
        }
        len -= chlen;
    }
    lines->width = linewidth;
    *ptr++ = '\0';              /* Terminate formatted string */

    if (count <= 0)
        exit(1);                /* Screwup */

    (lines + 1)->ptr = NULL;    /* terminate line array */
}

void pg_wcssize ( const unsigned char *  pwcs,
size_t  len,
int  encoding,
int *  width,
int *  height,
int *  format_size 
)

Definition at line 210 of file mbprint.c.

References PQdsplen(), and PQmblen().

Referenced by print_aligned_text(), and print_aligned_vertical().

{
    int         w,
                chlen = 0,
                linewidth = 0;
    int         width = 0;
    int         height = 1;
    int         format_size = 0;

    for (; *pwcs && len > 0; pwcs += chlen)
    {
        chlen = PQmblen((const char *) pwcs, encoding);
        if (len < (size_t) chlen)
            break;
        w = PQdsplen((const char *) pwcs, encoding);

        if (chlen == 1)         /* single-byte char */
        {
            if (*pwcs == '\n')  /* Newline */
            {
                if (linewidth > width)
                    width = linewidth;
                linewidth = 0;
                height += 1;
                format_size += 1;       /* For NUL char */
            }
            else if (*pwcs == '\r')     /* Linefeed */
            {
                linewidth += 2;
                format_size += 2;
            }
            else if (*pwcs == '\t')     /* Tab */
            {
                do
                {
                    linewidth++;
                    format_size++;
                } while (linewidth % 8 != 0);
            }
            else if (w < 0)     /* Other control char */
            {
                linewidth += 4;
                format_size += 4;
            }
            else    /* Output it as-is */
            {
                linewidth += w;
                format_size += 1;
            }
        }
        else if (w < 0)         /* Non-ascii control char */
        {
            linewidth += 6;     /* \u0000 */
            format_size += 6;
        }
        else    /* All other chars */
        {
            linewidth += w;
            format_size += chlen;
        }
        len -= chlen;
    }
    if (linewidth > width)
        width = linewidth;
    format_size += 1;           /* For NUL char */

    /* Set results */
    if (result_width)
        *result_width = width;
    if (result_height)
        *result_height = height;
    if (result_format_size)
        *result_format_size = format_size;
}

int pg_wcswidth ( const char *  pwcs,
size_t  len,
int  encoding 
)

Definition at line 176 of file mbprint.c.

References PQdsplen(), and PQmblen().

Referenced by describeOneTableDetails().

{
    int         width = 0;

    while (len > 0)
    {
        int         chlen,
                    chwidth;

        chlen = PQmblen(pwcs, encoding);
        if (len < (size_t) chlen)
            break;              /* Invalid string */

        chwidth = PQdsplen(pwcs, encoding);
        if (chwidth > 0)
            width += chwidth;

        pwcs += chlen;
        len -= chlen;
    }
    return width;
}