/*------------------------------------------------------------------------- * print_sub.c * functions for pretty-printing query results * * Change logs * 2001.06.11: Renew this program source. * (1) Add " " into the data part of output_row when the * value is NULL/ALL_BIT_OFF. * (2) Delete "--- RECORD n ---" of the HTML TABLE in the case of * "set OPTION_HEADER=FALSE;" * (3) Fix the bug that tel_no(e.g. 06-6390-9999) is a numeric type * (4) Change "
| header tag */ char *header_th[MAX_TABLE_TAG]; /* column value of | header tag */ int nbody_tag; /* no of column of | body tag */ char *body_tag[MAX_TABLE_TAG]; /* column value of | body tag */
int ninput_size;
int input_size[MAX_TABLE_TAG];
} TableTag;
static char *HTML_SPACE_COL =" ";
/*------------------------------------------------------------------------
* Functions list
*-----------------------------------------------------------------------*/
/*
* int alloc_printOpt(PSprintOpt *Opt, PGresult *res);
* int set_printOpt(PSprintOpt *Opt);
* void PSprint(FILE * fout, PSprintOpt * po, int encoding);
* void free_printOpt(PSprintOpt *Opt);
*/
static void expanded_html_print(FILE * fout, PSprintOpt * po);
static void expanded_plain_print(FILE * fout, PSprintOpt * po);
static void html_print(FILE * fout, PSprintOpt * po);
static void aligned_plain_print(FILE * fout, PSprintOpt * po, int encoding);
static void noaligned_plain_print(FILE * fout, PSprintOpt * po, int encoding);
static char *do_header(FILE * fout, PSprintOpt * po, short *fieldMax,
char **fieldNames, unsigned char *fieldNotNum, const int fs_len);
static char *del_last_blank(char *str);
static short check_fieldNotNum(char *value);
static int replace_string(char *pval, char *ppval, char *fieldSep,
int encoding);
static int replace_string_noalign(char *pval, char *ppval, char *fieldSep,
int encoding);
static void set_table_tag(char *opt, char **table_tag);
static void set_header_tr(char *opt, char **header_tr);
static void set_header_th(char *opt, int *ntag, char **tag);
static void set_body_tags(char *opt, int *ntag, char **tag);
static void set_input_size(char *opt, int *ntag, int *tag);
static int PSmblen(const unsigned char *s, int encoding);
/*#####################################################################
* Global print routines.
*#####################################################################
*/
void
PSprint(FILE * fout, PSprintOpt * po, int encoding)
/*************************************************************************
* output results of query.
*
* First, call alloc_printOpt() or set_printOpt().
* Second, call this PSprint().
* Last, call free_printOpt().
************************************************************************/
{
if (po->expanded)
{
/*-------- expanded format mode -------*/
if (po->html3)
expanded_html_print(fout, po);
else
expanded_plain_print(fout, po);
}
else
{
/*-------- HTML TABLE mode ------------*/
if (po->html3)
{
html_print(fout, po);
}
/*-------- text TABLE mode ------------*/
else
{
if( po->align)
aligned_plain_print(fout, po, encoding);
else
noaligned_plain_print(fout, po, encoding);
}
}
}
int
alloc_printOpt(PSprintOpt * po, PGresult * res)
/*************************************************************************
* set results to print option.
************************************************************************/
{
int i, j;
char *p;
po->nFields = PQnfields(res);
po->nTups = PQntuples(res);
po->fieldName = (char **) wrapCalloc(po->nFields, sizeof(char *));
po->fieldValue = (char **) wrapCalloc(po->nFields * (po->nTups + 1), sizeof(char *));
po->fieldMax = (short *) wrapCalloc(po->nFields, sizeof(short *));
po->fieldNotNum= (unsigned char *) wrapCalloc(po->nFields, sizeof(unsigned char *));
/*---------------- set Field Name ---------------------*/
for (j = 0; j < po->nFields; j++)
{
po->fieldName[j] = PQfname(res, j);
po->fieldNotNum[j] = 255;
if (!po->expanded && po->align && !po->html3)
{
p = del_last_blank(PQfname(res, j));
po->fieldMax[j] = strlen(p);
}
}
/*----------- set Value, NULL or ZERO string ----------*/
for (i = 0; i < po->nTups; i++)
{
for (j = 0; j < po->nFields; j++)
{
if (PQgetisnull(res, i, j) == 0)
{
if (PQgetlength(res, i, j) >= 1)
{
po->fieldValue[i*po->nFields+j] = PQgetvalue(res, i, j);
if (!po->expanded && po->align)
{
/*-- field length --*/
if (!po->html3)
{
p = del_last_blank(po->fieldValue[i*po->nFields+j]);
if ( po->fieldMax[j] < strlen(p) )
po->fieldMax[j] = strlen(p);
}
/*-- field number --*/
if (po->fieldNotNum[j] == 255)
po->fieldNotNum[j] = check_fieldNotNum(PQgetvalue(res, i,j));
}
}
else
po->fieldValue[i*po->nFields+j] = po->zerostr;
}
else
po->fieldValue[i * po->nFields + j] = po->nullstr;
}
}
return (0);
}
int
set_printOpt(PSprintOpt * po)
/*************************************************************************
* set results to print option.
*
* INPUT DATA:
* po->nFields
* po->nTups
* po->fieldName
* po->fieldValue
************************************************************************/
{
char **fieldValue = po->fieldValue;
int nFields = po->nFields;
int nTups = po->nTups;
char *pval;
int i, j;
/*--------------- malloc data ------------------------*/
po->fieldMax = (short *) wrapCalloc(po->nFields, sizeof(short *));
po->fieldNotNum= (unsigned char *) wrapCalloc(po->nFields, sizeof(unsigned char *));
/*---------------- set Field Name ---------------------*/
for (j = 0; j < po->nFields; j++)
{
po->fieldMax[j] = strlen(po->fieldName[j]);
po->fieldNotNum[j] = 255;
}
/*----------- set Value, NULL or ZERO string ----------*/
for (i = 0; i < po->nTups; i++)
{
for (j = 0; j < po->nFields; j++)
{
pval = fieldValue[i*nFields+j];
if (pval != NULL)
{
if (!po->expanded && po->align)
{
/*-- field length --*/
if(!po->html3 &&
po->fieldMax[j] < strlen(pval))
po->fieldMax[j] = strlen(pval);
/*-- field number --*/
if (!po->expanded && po->align &&
po->fieldNotNum[j] == 255)
po->fieldNotNum[j] = check_fieldNotNum(pval);
}
}
}
}
return (0);
}
void
free_printOpt(PSprintOpt * po)
{
if (po->fieldName != NULL)
free(po->fieldName);
if (po->fieldValue != NULL)
free(po->fieldValue);
if (po->fieldMax != NULL)
free(po->fieldMax);
}
/*#####################################################################
* Local print routines.
*#####################################################################
*/
static void
expanded_html_print(FILE * fout, PSprintOpt * po)
/*************************************************************************
* expanded HTML output mode.
************************************************************************/
{
TableTag tableTag;
int nFields = po->nFields;
int nTups = po->nTups;
char **fieldNames = po->fieldName;
char *pval, *pp2;
int i, j;
/*------------- check tuples and field -------------*/
if (nFields <= 0 || nTups <= 0)
return;
/*------------- set PSprintOpt.tableTag ------------*/
set_table_tag(po->table_tag, &tableTag.table_tag);
set_header_tr(po->header_tr, &tableTag.header_tr);
set_header_th(po->header_th, &tableTag.nheader_th, tableTag.header_th);
set_body_tags(po->body_tag, &tableTag.nbody_tag, tableTag.body_tag);
set_input_size(po->input_size, &tableTag.ninput_size, tableTag.input_size);
/*------------------ print CAPTION -------------------*/
if (po->caption)
fprintf(fout,"%s \n", po->caption); /*------------------ print HTML table ----------------*/ for (i = 0; i < nTups; i++) { /*-------------- HEADER --------------*/ if (po->header) fprintf(fout, "%s |
|---|---|---|---|
| %s | |||
| %s | %s | ||
| %s | |||
| %s | %s |
| , | ' nrag = 2 tag[0]=" | " tag[1]=" | " ***********************************************************************/ { static char HEADER_TH[HEADERTH_LEN]; char *p; char *p2; /*---------- initial setteings --------*/ *ntag = 0; if (opt == NULL || *opt == '\0') return; strcpy(HEADER_TH, opt); p = HEADER_TH; while (*p==' ') p++; if (*p == '\0' || *p != '<' ) return; /*---------- parse and set table tag --------------*/ while(*p) { if (strUcmp(p," | ", 4)==0 || strUcmp(p," | ", 4)==0 ) { if ( (p2 = strchr(p+1, '>')) == NULL ) return; tag[*ntag] = p; (*ntag)++; p2++; if( *p2 == '\0' ) break; else *p2 = '\0'; p = p2+1; while (*p==' ' || *p==',') p++; if (*p == '\0' || *p != '<' ) break; } else break; } return; } static void set_body_tags(char *opt, int *ntag, char **tag) /*********************************************************************** ntag : no of column of tags *tag[]: column value of tags example) opt=' | , | ' nrag = 2 tag[0]=" | ' is deleted. ***********************************************************************/ { static char BODY_TAG[BODYTAG_LEN]; char *p; char *p2; /*---------- initial setteings --------*/ *ntag = 0; if (opt == NULL || *opt == '\0') return; strcpy(BODY_TAG, opt); p = BODY_TAG; while (*p==' ') p++; if (*p == '\0' || *p != '<' ) return; /*---------- parse and set table tag --------------*/ while(*p) { if (strUcmp(p," | ", 4)==0 || strUcmp(p," | ", 4)==0 ) { if ( (p2 = strchr(p+1, '>')) == NULL ) return; tag[*ntag] = p; (*ntag)++; *p2 = '\0'; p = p2+1; while (*p==' ' || *p==',') p++; if (*p == '\0' || *p != '<' ) break; } else break; } return; } static void set_input_size(char *opt, int *ntag, int *tag) /*********************************************************************** ntag : no of column of tags *tag[]: column value of tags example) opt='-1,0,5' nrag = 3 tag[0]=-1 : normal print tag[1]=0 : tag[2]=5 : ***********************************************************************/ { static char INPUT_SIZE[INPUTSIZE_LEN]; int n; char *p, *p2; /*---------- initial setteings --------*/ *ntag = 0; if (opt == NULL || *opt == '\0') return; strcpy(INPUT_SIZE, opt); p = INPUT_SIZE; while (*p==' ') p++; if (*p == '\0') return; /*---------- parse and set table tag --------------*/ while(*p) { if ((p2=strchr(p, ',')) != NULL) { *p2 = '\0'; n = atoi(p); if( n < -1 || n > 256) break; tag[*ntag] = n; (*ntag)++; p2++; if( *p2 == '\0' ) break; p = p2; } else { n = atoi(p); if( n < -1 || n > 256) break; tag[*ntag] = n; (*ntag)++; break; } } return; } /*====================================================================== * returns the byte length of the word beginning s, using the * specified encoding. PSmblen is compatible with PostgreSQL-6.5/7.0/7.1 *====================================================================== */ #ifdef MULTIBYTE static int PSmblen(const unsigned char *s, int encoding) { if (encoding == 0 || encoding == SQL_ASCII) return 1; return (pg_encoding_mblen(encoding, s)); } #else static int PSmblen(const unsigned char *s, int encoding) { return 1; } #endif |
|---|