/****************************************************************************** PROGRAM: DB2TODB3 USAGE: DB2TODB3 This program creates a CODAS version 3 database from CODAS version 2 block files. It converts CODAS block files written under version 2 into CODAS version 3 block files and recreates the block directory file for the converted block files. The must contain the following keywords and information: DB_NAME PRD_NAME . . . See the file DB2TODB3.CNT for an example of the control file. The producer definition file must use the new version 3 format for producer definition files. See CODAS3.DOC for a description. See MWADCP.DEF for an example. 1989/05/02 - J. Ranada - University of Hawaii JIMAR */ #include "vc.h" #define BUFSIZE 50 int main(int argc, char *argv[]) { FILE *fp_cnt; char buf[BUFSIZE]; int db_id = 1; int im = 0; int ier; FILE_NAME_TYPE cnt_file, db_name, prd_name; char *fn_new_block; if (argc < 2) { printf("\n ENTER CONTROL FILE NAME ==> "); scanf(" %79s", cnt_file); } else strncpy(cnt_file, argv[1], 79); cnt_file[79] = '\0'; if ((fp_cnt = fopen(cnt_file, "rt")) == NULL) { printf("\n ERROR: Unable to open control file %s\n", cnt_file); return 1; } if ((getwords_nc(fp_cnt, buf, BUFSIZE, 2) != 2) || (sscanf(buf, "DB_NAME %511s", db_name) != 1)) { printf("\n ERROR: Scanning control file for database name\n"); return 1; } if ((getwords_nc(fp_cnt, buf, BUFSIZE, 2) != 2) || (sscanf(buf, "PRD_NAME %511s", prd_name) != 1)) { printf("\n ERROR: Scanning control file for producer definition file name\n"); return 1; } printf("\n DATABASE NAME: %511s", db_name); printf("\n PRODUCER DEFINITION FILE: %s\n", prd_name); DBCREATE(&db_id, db_name, prd_name, &im, &ier); if (DBERROR(&ier, "in DBCREATE")) return 1; while (getword_nc(fp_cnt, buf, BUFSIZE)) { printf("\n %s ---> ", buf); fn_new_block = add_block_file(buf); if (fn_new_block == NULL) { printf("\n ERROR: Adding block file = %s", buf); goto close_db; } printf("%s", fn_new_block); } close_db: DBCLOSE(&ier); DBERROR(&ier, "in DBCLOSE"); return 0; } /*----------------------------------------------------------------------------- FUNCTION: add_block_file It creates a block directory entry for the block file to be created and calls convert_block_file to perform the conversion. PARAMETER: fn_old_block = pointer to name of version 2 block file to be converted RETURNS: pointer to name of version 3 block file created, if successful NULL otherwis */ char *add_block_file(FILE_NAME_TYPE fn_old_block) { FILE *fp_new_block; /* ---> Set up a block directory entry and set the file ID. */ if (create_block_dir_entry()) goto error_found; /* ---> SET NAME OF BLOCK FILE */ sprintf( (db_ptr->block_file+db_ptr->path_nchar), db_ptr->block_dir_hdr.block_file_template, db_ptr->block_dir_entry_ptr->file_id); if (!EXISTS(db_ptr->block_file)) { if (convert_block_file(fn_old_block, db_ptr->block_file)) { printf("\n ERROR: Converting %s to %s", fn_old_block, db_ptr->block_file); goto error_found; } } /* ---> If a file with the new name exists, but this name is not the same as that of the current old block file, then there is a danger that the old file names may be out of sequence, or for some other reason we may be in danger of accidentally overwriting a block file. The file name comparison is done without case sensitivity. */ else if (stricmp(fn_old_block, db_ptr->block_file) != 0) { printf("\n ERROR: in add_block_file"); printf("\n File name duplicates an old one, but the files do not correspond.\n"); goto error_found; } if ( (fp_new_block = fopen(db_ptr->block_file, "rb")) == NULL ) { printf("\n ERROR: Can't open %s\n", db_ptr->block_file); goto error_found; } fread(&(db_ptr->block_hdr), BLOCK_HDR_SIZE, 1, fp_new_block); fclose(fp_new_block); update_block_dir_entry(); if (add_block_dir_entry()) goto error_found; db_ptr->new_block_is_open = 0; db_ptr->block_dir_modified = 1; return(db_ptr->block_file); error_found: report_db_error("add_block_file"); return(NULL); }