// toolsrecord.cpp Implementation file for ToolsRecord class /******************************************************************************* // MODULE NAME: ToolsRecord // INTERFACE FILE: toolsrecord.h // IMPLEMENTATION FILE: toolsrecord.cpp // // PURPOSE: To provide a ToolsRecord type useful in implementing the BAL program // which allows the user to create a record, update a field in the record, print // the current value of a field in the record, test the validity of a field in // the record, compare one record to another to determine sorting order, and // produce strings with the record information in them which are formatted for // output either to standard output or to a BAM file. // // FUNCTIONS: Name Purpose // ToolsRecord Paramterless constructor // ToolsRecord Constructor w/specified Tools Index // LessThan Test whether this record should precede another // GreaterThan Test whether this record should follow another // Equals Show whether this record has the same index as // another one // fieldOK Determine if anything is wrong with a field of // the record // FieldContains Print the contents of the field // FieldDefault Print the default contents of a field // FieldLimits Print the limits in effect on the field // FieldLabel Print the correct label for the field // FieldWidth Print the correct width for the field // IndexIs Indicate the index of the record // NewIndex Change the index of a record // Update Change the contents of a field // Reset Reset the value of a field to its default // Reset Reset all fields to their defaults // Replace Change all the fields of a record to the values // of all the fields of another record // List Create a string which holds the record's // contents formatted for on-screen viewing // BriefList Create string w/abbreviated record contents // Print Create a string which holds the record's // contents formatted for BAM file viewing // // // AUTHOR: Amy Langill *2B DATE: 3/23/1999 *******************************************************************************/ #include "toolsrecord.h" /********************************CONSTRUCTORS**********************************/ /******************************************************************************* // FUNCTION NAME: ToolsRecord // // DESCRIPTION OF FUNCTION: Parameterless Constructor // // DESCRIPTION OF ALGORITHM: Create an object of class ToolsRecord with default // values in all the fields by initializing each field to the appropriate // defaults with the member functions of the field types // // CALLED BY: client // CALLS: LimitInt::setLimit,LimitInt::SetSize,BALString::SetSize, // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: A ToolsRecord object has been created w/the appropriate // default field values // // AUTHOR: Amy Langill *2B DATE: 3/23/1999 *******************************************************************************/ ToolsRecord::ToolsRecord() { // Initialize tdex: tdex.setLimit(GREATERTHAN,T_TDEX_LIMIT); tdex.SetSize(T_TDEX_WIDTH); // Initialize shoes: shoes.SetSize(T_SHOES_WIDTH); // Initialize glove: glove.SetSize(T_GLOVE_WIDTH); // Initialize other: other.SetSize(T_OTHER_WIDTH); } /******************************************************************************* // FUNCTION NAME: ToolsRecord // // DESCRIPTION OF FUNCTION: Constructor w/specified Tools Index // // DESCRIPTION OF ALGORITHM: Creates an object of class ToolsRecord w/user- // specified index and default values for all other fields by calling the // appropriate member functions of each of the field types // // CALLED BY: client // CALLS: LimitInt::setLimit,LimitInt::SetSize,LimitInt::Store, // BALString::SetSize, // // PARAMETERS: / in / toolIndex - int value of desired index // // PRECONDITIONS: none // // POSTCONDITIONS: A new ToolsRecord has been created such that the value of // bdex is the specified toolIndex and all other fields contain default values // // AUTHOR: Amy Langill *2B DATE: 3/23/1999 *******************************************************************************/ ToolsRecord::ToolsRecord(int toolIndex) { // Initialize tdex: tdex.setLimit(GREATERTHAN,T_TDEX_LIMIT); tdex.SetSize(T_TDEX_WIDTH); tdex.Store(toolIndex); // Initialize shoes: shoes.SetSize(T_SHOES_WIDTH); // Initialize glove: glove.SetSize(T_GLOVE_WIDTH); // Initialize other: other.SetSize(T_OTHER_WIDTH); }; /*********************************OBSERVERS************************************/ /******************************************************************************* // FUNCTION NAME: LessThan // // DESCRIPTION OF FUNCTION: Test whether this record should precede another // // DESCRIPTION OF ALGORITHM: Uses the LessThan member function of LimitInt to // compare the indices of the two records // // CALLED BY: client // CALLS: LimitInt::LessThan // // PARAMETERS: / in / another - ToolsRecord this one is being compared to // // PRECONDITIONS: none // // POSTCONDITIONS: Returns true if the index of this record is less than that // of another record // // AUTHOR: Amy Langill *2B DATE: 3/23/1999 *******************************************************************************/ bool ToolsRecord::LessThan(ToolsRecord another) { return (tdex.LessThan(another.tdex)); } /******************************************************************************* // FUNCTION NAME: GreaterThan // // DESCRIPTION OF FUNCTION: Test whether this record should follow another one // // DESCRIPTION OF ALGORITHM: Using the GreaterThan member function of LimitInt, // compares the indices of the two records against each other. // // CALLED BY: client // CALLS: LimitInt::GreaterThan // // PARAMETERS: / in / another - ToolsRecord this one is being compared to // // PRECONDITIONS: none // // POSTCONDITIONS: if the index of this record is greater than that of the // other record, returns true. Returns false otherwise. // // AUTHOR: Amy Langill *2B DATE: 3/23/1999 *******************************************************************************/ bool ToolsRecord::GreaterThan(ToolsRecord another) { return (tdex.GreaterThan(another.tdex)); } /******************************************************************************* // FUNCTION NAME: Equals // // DESCRIPTION OF FUNCTION: Test whether this record has the same index value // as another record // // DESCRIPTION OF ALGORITHM: Using the Equals member function of LimitInt, // compares the index of this record with the index of another // // CALLED BY: client // CALLS: LimitInt::Equals // // PARAMETERS: / in / another - ToolsRecord this one is being compared to // // PRECONDITIONS: none // // POSTCONDITIONS: If the indices of the two records are exactly equal, returns // true. Returns false otherwise. // // AUTHOR: Amy Langill *2B DATE: 3/23/1999 *******************************************************************************/ bool ToolsRecord::Equals(ToolsRecord another) { return (tdex.Equals(another.tdex)); } /******************************************************************************* // FUNCTION NAME: fieldOK // // DESCRIPTION OF FUNCTION: Detect whether there's an error in a field // // DESCRIPTION OF ALGORITHM: Depending upon the fieldToCheck variable, function // calls the appropriate error-checking function for the type of field in // question and returns the type of error detected in the field // // CALLED BY: client // CALLS: LimitInt::isValid // // PARAMETERS: / in / fieldToCheck - ToolsFieldType that indicates which field // of the record is to be checked for errors // // PRECONDITIONS: none // // POSTCONDITIONS: Returns the type of error found in the given field, if any // // AUTHOR: Amy Langill *2B DATE: 3/23/1999 *******************************************************************************/ DataErrorType ToolsRecord::fieldOK(ToolsFieldType fieldToCheck) { DataErrorType error=NO_PROB; if (fieldToCheck==T_TDEX) // Only need to check the index field { if (!(tdex.isValid())) error=INDEX_RANGE_ERR; } return error; // Tell calling function status of that field } /******************************************************************************* // FUNCTION NAME: FieldContains // // DESCRIPTION OF FUNCTION: Report the contents of a given field // // DESCRIPTION OF ALGORITHM: Depending on the value of fieldToPrint, calls the // Print member function of that field's type // // CALLED BY: Replace, client // CALLS: LimitInt::Print,BALString::Print // // PARAMETERS: / in / fieldToPrint - ToolsFieldType that specifies which field // is to be printed to the output string // // PRECONDITIONS: none // // POSTCONDITIONS: A string is returned which contains the current value of the // specified field and is the same width as that field's print size // // AUTHOR: Amy Langill *2B DATE: 3/23/1999 *******************************************************************************/ string ToolsRecord::FieldContains(ToolsFieldType fieldToPrint) { string fieldString; // hold the string to report back switch (fieldToPrint) // Call the print function for the appropriate field { case T_TDEX: fieldString = tdex.Print(); break; case T_SHOES: fieldString = shoes.Print(); break; case T_GLOVE: fieldString = glove.Print(); break; case T_OTHER: fieldString = other.Print(); break; } return fieldString; // Return the string with the field info in it } /******************************************************************************* // FUNCTION NAME: FieldDefault // // DESCRIPTION OF FUNCTION: Print the default contents of the specified field // // DESCRIPTION OF ALGORITHM: Depending on the value of fieldToCheck, calls the // Default member function for that field's type // // CALLED BY: client // CALLS: LimitInt::Default,BALString::Default // // PARAMETERS: / in / fieldToCheck - ToolsFieldType that indicates which // field's default string is to be returned // // PRECONDITIONS: none // // POSTCONDITIONS: A string containing the default value of the specified field // which has the appropriate width for that field is returned // // AUTHOR: Amy Langill *2B DATE: 3/23/1999 *******************************************************************************/ string ToolsRecord::FieldDefault(ToolsFieldType fieldToCheck) { string defaultString; switch (fieldToCheck) // Call the Default() function for the appropriate field { case T_TDEX: defaultString = tdex.Default(); break; case T_SHOES: defaultString = shoes.Default(); break; case T_GLOVE: defaultString = glove.Default(); break; case T_OTHER: defaultString = other.Default(); break; } return defaultString; // Return the string with the field info in it } /******************************************************************************* // FUNCTION NAME: FieldLimits // // DESCRIPTION OF FUNCTION: Create a string that has the info for a field's // constraints. // // DESCRIPTION OF ALGORITHM: Depending on the value of fieldToCheck, calls the // Limits member function of the types that have limits or returns the string // literal "No Limits" for the BALString type fields // // CALLED BY: client // CALLS: LimitInt::Limits // // PARAMETERS: / in / fieldToCheck - ToolsFieldType that indicates which // field's limits are to be output // // PRECONDITIONS: none // // POSTCONDITIONS: A string containing a message about the limits in effect // on the specified field is returned. // // AUTHOR: Amy Langill *2B DATE: 3/23/1999 *******************************************************************************/ string ToolsRecord::FieldLimits(ToolsFieldType fieldToCheck) { string limitString; switch (fieldToCheck) { case T_TDEX: // Only this field has a limit limitString = tdex.Limits(); // break; // case T_SHOES: // Strings don't have any constraints on them case T_GLOVE: // case T_OTHER: // limitString = "No limits"; // break; } return limitString; // Report on the limits for that field } /******************************************************************************* // FUNCTION NAME: FieldLabel // // DESCRIPTION OF FUNCTION: Report the label of a given field // // DESCRIPTION OF ALGORITHM: Depending on the value of fieldToPrint, returns // the named constant which contains that field's label // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / fieldToPrint - ToolsFieldType that specifies which field // is being addressed // // PRECONDITIONS: none // // POSTCONDITIONS: A string is returned which contains the label as defined in // the record type's interface file. // // AUTHOR: Amy Langill *2B DATE: 3/28/1999 *******************************************************************************/ string ToolsRecord::FieldLabel(ToolsFieldType fieldToPrint) { string label; switch (fieldToPrint) { case T_TDEX: label = T_TDEX_LABEL; break; case T_SHOES: label = T_SHOES_LABEL; break; case T_GLOVE: label = T_GLOVE_LABEL; break; case T_OTHER: label = T_OTHER_LABEL; break; } return label; } /******************************************************************************* // FUNCTION NAME: FieldWidth // // DESCRIPTION OF FUNCTION: Reports the print size of the indicated field // // DESCRIPTION OF ALGORITHM: Depending on the value of fieldToCheck, returns // the named constant that represents that field's width // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / fieldToCheck - ToolsFieldType that indicates which part // of the record is being accessed // // PRECONDITIONS: none // // POSTCONDITIONS: An int is returned which indicates the defined width of the // desired field as specified by fieldToCheck. // // AUTHOR: Amy Langill *2B DATE: 3/28/1999 *******************************************************************************/ int ToolsRecord::FieldWidth(ToolsFieldType fieldToCheck) { int width=0; switch (fieldToCheck) { case T_TDEX: width = T_TDEX_WIDTH; break; case T_SHOES: width = T_SHOES_WIDTH; break; case T_GLOVE: width = T_GLOVE_WIDTH; break; case T_OTHER: width = T_OTHER_WIDTH; break; } return width; } /******************************************************************************* // FUNCTION NAME: IndexIs // // DESCRIPTION OF FUNCTION: Reports the index of the record // // DESCRIPTION OF ALGORITHM: Calls the LimitInt::valueIs function for tdex // // CALLED BY: client // CALLS: LimitInt::valueIs // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: The current value of the record's index is returned. // // AUTHOR: Amy Langill *2B DATE: 3/23/1999 *******************************************************************************/ int ToolsRecord::IndexIs() { return tdex.valueIs(); } /********************************TRANSFORMERS**********************************/ /******************************************************************************* // FUNCTION NAME: NewIndex // // DESCRIPTION OF FUNCTION: Changes the index of the record to a new value // // DESCRIPTION OF ALGORITHM: Assigns the newIndexValue to tdex with the // LimitInt::Store function // // CALLED BY: client // CALLS: LimitInt::Store // // PARAMETERS: / in / newIndexValue - int that tells what new index should be // // PRECONDITIONS: none // // POSTCONDITIONS: value of tdex has been changed to newIndexValue // // AUTHOR: Amy Langill *2B DATE: 3/28/1999 *******************************************************************************/ void ToolsRecord::NewIndex(int newIndexValue) { tdex.Store(newIndexValue); return; } /******************************************************************************* // FUNCTION NAME: Update // // DESCRIPTION OF FUNCTION: Change the value of a specified field to the value // contained in the dataString // // DESCRIPTION OF ALGORITHM: Depending on the field to be updated, as indicated // by fieldToChange, function converts the string to a different type if // necessary and calls the Store member function of the field's type // // CALLED BY: Replace, client // CALLS: LimitInt::Store,BALString::Store // // PARAMETERS: / in / dataString - string that contains the value to be stored // in the field // / in / fieldToChange - ToolsFieldType that indicates the field // to be changed to a new value // // PRECONDITIONS: dataString contains a value that can be converted to the // appropriate data type without difficulty // // POSTCONDITIONS: The field indicated by fieldToChange has been modified to // the value in dataString // // AUTHOR: Amy Langill *2B DATE: 3/23/1999 *******************************************************************************/ void ToolsRecord::Update(string dataString,ToolsFieldType fieldToChange) { istringstream data(dataString); // For converting from string type int newIntValue; // Hold converted int data switch (fieldToChange) // Store the data based on the field to be changed { case T_TDEX: data >> newIntValue; // Convert to int and store tdex.Store(newIntValue); // into tdex break; case T_SHOES: // No conversion, just store into shoes.Store(dataString); // the right field break; case T_GLOVE: // No conversion, just store into glove.Store(dataString); // the right field break; case T_OTHER: other.Store(dataString); // No conversion, just store into break; // the right field } return; } /******************************************************************************* // FUNCTION NAME: Reset // // DESCRIPTION OF FUNCTION: Reset the value of a specified field of the record // to its default value // // DESCRIPTION OF ALGORITHM: Depending on the value of fieldToChange, calls the // appropriate resetting member function of each field's type // // CALLED BY: client // CALLS: LimitInt::ResetValue,BALString::ResetValue, // // PARAMETERS: / in / fieldToChange - ToolsFieldType that indicates which field // is to be changed to its default value // // PRECONDITIONS: none // // POSTCONDITIONS: The value of the specified field has been changd to the // default value for that field // // AUTHOR: Amy Langill *2B DATE: 3/23/1999 *******************************************************************************/ void ToolsRecord::Reset(ToolsFieldType fieldToChange) { switch (fieldToChange) // depending on the field, call the function to change { // that element to its default value case T_TDEX: tdex.ResetValue(); break; case T_SHOES: shoes.ResetValue(); break; case T_GLOVE: glove.ResetValue(); break; case T_OTHER: other.ResetValue(); break; } return; } /******************************************************************************* // FUNCTION NAME: Reset // // DESCRIPTION OF FUNCTION: Reset the value of all the fields of the record to // their respective default values // // DESCRIPTION OF ALGORITHM: Calls the appropriate resetting member function of // each field type for each field in the record // // CALLED BY: client // CALLS: LimitInt::ResetValue,BALString::ResetValue // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: The value of all of the fields have been changed to the // default values for those fields // // AUTHOR: Amy Langill *2B DATE: 3/23/1999 *******************************************************************************/ void ToolsRecord::Reset() { // depending on the field, call the function to change // that element to its default value tdex.ResetValue(); shoes.ResetValue(); glove.ResetValue(); other.ResetValue(); return; } /******************************************************************************* // FUNCTION NAME: Replace // // DESCRIPTION OF FUNCTION: Replaces the contents of the current record with // the contents of another record. // // DESCRIPTION OF ALGORITHM: Updates each field in this record with the field // contents of newRecord. // // CALLED BY: client // CALLS: Update,FieldContains // // PARAMETERS: / in / newRecord - ToolsRecord whose contents are to be stored // into this record // // PRECONDITIONS: none // // POSTCONDITIONS: The field values of every field in newRecord have been // stored into the appropriate fields of this record // // AUTHOR: Amy Langill *2B DATE: 3/23/1999 *******************************************************************************/ void ToolsRecord::Replace(ToolsRecord newRecord) { ToolsFieldType currFld; for (currFld=T_TDEX;currFld<=T_OTHER;currFld=ToolsFieldType(currFld+1)) this->Update(newRecord.FieldContains(currFld),currFld); return; } /**********************************ITERATORS***********************************/ /******************************************************************************* // FUNCTION NAME: List // // DESCRIPTION OF FUNCTION: Generate a string that holds the record information // formatted for screen output // // DESCRIPTION OF ALGORITHM: Creates a string that holds field labels and // contents as created by the Print member functionof each field's type for // every field in the record, delimited with spaces between the fields. // // CALLED BY: client // CALLS: LimitInt::Print,BALString::Print // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: A string has been created which contains the current data // stored in the record formatted for screen viewing // // AUTHOR: Amy Langill *2B DATE: 3/23/1999 *******************************************************************************/ string ToolsRecord::List() { string outputString; // To hold formatted output // print out label, data, spacing character for each field outputString = T_TDEX_LABEL + tdex.Print() + T_FLD_SEPARATOR; outputString += T_SHOES_LABEL + shoes.Print() + T_FLD_SEPARATOR; outputString += T_GLOVE_LABEL + glove.Print() + '\n'; // Screen is only 80 characters wide, so need to print on two lines outputString += T_OTHER_LABEL + other.Print(); return outputString; // send back the screen-formatted output } /******************************************************************************* // FUNCTION NAME: BriefList // // DESCRIPTION OF FUNCTION: Generate a string that holds abbreviated record // information formatted for screen // // DESCRIPTION OF ALGORITHM: Creates a string that holds field labels and // contents as created by the Print member function of each field's type for // some fields in the record, delimited with spaces between the fields. // // CALLED BY: client // CALLS: LimitInt::Print,BALString::Print // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: A string has been created which contains an abbreviated // version of the current data stored in the record formatted for viewing // // AUTHOR: Amy Langill *2B DATE: 3/29/1999 *******************************************************************************/ string ToolsRecord::BriefList() { string outputString; // To hold formatted output // This stuff fits on one line of the screen: outputString = tdex.Print() + T_FLD_SEPARATOR; outputString += T_SHOES_LABEL + shoes.Print() + T_FLD_SEPARATOR; outputString += T_GLOVE_LABEL + glove.Print() + T_FLD_SEPARATOR; outputString += T_OTHER_LABEL + other.Print(); return outputString; // send back the screen-formatted output } /******************************************************************************* // FUNCTION NAME: Print // // DESCRIPTION OF FUNCTION: Generate a string that holds the record formatted // for BAM file output // // DESCRIPTION OF ALGORITHM: Creates a string which contains the field labels // and contents for each field delimited by spaces through successive // assignment to the output string // // CALLED BY: client // CALLS: LimitInt::Print,BALString::Print // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: A string has been created which contains the current data // stored in the record formatted for BAM file output // // AUTHOR: Amy Langill *2B DATE: 3/23/1999 *******************************************************************************/ string ToolsRecord::Print() { string outputString; // To hold formatted output // for each field, printout label, data, space outputString = T_TDEX_LABEL + tdex.Print() + T_FLD_SEPARATOR; outputString += T_SHOES_LABEL + shoes.Print() + T_FLD_SEPARATOR; outputString += T_GLOVE_LABEL + glove.Print() + T_FLD_SEPARATOR; outputString += T_OTHER_LABEL + other.Print(); return outputString; // send back the report-formatted output }