Code:
/*---------------------------------------------------------------------------
Proc : DumpCellData
Desc : Write a unit of data to the output file
Param: sqldata - the pointer of the data
sqltype - the type of the data
sqlind -
sqllen - the length of the data
colWidth - the width of the data to be outputed
outputFile - the output file pointer
Return: 0 - if successful
*--------------------------------------------------------------------------*/
int DumpCellData(char *sqldata, short sqltype, short *sqlind, short sqllen, short colWidth, FILE *outputFile)
{
int rc = 0;
char buf[100];
struct lstr
{
char len;
char *data;
} *pLstr;
int i;
short precision;
short scale;
short numBytes;
short top;
short bottom;
short byteNb;
short digNb;
struct lob
{
sqlint32 len;
char *data;
} *pLob;
if (sqltype & 1 && *sqlind < 0)
{
/* printf("%-*s", colWidth, " -"); */
}
else
{
switch (sqltype)
{
case SQL_TYP_DATE:
case SQL_TYP_NDATE:
case SQL_TYP_TIME:
case SQL_TYP_NTIME:
case SQL_TYP_STAMP:
case SQL_TYP_NSTAMP:
case SQL_TYP_VARCHAR:
case SQL_TYP_NVARCHAR:
case SQL_TYP_CHAR:
case SQL_TYP_NCHAR:
case SQL_TYP_LONG:
case SQL_TYP_NLONG:
case SQL_TYP_CSTR:
case SQL_TYP_NCSTR:
fprintf(outputFile, "%-*.*s", colWidth, colWidth, sqldata);
break;
case SQL_TYP_LSTR:
case SQL_TYP_NLSTR:
pLstr = (struct lstr *)sqldata;
for (i = 0; i < (int)pLstr->len; i++)
{
fprintf(outputFile, "%c", (pLstr->data)[i]);
}
while (i < colWidth)
{
fprintf(outputFile, " ");
i = i + 1;
}
break;
case SQL_TYP_FLOAT:
case SQL_TYP_NFLOAT:
/* Jacky Lam 17/04/2003 */
fprintf(outputFile, "%*f", colWidth, *((double *)sqldata));
break;
case SQL_TYP_INTEGER:
case SQL_TYP_NINTEGER:
fprintf(outputFile, "%*d", colWidth, *((int *)sqldata));
/* Jacky Lam 17/04/2003
fprintf(outputFile, "%*ld", colWidth, *((long *)sqldata)); */
break;
case SQL_TYP_SMALL:
case SQL_TYP_NSMALL:
fprintf(outputFile, "%*d", colWidth, *((short *)sqldata));
break;
case SQL_TYP_DECIMAL:
case SQL_TYP_NDECIMAL:
precision = (short)(((char *)&sqllen)[0]);
scale = (short)(((char *)&sqllen)[1]);
/* adjust the precision to odd value */
if ((precision % 2) == 0)
{
precision = precision + 1;
}
/* calculate numBytes */
numBytes = (short)(precision + 1) / 2;
/* determine the sign using bottom of the last byte */
bottom = *(sqldata + numBytes - 1) & 0x000F;
i = 0;
if (bottom == 0x000D || bottom == 0x000B)
{
buf[i] = '-';
i++;
}
else
{
buf[i] = ' ';
i++;
}
/* prepare the decimal number */
digNb = 0;
if (digNb == precision - scale)
{
buf[i] = '.';
i++;
}
/* (top + bottom) from first (numBytes - 1) bytes ... */
for (byteNb = 0; byteNb < numBytes - 1; byteNb = byteNb + 1)
{
top = *(sqldata + byteNb) & 0x00F0;
top = top >> 4;
bottom = *(sqldata + byteNb) & 0x000F;
buf[i] = top + '0';
i++;
digNb++;
if (digNb == precision - scale)
{
buf[i] = '.';
i++;
}
buf[i] = bottom + '0';
i++;
digNb++;
if (digNb == precision - scale)
{
buf[i] = '.';
i++;
}
}
/* ... and top of the last byte (bottom is the sign) */
top = *(sqldata + byteNb) & 0x00F0;
top = top >> 4;
buf[i] = top + '0';
i++;
digNb++;
if (digNb == precision - scale)
{
buf[i] = '.';
i++;
}
/* display decimal number */
buf[i] = '\0';
fprintf(outputFile, "%*s", colWidth, buf);
break; case SQL_TYP_CGSTR:
case SQL_TYP_NCGSTR:
case SQL_TYP_VARGRAPH:
case SQL_TYP_NVARGRAPH:
case SQL_TYP_GRAPHIC:
case SQL_TYP_NGRAPHIC:
case SQL_TYP_LONGRAPH:
case SQL_TYP_NLONGRAPH:
sprintf(buf, "Graphic data length: %5ld", sqllen);
fprintf(outputFile, "%-*s", colWidth, buf);
break;
case SQL_TYP_BLOB:
case SQL_TYP_NBLOB:
case SQL_TYP_CLOB:
case SQL_TYP_NCLOB:
case SQL_TYP_DBCLOB:
case SQL_TYP_NDBCLOB:
pLob = (struct lob *)sqldata;
if (*sqlind == 0)
{
sprintf(buf, "LOB length: %10ld", (long)pLob->len);
}
else
{
sprintf(buf, "Truncated LOB length: %10ld", (long)pLob->len);
}
fprintf(outputFile, "%-*s", colWidth, buf);
break;
default:
fprintf(outputFile, "%-*s", colWidth, "?");
break;
}
}
return 0;
} /* DumpCellData */