Getting the DATA TYPE from syscolumns: Code Sample #1:gettype.c
Posted in 1995
/***************************************************************************** * PROGRAM: gettype.c ( gettype ) AUTHOR: Tim Schaefer, The Computer Business Company, Inc. LATEST: Tue Oct 31 17:47:54 EST 1995 HOST LANGUAGE: Standard C DESCRIPTION: Returns the character-string representation of an INFORMIX data type. For a given column-type spcified in the syscolumns table and a given column-length, will convert these integer values into a meaningful string useful for use in program development. USAGE: gettype column-type column-length RETURNS: character-string without carriage-return or line-feed. EXAMPLES: A typical application of this program would be to build record structures for 4GL programs. See the get_dts.sh shell-script for an example. ****************************************************************************** / #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> char large_fld[12]; char small_fld[12]; short large_size; short small_size; char dt_size[36]; char intv_size[36]; char hex_string[5] = "" ; short null_size; int min_length; int max_length; char fld_nm[12]; short fld_size; char ct_str[51]; char not_null[10]; short msize; short nsize; short large_lngth; short small_lngth; main(argc, argv) int argc; char **argv; { int type_num = 0; int length_num = 0; type_num = atol( argv[1] ) ; length_num = atol( argv[2] ) ; convert_type (type_num, length_num); fprintf(stdout, "%s\\n", ct_str ); } convert_type (coltype_num, col_length) int coltype_num; int col_length ; { int cnv_coltype = 0; int cnv_collen = 0; strcpy( not_null , "" ) ; if ( coltype_num >= 256 ) { cnv_coltype = coltype_num - 256 ; coltype_num = cnv_coltype; strcpy( not_null , " NOT NULL " ); } switch( coltype_num ) { case 0: sprintf( ct_str, "CHAR (%d)", col_length ); break; case 1: strcpy( ct_str , "SMALLINT" ); break; case 2: strcpy( ct_str , "INTEGER" ); break; case 3: strcpy( ct_str , "FLOAT" ); break; case 4: strcpy( ct_str , "SMALLFLOAT" ); break; case 5: msize = col_length / 256; nsize = col_length % 256; if ( nsize == 255 || nsize <= 0 ) { sprintf(ct_str , "DECIMAL (%d)", msize ); } else { sprintf(ct_str , "DECIMAL (%d,%d)", msize, nsize ); } break; case 6: strcpy( ct_str , "SERIAL" ); break; case 7: strcpy( ct_str , "DATE" ); break; case 8: msize = col_length / 256 ; nsize = col_length % 256 ; sprintf( ct_str , "MONEY (%d", msize ); if ( nsize != 2 ) { strcat( ct_str, ", " ); strcat( ct_str, nsize ); } strcat( ct_str , ")" ); break; case 9: strcpy( ct_str , "UNDEFINED" ); break; case 10: cnvrt_dt (col_length) ; sprintf( ct_str , "DATETIME %s", dt_size ) ; break; case 11: strcpy ( ct_str , "BYTE" ); break; case 12: strcpy ( ct_str , "TEXT" ); break; case 13: min_length = 0; max_length = 0; if ( col_length >= 256 ) { cnv_collen = col_length - 256 ; col_length = cnv_collen; } cnvrt_varch (col_length) ; sprintf( ct_str , "" ); if ( min_length > 0 ) { sprintf( ct_str , "VARCHAR(%d,%d)", max_length, min_length ); } else { sprintf( ct_str , "VARCHAR(%d)", max_length ); } break; case 14: cnvrt_intvl(col_length) ; sprintf( ct_str , "INTERVAL %s", intv_size ); break; default: sprintf( ct_str , "UNDEFINED: %d", coltype_num ); } strcat( ct_str , not_null ); } cnvrt_dt (clngth) long clngth; { sprintf(hex_string, "" ); sprintf(hex_string, "%4x", clngth ); qual_fld ('l', hex_string[2]) ; strcpy( large_fld, fld_nm ); qual_fld ('s', hex_string[3]) ; strcpy( small_fld, fld_nm ); strcpy( dt_size , large_fld ); strcat( dt_size , " TO " ); strcat( dt_size , small_fld ); } cnvrt_intvl (clngth) long clngth ; { long fld_lngth ; short i; sprintf(hex_string, "" ); sprintf(hex_string, "%3x", clngth ); qual_fld ('l', hex_string[1]) ; strcpy( large_fld, fld_nm ); qual_fld ('s', hex_string[2]) ; strcpy( small_fld, fld_nm ); fld_lngth = intvl_lngth(clngth); if ( fld_lngth > 0 ) { strcpy ( large_fld , " (" ); strcat ( large_fld , fld_lngth ); strcat ( large_fld , ")" ); } strcpy( intv_size , large_fld ); strcat( intv_size , " TO " ); strcat( intv_size , small_fld ); } cnvrt_varch (clngth) long clngth ; { int str0; int str1; int str2; int str3; sprintf(hex_string, "" ); sprintf(hex_string, "%4x", clngth ); str0 = dec_digit(hex_string[0]) ; str1 = dec_digit(hex_string[1]) ; str2 = dec_digit(hex_string[2]) ; str3 = dec_digit(hex_string[3]) ; if ( str0 > 0 ) { min_length = str0 + str1 ; } else { min_length = 1 ; } max_length = clngth; } qual_fld (ftype, fvalue) char ftype; char fvalue; { switch( fvalue ) { case '0': strcpy ( fld_nm , "YEAR" ); fld_size = 4 ; break; case '2': strcpy ( fld_nm , "MONTH" ); fld_size = 2 ; break; case '4': strcpy ( fld_nm , "DAY" ); fld_size = 2 ; break; case '6': strcpy ( fld_nm , "HOUR" ); fld_size = 2 ; break; case '8': strcpy ( fld_nm , "MINUTE" ); fld_size = 2 ; break; cas