/* ** Copyright (C) 2001-2007 by Carnegie Mellon University. ** ** @OPENSOURCE_HEADER_START@ ** ** Use of the SILK system and related source code is subject to the terms ** of the following licenses: ** ** GNU Public License (GPL) Rights pursuant to Version 2, June 1991 ** Government Purpose License Rights (GPLR) pursuant to DFARS 252.225-7013 ** ** NO WARRANTY ** ** ANY INFORMATION, MATERIALS, SERVICES, INTELLECTUAL PROPERTY OR OTHER ** PROPERTY OR RIGHTS GRANTED OR PROVIDED BY CARNEGIE MELLON UNIVERSITY ** PURSUANT TO THIS LICENSE (HEREINAFTER THE "DELIVERABLES") ARE ON AN ** "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY ** KIND, EITHER EXPRESS OR IMPLIED AS TO ANY MATTER INCLUDING, BUT NOT ** LIMITED TO, WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE, ** MERCHANTABILITY, INFORMATIONAL CONTENT, NONINFRINGEMENT, OR ERROR-FREE ** OPERATION. CARNEGIE MELLON UNIVERSITY SHALL NOT BE LIABLE FOR INDIRECT, ** SPECIAL OR CONSEQUENTIAL DAMAGES, SUCH AS LOSS OF PROFITS OR INABILITY ** TO USE SAID INTELLECTUAL PROPERTY, UNDER THIS LICENSE, REGARDLESS OF ** WHETHER SUCH PARTY WAS AWARE OF THE POSSIBILITY OF SUCH DAMAGES. ** LICENSEE AGREES THAT IT WILL NOT MAKE ANY WARRANTY ON BEHALF OF ** CARNEGIE MELLON UNIVERSITY, EXPRESS OR IMPLIED, TO ANY PERSON ** CONCERNING THE APPLICATION OF OR THE RESULTS TO BE OBTAINED WITH THE ** DELIVERABLES UNDER THIS LICENSE. ** ** Licensee hereby agrees to defend, indemnify, and hold harmless Carnegie ** Mellon University, its trustees, officers, employees, and agents from ** all claims or demands made against them (and any related losses, ** expenses, or attorney's fees) arising out of, or relating to Licensee's ** and/or its sub licensees' negligent use or willful misuse of or ** negligent conduct or willful misconduct regarding the Software, ** facilities, or other rights or assistance granted by Carnegie Mellon ** University under this License, including, but not limited to, any ** claims of product liability, personal injury, death, damage to ** property, or violation of any laws or regulations. ** ** Carnegie Mellon University Software Engineering Institute authored ** documents are sponsored by the U.S. Department of Defense under ** Contract F19628-00-C-0003. Carnegie Mellon University retains ** copyrights in all material produced under this contract. The U.S. ** Government retains a non-exclusive, royalty-free license to publish or ** reproduce these documents, or allow others to do so, for U.S. ** Government purposes only pursuant to the copyright license under the ** contract clause at 252.227.7013. ** ** @OPENSOURCE_HEADER_END@ */ /* * rwrtd2split * * Read an input file in RWROUTED format and write the records to a * new file in RWSPLIT format. */ #include "silk.h" RCSIDENT("$SiLK: rwrtd2split.c 6947 2007-04-17 21:12:48Z mthomas $"); #include "rwpack.h" #include "utils.h" #include "sksite.h" /* LOCAL DEFINES AND TYPEDEFS */ /* where to write --help output */ #define USAGE_FH stdout /* LOCAL FUNCTIONS */ static void appUsageLong(void); static void appTeardown(void); static void appSetup(int argc, char **argv); static int appOptionsHandler(clientData cData, int opt_index, char *opt_arg); /* LOCAL VARIABLES */ static char *in_fpath; static char *out_fpath; static rwIOStruct *in_ios; static rwIOStruct *out_ios; /* OPTIONS SETUP */ /* ** typedef enum { ** } appOptionsEnum; */ static struct option appOptions[] = { {0,0,0,0} /* sentinel entry */ }; static const char *appHelp[] = { (char *)NULL }; /* FUNCTION DEFINITIONS */ /* * appUsageLong(); * * Print complete usage information to USAGE_FH. Pass this * function to skOptionsSetUsageCallback(); optionsParse() will * call this funciton and then exit the program when the --help * option is given. */ static void appUsageLong(void) { #define USAGE_MSG \ (" \n" \ "\tConvert INPUT_FILE, which should be in the FT_RWROUTED format,\n" \ "\tto an FT_RWSPLIT file and write the result to OUTPUT_FILE.\n") FILE *fh = USAGE_FH; skAppStandardUsage(fh, USAGE_MSG, appOptions, appHelp); sksiteOptionsUsage(fh); } /* * appTeardown() * * Teardown all modules, close all files, and tidy up all * application state. * * This function is idempotent. */ static void appTeardown(void) { static int teardownFlag = 0; if (teardownFlag) { return; } teardownFlag = 1; skAppUnregister(); } /* * appSetup(argc, argv); * * Perform all the setup for this application include setting up * required modules, parsing options, etc. This function should be * passed the same arguments that were passed into main(). * * Returns to the caller if all setup succeeds. If anything fails, * this function will cause the application to exit with a FAILURE * exit status. */ static void appSetup(int argc, char **argv) { int arg_index; int rv; /* verify same number of options and help strings */ assert((sizeof(appHelp)/sizeof(char *)) == (sizeof(appOptions)/sizeof(struct option))); /* register the application */ skAppRegister(argv[0]); skOptionsSetUsageCallback(&appUsageLong); /* register the options */ if (optionsRegister(appOptions, (optHandler)appOptionsHandler, NULL) || sksiteOptionsRegister(SK_SITE_FLAG_CONFIG_FILE)) { skAppPrintErr("unable to register options"); exit(EXIT_FAILURE); } /* parse the options */ arg_index = optionsParse(argc, argv); if (arg_index < 0) { /* options parsing should print error */ skAppUsage(); /* never returns */ } /* Ensure the site config is available */ if (sksiteConfigure(1)) { exit(EXIT_FAILURE); } /* Get the input file name */ if (arg_index == argc) { skAppPrintErr("Missing input file name"); skAppUsage(); /* never returns */ } in_fpath = argv[arg_index]; ++arg_index; /* Get the output file name */ if (arg_index == argc) { skAppPrintErr("Missing output file name"); skAppUsage(); /* never returns */ } out_fpath = argv[arg_index]; ++arg_index; /* We don't expect additional files */ if (arg_index != argc) { skAppPrintErr("Too many arguments or unrecognized switch '%s'", argv[arg_index]); skAppUsage(); } /* Open input file; check its type */ in_ios = rwOpenFile(in_fpath, NULL); if (in_ios == NULL) { exit(EXIT_FAILURE); } if (rwGetFileType(in_ios) != FT_RWROUTED) { skAppPrintErr("Input file '%s' not in RWROUTED format", in_fpath); exit(EXIT_FAILURE); } /* Output output file */ if ((rv = rwioCreate(&out_ios, out_fpath, SK_RWIO_WRITE)) || (rv = rwioSetFileType(out_ios, FT_RWSPLIT)) || (rv = rwioSetFileVersion(out_ios, rwGetFileVersion(in_ios))) || (rv = rwioSetFileByteorder(out_ios, rwGetFileByteorder(in_ios))) || (rv = rwioSetFileSTime(out_ios, rwGetFileSTime(in_ios), 0))) { rwioPrintLastErr(out_ios, rv, &skAppPrintErr); skAppPrintErr("Unable to open output file '%s'.", out_fpath); exit(EXIT_FAILURE); } return; /* OK */ } /* * status = appOptionsHandler(cData, opt_index, opt_arg); * * This function is passed to optionsRegister(); it will be called * by optionsParse() for each user-specified switch that the * application has registered; it should handle the switch as * required---typically by setting global variables---and return 1 * if the switch processing failed or 0 if it succeeded. Returning * a non-zero from from the handler causes optionsParse() to return * a negative value. * * The clientData in 'cData' is typically ignored; 'opt_index' is * the index number that was specified as the last value for each * struct option in appOptions[]; 'opt_arg' is the user's argument * to the switch for options that have a REQUIRED_ARG or an * OPTIONAL_ARG. */ static int appOptionsHandler( clientData UNUSED(cData), int UNUSED(opt_index), char UNUSED(*opt_arg)) { return 0; } int main(int argc, char **argv) { rwRec rwrec; int64_t rec_count = 0; int64_t file_size_real = 0; int64_t file_size_calc = 0; int64_t hdr_len; int64_t rec_len; int rv; /* Setup app: open input and output files; will exit(1) on error */ appSetup(argc, argv); hdr_len = out_ios->hdrLen; rec_len = out_ios->recLen; /* Process body */ while (rwRead(in_ios, &rwrec)) { rec_count++; rv = rwWrite(out_ios, &rwrec); if (rv != LIBRW_OK) { rwioPrintLastErr(out_ios, rv, &skAppPrintErr); if (LIBRW_ERROR_IS_FATAL(rv)) { skAppPrintErr("Error writing to '%s'. Stopping copy.", out_fpath); break; } } } rwCloseFile(in_ios); in_ios = NULL; rwCloseFile(out_ios); out_ios = NULL; file_size_real = fileSize(out_fpath); file_size_calc = hdr_len + rec_len * rec_count; if (file_size_real != file_size_calc) { skAppPrintErr(("ERROR: output filesize mismatch." " Calc. %lld vs real %lld"), (long long)file_size_calc, (long long)file_size_real); exit(EXIT_FAILURE); } /* done */ appTeardown(); return 0; } /* ** Local Variables: ** mode:c ** indent-tabs-mode:nil ** c-basic-offset:4 ** End: */