diff --git a/SConstruct b/SConstruct index bd2a4f534..834da561a 100644 --- a/SConstruct +++ b/SConstruct @@ -1442,8 +1442,6 @@ if env['python3_package'] == 'y' or env['python_package'] == 'full': if env['python_package'] == 'minimal': SConscript('interfaces/python_minimal/SConscript') -SConscript('build/src/apps/SConscript') - if env['CC'] != 'cl': VariantDir('build/platform', 'platform/posix', duplicate=0) SConscript('build/platform/SConscript') diff --git a/platform/posix/SConscript b/platform/posix/SConscript index cc14005fe..3fb6f7fa5 100644 --- a/platform/posix/SConscript +++ b/platform/posix/SConscript @@ -5,6 +5,10 @@ from buildutils import * Import('env', 'build', 'install') localenv = env.Clone() +# Copy man pages +if env['INSTALL_MANPAGES']: + install('$inst_mandir', mglob(localenv, '#platform/posix/man', '*')) + ### Generate customized scripts ### # 'setup_cantera' diff --git a/src/apps/SConscript b/src/apps/SConscript deleted file mode 100644 index cd5ed9c9f..000000000 --- a/src/apps/SConscript +++ /dev/null @@ -1,18 +0,0 @@ -from buildutils import * - -Import('env', 'build', 'install') -localenv = env.Clone() -localenv.Prepend(CPPPATH=['#include', '#src/apps']) - -def buildProgram(name, src): - prog = build(localenv.Program(target=pjoin('#build/bin', name), - source=src, - LIBS=env['cantera_libs'])) - install('$inst_bindir', prog) - -if env['layout'] != 'debian': - buildProgram('csvdiff', ['csvdiff.cpp', 'tok_input_util.cpp', 'mdp_allo.cpp']) - -# Copy man pages -if env['INSTALL_MANPAGES']: - install('$inst_mandir', mglob(localenv, '#platform/posix/man', '*')) diff --git a/src/apps/csvdiff.cpp b/src/apps/csvdiff.cpp deleted file mode 100644 index e4cf0db24..000000000 --- a/src/apps/csvdiff.cpp +++ /dev/null @@ -1,1150 +0,0 @@ -/* - * csvdiff File1.csv File2.csv - * - * Compares the variable values in two Excel formatted - * comma separated files. - * The comparison is done using a weighted norm basis. - * - * The two files should be basically equal. However, File1.csv is - * taken as the reference file, that has precedence, when there is - * something to be decided upon. - * - * Arguments: - * -h = prints this usage information - * - * Shell Return Values - * 0 = Comparison was successful - * 1 = One or more nodal values failed the comparison - * 2 = One or more of the header values failed the comparison - * 3 = Apples to oranges, the files can not even be compared against - * one another. - */ - -#include -#include -#include -#include -#include "cantera/base/config.h" -#ifndef _MSC_VER -#include -#else -#include -#endif -#include -#include -using namespace std; - -#if defined(__CYGWIN__) -#include -#endif - -#include "mdp_allo.h" -//#include "cantera/base/mdp_allo.h" -#include "tok_input_util.h" - -int Debug_Flag = true; -double grtol = 1.0E-3; -double gatol = 1.0E-9; - -#define RT_PASSED 0 -#define RT_FAILED_COL 1 -#define RT_FAILED_HDR 2 -#define RT_FAILED_OTHER 3 - -/* - * First iteration towards getting this variable - */ -int Max_Input_Str_Ln = MAX_INPUT_STR_LN; -/*****************************************************************************/ -/*****************************************************************************/ -/*****************************************************************************/ - -#ifdef _MSC_VER -/* - * Windows doesn't have getopt(). This is an incomplete version that - * does enough to handle required functionality. - */ -int optind = -1; -char* optarg = 0; - -int getopt(int argc, char** argv, const char*) -{ - static int currArg = 1; - static int currOptInd = 1; - string tok; - static int charPos = 0; - int rc = -1; - if (currArg >= argc) { - optarg = 0; - return -rc; - } - tok = string(argv[currArg]); - currOptInd = currArg+1; - if (currOptInd > argc - 1) { - currOptInd = -1; - optarg = 0; - } else { - optarg = argv[currArg+1]; - } - size_t len = strlen(tok.c_str()); - if (charPos == 0) { - bool found = false; - do { - tok = string(argv[currArg]); - len = strlen(tok.c_str()); - if (len > 1 && tok[0] == '-') { - found = true; - charPos = 1; - if (len > 2 && tok[1] == '-') { - charPos = 2; - } - } else { - if (optind == -1) { - optind = currArg; - } - } - if (!found) { - if (currArg < (argc-1)) { - currArg++; - } else { - optarg = 0; - return -1; - } - } - } while (!found); - } - - rc = tok[charPos]; - if (charPos < static_cast(len - 1)) { - charPos++; - } else { - charPos = 0; - } - return rc; -} - -#endif - -/*****************************************************************************/ -/*****************************************************************************/ -/*****************************************************************************/ - -static int diff_double(double d1, double d2, double rtol, double atol) - -/* - * Compares 2 doubles. If they are not within tolerance, then this - * function returns true. - */ -{ - if (fabs(d1-d2) > (atol + rtol * 0.5 * (fabs(d1) + fabs(d2)))) { - return 1; - } - return 0; -} - -static int diff_double_slope(double d1, double d2, double rtol, - double atol, double xtol, double slope1, double slope2) - -/* - * Compares 2 doubles. If they are not within tolerance, then this - * function returns true. - */ -{ - double atol2 = xtol*(fabs(slope1) + fabs(slope2)); - if (fabs(d1-d2) > (atol + atol2 + rtol * 0.5 * (fabs(d1) + fabs(d2)))) { - return 1; - } - return 0; -} - -/*****************************************************************************/ -/*****************************************************************************/ -/*****************************************************************************/ - -static double calc_rdiff(double d1, double d2, double rtol, double atol) - -/* - * Calculates the relative difference using a fuzzy comparison - */ - -{ - double rhs, lhs; - rhs = fabs(d1-d2); - lhs = atol + rtol * 0.5 * (fabs(d1) + fabs(d2)); - return rhs/lhs; -} - -/*****************************************************************************/ -/* - * breakStrCommas(): - * This routine will break a character string into stringlets according - * to the placement of commas. The commas are replaced by null - * characters. - * - * Argument: - * str => original string. On exit, this string will have been altered. - * strlets -> Vector of pointers to char *. The vector has a size - * larger than or equal to maxPieces. - * maxPieces -> largest number of pieces to divide the string into. - * - * Return: - * This returns the number of pieces that the string is actually - * broken up into. - */ - -static int breakStrCommas(char* str, char** strlets, int maxPieces) -{ - int numbreaks = 0; - if (strlets) { - strlets[0] = str; - if (str) { - char* cptr = str; - char* cetn = NULL; - do { - cetn = strchr(cptr, (int) ','); - if (cetn) { - numbreaks++; - cptr = cetn + 1; - strlets[numbreaks] = cptr; - *cetn = '\0'; - } - } while (cetn && (numbreaks < (maxPieces - 1))); - } - } - return numbreaks + 1; -} - -/**************************************************************************************************************************/ -/**************************************************************************************************************************/ -/* - * Here, we ensure consistency of the file - * ntitleLines of any content - * nColTitleLines of nCol columns. Each are separated by column. - * nDataRows of nCol columns. Each are separated by columns - * Each column is identified as either a text or double. Each double entry must be able to be read - * as a double. - */ -static void check_consistency(FILE* fp, const char *fileName, const int nTitleLines, const int nColTitleLines, - const int nCol, const int nDataRows, const std::vector& ColIsFloat) -{ - int retn, ncolsFound; - bool rerr; - TOKEN fieldToken; - char* scanLine = mdp_alloc_char_1(MAX_INPUT_STR_LN+1, '\0'); - char** strlets = (char**) mdp_alloc_ptr_1(nCol+200); - /* - * Rewind the file - */ - rewind(fp); - - for (int i = 0; i < nTitleLines; i++) { - retn = read_line(fp, scanLine, 0); - if (retn == -1) { - fprintf(stderr, "check_consistency() error for file %s, Line %d couldn't be read\n", - fileName, i); - exit(-1); - } - } - if (nColTitleLines == 0) { - if (nTitleLines > 0) { - fprintf(stderr, "check_consistency() error for file %s, number column title lines are zero but number title lines are greater than 0", - fileName); - exit(-1); - } - } - for (int i = 0; i < nColTitleLines; i++) { - retn = read_line(fp, scanLine, 0); - if (retn == -1) { - fprintf(stderr, "check_consistency() error for file %s, Line %d couldn't be read\n", - fileName, i); - exit(-1); - } - ncolsFound = breakStrCommas(scanLine, strlets, nCol); - if (ncolsFound != (nCol)) { - fprintf(stderr, "check_consistency() error for file %s, Line %d of " - "ColTitleLines didn't have correct commas: %d vs %d\n", fileName, i, ncolsFound, nCol); - fprintf(stderr, " %s\n", scanLine); - } - } - for (int i = 0; i < nDataRows; i++) { - retn = read_line(fp, scanLine, 0); - ncolsFound = breakStrCommas(scanLine, strlets, nCol); - if (retn == -1) { - fprintf(stderr, "check_consistency() error for file %s, Line %d couldn't be read\n", - fileName, i); - exit(-1); - } - if (ncolsFound != (nCol)) { - fprintf(stderr, "check_consistency() error for file %s, Line %d of DataLines didn't have correct commas: %d vs %d\n", - fileName, i, ncolsFound, nCol); - fprintf(stderr," %s\n", scanLine); - exit(-1); - } - for (int j = 0; j < ncolsFound; j++) { - char* fieldStr = strlets[j]; - fillTokStruct(&fieldToken, fieldStr); - if (ColIsFloat[j] > 0) { - (void) tok_to_double(&fieldToken, DBL_MAX, -DBL_MAX, 0.0, &rerr); - if (rerr) { - fprintf(stderr, "check_consistency() error for file %s, Line %d of DataLines, col %d, " - "couldn't be converted to a double\n", - fileName, i, j); - fprintf(stderr," %s\n", scanLine); - exit(-1); - } - } - } - } - mdp_safe_free((void**) &strlets); - mdp_safe_free((void**) &scanLine); -} -/**************************************************************************************************************************/ -#define LT_NULLLINE 0 -#define LT_TITLELINE 1 -#define LT_COLTITLE 2 -#define LT_DATALINE 3 -/* - * get_sizes() - * - * This routine obtains the sizes of the various elements of the file - * by parsing the file. - * (HKM: Note, this file could use some work. However, it's always - * going to be heuristic) - * - * Arguments: - * - * fp = File pointer - * nTitleLines = Number of title lines - * nColTitleLines = Number of column title lines - * nCol = Number of columns -> basically equal to the - * number of variables - * nDataRows = Number of rows of data in the file - * - */ - -static void get_sizes(FILE* fp, int& nTitleLines, int& nColTitleLines, - int& nCol, int& nDataRows, std::vector& ColIsFloat) -{ - int nScanLinesMAX = 100; - int nScanLines = nScanLinesMAX; - int retn, i, j; - int maxCommas = 0; - TOKEN fieldToken; - char* scanLine = mdp_alloc_char_1(MAX_INPUT_STR_LN+1, '\0'); - int* numCommas = mdp_alloc_int_1(nScanLinesMAX, -1); - - /* - * Rewind the file - */ - rewind(fp); - /* - * Read the scan lines - */ - for (i = 0; i < nScanLinesMAX; i++) { - retn = read_line(fp, scanLine, 0); - if (retn == -1) { - nScanLines = i; - break; - } - /* - * Strip a trailing comma from the scanline - - * -> These are not significant - */ - int ccount = static_cast(strlen(scanLine)); - if (ccount > 0) { - if (scanLine[ccount-1] == ',') { - scanLine[ccount-1] = '\0'; - } - } - /* - * Count the number of commas in the line - */ - char* cptr = scanLine; - char* cetn = NULL; - numCommas[i] = 0; - do { - cetn = strchr(cptr, (int) ','); - if (cetn) { - numCommas[i]++; - cptr = cetn + 1; - } - } while (cetn); - if (i > 1) { - if (maxCommas < numCommas[i]) { - maxCommas = numCommas[i]; - } - } else { - maxCommas = numCommas[0]; - } - } - /* - * set a preliminary value of nCol - */ - nCol = maxCommas + 1; - if (nScanLines == 0) { - nCol = 0; - } - char** strlets = (char**) mdp_alloc_ptr_1(maxCommas+1); - - if (ColIsFloat.size() < maxCommas+1) { - ColIsFloat.resize(maxCommas+1); - } - - /* - * Figure out if each column is a text or float - */ - rewind(fp); - ColIsFloat.assign(ColIsFloat.size(), 0); - - for (i = 0; i < nScanLines; i++) { - retn = read_line(fp, scanLine, 0); - int ncolsFound = breakStrCommas(scanLine, strlets, nCol); - if (ncolsFound == (maxCommas + 1)) { - for (j = 0; j < ncolsFound; j++) { - char* fieldStr = strlets[j]; - fillTokStruct(&fieldToken, fieldStr); - if (fieldToken.ntokes != 1) { - break; - } - bool rerr = false; - (void) tok_to_double(&fieldToken, DBL_MAX, -DBL_MAX, 0.0, &rerr); - if (!rerr) { - ColIsFloat[j]++; - } - } - } - } - std::vector::iterator it; - it = std::max_element(ColIsFloat.begin(), ColIsFloat.end()); - int maxFloats = *it; - it = std::min_element(ColIsFloat.begin(), ColIsFloat.end()); - int minFloats = *it; - // if maxFloats == 0, we're done. No column is float - if (maxFloats > 0) { - for (j = 0; j < maxCommas + 1; j++) { - if (ColIsFloat[j] != maxFloats) { - if (ColIsFloat[j] == minFloats) { - // hook for debugger - if (ColIsFloat[j] > 0) { - ColIsFloat[j] = 0; - } - } else { - printf("WARNING: type of column %d couldn't be uniquely determined, assuming text\n", j); - ColIsFloat[j] = 0; - } - } - } - } - - - int doingLineType = LT_TITLELINE; - if (nScanLines == 2) { - nTitleLines = 0; - doingLineType = LT_COLTITLE; - } - - - rewind(fp); - for (i = 0; i < nScanLines; i++) { - retn = read_line(fp, scanLine, 0); - /* - * Strip a trailing comma from the scanline - - * -> These are not significant - */ - int ccount = static_cast(strlen(scanLine)); - if (ccount > 0) { - if (scanLine[ccount-1] == ',') { - scanLine[ccount-1] = '\0'; - } - } - int ncolsFound = breakStrCommas(scanLine, strlets, nCol); - - if (doingLineType == LT_TITLELINE) { - if (numCommas[i] == maxCommas) { - doingLineType = LT_COLTITLE; - nTitleLines = i; - } - } - - if (doingLineType == LT_COLTITLE) { - bool goodDataLine = true; - bool rerr = false; - for (j = 0; j < ncolsFound; j++) { - char* fieldStr = strlets[j]; - fillTokStruct(&fieldToken, fieldStr); - if (fieldToken.ntokes != 1) { - goodDataLine = false; - break; - } - if ((ColIsFloat[j]) > 0) { - (void) tok_to_double(&fieldToken, DBL_MAX, -DBL_MAX, 0.0, &rerr); - if (rerr) { - goodDataLine = false; - break; - } - } - } - if (goodDataLine) { - doingLineType = LT_DATALINE; - } - nColTitleLines = i - nTitleLines; - } - if (doingLineType == LT_DATALINE) { - break; - } - } - - - /* - * Count the total number of lines in the file - */ - if (doingLineType == LT_DATALINE) { - for (i = nColTitleLines + nTitleLines; ; i++) { - retn = read_line(fp, scanLine, 0); - if (retn == -1) { - nDataRows = i - nColTitleLines - nTitleLines + 1; - break; - } - /* - * Strip a trailing comma from the scanline - - * -> These are not significant - */ - int ccount = static_cast(strlen(scanLine)); - if (ccount > 0) { - if (scanLine[ccount-1] == ',') { - scanLine[ccount-1] = '\0'; - } - } - int ncolsFound = breakStrCommas(scanLine, strlets, nCol); - bool goodDataLine = true; - bool rerr = false; - for (j = 0; j < ncolsFound; j++) { - char* fieldStr = strlets[j]; - fillTokStruct(&fieldToken, fieldStr); - if (fieldToken.ntokes != 1) { - goodDataLine = false; - break; - } - if (ColIsFloat[j] > 0) { - (void) tok_to_double(&fieldToken, DBL_MAX, - -DBL_MAX, 0.0, &rerr); - if (rerr) { - goodDataLine = false; - break; - } - } - } - if (! goodDataLine) { - doingLineType = LT_NULLLINE; - nDataRows = i - nColTitleLines - nTitleLines + 1; - break; - } - } - } - mdp_safe_free((void**) &strlets); - mdp_safe_free((void**) &scanLine); - mdp_safe_free((void**) &numCommas); - return; -} - -/*****************************************************************************/ -/*****************************************************************************/ -/*****************************************************************************/ - -static void -read_title(FILE* fp, char** *title, int nTitleLines) -{ - int retn; - *title = (char**) mdp_alloc_ptr_1(nTitleLines); - char* scanLine = mdp_alloc_char_1(Max_Input_Str_Ln + 1, '\0'); - for (int i = 0; i < nTitleLines ; i++) { - retn = read_line(fp, scanLine, 0); - if (retn >= 0) { - /* - * Strip a trailing comma from the scanline - - * -> These are not significant - */ - int ccount = static_cast(strlen(scanLine)); - if (ccount > 0) { - if (scanLine[ccount-1] == ',') { - scanLine[ccount-1] = '\0'; - } - } - (*title)[i] = mdp_copy_string(scanLine); - } - } - mdp_safe_free((void**) &scanLine); -} - -/*****************************************************************************/ -/*****************************************************************************/ -/*****************************************************************************/ - -static void -read_colTitle(FILE* fp, char**** ColMLNames_ptr, int nColTitleLines, int nCol) -{ - int retn, j; - *ColMLNames_ptr = (char***) mdp_alloc_ptr_1(nCol); - char** *ColMLNames = *ColMLNames_ptr; - char* scanLine = mdp_alloc_char_1(Max_Input_Str_Ln + 1, '\0'); - char** strlets = (char**) mdp_alloc_ptr_1(nCol+1); - if (nColTitleLines > 0) { - for (int i = 0; i < nColTitleLines ; i++) { - retn = read_line(fp, scanLine, 0); - if (retn >= 0) { - /* - * Strip a trailing comma from the scanline - - * -> These are not significant - */ - int ccount = static_cast(strlen(scanLine)); - if (ccount > 0) { - if (scanLine[ccount-1] == ',') { - scanLine[ccount-1] = '\0'; - } - } - int ncolsFound = breakStrCommas(scanLine, strlets, nCol); - ColMLNames[i] = mdp_alloc_VecFixedStrings(nCol, MAX_TOKEN_STR_LN+1); - for (j = 0; j < ncolsFound; j++) { - strip(strlets[j]); - strcpy(ColMLNames[i][j], strlets[j]); - } - } - } - } else { - ColMLNames[0] = mdp_alloc_VecFixedStrings(nCol, MAX_TOKEN_STR_LN+1); - for (j = 0; j < nCol; j++) { - char cbuff[256]; - sprintf(cbuff, "Col_%d", j+1); - strcpy(ColMLNames[0][j], cbuff); - } - } - mdp_safe_free((void**) &scanLine); - mdp_safe_free((void**) &strlets); -} - -/*****************************************************************************/ -/*****************************************************************************/ -/*****************************************************************************/ - -static double get_atol(const double* values, const int nvals, - const double atol) -{ - int i; - double sum = 0.0, retn; - if (nvals <= 0) { - return gatol; - } - for (i = 0; i < nvals; i++) { - retn = values[i]; - sum += retn * retn; - } - sum /= nvals; - retn = sqrt(sum); - return (retn + 1.0) * atol; -} - -/*****************************************************************************/ -/*****************************************************************************/ -/*****************************************************************************/ - -static void -read_values(FILE* fp, double** NVValues, char** *NSValues, int nCol, int nDataRows, - std::vector& ColIsFloat) -{ - char** strlets = (char**) mdp_alloc_ptr_1(nCol+1); - char* scanLine = mdp_alloc_char_1(Max_Input_Str_Ln + 1, '\0'); - TOKEN fieldToken; - double value; - int retn, j; - for (int i = 0; i < nDataRows; i++) { - retn = read_line(fp, scanLine, 0); - if (retn == -1) { - break; - } - /* - * Strip a trailing comma from the scanline - - * -> These are not significant - */ - int ccount = static_cast(strlen(scanLine)); - if (ccount > 0) { - if (scanLine[ccount-1] == ',') { - scanLine[ccount-1] = '\0'; - } - } - int ncolsFound = breakStrCommas(scanLine, strlets, nCol); - bool goodDataLine = true; - bool rerr = false; - for (j = 0; j < ncolsFound; j++) { - char* fieldStr = strlets[j]; - NSValues[j][i] = mdp_copy_string(strlets[j]); - fillTokStruct(&fieldToken, fieldStr); - if (fieldToken.ntokes != 1) { - goodDataLine = false; - break; - } - if (ColIsFloat[j]) { - value = tok_to_double(&fieldToken, DBL_MAX, - -DBL_MAX, 0.0, &rerr); - if (rerr) { - goodDataLine = false; - break; - } - NVValues[j][i] = value; - } - } - if (! goodDataLine) { - break; - } - } - mdp_safe_free((void**) &strlets); - mdp_safe_free((void**) &scanLine); -} -/*****************************************************************************/ -/*****************************************************************************/ -/*****************************************************************************/ - -static void print_usage() -{ - printf("\t\n"); - printf(" csvdiff [-h] [-a atol] [-r rtol] File1.csv File2.csv\n"); - printf("\t\n"); - printf("\tCompares the variable values in two Excel formatted " - "comma separated files.\n"); - printf("\tThe comparison is done using a weighted norm basis.\n"); - printf("\t\n"); - printf("\tThe two files should be basically equal. However, File1.csv is\n"); - printf("\ttaken as the reference file that has precedence, when there is\n"); - printf("\tsomething to be decided upon.\n"); - printf("\t\n"); - printf("\t Arguments:\n"); - printf("\t -h = Usage info\n"); - printf("\t -a atol = Set absolute tolerance parameter - default = 1.0E-9\n"); - printf("\t -r rtol = Set relative tolerance parameter - default = 1.0E-3\n"); - printf("\t\n"); - printf("\t Shell Return Values:\n"); - printf("\t 0 = Comparison was successful\n"); - printf("\t 1 = One or more nodal values failed the comparison\n"); - printf("\t 2 = One or more header values failed the comparison\n"); - printf("\t 3 = Apples to oranges, the files can not even be compared against\n"); - printf("\t one another.\n"); - printf("\t\n"); -} -/*****************************************************************************/ -/*****************************************************************************/ -/*****************************************************************************/ - -int main(int argc, char* argv[]) - -/* - * main driver for csvdiff. - */ -{ - int opt_let; - char* fileName1=NULL, *fileName2=NULL; /* Names of the csv files */ - FILE* fp1=NULL, *fp2=NULL; - int nTitleLines1 = 0, nTitleLines2 = 0; - int nColTitleLines1 = 0, nColTitleLines2 = 0; - int nCol1 = 0, nCol2 = 0, nColMAX = 0, nColcomparisons = 0; - int nDataRows1 = 0, nDataRows2 = 0; - char** title1 = 0, **title2 = 0; - int** compColList = NULL; - char** *ColMLNames1 = NULL, *** ColMLNames2 = NULL; - char** ColNames1 = NULL, **ColNames2 = NULL; - double** NVValues1 = NULL, **NVValues2 = NULL; - char** *NSValues1 = NULL, *** NSValues2 = NULL; - std::vector ColIsFloat1; - std::vector ColIsFloat2; - double* curVarValues1 = NULL, *curVarValues2 = NULL; - char** curStringValues1 = NULL, **curStringValues2 = NULL; - int i, j, ndiff, jmax=0, i1, i2, k; - bool found; - double max_diff, rel_diff; - int testPassed = RT_PASSED; - double atol_j, atol_arg = 0.0, rtol_arg = 0.0; - - /********************** BEGIN EXECUTION ************************************/ - int id = 0; - int id2 = 0; - char* ggg = 0; - char* rrr = 0; - /* - * Interpret command line arguments - */ - /* Loop over each command line option */ - while ((opt_let = getopt(argc, argv, "ha:r:")) != EOF) { - - /* case over the option letter */ - switch (opt_let) { - - case 'h': - /* Usage info was requested */ - print_usage(); - exit(0); - - case 'a': - /* atol parameter */ - - ggg = optarg; - //printf("a = %s\n", ggg); - id = sscanf(ggg,"%lg", &atol_arg); - if (id != 1) { - printf(" atol param bad: %s\n", ggg); - exit(-1); - } - gatol = atol_arg; - break; - - case 'r': - /* rtol parameter */ - - rrr = optarg; - //printf("r = %s\n", ggg); - id2 = sscanf(rrr,"%lg", &rtol_arg); - if (id2 != 1) { - printf(" rtol param bad: %s\n", rrr); - exit(-1); - } - grtol = rtol_arg; - break; - - - default: - /* Default case. Error on unknown argument. */ - printf("default called opt_let = %c\n", opt_let); - fprintf(stderr, "ERROR in command line usuage:\n"); - print_usage(); - return 0; - } /* End "switch(opt_let)" */ - - } /* End "while((opt_let=getopt(argc, argv, "i")) != EOF)" */ - - if (optind != argc-2) { - print_usage(); - exit(-1); - } else { - fileName1 = argv[argc-2]; - fileName2 = argv[argc-1]; - } - - /* - * Print Out Header - */ - printf("\n"); - printf("----------------------------------------------------------\n"); - printf("csvdiff: CSVFile comparison utility program\n"); - printf(" Harry K. Moffat Div. 9114 Sandia National Labs\n"); - printf(" \n"); - printf(" First CSV File = %s\n", fileName1); - printf(" Second CSV file = %s\n", fileName2); - printf("\n"); - printf(" Absolute tol = %g\n", gatol); - printf(" Relative tol = %g\n", grtol); - printf("----------------------------------------------------------\n"); - printf("\n"); - - /* - * Open up the two ascii Files #1 and #2 - */ - if (!(fp1 = fopen(fileName1, "r"))) { - fprintf(stderr,"Error opening up file1, %s\n", fileName1); - exit(-1); - } - if (!(fp2 = fopen(fileName2, "r"))) { - fprintf(stderr, "Error opening up file2, %s\n", fileName2); - exit(-1); - } - - ColIsFloat1.resize(200, 0); - ColIsFloat2.resize(200, 0); - - /* - * Obtain the size of the problem information: Compare between files. - */ - - get_sizes(fp1, nTitleLines1, nColTitleLines1, nCol1, nDataRows1, ColIsFloat1); - if (nCol1 == 0) { - printf("Number of columns in file %s is zero\n", fileName1); - testPassed = RT_FAILED_OTHER; - exit(RT_FAILED_OTHER); - } - if (nDataRows1 == 0) { - printf("Number of data rows in file %s is zero\n", fileName1); - testPassed = RT_FAILED_OTHER; - exit(RT_FAILED_OTHER); - } - - - check_consistency(fp1, fileName1, nTitleLines1, nColTitleLines1, nCol1, nDataRows1, ColIsFloat1); - - get_sizes(fp2, nTitleLines2, nColTitleLines2, nCol2, nDataRows2, ColIsFloat2); - if (nCol2 == 0) { - printf("Number of columns in file %s is zero\n", fileName2); - testPassed = RT_FAILED_OTHER; - exit(RT_FAILED_OTHER); - } - if (nDataRows2 == 0) { - printf("Number of data rows in file %s is zero\n", fileName2); - testPassed = RT_FAILED_OTHER; - exit(RT_FAILED_OTHER); - } - - if (nTitleLines1 != nTitleLines2) { - printf("Number of Title Lines differ:, %d %d\n",nTitleLines1, nTitleLines2); - testPassed = RT_FAILED_OTHER; - } else if (Debug_Flag) { - printf("Number of Title Lines in each file = %d\n", nTitleLines1); - } - if (nColTitleLines1 != nColTitleLines2) { - printf("Number of Column title lines differ:, %d %d\n", nColTitleLines1, - nColTitleLines2); - testPassed = RT_FAILED_OTHER; - } else if (Debug_Flag) { - printf("Number of column title lines in each file = %d\n", nColTitleLines1); - } - - check_consistency(fp2, fileName2, nTitleLines2, nColTitleLines2, nCol2, nDataRows2, ColIsFloat2); - - /* - * Right now, if the number of data rows differ, we will punt. - * Maybe later we can do something more significant - */ - int nDataRowsMIN = min(nDataRows1, nDataRows2); - int nDataRowsMAX = max(nDataRows1, nDataRows2); - if (nDataRows1 != nDataRows2) { - printf("Number of Data rows in file1, %d, is different than file2, %d\n", - nDataRows1, nDataRows2); - } else { - printf("Number of Data rows in both files = %d\n", nDataRowsMIN); - } - - rewind(fp1); - rewind(fp2); - read_title(fp1, &title1, nTitleLines1); - read_title(fp2, &title2, nTitleLines2); - - if (nTitleLines1 > 0 || nTitleLines2 > 0) { - int n = min(nTitleLines1, nTitleLines2); - for (i = 0; i < n; i++) { - if (strcmp(title1[i], title2[i]) != 0) { - printf("Title Line %d differ:\n\t\"%s\"\n\t\"%s\"\n", i, title1[i], title2[i]); - testPassed = RT_FAILED_HDR; - } else if (Debug_Flag) { - printf("Title Line %d for each file: \"%s\"\n", i, title1[i]); - } - } - if (nTitleLines1 != nTitleLines2) { - printf("Number of Title Lines differ: %d %d\n", nTitleLines1, nTitleLines2); - testPassed = RT_FAILED_HDR; - } - } else { - if (nTitleLines1 != nTitleLines2) { - if (nTitleLines1) { - printf("Titles differ: title for first file: \"%s\"\n", - title1[0]); - testPassed = RT_FAILED_HDR; - } - if (nTitleLines2) { - printf("Titles differ: title for second file: \"%s\"\n", - title2[0]); - } - testPassed = RT_FAILED_HDR; - } - } - - /* - * Get the number of column variables in each file - */ - - if (nCol1 != nCol2) { - printf("Number of column variables differ:, %d %d\n", - nCol1, nCol2); - testPassed = RT_FAILED_OTHER; - } else if (Debug_Flag) { - printf("Number of column variables in both files = %d\n", - nCol1); - } - - /* - * Read the names of the column variables - */ - read_colTitle(fp1, &ColMLNames1, nColTitleLines1, nCol1); - read_colTitle(fp2, &ColMLNames2, nColTitleLines2, nCol2); - ColNames1 = ColMLNames1[0]; - ColNames2 = ColMLNames2[0]; - - /* - * Do a Comparison of the names to find the maximum number - * of matches. - */ - nColMAX = max(nCol1, nCol2); - - compColList = mdp_alloc_int_2(nColMAX, 2, -1); - nColcomparisons = 0; - for (i = 0; i < nCol1; i++) { - found = false; - for (j = 0; j < nCol2; j++) { - if (!strcmp(ColNames1[i], ColNames2[j])) { - compColList[nColcomparisons][0] = i; - compColList[nColcomparisons][1] = j; - nColcomparisons++; - found = true; - break; - } - } - if (!found) { - printf("csvdiff WARNING Variable %s (%d) in first file not found" - " in second file\n", ColNames1[i], i); - testPassed = RT_FAILED_OTHER; - } - } - for (j = 0; j < nCol2; j++) { - found = false; - for (i = 0; i < nColcomparisons; i++) { - if (compColList[i][1] == j) { - found = true; - } - } - if (! found) { - printf("csvdiff WARNING Variable %s (%d) in second file " - "not found in first file\n", - ColNames2[j], j); - testPassed = RT_FAILED_OTHER; - } - } - - /* - * Allocate storage for the column variables - */ - NVValues1 = mdp_alloc_dbl_2(nCol1, nDataRowsMAX, 0.0); - NVValues2 = mdp_alloc_dbl_2(nCol2, nDataRowsMAX, 0.0); - - /* - * Allocate storage for the column variables - */ - NSValues1 = (char***) mdp_alloc_ptr_2(nCol1, nDataRowsMAX); - NSValues2 = (char***) mdp_alloc_ptr_2(nCol2, nDataRowsMAX); - - /* - * Read in the values to the arrays - */ - read_values(fp1, NVValues1, NSValues1, nCol1, nDataRows1, ColIsFloat1); - read_values(fp2, NVValues2, NSValues2, nCol2, nDataRows2, ColIsFloat2); - - /* - * Compare the solutions in each file - */ - int method = 1; - double slope1, slope2, xatol; - int notOK; - for (k = 0; k < nColcomparisons; k++) { - - i1 = compColList[k][0]; - i2 = compColList[k][1]; - bool doFltComparison = true; - if (!ColIsFloat1[i1]) { - doFltComparison = false; - jmax = -1; - } - if (!ColIsFloat2[i2]) { - doFltComparison = false; - jmax = -1; - } - curStringValues1 = NSValues1[i1]; - curStringValues2 = NSValues2[i2]; - max_diff = 0.0; - ndiff = 0; - if (doFltComparison) { - curVarValues1 = NVValues1[i1]; - curVarValues2 = NVValues2[i2]; - atol_j = get_atol(curVarValues1, nDataRows1, gatol); - atol_j = min(atol_j, get_atol(curVarValues2, nDataRows2, gatol)); - for (j = 0; j < nDataRowsMIN; j++) { - - slope1 = 0.0; - slope2 = 0.0; - xatol = fabs(grtol * (NVValues1[0][j] - NVValues1[0][j-1])); - if (j > 0 && k > 0) { - slope1 = (curVarValues1[j] - curVarValues1[j-1])/ - (NVValues1[0][j] - NVValues1[0][j-1]); - slope2 = (curVarValues2[j] - curVarValues2[j-1])/ - (NVValues2[0][j] - NVValues2[0][j-1]); - } - if (method) { - notOK = diff_double_slope(curVarValues1[j], curVarValues2[j], - grtol, atol_j, xatol, slope1, slope2); - } else { - notOK = diff_double(curVarValues1[j], curVarValues2[j], - grtol, atol_j); - } - if (notOK) { - ndiff++; - rel_diff = calc_rdiff((double) curVarValues1[j], - (double) curVarValues2[j], grtol, atol_j); - if (rel_diff > max_diff) { - jmax = j; - max_diff = rel_diff; - } - if (ndiff < 10) { - printf("\tColumn variable %s at data row %d ", ColNames1[i1], j + 1); - printf(" differ: %g %g\n", curVarValues1[j], - curVarValues2[j]); - } - } - } - } else { - for (j = 0; j < nDataRowsMIN; j++) { - strip(curStringValues1[j]); - strip(curStringValues2[j]); - notOK = false; - if (strcmp(curStringValues1[j], curStringValues2[j])) { - notOK = true; - ndiff++; - if (ndiff < 10) { - printf("\tColumn String variable %s at data row %d ", ColNames1[i1], j + 1); - printf(" differ: %s %s\n", curStringValues1[j], - curStringValues2[j]); - } - } - } - } - - if (nDataRowsMIN != nDataRowsMAX) { - ndiff += nDataRowsMAX - nDataRowsMIN; - if (ndiff < 10) { - if (nDataRows1 > nDataRows2) { - for (j = nDataRowsMIN; j < nDataRowsMAX; j++) { - printf("\tColumn variable %s at data row %d ", ColNames1[i1], j + 1); - printf(" differ: %g NA\n", curVarValues1[j]); - } - } else { - for (j = nDataRowsMIN; j < nDataRowsMAX; j++) { - printf("\tColumn variable %s at data row %d ", ColNames1[i1], j + 1); - printf(" differ: NA %g \n", curVarValues2[j]); - } - } - } - } - - /* - * Print out final results of nodal variable test - */ - - if (ndiff > 0) { - printf( - "Column variable %s failed comparison test for %d occurrences\n", - ColNames1[i1], ndiff); - if (jmax >= 0) { - printf(" Largest difference was at data row %d ", jmax + 1); - printf(": %g %g\n", curVarValues1[jmax], curVarValues2[jmax]); - } - testPassed = RT_FAILED_COL; - } else if (Debug_Flag) { - printf("Column variable %s passed\n", ColNames1[i1]); - } - - } - - return testPassed; - -} /************END of main() *************************************************/ -/*****************************************************************************/ diff --git a/src/apps/mdp_allo.cpp b/src/apps/mdp_allo.cpp deleted file mode 100644 index 90a83a2db..000000000 --- a/src/apps/mdp_allo.cpp +++ /dev/null @@ -1,1589 +0,0 @@ -#include -#include -#include - -#include -#include -#include - -#include "mdp_allo.h" - -/* - * Allocate global storage for 2 debugging ints that are used in IO of - * error information. - */ -#ifdef MDP_MPDEBUGIO -int MDP_MP_Nprocs = 1; -int MDP_MP_myproc = 0; -#endif -/* - * Error Handling - * 7 print and exit - * 6 exit - * 5 print and create a divide by zero for stack trace analysis. - * 4 create a divide by zero for stack trace analysis. - * 3 print a message and throw the bad_alloc exception. - * 2 throw the bad_alloc exception and be quite - * 1 print a message and return from package with the NULL pointer - * 0 Keep completely silent about the matter and return with - * a null pointer. - * - * -> Right now, the only way to change this option is to right here - */ -int MDP_ALLO_errorOption = 3; - -#define MDP_ALLOC_INTERFACE_ERROR 230346 - -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -static void mdp_alloc_eh(const char* rname, size_t bytes) - -/************************************************************************* -* -* mdp_alloc_eh: -* -* Error Handling -* 7 print and exit -* 6 exit -* 5 print and create a divide by zero for stack trace analysis. -* 4 create a divide by zero for stack trace analysis. -* 3 print a message and throw the bad_alloc exception. -* 2 throw the bad_alloc exception and be quite -* 1 print a message and return from package with the NULL pointer -* 0 Keep completely silent about the matter and return with -* a null pointer. -**************************************************************************/ -{ - double cd = 0.0; - static char mesg[64]; - if (bytes == MDP_ALLOC_INTERFACE_ERROR) { -#ifdef MDP_MPDEBUGIO - sprintf(mesg,"MDP_ALLOC Interface ERROR P_%d: %s", MDP_MP_my_proc, - rname); -#else - sprintf(mesg,"MDP_ALLOC Interface ERROR: %s", rname); -#endif - } else { - sprintf(mesg,"%s ERROR: out of memory while mallocing %d bytes", - rname, (int) bytes); - } - if (MDP_ALLO_errorOption % 2 == 1) { - fprintf(stderr, "\n%s", mesg); -#ifdef MDP_MPDEBUGIO - if (MDP_MP_Nprocs > 1) { - fprintf(stderr,": proc = %d\n", MDP_MP_myproc); - } else { - fprintf(stderr,"\n"); - } -#else - fprintf(stderr,"\n"); -#endif - } - fflush(stderr); - if (MDP_ALLO_errorOption == 2 || MDP_ALLO_errorOption == 3) { - throw std::bad_alloc(); - } - if (MDP_ALLO_errorOption == 4 || MDP_ALLO_errorOption == 5) { - cd = 1.0 / cd; - } - if (MDP_ALLO_errorOption > 5) { - exit(-1); - } -} - -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -static void mdp_alloc_eh2(const char* rname) - -/************************************************************************* -* -* mdp_alloc_eh2: -* -* Second Level Error Handling -* This routine is used at the second level. -* It will be triggered after mdp_allo_eh() has -* been triggered. Thus, it only needs to deal with 1 -> print mess -* -* 3 create a divide by zero for stack trace analysis. -* 2 or above -> -* 1 print a message and return with the NULL pointer -* 0 Keep completely silent about the matter. -**************************************************************************/ -{ - if (MDP_ALLO_errorOption == 1) { - fprintf(stderr,"%s ERROR: returning with null pointer", rname); -#ifdef MDP_MPDEBUGIO - if (MDP_MP_Nprocs > 1) { - fprintf(stderr,": proc = %d", MDP_MP_myproc); - } -#endif - fprintf(stderr,"\n"); - } -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -#define Fprintf (void) fprintf - -/****************************************************************************/ -#ifndef HAVE_ARRAY_ALLOC -/****************************************************************************/ -static double* smalloc(size_t n); - -/***************************************************************************** - * - * Dynamic Allocation of Multidimensional Arrays - *---------------------------------------------------------------------------- - * - * Example Usage: - * - * typedef struct - * { int bus1; - * int bus2; - * int dest; - * } POINT; - * - * POINT **points, corner; - * - * points = (POINT **) array_alloc (2, x, y, sizeof(POINT)); - * ^ ^ ^ - * | | | - * number of dimensions--+ | | - * | | - * first dimension max----+ | - * | - * second dimension max-------+ - * - * (points may be now be used as if it were declared - * POINT points[x][y]) - * - * Note, the inner loop of the memory layout is the over the - * last column. - * - * - * corner = points[2][3]; (refer to the structure as you would any array) - * - * free (points); (frees the entire structure in one fell swoop) - * - ****************************************************************************/ -/***************************************************************************** -* The following section is a commented section containing -* an example main code: -****************************************************************************** -*double *array_alloc(); -*main() -*{ -* int ***temp; -* int *temp2; -* int i, j, k; -* int il, jl, kl; -* -* malloc_debug(2); -* il = 2; -* jl = 3; -* kl = 3; -* temp = (int ***) array_alloc(3,il,jl,kl,sizeof(int)); -* for (i=0; i 4) { - Fprintf(stderr, - "mdp_array_alloc ERROR: number of dimensions, %d, is > 4\n", - numdim); - return NULL; - } - - dim[0].index = va_arg(va, int); - - if (dim[0].index <= 0) { -#ifdef DEBUG - Fprintf(stderr, "WARNING: mdp_array_alloc called with first " - "dimension <= 0, %d\n\twill return the nil pointer\n", - (int)(dim[0].index)); -#endif - return((double*) NULL); - } - - dim[0].total = dim[0].index; - dim[0].size = sizeof(void*); - dim[0].off = 0; - for (i = 1; i < numdim; i++) { - dim[i].index = va_arg(va, int); - if (dim[i].index <= 0) { - Fprintf(stderr, - "WARNING: mdp_array_alloc called with dimension %d <= 0, " - "%d\n", i+1, (int)(dim[i].index)); - Fprintf(stderr, "\twill return the nil pointer\n"); - return((double*) NULL); - } - dim[i].total = dim[i-1].total * dim[i].index; - dim[i].size = sizeof(void*); - dim[i].off = dim[i-1].off + dim[i-1].total * dim[i-1].size; - } - - dim[numdim-1].size = va_arg(va, int); - va_end(va); - - /* - * Round up the last offset value so data is properly aligned. - */ - - dim[numdim-1].off = dim[numdim-1].size * - ((dim[numdim-1].off+dim[numdim-1].size-1)/dim[numdim-1].size); - - total = dim[numdim-1].off + dim[numdim-1].total * dim[numdim-1].size; - - dfield = (double*) smalloc((size_t) total); - field = (char*) dfield; - - for (i = 0; i < numdim - 1; i++) { - ptr = (char**)(field + dim[i].off); - data = (char*)(field + dim[i+1].off); - for (j = 0; j < dim[i].total; j++) { - ptr[j] = data + j * dim[i+1].size * dim[i+1].index; - } - } - - return dfield; -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -static double* smalloc(size_t n) - -/************************************************************************** -* smalloc: safe version of malloc -* -* This version of smalloc assigns space in even chunks of 8 bytes only -**************************************************************************/ -{ -#ifdef MDP_MEMDEBUG - static int firsttime = 1; - FILE* file; -#endif - double* pntr; - if (n == 0) { - pntr = NULL; - } else { - n = ((n - 1) / 8); - n = (n + 1) * 8; - pntr = (double*) malloc((size_t) n); - } - if (pntr == NULL && n != 0) { - Fprintf(stderr, "smalloc : Out of space - number of bytes " - "requested = %d\n", int(n)); - } -#ifdef MDP_MEMDEBUG - if (firsttime) { - firsttime = 0; - file = fopen("memops.txt", "w"); - } else { - file = fopen("memops.txt", "a"); - } - Fprintf(file, "%x %d malloc\n", pntr, n); - if ((int) pntr == 0x00000001) { - Fprintf(stderr, "FOUND IT!\n"); - exit(-1); - } - fclose(file); -#endif - return pntr; -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -void mdp_safe_free(void** ptr) - -/************************************************************************* -* -* mdp_safe_free(): -* -* This version of free calls the system's free function -* with maximum error checking. It also doesn't call free if ptr is -* the NULL pointer already. -* It will then set the freed pointer to NULL. Thus, a convention may -* be established wherein all pointers that can be malloced can be -* set to NULL if they are not malloced. -**************************************************************************/ -{ -#ifdef MDP_MEMDEBUG - FILE* file; -#endif - if (ptr == NULL) { - mdp_alloc_eh("mdp_safe_free: handle is NULL", MDP_ALLOC_INTERFACE_ERROR); - } - if (*ptr != NULL) { -#ifdef MDP_MEMDEBUG - file = fopen("memops.txt", "a"); - Fprintf(file, "%x free\n", *ptr); - fflush(file); - if ((int) *ptr == 0x00000001) { - Fprintf(stderr, "FOUND IT!\n"); - exit(-1); - } - fclose(file); -#endif - free(*ptr); - /* - * Set the value of ptr to NULL, so that further references - * to it will be flagged. - */ - *ptr = NULL; - } -} -/****************************************************************************/ -#endif -/***************************************************************************** -* -* Wrapper Functions -* -------------------- -* -* The function definitions below are wrappers around array_alloc for -* common operations. The following principles are followed: -* -* Argument dimensions le 0 are increased to 1 before calling array_alloc. -* Thus, something is always malloced during a call. The reason for this is -* that it minimizes the number of special cases in the calling program. -* -* A pointer to something else other than NULL indicates that that pointer -* has been previously malloced. Thus, it can be freed. Note, after a free -* operation, this package always sets the pointer to NULL before returning. -* -* "safe_alloc" routines try to free the pointer if nonNULL, before calling -* the base alloc_int_#() routines. -* -* The regular routines always initialize the previously malloced space. -* -*****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -int* mdp_alloc_int_1(int nvalues, const int val) - -/************************************************************************** -* -* mdp_alloc_int_1: -* -* Allocate and initialize a one dimensional array of integers. -* -* Input -* ------- -* nvalues = Length of the array -* val = initialization value -* Return -* ------ -* Pointer to the initialized integer array -* Failures are indicated by returning the NULL pointer. -**************************************************************************/ -{ - int* array; - if (nvalues <= 0) { - nvalues = 1; - } - array= (int*) mdp_array_alloc(1, nvalues, sizeof(int)); - if (array != NULL) { - if (val != MDP_INT_NOINIT) { - if (val == 0) { - (void) memset(array, 0, sizeof(int)*nvalues); - } else { - for (int i = 0; i < nvalues; i++) { - array[i] = val; - } - } - } - } else { - mdp_alloc_eh("mdp_alloc_int_1", nvalues * sizeof(int)); - } - return array; -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -void mdp_safe_alloc_int_1(int** array_hdl, int nvalues, const int val) - -/************************************************************************* -* -* mdp_safe_alloc_int_1: -* -* Allocates and/or initialize a one dimensional array of integers. -* -* Input -* ------- -* *array_hdl = Previous value of pointer. If non-NULL will try -* to free the memory at this address. -* nvalues = Length of the array -* val = initialization value -* Output -* ------ -* *array_hdl = This value is initialized to the correct address -* of the array. -* A NULL value in the position indicates an error. -**************************************************************************/ -{ - if (array_hdl == NULL) { - mdp_alloc_eh("mdp_safe_alloc_int_1: handle is NULL", - MDP_ALLOC_INTERFACE_ERROR); - return; - } - if (*array_hdl != NULL) { - mdp_safe_free((void**) array_hdl); - } - *array_hdl = mdp_alloc_int_1(nvalues, val); - if (*array_hdl == NULL) { - mdp_alloc_eh2("mdp_safe_alloc_int_1"); - } -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -void -mdp_realloc_int_1(int** array_hdl, int new_length, int old_length, - const int defval) - -/************************************************************************* -* -* mdp_realloc_int_1_(array_hdl, new_num_ptrs, old_num_ptrs); -* -* Reallocates a one dimensional array of ints. -* This routine always allocates space for at least one int. -* Calls the smalloc() routine to ensure that all malloc -* calls go through one location. This routine will then copy -* the pertinent information from the old array to the -* new array. -* -* Input -* ------- -* array_hdl = Pointer to the global variable that -* holds the old and (eventually new) -* address of the array of integers to be reallocated -* new_length = Length of the array -* old_length = Length of the old array -**************************************************************************/ -{ - if (new_length == old_length) { - return; - } - if (new_length <= 0) { -#ifdef MDP_MPDEBUGIO - fprintf(stderr, - "Warning: mdp_realloc_int_1 P_%d: called with n = %d ", - MDP_MP_myproc, new_length); -#else - fprintf(stderr, - "Warning: mdp_realloc_int_1: called with n = %d ", - new_length); -#endif - new_length = 1; - } - if (old_length < 0) { - old_length = 0; - } - if (new_length == old_length) { - return; - } - size_t bytenum = new_length * sizeof(int); - int* array = (int*) smalloc(bytenum); - if (array != NULL) { - if (*array_hdl) { - if (old_length > 0) { - bytenum = sizeof(int) * old_length; - } else { - bytenum = 0; - } - if (new_length < old_length) { - bytenum = sizeof(int) * new_length; - } - (void) memcpy((void*) array, (void*) *array_hdl, bytenum); - mdp_safe_free((void**) array_hdl); - } else { - old_length = 0; - } - *array_hdl = array; - if ((defval != MDP_INT_NOINIT) && (new_length > old_length)) { - if (defval == 0) { - bytenum = sizeof(int) * (new_length - old_length); - (void) memset((void*)(array+old_length), 0, bytenum); - } else { - for (int i = old_length; i < new_length; i++) { - array[i] = defval; - } - } - } - } else { - mdp_alloc_eh("mdp_realloc_int_1", bytenum); - } -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -int** mdp_alloc_int_2(int ndim1, int ndim2, const int val) - -/************************************************************************* -* -* mdp_alloc_int_2: -* -* Allocate and initialize a two dimensional array of ints. -* -* Input -* ------- -* ndim1 = Length of the first dimension of the array -* ndim2 = Length of the second dimension of the array -* val = initialization value -* Return -* ------ -* Pointer to the initialized integer array -* Failures are indicated by returning the NULL pointer. -**************************************************************************/ -{ - int i; - int** array, *dptr; - if (ndim1 <= 0) { - ndim1 = 1; - } - if (ndim2 <= 0) { - ndim2 = 1; - } - array = (int**) mdp_array_alloc(2, ndim1, ndim2, sizeof(int)); - if (array != NULL) { - if (val != MDP_INT_NOINIT) { - if (val == 0) { - (void) memset((void*) array[0], 0, ndim1 * ndim2 * sizeof(int)); - } else { - dptr = &(array[0][0]); - for (i = 0; i < ndim1 * ndim2; i++) { - dptr[i] = val; - } - } - } - } else { - mdp_alloc_eh("mdp_alloc_int_2", - sizeof(int) * ndim1 * ndim2 + - ndim1 * sizeof(void*)); - } - return array; -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -double* mdp_alloc_dbl_1(int nvalues, const double val) - -/************************************************************************* -* -* mdp_alloc_dbl_1: -* -* Allocate and initialize a one dimensional array of doubles. -* -* Input -* ------- -* nvalues = Length of the array -* val = initialization value -* Return -* ------ -* Pointer to the initialized integer array -* Failures are indicated by returning the NULL pointer. -**************************************************************************/ -{ - int i; - double* array; - if (nvalues <= 0) { - nvalues = 1; - } - array = (double*) mdp_array_alloc(1, nvalues, sizeof(double)); - if (array != NULL) { - if (val != MDP_DBL_NOINIT) { - if (val == 0.0) { - (void) memset((void*) array, 0, nvalues * sizeof(double)); - } else { - for (i = 0; i < nvalues; i++) { - array[i] = val; - } - } - } - } else { - mdp_alloc_eh("mdp_alloc_dbl_1", nvalues * sizeof(double)); - } - return array; -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -void mdp_safe_alloc_dbl_1(double** array_hdl, int nvalues, const double val) - -/************************************************************************* -* -* mdp_safe_alloc_dbl_1: -* -* Allocates and/or initializse a one dimensional array of doubles. -* -* Input -* ------- -* *array_hdl = Previous value of pointer. If non-NULL will try -* to free the memory at this address. -* nvalues = Length of the array -* val = intialization value -* Output -* ------ -* *array_hdl = This value is initialized to the correct address -* of the array. -* A NULL value in the position indicates an error. -**************************************************************************/ -{ - if (array_hdl == NULL) { - mdp_alloc_eh("mdp_safe_alloc_dbl_1: handle is NULL", - MDP_ALLOC_INTERFACE_ERROR); - return; - } - if (*array_hdl != NULL) { - mdp_safe_free((void**) array_hdl); - } - *array_hdl = mdp_alloc_dbl_1(nvalues, val); - if (*array_hdl == NULL) { - mdp_alloc_eh2("mdp_safe_alloc_dbl_1"); - } -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -void mdp_realloc_dbl_1(double** array_hdl, int new_length, - int old_length, const double defval) - -/************************************************************************* -* -* mdp_realloc_dbl_1_(array_hdl, new_num_ptrs, old_num_ptrs); -* -* Reallocates a one dimensional array of doubles. -* This routine always allocates space for at least one dbl. -* Calls the smalloc() routine to ensure that all malloc -* calls go through one location. This routine will then copy -* the pertinent information from the old array to the -* new array. -* -* Input -* ------- -* array_hdl = Pointer to the global variable that -* holds the old and (eventually new) -* address of the array of doubles to be reallocated -* new_length = Length of the array -* old_length = Length of the old array -**************************************************************************/ -{ - if (new_length == old_length) { - return; - } - if (new_length <= 0) { -#ifdef MDP_MPDEBUGIO - fprintf(stderr, "Warning: mdp_realloc_dbl_1 P_%d: called with n = %d ", - MDP_MP_myproc, new_length); -#else - fprintf(stderr, "Warning: mdp_realloc_dbl_1: called with n = %d ", - new_length); -#endif - new_length = 1; - } - if (old_length < 0) { - old_length = 0; - } - if (new_length == old_length) { - return; - } - size_t bytenum = new_length * sizeof(double); - double* array = (double*) smalloc(bytenum); - if (array != NULL) { - if (*array_hdl) { - if (old_length > 0) { - bytenum = sizeof(double) * old_length; - } else { - bytenum = 0; - } - if (new_length < old_length) { - bytenum = sizeof(double) * new_length; - } - (void) memcpy((void*) array, (void*) *array_hdl, bytenum); - mdp_safe_free((void**) array_hdl); - } else { - old_length = 0; - } - *array_hdl = array; - if ((defval != MDP_DBL_NOINIT) && (new_length > old_length)) { - if (defval == 0) { - bytenum = sizeof(double) * (new_length - old_length); - (void) memset((void*)(array+old_length), 0, bytenum); - } else { - for (int i = old_length; i < new_length; i++) { - array[i] = defval; - } - } - } - } else { - mdp_alloc_eh("mdp_realloc_dbl_1", bytenum); - } -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -char* mdp_alloc_char_1(int nvalues, const char val) - -/************************************************************************* -* -* mdp_alloc_char_1: -* -* Allocate and initialize a one dimensional array of characters. -* -* Input -* ------- -* nvalues = Length of the array -* val = initialization value -* Return -* ------ -* Pointer to the initialized character array -* Failures are indicated by returning the NULL pointer. -**************************************************************************/ -{ - int i; - char* array; - if (nvalues <= 0) { - nvalues = 1; - } - array = (char*) mdp_array_alloc(1, nvalues, sizeof(char)); - if (array != NULL) { - for (i = 0; i < nvalues; i++) { - array[i] = val; - } - } else { - mdp_alloc_eh("mdp_alloc_char_1", nvalues * sizeof(char)); - } - return array; -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -void mdp_safe_alloc_char_1(char** array_hdl, int nvalues, const char val) - -/************************************************************************* -* -* mdp_safe_alloc_char_1: -* -* Allocates and/or initializse a one dimensional array of characters. -* -* Input -* ------- -* array_hdl = Previous value of pointer. If non-NULL will try -* to free the memory at this address. -* nvalues = Length of the array -* val = intialization value -* Output -* ------ -* *array_hdl = This value is initialized to the correct address -* of the array. -* A NULL value in the position indicates an error. -**************************************************************************/ -{ - if (array_hdl == NULL) { - mdp_alloc_eh("mdp_safe_alloc_char_1: handle is NULL", - MDP_ALLOC_INTERFACE_ERROR); - return; - } - if (*array_hdl != NULL) { - mdp_safe_free((void**) array_hdl); - } - *array_hdl = mdp_alloc_char_1(nvalues, val); - if (*array_hdl == NULL) { - mdp_alloc_eh2("mdp_safe_alloc_char_1"); - } -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -double** mdp_alloc_dbl_2(int ndim1, int ndim2, const double val) - -/************************************************************************* -* -* mdp_alloc_dbl_2: -* -* Allocate and initialize a two dimensional array of doubles. -* -* Input -* ------- -* ndim1 = Length of the first dimension of the array -* ndim2 = Length of the second dimension of the array -* val = initialization value -* Return -* ------ -* Pointer to the initialized double array -* Failures are indicated by returning the NULL pointer. -**************************************************************************/ -{ - int i; - double** array, *dptr; - if (ndim1 <= 0) { - ndim1 = 1; - } - if (ndim2 <= 0) { - ndim2 = 1; - } - array = (double**) mdp_array_alloc(2, ndim1, ndim2, sizeof(double)); - if (array != NULL) { - if (val != MDP_DBL_NOINIT) { - if (val == 0.0) { - (void) memset((void*) array[0], 0, ndim1*ndim2 * sizeof(double)); - } else { - dptr = &(array[0][0]); - for (i = 0; i < ndim1*ndim2; i++) { - dptr[i] = val; - } - } - } - } else { - mdp_alloc_eh("mdp_alloc_dbl_2", - sizeof(double) * ndim1 * ndim2 + - ndim1 * sizeof(void*)); - } - return array; -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -void mdp_safe_alloc_dbl_2(double** *array_hdl, int ndim1, int ndim2, - const double val) - -/************************************************************************* -* -* mdp_safe_alloc_dbl_2: -* -* Allocate and initialize a two dimensional array of doubles. -* -* Input -* ------- -* *array_hdl = Previous value of pointer. If non-NULL will try -* to free the memory at this address. -* ndim1 = Length of the array -* ndim2 = Length of inner loop of the array -* val = intialization value -* Return -* ------ -* *array_hdl = This value is initialized to the correct address -* of the array. -* A NULL value in the position indicates an error. -**************************************************************************/ -{ - if (array_hdl == NULL) { - mdp_alloc_eh("mdp_safe_alloc_dbl_2: handle is NULL", - MDP_ALLOC_INTERFACE_ERROR); - return; - } - if (*array_hdl != NULL) { - mdp_safe_free((void**) array_hdl); - } - *array_hdl = mdp_alloc_dbl_2(ndim1, ndim2, val); - if (*array_hdl == NULL) { - mdp_alloc_eh2("mdp_safe_alloc_dbl_2"); - } -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -void mdp_realloc_dbl_2(double** *array_hdl, int ndim1, int ndim2, - int ndim1Old, int ndim2Old, const double val) - -/************************************************************************* -* -* mdp_realloc_dbl_2: -* -* mdp_realloc_dbl_2(array_hdl, int ndim1, int ndim2, -* int ndim1Old, int ndim2Old, const double val) -* -* Reallocates a two dimensional array of doubles. -* This routine will then copy the pertinent information from -* the old array to the new array. -* -* If both old dimensions are set to zero or less, then this routine -* will free the old memory before mallocing the new memory. This may -* be a benefit for extremely large mallocs. -* In all other cases, the new and the old malloced arrays will -* exist for a short time together. -* -* Input -* ------- -* array_hdl = Pointer to the global variable that -* holds the old and (eventually new) -* address of the array of doubles to be reallocated -* ndim1 = First dimension of the new array -* ndim2 = Second dimension of the new array -* ndim1Old = First dimension of the old array -* ndim2Old = Second dimension of the old array -* val = Default fill value. -**************************************************************************/ -{ - if (ndim1 <= 0) { - ndim1 = 1; - } - if (ndim2 <= 0) { - ndim2 = 1; - } - ndim1Old = std::max(ndim1Old, 0); - ndim2Old = std::max(ndim2Old, 0); - /* - * One way to do it, if old information isn't needed. In this algorithm - * the arrays are never malloced at the same time. - */ - if ((*array_hdl == NULL) || (ndim1Old <= 0 && ndim2Old <= 0)) { - mdp_safe_free((void**) array_hdl); - *array_hdl = mdp_alloc_dbl_2(ndim1, ndim2, val); - if (*array_hdl == NULL) { - mdp_alloc_eh2("mdp_realloc_dbl_2"); - } - } - /* - * Other way to do when old information is available and needed - */ - else { - double** array_old = *array_hdl; - *array_hdl = (double**) mdp_array_alloc(2, ndim1, ndim2, sizeof(double)); - if (*array_hdl == NULL) { - mdp_alloc_eh2("mdp_realloc_dbl_2"); - } else { - /* - * Now, let's initialize the arrays - */ - int ndim1Min = std::min(ndim1, ndim1Old); - int ndim2Min = std::min(ndim2, ndim2Old); - double** array_new = *array_hdl; - /* - * When the second dimensions are equal, we can copy blocks - * using the very efficient bit moving kernels. - */ - if (ndim2 == ndim2Old) { - size_t sz = ndim1Min * ndim2 * sizeof(double); - (void) memcpy((void*) array_new[0], (void*) array_old[0], sz); - } - /* - * If the second dimensions aren't equal, then we have to - * break up the bit operations even more - */ - else { - size_t sz = ndim2Min * sizeof(double); - size_t sz2 = (ndim2 - ndim2Min) * sizeof(double); - for (int i = 0; i < ndim1Min; i++) { - (void) memcpy((void*) array_new[i], (void*) array_old[i], sz); - if (ndim2 > ndim2Min && val != MDP_DBL_NOINIT) { - if (val == 0.0) { - (void) memset((void*)(array_new[i] + ndim2Min), 0, sz2); - } else { - double* dptr = array_new[i]; - for (int j = ndim2Min; j < ndim2; j++) { - dptr[j] = val; - } - } - } - } - } - /* - * finish up initializing the rest of the array - */ - if (ndim1 > ndim1Min && val != MDP_DBL_NOINIT) { - if (val == 0.0) { - size_t sz = (ndim1 - ndim1Min) * ndim2 * sizeof(double); - (void) memset((void*) array_new[ndim1Min], 0, sz); - } else { - double* dptr = array_new[ndim1Min]; - int num = (ndim1 - ndim1Min) * ndim2; - for (int i = 0; i < num; i++) { - dptr[i] = val; - } - } - } - /* - * Free the old array - */ - mdp_safe_free((void**) &array_old); - } - } -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -char** mdp_alloc_VecFixedStrings(int numStrings, int lenString) - -/************************************************************************* -* -* mdp_alloc_VecFixedStrings: -* -* Allocate and initialize a vector of fixed-length -* strings. Each string is initialized to the NULL string. -* -* Input -* ------- -* numStrings = Number of strings -* lenString = Length of each string including the trailing null -* character -* Return -* ------ -* This value is initialized to the correct address of the array. -* A NULL value in the position indicates an error. -**************************************************************************/ -{ - int i; - char** array; - if (numStrings <= 0) { - numStrings = 1; - } - if (lenString <= 0) { - lenString = 1; - } - array = (char**) mdp_array_alloc(2, numStrings, lenString, sizeof(char)); - if (array != NULL) { - for (i = 0; i < numStrings; i++) { - array[i][0] = '\0'; - } - } else { - mdp_alloc_eh("mdp_alloc_VecFixedStrings", - sizeof(char) * numStrings * lenString + - numStrings * sizeof(void*)); - } - return array; -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -void mdp_realloc_VecFixedStrings(char** *array_hdl, int numStrings, - int numOldStrings, int lenString) - -/************************************************************************* -* -* mdp_realloc_VecFixedStrings: -* -* Reallocate and initialize a vector of fixed-length -* strings. Each new string is initialized to the NULL string. -* old strings are copied. -* -* Input -* ------- -* ***array_hdl = The pointer to the char ** location holding -* the data to be reallocated. -* numStrings = Number of strings -* numOldStrings = Number of old strings -* lenString = Length of each string including the trailing null -* character -**************************************************************************/ -{ - int i; - char** array, **ao; - if (numStrings <= 0) { - numStrings = 1; - } - if (numStrings == numOldStrings) { - return; - } - if (lenString <= 0) { - lenString = 1; - } - array = (char**) mdp_array_alloc(2, numStrings, lenString, sizeof(char)); - if (array != NULL) { - int len = std::min(numStrings, numOldStrings); - ao = *array_hdl; - if (ao) { - for (i = 0; i < len; i++) { - strncpy(array[i], ao[i], lenString); - } - } - if (numStrings > numOldStrings) { - for (i = numOldStrings; i < numStrings; i++) { - array[i][0] = '\0'; - } - } - mdp_safe_free((void**) array_hdl); - *array_hdl = array; - - } else { - mdp_alloc_eh("mdp_realloc_VecFixedStrings", - sizeof(char) * numStrings * lenString + - numStrings * sizeof(void*)); - } -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -void mdp_safe_alloc_VecFixedStrings(char** *array_hdl, - int numStrings, int lenString) - -/************************************************************************* -* -* mdp_safe_alloc_VecFixedStrings -* -* Allocate and initialize an array of strings of fixed length -* -* Input -* ------- -* *array_hdl = Previous value of pointer. If non-NULL will try -* to free the memory at this address. -* numStrings = Number of strings -* lenString = Length of each string including the trailing null -* character -* Output -* ------ -* *array_hdl = This value is initialized to the correct address -* of the array. -* A NULL value in the position indicates an error. -**************************************************************************/ -{ - if (array_hdl == NULL) { - mdp_alloc_eh("mdp_safe_alloc_VecFixedStrings: handle is NULL", - MDP_ALLOC_INTERFACE_ERROR); - return; - } - if (*array_hdl != NULL) { - mdp_safe_free((void**) array_hdl); - } - *array_hdl = mdp_alloc_VecFixedStrings(numStrings, lenString); - if (*array_hdl == NULL) { - mdp_alloc_eh2("mdp_safe_alloc_VecFixedStrings"); - } -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -C16_NAME* mdp_alloc_C16_NAME_1(int numStrings, const int init) - -/************************************************************************** -* -* mdp_alloc_C16_NAME_1: -* -* Allocate and initialize a vector of fixed-length -* strings of type C16_NAME -* -* Input -* ------- -* numStrings = Number of strings -* init = If true, this routine initializes the space to the -* space character. -* Return -* ------ -* This value is initialized to the correct address of the array. -* A NULL value in the position indicates an error. -**************************************************************************/ -{ - int i, j; - char* c_ptr; - if (numStrings <= 0) { - numStrings = 1; - } - C16_NAME* array = (C16_NAME*) mdp_array_alloc(1, numStrings, sizeof(C16_NAME)); - if (array != NULL) { - if (init) { - for (i = 0; i < numStrings; i++) { - c_ptr = (char*)(array + i); - for (j = 0; j < (int) sizeof(C16_NAME); j++) { - c_ptr[j] = ' '; - } - } - } - } else { - mdp_alloc_eh("mdp_alloc_C16_NAME_1", - sizeof(C16_NAME) * numStrings); - } - return array; -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -void mdp_safe_alloc_C16_NAME_1(C16_NAME** array_hdl, int numStrings, - const int init) - -/************************************************************************* -* -* mdp_safe_alloc_C16_NAME_1: -* -* Allocate and initialize a vector of fixed-length -* strings of type C16_NAME -* -* Input -* ------- -* *array_hdl = Previous value of pointer. If non-NULL will try -* to free the memory at this address. -* numStrings = Number of strings -* init = If true, this routine initializes the space to the -* space character. -* Output -* ------ -* *array_hdl = This value is initialized to the correct address -* of the array. -* A NULL value in the position indicates an error. -**************************************************************************/ -{ - if (array_hdl == NULL) { - mdp_alloc_eh("mdp_safe_alloc_C16_NAME_1: handle is NULL", - MDP_ALLOC_INTERFACE_ERROR); - return; - } - if (*array_hdl != NULL) { - mdp_safe_free((void**) array_hdl); - } - *array_hdl = mdp_alloc_C16_NAME_1(numStrings, init); - if (*array_hdl == NULL) { - mdp_alloc_eh2("mdp_safe_alloc_C16_NAME_1"); - } -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -void** mdp_alloc_ptr_1(int numPointers) - -/************************************************************************* -* -* mdp_alloc_ptr_1: -* -* Allocate and initialize a vector of pointers -* of type pointer to void. All pointers are initialized to the NULL -* value. -* -* Input -* ------- -* numPointers = Number of pointers -* Return -* ------ -* This value is initialized to the correct address of the vector. -* A NULL value in the position indicates an error. -**************************************************************************/ -{ - int i; - void** array; - if (numPointers <= 0) { - numPointers = 1; - } - array = (void**) mdp_array_alloc(1, numPointers, sizeof(void*)); - if (array != NULL) { - for (i = 0; i < numPointers; i++) { - array[i] = NULL; - } - } else { - mdp_alloc_eh("mdp_alloc_ptr_1", - sizeof(void*) * numPointers); - } - return array; -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -void mdp_safe_alloc_ptr_1(void** *array_hdl, int numPointers) - -/************************************************************************** -* -* mdp_safe_alloc_ptr_1: -* -* Allocate and initialize a vector of pointers -* of type pointer to void. All pointers are initialized to the NULL -* value. -* -* Input -* ------- -* *array_hdl = Previous value of pointer. If non-NULL will try -* to free the memory at this address. -* numPointers = Number of pointers -* Output -* ------ -* *array_hdl = This value is initialized to the correct address -* of the array. -* A NULL value in the position indicates an error. -**************************************************************************/ -{ - if (array_hdl == NULL) { - mdp_alloc_eh("mdp_safe_alloc_ptr_1: handle is NULL", - MDP_ALLOC_INTERFACE_ERROR); - return; - } - if (*array_hdl != NULL) { - mdp_safe_free((void**) array_hdl); - } - *array_hdl = mdp_alloc_ptr_1(numPointers); - if (*array_hdl == NULL) { - mdp_alloc_eh2("mdp_safe_alloc_ptr_1"); - } -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -void mdp_realloc_ptr_1(void** *array_hdl, int numLen, int numOldLen) - -/************************************************************************* -* -* mdp_realloc__ptr_1: -* -* Reallocate and initialize a vector of pointers -* Each new pointer is initialized to NULL. -* old Pointers are copied. -* -* Input -* ------- -* ***array_hdl = The pointer to the char ** location holding -* the data to be reallocated. -* numLen = Number of strings -* numOldLen = Number of old strings -**************************************************************************/ -{ - if (array_hdl == NULL) { - mdp_alloc_eh("mdp_safe_alloc_ptr_1: handle is NULL", - MDP_ALLOC_INTERFACE_ERROR); - return; - } - if (numLen <= 0) { - numLen = 1; - } - if (numOldLen < 0) { - numOldLen = 0; - } - if (numLen == numOldLen) { - return; - } - size_t bytenum = sizeof(void*) * numLen; - void** array = (void**) smalloc(bytenum); - if (array != NULL) { - int len = std::min(numLen, numOldLen); - if (*array_hdl) { - void** ao = *array_hdl; - for (int i = 0; i < len; i++) { - array[i] = ao[i]; - } - } else { - numOldLen = 0; - } - if (numLen > numOldLen) { - bytenum = sizeof(void*) * (numLen - numOldLen); - (void) memset((void*)(array + numOldLen), 0, bytenum); - } - mdp_safe_free((void**) array_hdl); - *array_hdl = array; - } else { - mdp_alloc_eh("mdp_realloc_ptr_1", sizeof(void*) * numLen); - } -} - -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -void*** mdp_alloc_ptr_2(int ndim1, int ndim2) - -/************************************************************************* -* -* mdp_alloc_ptr_2: -* -* Allocate and initialize an array of pointers -* of type pointer to void. All pointers are initialized to the NULL -* value. -* referenced by ptrArray[ndim1][ndim2] -* -* Input -* ------- -* ndim1 = Number of pointers in din 1 -* ndim2 = Number of pointers in dim 2 -* Return -* ------ -* This value is initialized to the correct address of the vector. -* A NULL value in the position indicates an error. -**************************************************************************/ -{ - void** *array; - if (ndim1 <= 0) { - ndim1 = 1; - } - if (ndim2 <= 0) { - ndim2 = 1; - } - array = (void***) mdp_array_alloc(2, ndim1, ndim2, sizeof(void*)); - if (array != NULL) { - (void) memset((void*) array[0], 0, ndim1*ndim2 * sizeof(void*)); - } else { - mdp_alloc_eh("mdp_alloc_ptr_2", - sizeof(void*) * ndim1 * ndim2); - } - return array; -} - -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -char* mdp_copy_C16_NAME_to_string(const C16_NAME copyFrom) - -/************************************************************************* -* -* mdp_copy_C16_NAME_to_string: -* -* Allocates space for and copies a C16_NAME -* -* Input -* ------- -* copyFrom = C16_NAME string (note, this doesn't necessarily have -* to be null terminate. This is the reason for this -* subroutine. -* If NULL is supplied, then nothing is malloced and -* a NULL value is returned. -* Return -* ------ -* This value is initialized to the correct address of the array. -* A NULL value in the position either indicates an error, or -* that the original pointer to the string was NULL. -**************************************************************************/ -{ - /* - * This routine creates a temporary string with a null terminator at the - * end (assured). Then it uses mdp_copy_string() to do the work - */ - C16_NAME_STR tmpString; - if (copyFrom == NULL) { - return NULL; - } - tmpString[sizeof(C16_NAME)] = '\0'; - (void) strncpy(tmpString, copyFrom, sizeof(C16_NAME)); - return mdp_copy_string(tmpString); -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -char* mdp_copy_string(const char* copyFrom) - -/************************************************************************* -* -* mdp_copy_string: -* -* Allocates space for and copies a string -* -* Input -* ------- -* copyFrom = null terminated string. If NULL is supplied, then -* nothing is malloced and a NULL value is returned. -* Return -* ------ -* This value is initialized to the correct address of the array. -* A NULL value in the position either indicates an error, or -* that the original pointer to the string was NULL. -**************************************************************************/ -{ - char* cptr; - if (copyFrom == NULL) { - return NULL; - } - cptr = (char*) mdp_array_alloc(1, strlen(copyFrom) + 1, sizeof(char)); - if (cptr != NULL) { - (void) strcpy(cptr, copyFrom); - } else { - mdp_alloc_eh("mdp_copy_string", sizeof(char) * (strlen(copyFrom) + 1)); - } - return cptr; -} -/****************************************************************************/ -/****************************************************************************/ -/****************************************************************************/ - -void mdp_safe_copy_string(char** string_hdl, const char* copyFrom) - -/************************************************************************* -* -* mdp_safe_copy_string: -* -* Allocates space for and copies a string -* -* Input -* ------- -* *string_hdl = Previous value of pointer. If non-NULL will try -* to free the memory at this address. -* *copyFrom = String to be copied -* Output -* ------ -* *string_hdl = Pointer to the copied string -* A NULL value in the position indicates an error. -**************************************************************************/ -{ - if (string_hdl == NULL) { - mdp_alloc_eh("mdp_safe_copy_string: string_hdl is NULL", - MDP_ALLOC_INTERFACE_ERROR); - return; - } - if (*string_hdl != NULL) { - mdp_safe_free((void**) string_hdl); - } - if (copyFrom == NULL) { - *string_hdl = NULL; - return; - } - *string_hdl = mdp_copy_string(copyFrom); - if (*string_hdl == NULL) { - mdp_alloc_eh2("mdp_safe_copy_string"); - } - return; -} -/****************************************************************************/ -/* END of mdp_allo.cpp */ -/****************************************************************************/ diff --git a/src/apps/mdp_allo.h b/src/apps/mdp_allo.h deleted file mode 100644 index c07d6bacd..000000000 --- a/src/apps/mdp_allo.h +++ /dev/null @@ -1,89 +0,0 @@ -#ifndef MDP_ALLO_H -#define MDP_ALLO_H - -#include -/*****************************************************************************/ -/* -* If we have array_alloc() from another Sandia program, we will not use -* the one from this mdp_array_alloc. Instead we will redefine the names -*/ - -#ifdef HAVE_ARRAY_ALLOC -# define mdp_array_alloc array_alloc -# define mdp_safe_free safe_free -#endif - -/* - * These are a poor man's way of specifying whether a value should be - * initialized. These are seldom used numbers whic - * $Name: $ - *====================h can be used in place - * of real ints and dbls to indicate that initialization shouldn't take - * place. - */ -#define MDP_INT_NOINIT -68361 -#define MDP_DBL_NOINIT -1.241E11 - - -#ifndef _C16_NAME_DEF -# define _C16_NAME_DEF -typedef char C16_NAME[16]; /* Character array used for fortran names */ -typedef char C16_NAME_STR[17]; -#endif -/*****************************************************************************/ -/* - * Externals that should be set by the calling program. - * These are only used for debugging purposes. - */ -#ifdef MDP_MPDEBUGIO -extern int MDP_MP_Nprocs; -extern int MDP_MP_myproc; -#endif -/*****************************************************************************/ - -#define mdp_alloc_struct(x, num) (x *) mdp_array_alloc(1, (num), sizeof(x)) - - -/* function declarations for dynamic array allocation */ - -extern double* mdp_array_alloc(int numdim, ...); -extern void mdp_safe_free(void**); - -extern int* mdp_alloc_int_1(int, const int); -extern void mdp_safe_alloc_int_1(int**, int, const int); -extern void mdp_realloc_int_1(int**, int, int, - const int defval = MDP_INT_NOINIT); -extern int** mdp_alloc_int_2(int, int, const int); - -extern double* mdp_alloc_dbl_1(int, const double); -extern void mdp_safe_alloc_dbl_1(double**, int, const double); -extern void mdp_realloc_dbl_1(double**, int, int, const double); -extern void mdp_realloc_dbl_2(double***, int, int, int, int, - const double); - -extern char* mdp_alloc_char_1(int, const char); -extern void mdp_safe_alloc_char_1(char**, int, const char); -extern char** mdp_alloc_VecFixedStrings(int, int); -extern void mdp_safe_alloc_VecFixedStrings(char***, int, int); -extern void mdp_realloc_VecFixedStrings(char***, int, int, int); - -extern double** mdp_alloc_dbl_2(int, int, const double); -extern void mdp_safe_alloc_dbl_2(double***, int, int, - const double = MDP_DBL_NOINIT); - -extern C16_NAME* mdp_alloc_C16_NAME_1(int, const int); -extern void mdp_safe_alloc_C16_NAME_1(C16_NAME**, int, const int); - -extern void** mdp_alloc_ptr_1(int); -extern void mdp_safe_alloc_ptr_1(void***, int); -extern void mdp_realloc_ptr_1(void***, int, int); -extern char* mdp_copy_C16_NAME_to_string(const C16_NAME); -extern char* mdp_copy_string(const char*); -extern void mdp_safe_copy_string(char**, const char*); - -extern void*** mdp_alloc_ptr_2(int, int); - -/*****************************************************************************/ -#endif -/*****************************************************************************/ - diff --git a/src/apps/tok_input_util.cpp b/src/apps/tok_input_util.cpp deleted file mode 100644 index bd7b53447..000000000 --- a/src/apps/tok_input_util.cpp +++ /dev/null @@ -1,1640 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "tok_input_util.h" - -static char DEFAULT_STR[8] = "default"; -static char DELIMITERS[8] = " \t\n\f\r\v"; /* Defn of white space in isspace() - - Used in tokenizing lines */ -static char COM_CHAR = '!'; /* This is used as a comment character */ -static char COM_CHAR2 = '#'; /* This is used as a 2nd comment character */ -static char KEY_CHAR = '='; /* This is used to separate the key_string - from the rest of the line */ -static int PrintInputFile = true; /* Used to turn on and off the - printing of the input file */ - -/*************** R O U T I N E S I N T H E F I L E ******************* -* -* NAME TYPE CALLED_BY -*-------------------------------------------------------------------- -* get_next_keyLine bool extern -* tok_to_int int extern -* str_to_int int extern, tok_to_int -* tok_to_double double extern -* str_to_double double extern,tok_to_double -* tok_to_boolean bool extern -* str_to_boolean bool extern,tok_to_boolean -* tok_to_string char * extern -* -* scan_for_int int extern -* scan_for_double double extern -* scan_for_string char * extern -* scan_for_boolean bool extern -* scan_for_line int extern -* read_line int scan_for_line, -* get_next_keyLine -* interpret_int static bool str_to_int + others -* interpret_boolean static bool str_to_boolean -* interpret_double static bool str_to_double -* strip int read_input_file, -* look_for, -* get_next_keyLine -* read_string static void scan_for_line -* stokenize int fillTokStruct -* outofbnds static bool all -* strmatch bool extern, toktokmatch -* strstrmatch bool extern -* strtokmatch bool extern -* toktokmatch bool extern, strtokmatch -* strstrmatch -* fillTokStruct void extern, strtokmatch -* strstrmatch, -* get_next_keyLine -* copyTokStruct void extern -* -******************************************************************************/ -/* -* Definitions of static functions: -*/ - -static bool outofbnds(const double, const double, const double); -static bool interpret_boolean(const char*, int*, const int); -static bool interpret_int(const char*, int*, const int, const int, - const int); -static bool interpret_double(const char*, double*, const double, - const double, const double); - - -/************ Member Functions for the TOKEN Structure ********************/ - -TOKEN::TOKEN(void) : - orig_str(0), - tok_str(0), - ntokes(0) -{ - orig_str = copy_string(""); - tok_str = copy_string(""); - tok_ptr[0] = orig_str; -} - - -TOKEN::TOKEN(const char* str) : - orig_str(0), - tok_str(0), - ntokes(0) -{ - if (str == NULL) { - orig_str = copy_string(""); - tok_str = copy_string(""); - tok_ptr[0] = orig_str; - ntokes = 0; - } else { - orig_str = copy_string(str); - tok_str = copy_string(str); - ntokes = stokenize(tok_str, DELIMITERS, tok_ptr, MAXTOKENS); - } -} - -TOKEN::~TOKEN() -{ - if (orig_str) { - free(orig_str); - } - orig_str = NULL; - if (tok_str) { - free(tok_str); - } - tok_str = NULL; -} - -/**************************************************************************/ - -bool get_next_keyLine(FILE* ifp, TOKEN* keyLineTok, TOKEN* keyArgTok) -/* - * This routine reads the input file to obtain the next line of - * uncommented - * data. The results are returned in two TOKEN structures. keyLineTok - * contains the key Line (everything before the first equals sign). - * keyArgTok contains everything after the equals sign. - * Note - Either keyLineTok or keyArgTok may be the null token - * (but not both) - * - * The definition of a token structure, given in .h file, - * is as follows: - * - * struct TOKEN { - * char orig_str[MAX_INPUT_STR_LN + 1]; - * char tok_str[MAX_INPUT_STR_LN + 1]; - * char *tok_ptr[MAXTOKENS]; - * int ntokes; - * }; - * mdp_allo.h - * orig_str Contains the original string, unmodified. - * tok_str Contains a modified version of the string, - * whose positions - * are pointed to by tok_ptr[i] values. It is usually not - * referenced directly. - * tok_ptr[i] Contains the i_th token of the original string. This - * is a stripped character string. 0 <=i <= i-1 - * ntokes Number of tokens in the string. - * - * - * Comments are denoted by either '!' or '#'. - * Everything after the comment character on a - * line is stripped first. The comment character can occur - * anywhere in the line. - * - * Arguments to the keyLine are denoted by everything after a - * '=' character on the line. - * - * Example: - * --------------------------------------------------------------- - * ! Jack and Jill went up the hill to fetch a pale of water - * ! Jack by nimble, Jack be swift; Jack jump over the candle stick - * - * The meaning of life is = 36.243 24 136 Not even ! close - * ----------------------------------------------------------------- - * - * Then, the routine would return (amongst other things): - * keyLineTok->orig_str = "The meaning of life is" - * keyArgTok->orig_str = "36.243 24 36 Not even" - * keyArgTok->ntokes = 5 - * keyArgTok->tok_ptr[0] = "36.243" - * keyArgTok->tok_ptr[1] = "24" - * keyArgTok->tok_ptr[2] = "136" - * keyArgTok->tok_ptr[3] = "Not" - * keyArgTok->tok_ptr[4] = "Even" - * - * The function returns true if there is a next line to process. - * It returns false if an EOF is encountered. - */ -{ - int retn_value, i; - char save_input[MAX_INPUT_STR_LN + 1]; - char* token_start = NULL; - - /* - * Check the arguments to the routine. This routine needs to be - * supplied with valid pointers to files and spaces for storage - * of its output in the return tokens. - */ - if (ifp == NULL || keyLineTok == NULL || keyArgTok == NULL) { - fprintf(stderr, "get_next_keyLine ERROR, arguments are bad\n"); - return false; - } - - /* - * Read a chunk of text, either up to a newline from the file pointer, - * ifp. If an EOF occurs, return without changing the input structure - */ -do_it_again: - do { - if ((retn_value = read_string(ifp, save_input, '\n')) < 0) { - return false; - } - if (PrintInputFile) { - if (retn_value <=0) { - printf("%s\n", save_input); - } else { - printf("%s", save_input); - } - } - for (i = 0; i < (int) strlen(save_input); i++) { - if (save_input[i] == COM_CHAR || save_input[i] == COM_CHAR2) { - save_input[i] = '\0'; - break; - } - } - } while (strip(save_input) == 0); - - /* - * Discover whether there are arguments in the line - * and then separate the line into two - */ - - for (i = 0; i < (int) strlen(save_input); i++) { - if (save_input[i] == KEY_CHAR) { - save_input[i] = '\0'; - token_start = save_input + i + 1; - break; - } - } - - /* - * Strip the two strings of leading and trailing white space. - * If both strings are now the null string (because the line - * consisted of a single '=' character for example), - * go back and get a new line. - */ - - i = strip(token_start); - if (!strip(save_input)) if (!i) { - goto do_it_again; - } - - /* - * Now that we have two strings representing the Key String and - * associated arguments, process them into TOKEN structures. - * Note - if token_start still points to NULL, then fillTokStruct - * will fill keyArgTok with a "null token". - */ - - fillTokStruct(keyLineTok, save_input); - fillTokStruct(keyArgTok, token_start); - return (true); -} -/**************************************************************************/ -/**************************************************************************/ -/**************************************************************************/ - -int tok_to_int(const TOKEN* tokPtr, const int maxVal, const int minVal, - const int defaultVal, bool* error) -/* - * Interprets the first string of a TOKEN structure as an int. - * Returns the interpreted value as the return value. - * AnErrors condition is created if more than one token is found - * in the struct TOKEN. - * Bounds checking is done on the value before returning. Value - * must be between the maxVal and minVal; it can equal the max or min - * value. - * - * Certain ascii strings are checked for first (case is insignificant): - * - * String Retn_Value (defined in - * --------- -------------- - * INT_MAX, max, all INT_MAX - * INT_MIN INT_MIN - * N/A, Not Available INT_MIN - * default or "" defaultVal - * - * A default may be specified on the command line. The absence of a - * default may also be specified by setting default_value to - * NO_DEFAULT_INT. - * - * If there is an error, *error is set to true. *error isn't touched - * if there isn't an error. - */ -{ - if (tokPtr->ntokes == 0) { - return str_to_int(DEFAULT_STR, maxVal, minVal, defaultVal, error); - } else if (tokPtr->ntokes > 1) { - (void) fprintf(stderr, "ERROR: tok_to_int, ntokes > 1: %s\n", - tokPtr->orig_str); - *error = true; - } - return str_to_int(tokPtr->tok_ptr[0], maxVal, minVal, defaultVal, error); -} -/**************************************************************************/ -/**************************************************************************/ -/**************************************************************************/ - -int str_to_int(const char* int_string, const int maxVal, const int minVal, - const int defaultVal, bool* error) -/* - * Interprets a stripped character string as an integer. - * Bounds checking is done on the value before returning. Value - * must be between the max and min; it can equal the max or min value. - * - * Certain ascii strings are checked for first (case is insignificant): - * - * String Retn_Value (defined in - * --------- -------------- - * INT_MAX, max, all INT_MAX - * INT_MIN INT_MIN - * N/A, Not Available INT_MIN - * default default_value - * - * A default may be specified on the command line. The absence of a - * default may also be specified by setting default_value to - * NO_DEFAULT_INT. - * - * If there is an error, *error is set to true. *error isn't touched - * if there isn't an error. - */ -{ - int retn_value, check = false; - double LmaxVal, LminVal; - if (defaultVal == NO_DEFAULT_INT) { - check = true; - } - if (interpret_int(int_string, &retn_value, maxVal, minVal, defaultVal)) { - if (check) { - if (retn_value == NO_DEFAULT_INT) { - (void) fprintf(stderr, - "ERROR: str_to_int: Default not allowed\n"); - *error = true; - } - } - if (maxVal < INT_MAX) { - LmaxVal = (double) maxVal + 0.01; - } else { - LmaxVal = (double) maxVal; - } - if (minVal > INT_MIN) { - LminVal = (double) minVal - 0.01; - } else { - LminVal = (double) minVal; - } - if (outofbnds((double) retn_value, LmaxVal, LminVal)) { - fprintf(stderr, - "ERROR: str_to_int outofbnds:\n\t\"%s\"\n", - int_string); - fprintf(stderr,"\tmax = %d, min = %d\n", maxVal, minVal); - *error = true; - } - } else { - *error = true; - } - return retn_value; -} -/**************************************************************************/ -/**************************************************************************/ -/**************************************************************************/ - -double tok_to_double(const TOKEN* tokPtr, const double maxVal, - const double minVal, const double defaultVal, - bool* error) -/* - * Interprets the first string of a TOKEN structure as a double. - * Returns the interpreted value as the return value. - * Errors conditions are created if more than one token is found. - * - * Bounds checking is then done on the value before returning. Value - * must be between the max and min; it can equal the max or min. - * - * Useful values for bounds (specified in : - * DBL_MAX = largest legitimate value of a double ~ 2.0E-308 - * -DBL_MAX = smallest legitimate value of a double ~ 2.0E-308 - * DBL_EPSILON = smallest value of a double that can be added to one - * and produce a different number. ~ 2.0E-16 - * DBL_MIN = smallest value of a double ~ 2.0E-308 - * For example: - * If 0.0 is not a legitimate number for value, set min = DBL_MIN - * If value>=0.0 is legitimate, set min = 0.0 - * if value<=100., set max = 100. - * If no range checking is required, set max = DBL_MAX, min = -DBL_MAX - * - * Certain ascii strings are checked for first (case is insignificant): - * - * Token_String Retn_Value ( specified in - * --------- --------------------------------------- - * FLT_MAX, all FLT_MAX - * DBL_MAX, max DBL_MAX - * N/A -DBL_MAX - * default or "" default_value - * small, DBL_MIN DBL_MIN - * DBL_EPSILON DBL_EPSILON - * - * A default may be specified by either specifying the value as - * "default" or by the absense of any tokens in the TOKEN struct. - * The absence of a default may also be specified by setting the - * value of default_value to NO_DEFAULT_DOUBLE. - * - * If there is an error, *error is set to true. *error isn't touched - * if there isn't an error. - */ -{ - if (tokPtr->ntokes == 0) { - return str_to_double(DEFAULT_STR, maxVal, minVal, defaultVal, error); - } else if (tokPtr->ntokes > 1) { - (void) fprintf(stderr, "ERROR: tok_to_double, ntokes > 1: %s\n", - tokPtr->orig_str); - *error = true; - } - return str_to_double(tokPtr->tok_ptr[0], maxVal, minVal, defaultVal, error); -} -/**************************************************************************/ -/**************************************************************************/ -/**************************************************************************/ - -double str_to_double(const char* dbl_string, const double maxVal, - const double minVal, const double defaultVal, - bool* error) -/* - * Interprets a stripped character string as a double. Returns the - * interpreted value as the return value. - * - * Bounds checking is then done on the value before returning. Value - * must be between the max and min; it can equal the max or min. - * - * Useful values for bounds (specified in : - * DBL_MAX = largest legitimate value of a double ~ 2.0E-308 - * -DBL_MAX = smallest legitimate value of a double ~ 2.0E-308 - * DBL_EPSILON = smallest value of a double that can be added to one - * and produce a different number. ~ 2.0E-16 - * DBL_MIN = smallest value of a double ~ 2.0E-308 - * For example: - * If 0.0 is not a legitimate number for value, set min = DBL_MIN - * If value>=0.0 is legitimate, set min = 0.0 - * if value<=100., set max = 100. - * If no range checking is required, set max = DBL_MAX, min = -DBL_MAX - * - * Certain ascii strings are checked for first (case is insignificant): - * - * String Retn_Value ( specified in - * --------- --------------------------------------- - * FLT_MAX, all FLT_MAX - * DBL_MAX, max DBL_MAX - * N/A -DBL_MAX - * default default_value - * small, DBL_MIN DBL_MIN - * DBL_EPSILON DBL_EPSILON - * - * A default may be specified. The absence of a - * default may also be specified by setting the value of default_value - * to NO_DEFAULT_DOUBLE. - * - * If there is an error, *error is set to true. *error isn't touched - * if there isn't an error. - */ -{ - double retn_value; - int check = false; - if (defaultVal == NO_DEFAULT_DOUBLE) { - check = true; - } - if (interpret_double(dbl_string, &retn_value, maxVal, minVal, defaultVal)) { - if (check) - if (retn_value == NO_DEFAULT_DOUBLE) { - (void) fprintf(stderr, - "ERROR: keyLine_double: Default not allowed\n"); - *error = true; - } - if (outofbnds(retn_value, maxVal, minVal)) { - (void) fprintf(stderr, "ERROR: keyLine_double outofbnds:\n\t\"%s\"\n", - dbl_string); - (void) fprintf(stderr,"\tmax = %e, min = %e\n", maxVal, minVal); - *error = true; - } - } else { - *error = true; - } - return retn_value; -} -/*****************************************************************************/ -/*****************************************************************************/ -/*****************************************************************************/ - -bool tok_to_boolean(const TOKEN* tokPtr, const int default_value, - bool* error) -/* -* Interprets the first string of a TOKEN structure as a bool. -* (i.e., true or false). Returns the interpreted value as the -* return value. -* Errors conditions are created if more than one token is found. -* -* The following character strings are interpreted -* (case doesn't matter): -* -* true = "YES", "true", "T", "Y" -* false = "NO, "false", "N", "F" -* default_value = "DEFAULT" or "" -* -* A default may be specified on the command line. The absence of a -* default may also be specified by using the value of NO_DEFAULT_INT. -* If tokPtr contains no tokens, this routine will try to use the -* default value. -* -* If there is an error, *error is set to true. *error isn't touched -* if there isn't an error. -*/ -{ - if (tokPtr->ntokes == 0) { - return str_to_boolean(DEFAULT_STR, default_value, error); - } else if (tokPtr->ntokes > 1) { - (void) fprintf(stderr, "ERROR: tok_to_boolean, ntokes > 1: %s\n", - tokPtr->orig_str); - *error = true; - } - return str_to_boolean(tokPtr->tok_ptr[0], default_value, error); -} -/******************************************************************************/ -/******************************************************************************/ -/******************************************************************************/ - -bool str_to_boolean(const char* string, const int default_value, - bool* error) -/* -* Interprets a stripped character string as a bool value -* (i.e., true or false). It returns that value as the return value. -* -* The following character strings are interpreted -* (case doesn't matter): -* -* true = "YES", "true", "T", "Y" -* false = "NO, "false", "N", "F" -* default_value = "DEFAULT" -* -* A default may be specified on the command line. The absence of a -* default may also be specified by using the value of NO_DEFAULT_INT. -* -* If there is an error, *error is set to true. *error isn't touched -* if there isn't an error. -*/ -{ - int retn_value; - if (interpret_boolean(string, &retn_value, default_value)) { - if (retn_value == NO_DEFAULT_BOOLEAN) { - (void) fprintf(stderr, - "ERROR: keyLine_boolean: Default not allowed\n"); - *error = true; - } - } else { - *error = true; - } - return (retn_value != 0); -} -/**************************************************************************/ -/**************************************************************************/ -/**************************************************************************/ - -char* tok_to_string(const TOKEN* tokPtr, const int maxTok, - const int minTok, const char* defaultVal, bool* error) -/* - * Interprets the arguments in a TOKEN structure as a string. - * It mallocs new space for the string, are returns the pointer to it. - * The number of tokens in the string is checked before returning. - * The value must be between the maxTok and minTok; it can equal the - * max or min value. - * - * Certain ascii strings are checked for first (case is insignificant): - * - * String Retn_Value - * --------- -------------- - * default or "" defaultVal - * - * A default may be specified on the command line. The absence of a - * default may also be specified by setting default_value to - * NO_DEFAULT_INT. - * - * If there is an error, *error is set to true. *error isn't touched - * if there isn't an error. - */ -{ - char* str; - if (tokPtr->ntokes == 0) { - str = str_to_string(DEFAULT_STR, defaultVal, error); - } else { - str = str_to_string(tokPtr->orig_str, defaultVal, error); - } - if (outofbnds((double) tokPtr->ntokes, (double) maxTok, - (double) minTok)) { - (void) fprintf(stderr, "ERROR: tok_to_String:\n\t\"%s\"\n", - tokPtr->orig_str); - (void) fprintf(stderr,"\tmaxTok = %d, minTok = %d\n", maxTok, minTok); - *error = true; - } - return str; -} -/*****************************************************************************/ -/*****************************************************************************/ -/*****************************************************************************/ - -char* str_to_string(const char* str, const char* defaultVal, - bool* error) -/* - * Interprets the argument string as a string. - * It mallocs new space for the string, are returns the pointer to it. - * - * Certain ascii strings are checked for first (case is insignificant): - * - * String Retn_Value - * --------- -------------- - * default defaultVal - * - * A default may be specified on the command line. The absence of a - * default may also be specified by setting default_value to - * NO_DEFAULT_INT. - * - * If there is an error, *error is set to true. *error isn't touched - * if there isn't an error. - */ -{ - if (str == NULL) { - *error = true; - (void) fprintf(stderr,"ERROR str_to_string: str is uninialized\n"); - return NULL; - } - if (strmatch(str, DEFAULT_STR)) { - if (strmatch(defaultVal, NO_DEFAULT_STR)) { - *error = true; - (void) fprintf(stderr,"ERROR str_to_string: no default allowed\n"); - return copy_string(NO_DEFAULT_STR); - } else { - return copy_string(defaultVal); - } - } - return copy_string(str); -} -/**************************************************************************/ -/**************************************************************************/ -/**************************************************************************/ - -int scan_for_int(FILE* ifp, const char* str, const int maxVal, - const int minVal) -/* - * Scans the file for a line matching string. Then, interprets - * everything after the equals sign as a single integer. - * Bounds checking is done on the value before returning. Value - * must be between the max and min; it can equal the max or min value. - * - * Certain ascii strings are checked for first (case is insignificant): - * - * String Retn_Value - * --------- -------------- - * INT_MAX, max, all INT_MAX - * INT_MIN, default INT_MIN - * N/A, Not Available INT_MIN - * - * Because this is a fixed format input file routine, errors are - * handled by terminally exiting the program. - */ -{ - int retn_value, defaultVal = INT_MIN; - char* tok_ptr[2]; - char input[MAX_INPUT_STR_LN + 1]; - if (scan_for_line(ifp, str, input, KEY_CHAR, PrintInputFile) < 0) { - exit(-1); - } - if (stokenize(input, DELIMITERS, tok_ptr, 2) == 1) { - if (interpret_int(tok_ptr[0], &retn_value, - maxVal, minVal, defaultVal)) { - if (outofbnds((double) retn_value, (double) maxVal, - (double) minVal)) { - fprintf(stderr, - "ERROR: scan_for_int outofbnds:\n\t\"%s\"\n",str); - fprintf(stderr,"\tmax = %d, min = %d\n", maxVal, minVal); - exit(-1); - } else { - return retn_value; - } - } - } - fprintf(stderr, "ERROR while processing line, \"%s\"\n", str); - exit(-1); /*NOTREACHED*/ -} -/**************************************************************************/ -/**************************************************************************/ -/**************************************************************************/ - -bool scan_for_boolean(FILE* ifp, const char* string) -/* - * Scans the file for a line matching string. Then, interprets - * everything after the equals sign as a single boolean value - * - * Because this is a fixed format input file routine, errors are - * handled by terminally exiting the program. - */ -{ - int ret_value; - char* tok_ptr[2]; - char input[MAX_INPUT_STR_LN + 1]; - if (scan_for_line(ifp, string, input, KEY_CHAR, PrintInputFile) < 0) { - exit(-1); - } - if (stokenize(input, DELIMITERS, tok_ptr, 2) == 1) { - if (interpret_boolean(tok_ptr[0], &ret_value, NO_DEFAULT_BOOLEAN)) { - if (ret_value == NO_DEFAULT_BOOLEAN) { - (void) fprintf(stderr, "scan_for_boolean: default not allowed\n"); - exit(-1); - } - return (ret_value != 0); - } - } - (void) fprintf(stderr, "scan_for_boolean: ERROR on line \"%s\"\n", string); - exit(-1); /*NOTREACHED*/ -} -/******************************************************************************/ -/******************************************************************************/ -/******************************************************************************/ - -double scan_for_double(FILE* ifp, const char* string, const double maxVal, - const double minVal) -/* - * Scans the file for a line matching string. Then, interprets - * everything after the equals sign as a single floating point number. - * Bounds checking is then done on the value before returning. Value - * must be between the max and min; it can equal the max or min. - * - * Useful values for bounds: - * DBL_MAX = largest legitimate value of a double ~ 2.0E-308 - * -DBL_MAX = smallest legitimate value of a double ~ 2.0E-308 - * DBL_EPSILON = smallest value of a double that can be added to one - * and produce a different number. ~ 2.0E-16 - * DBL_MIN = smallest value of a double ~ 2.0E-308 - * For example: - * If 0.0 is not a legitimate number for value, set min = DBL_MIN - * If value>=0.0 is legitimate, set min = 0.0 - * if value<=100., set max = 100. - * If no range checking is required, set max = DBL_MAX, min = -DBL_MAX - * - * Certain ascii strings are checked for first (case is insignificant): - * - * String Retn_Value - * --------- -------------- - * FLT_MAX, all FLT_MAX - * DBL_MAX, max DBL_MAX - * N/A, default -DBL_MAX - * small, DBL_MIN DBL_MIN - * DBL_EPSILON DBL_EPSILON - * - * Because this is a fixed format input file routine, errors are - * handled by terminally exiting the program. - */ -{ - double retn_value; - char* tok_ptr[2]; - char input[MAX_INPUT_STR_LN + 1]; - if (scan_for_line(ifp, string, input, KEY_CHAR, true) < 0) { - exit(-1); - } - if (stokenize(input, DELIMITERS, tok_ptr, 2) > 0) { - if (interpret_double(tok_ptr[0], &retn_value, maxVal, minVal, - NO_DEFAULT_DOUBLE)) { - if (retn_value == NO_DEFAULT_DOUBLE) { - (void) fprintf(stderr, - "ERROR: scan_for_double has no default\n"); - exit(-1); - } - if (outofbnds(retn_value, maxVal, minVal)) { - (void) fprintf(stderr, - "ERROR: scan_for_double outofbnds:\n \"%s = %e\"\n", - string, retn_value); - (void) fprintf(stderr,"\tmax = %e, min = %e\n", maxVal, minVal); - exit(-1); - } else { - return retn_value; - } - } - } - (void) fprintf(stderr, "ERROR scan_for_double: \"%s\"\n", string); - exit(-1); /*NOTREACHED*/ -} -/******************************************************************************/ -/******************************************************************************/ -/******************************************************************************/ - -char* scan_for_string(FILE* ifp, const char* string, const int maxVal, - const int minVal) -/* - * Scans the file for a line matching string. Then, interprets - * everything after the equals sign as string to be returned. - * Storage for the resulting string is malloced, and the address - * of the string is returned. - * The string is returned stripped of leading and trailing white space, - * and of comments. - * - * Length checking is then done on the number of characters returned. - * - * Because this is a fixed format input file routine, errors are - * handled by terminally exiting the program. - */ -{ - size_t len; - char input[MAX_INPUT_STR_LN + 1]; - if (scan_for_line(ifp, string, input, KEY_CHAR, PrintInputFile) < 0) { - exit(-1); - } - len = strlen(input); - if (outofbnds((double) len, (double) maxVal, (double) minVal)) { - (void) fprintf(stderr, - "ERROR: scan_for_string string length: \"%s = %s\"\n", string, input); - (void) fprintf(stderr, "\tlength max = %d, min = %d\n", maxVal, minVal); - exit(-1); - } - return copy_string(input); -} -/**************************************************************************/ -/**************************************************************************/ -/**************************************************************************/ - -int scan_for_line(FILE* ifp, const char* str, char input[], - const char ch_term, const int print_flag) -/* -* Scan the input file (reading in strings according to * 'read_string(ifp,)' -* specifications) until the character pattern in 'string' is matched. -* Returns all of the characters after the termination character in -* a null-character terminated string, -* -* Parameter list: -* -* ifp == pointer to file "input" -* string == contains string pattern to be matched. -* input == buffer array to hold characters that are read in. -* On output, it contains the return character string -* ch_term== Termination character. When scanning a line of input -* is read until either a newline, the 'ch' termination -* character is read, or the end-of-file is read. -* -* Output: -* This function returns the number of characters in the string input, -* excluding the null character. Error conditions are currently -* handled by returning with negative return values. -*/ -{ - int retn_value, i; - bool found = false; - char match_string[MAX_INPUT_STR_LN+1], - save_input[MAX_INPUT_STR_LN+1]; - static const char* ename = "ERROR scan_for_line: "; - - /* - * Error test the input match string - */ - - if (strlen(str) > MAX_INPUT_STR_LN) { - fprintf(stderr,"%sMatch string is too long:\n\t%s\n", - ename, str); - return -1; - } - - /* - * Make it an error to have the comment indicator in a - * match string - */ - - for (i = 0; i < (int) strlen(str); i++) { - if (str[i] == COM_CHAR || str[i] == COM_CHAR2) { - fprintf(stderr, "%s Comment in match string\n\t%s\n", - ename, str); - return -1; - } - } - - /* - * Strip the string of leading and trailing white space - */ - - if ((retn_value = strip(strcpy(match_string, str))) <= 0) { - fprintf(stderr, "%sMatch string is white space: \"%s\"\n", - ename, str); - return -1; - } - - /* - * Start the search for the string - */ - - do { - /* - * Read a chunk of text, either up to a newline or to the - * character ch_term, from the file pointer, ifp. - */ - - if ((retn_value = read_string(ifp, save_input, ch_term)) < 0) { - fprintf(stderr, - "%sEOF found in input file while searching for:\n", ename); - fprintf(stderr, "\t\"%s\"\n", match_string); - return retn_value; - } - - /* - * copy the string just read to the output string, and the - * strip it of leading and trailing white space, and comments. - * Then, compare the stripped input string with the stripped - * match_string - */ - - strcpy(input, save_input); - if (strip(input) > 0) { - found = strmatch(input, match_string); - } - - /* - * If requested print the line, including comments, on standard output. - * Use the retn_value from read_string to test whether a \n needs to - * be written. - */ - - if (print_flag) { - if (found) { - printf("->\t"); - } - if (retn_value == 0) { - printf("%s\n", save_input); - } else { - printf("%s%c", save_input, ch_term); - } - } - - /* - * Read and print the rest of the line, if we are in the middle of it. - */ - - if (retn_value > 0) { - if ((retn_value = read_line(ifp, input, print_flag)) < 0) { - fprintf(stderr, - "ERROR, EOF found in input file while reading line:\n"); - fprintf(stderr, "%s %c\n", str, ch_term); - return(retn_value); - } - } else { - input[0] = '\0'; - } - } while (!found); - return (retn_value); -} -/*****************************************************************************/ -/*****************************************************************************/ -/*****************************************************************************/ - -int read_line(FILE* ifp, char input[], const int print_flag) - -/* -* read_line: -* -* Reads a line of input. The line is -* printed to standard output, if print_flag is true. -* The line is returned in the character -* string pointed to by input. Leading and trailing white spaces are -* stripped from the line. -* The number of characters, excluding the null character, is -* returned, except when read_string encounters an error condition -* (negative return values). Then, the error condition is returned. -*/ -{ - int retn_value; - - /* - * read the file up to the next new line, read_string will return - * a 0 for a success, and a negative value for failure - */ - - retn_value = read_string(ifp, input, '\n'); - - /* - * Print out the line before stripping it of comments and white space - */ - - if (print_flag) { - printf("%s\n", input); - } - - /* - * Strip the return line of comments and leading/trailing white space - * Use the function strip to return the number of characters remaining - */ - - if (retn_value == 0) { - return strip(input); - } - - /* - * If an error condition occurred in read_string, return the error - * condition value instead of the character count - */ - - (void) strip(input); - return retn_value; -} -/**************************************************************************/ -/**************************************************************************/ -/**************************************************************************/ - -int read_string(FILE* ifp, char string[], const char ch) -/* - * This routine reads the standard input until encountering - * the end-of-file, a newline, the character 'ch' or until - * MAX_INPUT_STR_LN characters are read. The inputted characters - * are read into 'string'. - * If an EOF occurs, -1 is returned. - * If a line is longer than MAX_INPUT_STR_LN, a -2 is returned - * and an error message is written to standard error. - * string[] will be returned null-terminated with the - * first MAX_INPUT_STR_LN of the line. - * Upon successful completion with the read terminated by the - * character 'ch', the number of characters read plus 1 for the - * null character at the end of the string is returned. If the - * read is terminated by '\n', a 0 is returned, even if ch = '\n' - * - * - * Parameter list: - * - * ifp == pointer to file "input" - * string == On output, 'string' contains the characters read - * from the input stream. However, the termination character - * or the newline character is not included - * ch == Additional Termination character. That is, input function - * stops when 'ch' or '\n' is read. - */ -{ - int i = 0, rtn_value, new_ch; - - /* - * Read from the file, until termination conditions occur - * The order in the while statement is important. - */ - while ((i < MAX_INPUT_STR_LN) - && ((new_ch = getc(ifp)) != ch) - && (new_ch != '\n') - && (new_ch != EOF)) { - string[i++] = new_ch; - } - - /* - * Check for termination conditions - */ - if (new_ch == EOF) { - rtn_value = -1; - } else if (i == MAX_INPUT_STR_LN) { - fprintf(stderr, - "read_string ERROR: Maxed line character count, %d," - " before finding (%c)\n", MAX_INPUT_STR_LN, ch); - rtn_value = -2; - } else if (new_ch == '\n') { - rtn_value = 0; - } else { - rtn_value = i + 1; - } - - /* - * Make sure the string is null terminated and return - */ - string[i] = '\0'; - return rtn_value; -} -/**************************************************************************/ - -static bool interpret_boolean(const char* token, int* ret_value, - const int default_value) -/* -* This routine interprets a string token to be either true or false -* and then returns the appropriate answer as an int value in the -* variable ret_value. It is int because the default value may not -* be only 0 or 1 -*/ -{ - /* lower_case (token); */ - if (token[0] == '\0') { - *ret_value = default_value; - } else if (strlen(token) == 1) { - switch (token[0]) { - case 't': - case 'y': - *ret_value = true; - break; - case 'f': - case 'n': - *ret_value = false; - break; - default: - return false; - } - } else { - if (strmatch(token,"true") || strmatch(token,"yes")) { - *ret_value = true; - } else if (strmatch(token,"false") || strmatch(token,"no")) { - *ret_value = false; - } else if (strmatch(token,DEFAULT_STR) == 0) { - *ret_value = default_value; - } else { - return false; - } - } - return (true); -} -/**************************************************************************/ - -static bool interpret_int(const char* token, int* retn_value, - const int maxVal, const int minVal, - const int defaultVal) -/* -* This routine interprets a string token to be an integer -* and then returns the appropriate answer as an int value in the -* variable ret_value. -* Errors are indicated by returning false. Success is indicated -* by returning true. -* -* Certain ascii strings are checked for first (case is insignificant): -* -* String Retn_Value -* --------- -------------- -* INT_MAX, all INT_MAX -* INT_MIN INT_MIN -* max maxVal -* min minVal -* default defaultVal -* NULL string defaultVal -* N/A, Not_Available INT_MIN -*/ -{ - int retn; - - /* - * Allow a few key ascii phrases in place of an actual int - */ - - /* lower_case(token); */ - if (token[0] == '\0') { - *retn_value = defaultVal; - } else if ((strmatch(token,"all")) || strmatch(token,"int_max")) { - *retn_value = INT_MAX; - } else if (strmatch(token,"int_min")) { - *retn_value = INT_MIN; - } else if (strmatch(token,"max")) { - *retn_value = maxVal; - } else if (strmatch(token,"min")) { - *retn_value = minVal; - } else if (strmatch(token,DEFAULT_STR)) { - *retn_value = defaultVal; - } else if (strmatch(token,"n/a") || strmatch(token,"not_available")) { - *retn_value = INT_MIN; - } else { - if ((retn = sscanf(token, "%d", retn_value)) != 1) { - *retn_value = retn; - return false; - } - } - return true; -} -/******************************************************************************/ -/******************************************************************************/ -/******************************************************************************/ - -static bool interpret_double(const char* token, double* retn_value, - const double maxVal, const double minVal, - const double defaultVal) -/* -* This routine interprets a string token to be a double -* and then returns the appropriate answer as a double value in the -* variable retn_value. -* The function itself returns true if successful or false if unsuccessful -* -* -* Certain ascii strings are checked for first (case is insignificant): -* -* String Retn_Value -* --------- -------------- -* FLT_MAX, all FLT_MAX -* FLT_MIN FLT_MIN -* DBL_MAX DBL_MAX -* max maxVal -* min minVal -* default defaultVal -* NULL string defaultVal -* N/A -DBL_MAX -* small, DBL_MIN DBL_MIN -* DBL_EPSILON DBL_EPSILON -* -* DBL_MAX = largest legitimate value of a double ~ 2.0E-308 -* -DBL_MAX = smallest legitimate value of a double ~ - 2.0E-308 -* DBL_EPSILON = smallest value of a double that can be added to one -* and produce a different number. ~ 2.0E-16 -* DBL_MIN = tiniest value of a double ~ 2.0E-308 -*/ -{ - int retn; - double retn_float; - - /* - * Allow a few key ascii phrases in place of an actual float - */ - - /* lower_case(token); */ - if (token[0] == '\0') { - *retn_value = defaultVal; - } else if ((strmatch(token,"all")) || strmatch(token,"flt_max")) { - *retn_value = FLT_MAX; - } else if (strmatch(token,"flt_min")) { - *retn_value = FLT_MIN; - } else if (strmatch(token,"dbl_max")) { - *retn_value = DBL_MAX; - } else if (strmatch(token,"max")) { - *retn_value = maxVal; - } else if (strmatch(token,"min")) { - *retn_value = minVal; - } else if (strmatch(token,"n/a")) { - *retn_value = -DBL_MAX; - } else if (strmatch(token, DEFAULT_STR)) { - *retn_value = defaultVal; - } else if (strmatch(token,"small") || strmatch(token,"dbl_min")) { - *retn_value = DBL_MIN; - } else if (strmatch(token,"dbl_epsilon")) { - *retn_value = DBL_EPSILON; - } else { - if ((retn = sscanf(token, "%le", &retn_float)) != 1) { - *retn_value = (double) retn; - return false; - } else { - *retn_value = (double) retn_float; - } - } - return true; -} -/******************************************************************************/ -/******************************************************************************/ -/******************************************************************************/ - -int strip(char str[]) -/* -* This routine strips off blanks and tabs (only leading and trailing -* characters) in 'str'. On return, it returns the number of -* characters still included in the string (excluding the null character). -* -* Comments are excluded -> All instances of the comment character, '!', -* are replaced by '\0' thereby terminating -* the string -* -* Parameter list: -* -* str == On output 'str' contains the same characters as on -* input except the leading and trailing white space and -* comments have been removed. -*/ -{ - int i = 0, j = 0; - char ch; - - /* - * Quick Returns - */ - - if ((str == NULL) || (str[0] == '\0')) { - return 0; - } - - /* Find first non-space character character */ - - while (((ch = str[i]) != '\0') && isspace(ch)) { - i++; - } - - /* - * Move real part of str to the front by copying the string - * - Comments are handled here, by terminating the copy at the - * first comment indicator, and inserting the null character at - * that point. - */ - - while ((ch = str[j+i]) != '\0' && - (ch != COM_CHAR) && - (ch != COM_CHAR2)) { - str[j] = ch; - j++; - } - str[j] = '\0'; - j--; - - /* Remove trailing white space by inserting a null character */ - - while ((j != -1) && isspace(str[j])) { - j--; - } - j++; - str[j] = '\0'; - return j; -} -/**************************************************************************/ -/**************************************************************************/ -/**************************************************************************/ - -void lower_case(char str[]) -/* -* lower_case: -* Translates a string delimited by a NULL character -* to lower case. There is no error checking in this version. -* Relies on stlib function, tolower. -*/ -{ - int i; - for (i = 0; i < (int) strlen(str); i++) { -# if defined(_INCLUDE_XOPEN_SOURCE) && ! defined(__lint) - str[i] = _tolower((str[i])); -# else - str[i] = tolower(str[i]); -# endif - } -} -/******************************************************************************/ -/******************************************************************************/ -/******************************************************************************/ - -char* TokToStrng(const TOKEN* keyptr) -/* -* TokToStrng: -* Mallocs a new character string and copies -* the tokens character string to it, appending all tokens together -* into a single string separated by a single space character. -* It returns the pointer to the new string; -* The new string should be freed when no longer needed. -*/ -{ - int i; - if (!keyptr) { - return NULL; - } - if (!keyptr->orig_str) { - return NULL; - } - size_t iln = strlen(keyptr->orig_str) + 1 + keyptr->ntokes; - char* fstr = (char*) malloc(iln * sizeof(char)); - - char* const* str = &(keyptr->tok_ptr[0]); - for (i = 0, fstr[0]= '\0'; i < (keyptr->ntokes - 1); i++, str++) { - (void) strcat(strcat(fstr, *str), " "); - } - return strcat(fstr, *str); -} -/******************************************************************************/ -/******************************************************************************/ -/******************************************************************************/ - -int stokenize(char* string, const char* delimiters, char* tok_ptr[], - const int max_tokens) -/* - * stokenize - * - * This function will break up a string into its respective "tokens". - * It returns the number of tokens found. See the strtok(3) man page. - * - * input - * ---------- - * string - String to be tokenized. Note, that the string is - * changed by this procedure. Null characters are - * put between each symbol. - * delimiters - String containing a list of delimiters. - * The example below covers 'white space' - * e.g., char *delimiters = " \t\n"; - * max_tokens - Maximum number of tokens to be found - * - * output - * ----------- - * tok_ptr - Vector of pointers to strings, that contain the input - * string's tokens - */ -{ - int i = 0; - if (string == NULL) { - tok_ptr[0] = NULL; - return 0; - } - if (strlen(string) == 0) { - tok_ptr[0] = string; - return 0; - } - if ((tok_ptr[0] = strtok(string, delimiters)) != NULL) { - do { - if ((++i) == max_tokens) { - break; - } - } while ((tok_ptr[i] = strtok(NULL, delimiters)) != NULL); - } - return i; -} -/*****************************************************************************/ -/*****************************************************************************/ -/*****************************************************************************/ - -static bool outofbnds(const double value, const double maxVal, - const double minVal) -/* - * This routine checks the bounds of a single double value. - * If it is inside or on the bounds, it returns false. - * If it is outside the bounds, it returns true. - */ -{ - if ((value <= maxVal) && (value >= minVal)) { - return false; - } - return true; -} -/****************************************************************************** - * - * strmatch(): - * - * This routine checks whether one string is the same as another. - * Upper case is transformed into lower case before the comparison is done. - * Thus, case doesn't matter in the comparison. However, white space - * does matter in this comparison. - * If they are, it returns true - * If they aren't, it returns false - */ -bool strmatch(const char* s1, const char* s2) -{ - while (*s1 != '\0') { -# if defined (_INCLUDE_XOPEN_SOURCE) && ! defined(__lint) - if (_tolower((*s1++)) != _tolower((*s2++))) { - return false; - } -# else - if (tolower(*s1) != tolower(*s2)) { - return false; - } - s1++; - s2++; -# endif - } - if (*s2 != '\0') { - return false; - } - return true; -} - -/***************************************************************************** - * - * strstrmatch(): - * - * This routine checks whether two strings are the same modulo differences - * in their white space - */ -bool strstrmatch(const char* s1, const char* s2) -{ - struct TOKEN tmpKeyStruct1, tmpKeyStruct2; - fillTokStruct(&tmpKeyStruct1, s1); - fillTokStruct(&tmpKeyStruct2, s2); - return toktokmatch(&tmpKeyStruct2, &tmpKeyStruct1); -} - -/******************************************************************************* - * - * strtokmatch(): - * - * This routine checks whether a string matches the string contained in - * the tokens of a keyLineStr. - * White space and case are ignored. - * If they are, it returns true - * If they aren't, it returns false - */ -bool strtokmatch(const TOKEN* keyptr, const char* s2) -{ - struct TOKEN tmpKeyStruct; - fillTokStruct(&tmpKeyStruct, s2); - return toktokmatch(keyptr, &tmpKeyStruct); -} - -/************************************************************************** - * - * toktokmatch() - * - * This routine checks whether two TOKEN structures contain the - * same data up to differences in white space. - * Case is ignored as well, as strmatch is called. - */ -bool toktokmatch(const TOKEN* keyptr1, const TOKEN* keyptr2) -{ - int i = keyptr1->ntokes; - if (i != keyptr2->ntokes) { - return false; - } - while (i > 0) { - i--; - if (!strmatch(keyptr1->tok_ptr[i], keyptr2->tok_ptr[i])) { - return false; - } - } - return true; -} - -/**************************************************************************/ -/* - * fillTokStruct() - * - * Fill in a keyLineStruct with a string. Use the defn of white space - * at the start of the file to tokenize the string, storing it in the - * TOKEN structure. - */ -void fillTokStruct(TOKEN* keyptr1, const char* s2) -{ - if (keyptr1 == NULL) { - return; - } - if (keyptr1->orig_str) { - free(keyptr1->orig_str); - } - if (keyptr1->tok_str) { - free(keyptr1->tok_str); - } - if (s2 == NULL) { - keyptr1->orig_str = copy_string(""); - keyptr1->tok_str = copy_string(""); - keyptr1->ntokes = 0; - keyptr1->tok_ptr[0] = keyptr1->orig_str; - return; - } - keyptr1->orig_str = copy_string(s2); - keyptr1->tok_str = copy_string(s2); - keyptr1->ntokes = stokenize(keyptr1->tok_str, DELIMITERS, keyptr1->tok_ptr, - MAXTOKENS); -} - -/****************************************************************************** - * - * copyTokStruct(): - * - * Copies the information stored in keyptr2 into keyptr1 - */ -void copyTokStruct(TOKEN* keyptr1, const TOKEN* keyptr2) -{ - if (keyptr1 == NULL) { - return; - } - if (keyptr2 == NULL) { - return; - } - if (keyptr1->orig_str) { - free(keyptr1->orig_str); - } - if (keyptr1->tok_str) { - free(keyptr1->tok_str); - } - if (keyptr2->orig_str == NULL) { - keyptr1->orig_str = copy_string(""); - keyptr1->tok_str = copy_string(""); - keyptr1->ntokes = 0; - keyptr1->tok_ptr[0] = keyptr1->orig_str; - return; - } - keyptr1->orig_str = copy_string(keyptr2->orig_str); - keyptr1->tok_str = copy_string(keyptr2->orig_str); - keyptr1->ntokes = stokenize(keyptr1->tok_str, DELIMITERS, keyptr1->tok_ptr, - MAXTOKENS); -} - -/****************************************************************************** - * - * in_char_list(): - * - * Finds a match of one string against a list of strings. Returns - * the position that the first match occurred. - * If no match occurred, returns -1. - * The comparisons ignore differences in white space. - */ -int in_char_list(const char* const str1, const char** const list, - int num_list) -{ - int i; - for (i = 0; i < num_list; i++) if (strstrmatch(str1, list[i])) { - return i; - } - return -1; -} -/*****************************************************************************/ -/*****************************************************************************/ -/*****************************************************************************/ - -char* copy_string(const char* string) -/* -* copy_string: -* Mallocs a new character string and copies the old string to it -* -* NOTE: Memory leak may result if the calling program doesn't free -* the malloced space -*/ -{ - char* new_string; - new_string = (char*) malloc(strlen(string)+1); - if (new_string == NULL) { - (void) fprintf(stderr, "copy_string ERROR: malloc returned NULL"); - } else { - (void) strcpy(new_string, string); - } - return new_string; -} - -/****************************************************************************** - * - * strip_item_from_token (): - * - * Change the token by taking eliminating the iword'th token from the token - * structure and reformulating the token expression - */ -void strip_item_from_token(int iword, TOKEN* tok) -{ - if (!tok) { - return; - } - if (iword < 0 || iword > tok->ntokes) { - return; - } -#ifdef _MSC_VER - __w64 size_t ioffset = tok->tok_ptr[iword] - tok->tok_str; -#else - size_t ioffset = tok->tok_ptr[iword] - tok->tok_str; -#endif - size_t ilength = strlen(tok->tok_ptr[iword]); -#ifdef _MSC_VER - __w64 size_t i = ioffset; - __w64 size_t j = ioffset + ilength; -#else - size_t i = ioffset; - size_t j = ioffset + ilength; -#endif - if (j <= strlen(tok->orig_str)) { - while (tok->orig_str[j] != '\0') { - tok->orig_str[i] = tok->orig_str[j]; - i++; - j++; - } - tok->orig_str[i] = '\0'; - } - strcpy(tok->tok_str, tok->orig_str); - tok->ntokes = stokenize(tok->tok_str, DELIMITERS, tok->tok_ptr, - MAXTOKENS); -} - -/*****************************************************************************/ diff --git a/src/apps/tok_input_util.h b/src/apps/tok_input_util.h deleted file mode 100644 index 97910ef50..000000000 --- a/src/apps/tok_input_util.h +++ /dev/null @@ -1,84 +0,0 @@ -#ifndef TOK_INPUT_UTIL_H -#define TOK_INPUT_UTIL_H - -#include "stdio.h" - -#ifndef MAX_INPUT_STR_LN -#define MAX_INPUT_STR_LN 16100 -#endif -#ifndef MAX_TOKEN_STR_LN -#define MAX_TOKEN_STR_LN 255 -#endif -#ifndef MAXTOKENS -#define MAXTOKENS 20 -#endif - -/**************************************************************************/ -/* TOKEN structure: - * This structure is used to parse strings. The original string is - * tokenized into a set of tokens via separation wrt white space. - * Both the tokens and the original - * string are stored within the structure - */ - -struct TOKEN { - char* orig_str; - char* tok_str; - char* tok_ptr[MAXTOKENS]; - int ntokes; - TOKEN(void); - ~TOKEN(); - TOKEN(const char* str); -}; - - -#define NO_DEFAULT_INT -68361 -#define NO_DEFAULT_BOOLEAN NO_DEFAULT_INT -#define NO_DEFAULT_DOUBLE -1.241E+11 -#define NO_DEFAULT_STR "NO_DEFAULT" - - -/**************************************************************************/ -/* Prototypes for routines tok_input_util.c */ -/**************************************************************************/ - -extern bool get_next_keyLine(FILE*, TOKEN*, TOKEN*); -extern int tok_to_int(const TOKEN*, const int, const int, - const int, bool*); -extern int str_to_int(const char*, const int, const int, - const int, bool*); -extern double tok_to_double(const TOKEN*, const double, const double, - const double, bool*); -extern double str_to_double(const char*, const double, const double, - const double, bool*); -extern bool tok_to_boolean(const TOKEN*, const int, bool*); -extern bool str_to_boolean(const char*, const int, bool*); -extern char* tok_to_string(const TOKEN*, const int, const int, - const char*, bool*); -extern char* str_to_string(const char*, const char*, bool*); -extern int scan_for_int(FILE*, const char*, const int, const int); -extern double scan_for_double(FILE*, const char*, const double, - const double); -extern char* scan_for_string(FILE*, const char*, const int, const int); -extern bool scan_for_boolean(FILE*, const char*); -extern int scan_for_line(FILE*, const char*, char [], const char, - const int); -extern int read_line(FILE*, char [], const int); -extern int read_string(FILE*, char [], const char); -extern int strip(char []); -extern void lower_case(char []); -extern char* TokToStrng(const TOKEN*); -extern int stokenize(char*, const char*, char *[], const int); -extern bool strmatch(const char*, const char*); -extern bool strstrmatch(const char*, const char*); -extern bool strtokmatch(const TOKEN*, const char*); -extern bool toktokmatch(const TOKEN*, const TOKEN*); -extern void fillTokStruct(TOKEN*, const char*); -extern void copyTokStruct(TOKEN*, const TOKEN*); -extern int in_char_list(const char* const, const char** const, int); -extern char* copy_string(const char*); -extern void strip_item_from_token(int, TOKEN*); -/**************************************************************************/ -#endif /* END OF TOK_INPUT_UTIL_H */ -/**************************************************************************/ -