home *** CD-ROM | disk | FTP | other *** search
- /*
- ** example5.c
- **
- ** This example illustrates dbconvert(). It converts a
- ** number of constants to strings, a number of strings
- ** to numerical or binary quantities, and a number of
- ** numerical quantities to other numerical types.
- **
- */
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <sybfront.h>
- #include <sybdb.h>
-
- #define ARRAY_LEN 20
-
- /* Function Prototypes */
- int err_handler(DBPROCESS *, int, int, int, char *, char *);
- int msg_handler(DBPROCESS *, DBINT, int, int, char *, char *,
- char *, DBUSMALLINT);
- int main();
-
- main()
- {
- /* These variables will hold the results of data conversions. */
- static DBBINARY my_binary_array[ARRAY_LEN]
- = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0};
- DBFLT8 my_flt8;
- DBINT my_int4;
- DBMONEY my_money;
- DBCHAR my_string[ARRAY_LEN];
- DBCHAR *temp_test = "Jan 12, 1990 12:12:12:123AM";
- DBDATETIME temp_testdt;
-
- /* Initialize DB-Library. */
- if (dbinit() == FAIL)
- exit(ERREXIT);
-
- /* Install the user-supplied error-handling and message-handling
- * routines. They are defined at the bottom of this source file.
- */
- dberrhandle(err_handler);
- dbmsghandle(msg_handler);
-
- /* Convert numerical and binary constants to strings. */
-
- dbconvert
- ((DBPROCESS *)NULL,
- SYBBINARY, my_binary_array, (DBINT)8, SYBCHAR, my_string, (DBINT)-1);
- printf("Binary constant 0x123456789abcdef0 converted to string ");
- printf("\"%s\".\n\n", my_string);
-
- my_flt8 = 55.555;
- dbconvert
- ((DBPROCESS *)NULL, SYBFLT8, &my_flt8, (DBINT)-1, SYBCHAR, my_string,
- (DBINT)-1);
- printf
- ("Floating-pt constant 55.555 converted to string \"%s\".\n\n",
- my_string);
-
- /* Convert string constants to numerical and binary quantities. */
-
- dbconvert
- ((DBPROCESS *)NULL, SYBCHAR, "123", (DBINT)-1, SYBINT4, &my_int4,
- (DBINT)-1);
- printf
- ("String constant \"123\" converted to 4-byte integer %ld.\n\n",
- my_int4);
-
- dbconvert
- ((DBPROCESS *)NULL, SYBCHAR, "0xfedc", (DBINT)-1, SYBBINARY,
- my_binary_array, (DBINT)ARRAY_LEN);
- printf("String constant \"0xfedc\" converted to binary sequence ");
- printf("%x.\n\n", *((int *)my_binary_array));
-
- dbconvert
- ((DBPROCESS *)NULL, SYBCHAR, "123.456", (DBINT)-1, SYBFLT8,
- &my_flt8, (DBINT)-1);
- printf("String constant \"123.456\" converted to ");
- printf("floating-pt number %f.\n\n", my_flt8);
-
- /* Convert numerical types to other numerical types. */
-
- my_flt8 = 98.76;
- dbconvert
- ((DBPROCESS *)NULL, SYBFLT8, &my_flt8, (DBINT)-1, SYBMONEY,
- &my_money, (DBINT)-1);
- dbconvert
- ((DBPROCESS *)NULL, SYBMONEY, &my_money, (DBINT)-1, SYBCHAR,
- my_string, (DBINT)-1);
- printf
- ("floating-pt number %f converted to money value %s.\n\n",
- my_flt8, my_string);
-
- dbconvert
- ((DBPROCESS *)NULL, SYBMONEY, &my_money, (DBINT)-1, SYBFLT8,
- &my_flt8, (DBINT)-1);
- printf
- ("money value %s converted to floating-pt value %f.\n\n",
- my_string, my_flt8);
-
- /* DATETIME conversion tests */
- dbconvert((DBPROCESS *)NULL, SYBCHAR, temp_test, (DBINT)-1, SYBDATETIME,
- &temp_testdt, (DBINT)-1);
- dbconvert((DBPROCESS *)NULL, SYBDATETIME, &temp_testdt, (DBINT)-1, SYBCHAR,
- temp_test, (DBINT)-1);
- printf("date: %s\n",temp_test);
-
- exit(STDEXIT);
- dbexit();
- }
-
- int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
- DBPROCESS *dbproc;
- int severity;
- int dberr;
- int oserr;
- char *dberrstr;
- char *oserrstr;
- {
- if ((dbproc == NULL) || (DBDEAD(dbproc)))
- return(INT_EXIT);
- else
- {
- printf("DB-Library error:\n\t%s\n", dberrstr);
-
- if (oserr != DBNOERR)
- printf("Operating-system error:\n\t%s\n", oserrstr);
-
- return(INT_CANCEL);
- }
- }
-
- int msg_handler(dbproc, msgno, msgstate, severity, msgtext,
- srvname, procname, line)
-
- DBPROCESS *dbproc;
- DBINT msgno;
- int msgstate;
- int severity;
- char *msgtext;
- char *srvname;
- char *procname;
- DBUSMALLINT line;
-
- {
- printf ("Msg %ld, Level %d, State %d\n",
- msgno, severity, msgstate);
-
- if (strlen(srvname) > 0)
- printf ("Server '%s', ", srvname);
- if (strlen(procname) > 0)
- printf ("Procedure '%s', ", procname);
- if (line > 0)
- printf ("Line %d", line);
-
- printf("\n\t%s\n", msgtext);
-
- return(0);
- }
-