void store_db_file(prolog_database *db, const char *file_name, long offset) { FILE *f; /* Open the file with a+ to create it if necessary, then reopen with r+ to allow writing to the interior of the file. */ f = safe_fopen(file_name, "a+b"); safe_freopen(file_name, "r+b", f); store_db(db, f, offset, file_store); fclose(f); }
int main(int argc, char **argv) { FILE *f = NULL; char outfile[CI_MAX_PATH]; char line[MAXLINE]; int len; void *key, *val; int keysize,valsize; CI_DEBUG_LEVEL = 1; ci_cfg_lib_init(); if (!ci_args_apply(argc, argv, options) || (!txtfile && !DUMP_MODE)) { ci_args_usage(argv[0], options); exit(-1); } #if ! defined(_WIN32) __log_error = (void (*)(void *, const char *,...)) log_errors; /*set c-icap library log function */ #else __vlog_error = vlog_errors; /*set c-icap library log function for win32..... */ #endif if(!(allocator = ci_create_os_allocator())) { ci_debug_printf(1, "Error allocating mem allocator!\n"); return -1; } if (DUMP_MODE && !dbfile) { ci_debug_printf(1, "\nError: You need to specify the database to dump ('-o file.db')\n\n"); ci_args_usage(argv[0], options); exit(-1); } if(!dbfile) { strncpy(outfile, txtfile, CI_MAX_PATH); outfile[CI_MAX_PATH-1] = '\0'; len=strlen(outfile); if(len > CI_MAX_PATH-5) { ci_debug_printf(1,"The filename %s is too long\n", outfile); exit(0); } strcat(outfile,".db"); } else { strncpy(outfile, dbfile, CI_MAX_PATH); outfile[CI_MAX_PATH-1] = '\0'; } if (!open_db(outfile)) { ci_debug_printf(1, "Error opening bdb file %s\n", outfile); if (f) fclose(f); return -1; } if( DUMP_MODE ){ dump_db(); } else { if ((f = fopen(txtfile, "r+")) == NULL) { ci_debug_printf(1, "Error opening file: %s\n", txtfile); return -1; } while(fgets(line,MAXLINE,f)) { line[MAXLINE-1]='\0'; if(!record_extract(line, &key, &keysize, &val, &valsize)) { ci_debug_printf(1, "Error parsing line : %s\n", line); break; } else if (key) /*if it is not comment or blank line */ store_db(key, keysize, val, valsize); } fclose(f); } close_db(); ci_mem_allocator_destroy(allocator); return 0; }