//----------------------------------------------------------------------------- // File: BioE2151String.cpp // Class: None // Parent: None // Children: None // Purpose: String Module for SimTK originally created for Lab 1 //----------------------------------------------------------------------------- // The following are standard C/C++ header files. // If a filename is enclosed inside < > it means the header file is in the Include directory. // If a filename is enclosed inside " " it means the header file is in the current directory. #include // Character Types #include // Mathematical Constants #include // Variable Argument Lists #include // Standard Input/Output Functions #include // Utility Functions #include // String Operations #include // Signals (Contol-C + Unix System Calls) #include // Nonlocal Goto (For Control-C) #include // Time and Date information #include // Verify Program Assertion #include // Error Codes (Used in Unix system()) #include // Floating Point Constants #include // Implementation Constants #include // Standard Definitions #include // Exception handling (e.g., try, catch throw) #include "BioE215StringModule.h" // Additional String Functions created for BioE215#include "BioE215String.h" // Character Types bool GetStringFromKeyboard( char inputString[], unsigned long sizeOfString ) { return GetStringFromFile(inputString, sizeOfString, stdin); } bool GetStringFromFile( char inputString[], unsigned long sizeOfString, FILE *fprt ) { char * returncode; bool error = false; returncode = fgets(inputString, sizeOfString, stdin); if(returncode != NULL) { error = true; } return error; } bool WriteStringToScreen( const char outputString[] ) { return WriteStringToFile(outputString, stdout); } //----------------------------------------------------------------------------- bool WriteStringToFile( const char outputString[], FILE *fptr ) { int errorcode = fprintf(stdout, "%s", outputString); bool error = false; // Fprint F returns number of chars written if successful and a negative number if it fails. if (errorcode > 0) error = true; return error; } //----------------------------------------------------------------------------- const char* ConvertStringToDouble( const char *s, double &returnValue, double defaultValue ) { char* errorcode = ""; char* endpointer; double *pointer = &returnValue; double convertedValue; convertedValue = strtod(s, &endpointer); if( (convertedValue == 0.0) || (convertedValue == HUGE_VAL) ) { fprintf(stdout, "Conversion to Double Failed in strtod function.\n"); *pointer = defaultValue; } else { *pointer = convertedValue; } return errorcode; } //----------------------------------------------------------------------------- const char* ConvertStringToLong( const char *s, long int &returnValue, long int defaultValue ) { char* errorcode = ""; long int *pointer = &returnValue; long int convertedValue; convertedValue = atol(s); if( (convertedValue == INT_MAX) || (convertedValue == INT_MAX) ) { fprintf(stdout, "Conversion to Double Failed in strtod function.\n"); *pointer = defaultValue; } else { *pointer = convertedValue; } return errorcode; } //----------------------------------------------------------------------------- bool WriteDoubleToFileWithEExponentInSpecifiedPrecision( double x, int precision, FILE *fptr) { char *formatLeadIn = " %- "; char *formatLeadOut = "E "; int fieldwidth = precision + 5; bool returnValue; char writebuffer[10] = ""; sprintf(writebuffer, "%d.%d", fieldwidth, precision); char finalArray[30]; strcpy(finalArray, formatLeadIn); strcat(finalArray, writebuffer); strcat(finalArray, formatLeadOut); int errorcode = fprintf(fptr, finalArray, x); (errorcode > 0) ? returnValue = true : returnValue = false; return returnValue; } //----------------------------------------------------------------------------- bool WriteDoubleToFileInSpecifiedPrecision( double x, int precision, FILE *fptr) { char *formatLeadIn = " %- "; char *formatLeadOut = "f "; int fieldwidth = precision + 5; bool returnValue; char writebuffer[10] = ""; sprintf(writebuffer, "%d.%d", fieldwidth, precision); char finalArray[30]; strcpy(finalArray, formatLeadIn); strcat(finalArray, writebuffer); strcat(finalArray, formatLeadOut); int errorcode = fprintf(fptr, finalArray, x); (errorcode > 0) ? returnValue = true : returnValue = false; return returnValue; }