/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ /* Populate_DataFormat.pc */ /* Load to the NCEDC database the Data Format info. */ /* Errors can be found in Error.log */ /* */ /* Zuzlewski Stephane @1998 */ /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/ #include #include #include #define CONNECT_STRING "user/passwd@db" EXEC SQL INCLUDE sqlca.h; typedef char asciz[20]; typedef char vc2_arr[11]; EXEC SQL BEGIN DECLARE SECTION; /* User-defined type for null-terminated strings */ EXEC SQL TYPE asciz IS STRING(20) REFERENCE; /* User-defined type for a VARCHAR array element */ EXEC SQL TYPE vc2_arr IS VARCHAR2(11) REFERENCE; asciz username; asciz password; char user_pwd[80]; int i; int Family; char Name[81]; int ms_id; int Nb_Keys; char Decoder[81]; EXEC SQL END DECLARE SECTION; long SQLCODE; FILE* f_err; /* Error File descriptor */ void sql_error(); /* handles unrecoverable errors */ /*-*-*-*-*-*-*-*-*/ /* Main function */ /*-*-*-*-*-*-*-*-*/ main (int argc, char* argv[]) { FILE* f_data; /* Data format file descriptor */ char line[256]; /* Line information */ char stmp[32]; if (argc != 2) { printf ("\n %s \n\n", argv[0]); exit (0); } /* Opening the data format file */ if ((f_data = fopen (argv[1], "rt")) == NULL) { printf ("\n Error [File (%s) could not be opened].\n\n", argv[1]); exit (0); } /* Opening the error file */ if ((f_err = fopen ("Error.log", "w+t")) == NULL) { printf ("\n Error [File (Error.log) could not be opened].\n\n"); exit (0); } /* Connect to ORACLE. */ EXEC SQL WHENEVER SQLERROR DO sql_error(); strcpy (user_pwd, CONNECT_STRING); EXEC SQL CONNECT :user_pwd; printf ("\n Connected to ORACLE as user: %s\n", username); /* Parsing data format file */ fgets (line, 255, f_data); while (strcmp (line, "beg\n")) { fgets (line, 255, f_data); if (feof (f_data)) break; } while (!feof (f_data)) { fgets (line, 255, f_data); sscanf (line, "%d %d %d %s", &Family, &Nb_Keys, &ms_id, stmp); strcpy (Name, strstr (line, stmp)); Name[strlen (Name) - 1] = '\0'; printf ("\n\n Family: %03d -- MiniSEED: %02d -- Number of Keys: %02d -- %s\n", Family, ms_id, Nb_Keys, Name); EXEC SQL INSERT INTO D_Format (id, name, family, ms_id) VALUES (forseq.NEXTVAL, :Name, :Family, :ms_id); for (i=1;i<=Nb_Keys;i++) { fgets (line, 255, f_data); strcpy (Decoder, line); Decoder[strlen (Decoder) - 1] = '\0'; printf ("\n\t Decoder Keys: %s", Decoder); EXEC SQL INSERT INTO D_Format_Data (id, row_id, key_d) VALUES (forseq.CURRVAL, :i, :Decoder); } while (strcmp (line, "beg\n")) { fgets (line, 255, f_data); if (feof (f_data)) break; } } fprintf (f_err, "\n"); printf ("\n\n"); /* Closing error file */ fclose (f_err); /* Closing sensor file */ fclose (f_data); /* Disconnect from the database */ printf ("\n Disconnecting from ORACLE ...\n\n"); EXEC SQL COMMIT WORK RELEASE; exit(0); } /*************************************/ /* Handle errors. Exit on any error. */ /*************************************/ void sql_error() { char msg[512]; size_t buf_len, msg_len; EXEC SQL WHENEVER SQLERROR CONTINUE; buf_len = sizeof(msg); sqlglm(msg, &buf_len, &msg_len); fprintf(f_err, "\nORACLE error detected:"); fprintf(f_err, "\n%.*s \n", msg_len, msg); printf("\nORACLE error detected:"); printf("\n%.*s \n", msg_len, msg); }