// balstring.cpp Implementation file for the truly trivial BALString class /******************************************************************************* // MODULE NAME: BALString // INTERFACE FILE: balstring.h // IMPLEMENTATION FILE: balstring.cpp // // PURPOSE: To provide a BALString class useful for an implementation of the BAL // program which allows the user to specify a field width for a string, store // a default value for the string, store a supplied value for the string, reset // either the string stored in the object or the width of the field, and print // out a string with the appropriate width that contains either the current // contents of the object or the default value for the object // // FUNCTIONS: Name Purpose // BALString Constructor w/specified field width // BALString Parameterless constructor // isDefault Indicate whether the object contains the default // isEmpty Test whether the object contains no value // valueIs Report on the string currently stored in the // object // Print Print out, with appropriate width, the contents // of the object // Default Print out, with appropriate width, the default // contents of the object // Store Store a new string value into the object // SetSize Change the field width // ResetValue Change the string to the default string // ResetSize Change the field width to the default size // Reset Change field width & string to defaults // // AUTHOR: Amy Langill *2B Date 3/22/1999 *******************************************************************************/ #include "balstring.h" /********************************CONSTRUCTORS**********************************/ /******************************************************************************* // FUNCTION NAME: BALString // // DESCRIPTION OF FUNCTION: Parameterless constructor for BALString class // // DESCRIPTION OF ALGORITHM: Assigns default values to this->fieldSize and // this->value // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: BALString object has been created with default string and // field size // // AUTHOR: Amy Langill *2B DATE: 3/22/1999 *******************************************************************************/ BALString::BALString() { this->fieldSize=DEFAULT_STR_SIZE; this->value=DEFAULT_STRING; } /******************************************************************************* // FUNCTION NAME: BALString // // DESCRIPTION OF FUNCTION: Creates a BALString object of specified width // // DESCRIPTION OF ALGORITHM: Assigns default to this->value and user-supplied // width to this->fieldSize // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / width - int that is to be stored in fieldSize // // PRECONDITIONS: none // // POSTCONDITIONS: BALString object has been created w/default string value and // indicated field width // // AUTHOR: Amy Langill *2B DATE: 3/22/1999 *******************************************************************************/ BALString::BALString(int width) { this->fieldSize = width; this->value = DEFAULT_STRING; } /*********************************OBSERVERS************************************/ /******************************************************************************* // FUNCTION NAME: isDefault // // DESCRIPTION OF FUNCTION: Indicates whether the object contains the default // string value // // DESCRIPTION OF ALGORITHM: Tests this->value against DEFAULT_STRING for // exact equality // // CALLED BY: // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: If value is exactly equal to the default string, returns // true. Returns false otherwise. // // AUTHOR: Amy Langill *2B DATE: 3/22/1999 *******************************************************************************/ bool BALString::isDefault() { return (value==DEFAULT_STRING); } /******************************************************************************* // FUNCTION NAME: isEmpty // // DESCRIPTION OF FUNCTION: Indicates whether the string contained in the // object is empty // // DESCRIPTION OF ALGORITHM: Tests to see if the length of the string contained // in the object is exactly equal to zero // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns true if and only if the length of the string stored // in this->value is exactly equal to zero // // AUTHOR: Amy Langill *2B DATE: 3/22/1999 *******************************************************************************/ bool BALString::isEmpty() { return ((value.length())==0); } /******************************************************************************* // FUNCTION NAME: valueIs // // DESCRIPTION OF FUNCTION: Indicates what string is stored in the object // // DESCRIPTION OF ALGORITHM: Returns this->value // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Returns the string stored in value // // AUTHOR: Amy Langill *2B DATE: 3/22/1999 *******************************************************************************/ string BALString::valueIs() { return value; } /******************************************************************************* // FUNCTION NAME: Print // // DESCRIPTION OF FUNCTION: Print out the contents of the string to fit the // field width // // DESCRIPTION OF ALGORITHM: Assigns the string in this->value to a string // called output. If output is too short to fill the field, calls PrintBlank // to pad output with enough spaces. If output isn't too short to fill the // field, it's clipped off to fieldSize characters. Output is returned. // // CALLED BY: client // CALLS: PrintBlank // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: A string of length fieldSize, containing at least the first // fieldSize characters of value is returned. // // AUTHOR: Amy Langill *2B DATE: 3/22/1999 *******************************************************************************/ string BALString::Print() { string output=value; // For holding string before output if (output.length()PrintBlank(output, fieldSize); // Pad it w/ extra spaces else output = value.substr(0,fieldSize); // Otherwise, take the first fieldSize // chars of value return output; // Return the pretty output string } // Print out the default contents of the string /******************************************************************************* // FUNCTION NAME: Default // // DESCRIPTION OF FUNCTION: Print out the default contents of the object into // a string of the appropriate width // // DESCRIPTION OF ALGORITHM: Create a string that holds the default value. If // this default is too short to fill the field, pad it with extra spaces. If // the string is not too short, take the first fieldSize chars of // DEFAULT_STRING instead of the whole string // // CALLED BY: client // CALLS: PrintBlank // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: A string of length fieldSize has been printed which contains // at least the first fieldSize characters of the default string DEFAULT_STRING // // AUTHOR: Amy Langill *2B DATE: 3/22/1999 *******************************************************************************/ string BALString::Default() { string defaultString=DEFAULT_STRING; // for holding default if (defaultString.length()value the string that is passed as // a parameter to the function // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / newValue - string that is to be stored into the object // // PRECONDITIONS: none // // POSTCONDITIONS: The string passed as a parameter to the function has been // stored into this->value // // AUTHOR: Amy Langill *2B DATE: 3/22/1999 *******************************************************************************/ void BALString::Store(string newValue) { value=newValue; return; } /******************************************************************************* // FUNCTION NAME: SetSize // // DESCRIPTION OF FUNCTION: Change the printing width of the field to a user- // specified value // // DESCRIPTION OF ALGORITHM: If the specified width is negative, assign 0 to // the field size for the object. Otherwise, assign the newfieldSize passed to // the function as a parameter to this->fieldSize // // CALLED BY: client // CALLS: none // // PARAMETERS: / in / newfieldSize - int value of the new field width // // PRECONDITIONS: none // // POSTCONDITIONS: If an impossible (negative) field width was passed to the // function, the field size was set to zero. Otherwise, the field width has // been changed to the specified value // // AUTHOR: Amy Langill *2B DATE: 3/22/1999 *******************************************************************************/ void BALString::SetSize(int newfieldSize) { if (newfieldSize<0) fieldSize=0; else fieldSize=newfieldSize; return; } /******************************************************************************* // FUNCTION NAME: ResetValue // // DESCRIPTION OF FUNCTION: Store the default value into the object // // DESCRIPTION OF ALGORITHM: Assigns the default string to this->value // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: This->value has been changed to the default string value // // AUTHOR: Amy Langill *2B DATE: 3/22/1999 *******************************************************************************/ void BALString::ResetValue() { value=DEFAULT_STRING; return; } /******************************************************************************* // FUNCTION NAME: ResetSize // // DESCRIPTION OF FUNCTION: Change the field width to the default width // // DESCRIPTION OF ALGORITHM: Assigns the default string size to this->fieldSize // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: The fieldSize of the object has been changed to the default // string size // // AUTHOR: Amy Langill *2B DATE: 3/22/1999 *******************************************************************************/ void BALString::ResetSize() { fieldSize=DEFAULT_STR_SIZE; return; } /******************************************************************************* // FUNCTION NAME: Reset // // DESCRIPTION OF FUNCTION: Set field width & string to their default values // // DESCRIPTION OF ALGORITHM: Assigns DEFAULT_STRING to this->value and // DEFAULT_STR_SIZE to this->fieldSize // // CALLED BY: client // CALLS: none // // PARAMETERS: none // // PRECONDITIONS: none // // POSTCONDITIONS: Value has been set to DEFAULT_STRING and fieldSize has been // changed to DEFAULT_STR_SIZE // // AUTHOR: Amy Langill *2B DATE: 3/22/1999 *******************************************************************************/ void BALString::Reset() { value=DEFAULT_STRING; fieldSize=DEFAULT_STR_SIZE; return; } // THIS ONE IS PRIVATE /******************************************************************************* // FUNCTION NAME: PrintBlank // // DESCRIPTION OF FUNCTION: Pads a string with enough spaces to fill a given // fieldSize // // DESCRIPTION OF ALGORITHM: Calculates difference between current length of // string and desired size. Adds a space for each character difference to the // string. // // CALLED BY: Print, Default // CALLS: none // // PARAMETERS: / inout / printMe - string to be padded with extra spaces // / in / fieldSize - int desired length of string // // PRECONDITIONS: none // // POSTCONDITIONS: If fieldSize is not less than the length of printMe, printMe // will be changed to the same length as fieldSize by the addition of spaces at // the end of the printMe string // // AUTHOR: Amy Langill *2B DATE: 3/22/1999 *******************************************************************************/ void BALString::PrintBlank(string& printMe,int fieldSize) { int counter,difference; difference = fieldSize - printMe.length(); for (counter=0;counter