SamStatus.cpp

00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include "SamStatus.h"
00019 
00020 const char* SamStatus::enumStatusString[] = {
00021     "SUCCESS",
00022     "UNKNOWN",
00023     "NO_MORE_RECS",
00024     "FAIL_IO",
00025     "FAIL_ORDER",
00026     "FAIL_PARSE",
00027     "INVALID_SORT",
00028     "INVALID", 
00029     "FAIL_MEM"
00030 };
00031 
00032 
00033 const char* SamStatus::getStatusString(SamStatus::Status statusEnum)
00034 {
00035     return(enumStatusString[statusEnum]);
00036 }
00037 
00038 
00039 // Returns whether or not it is "safe" to keep processing the file
00040 // after the specified status return.
00041 bool SamStatus::isContinuableStatus(SamStatus::Status status)
00042 {
00043     if(status == SamStatus::SUCCESS || status == SamStatus::FAIL_PARSE || 
00044        status == SamStatus::INVALID_SORT || status == SamStatus::INVALID)
00045     {
00046         // The status is such that file processing can continue.
00047         return(true);
00048     }
00049     // UNKNOWN, NO_MORE_RECS, FAIL_IO, FAIL_ORDER, FAIL_MEM
00050     return(false);
00051 }
00052 
00053 
00054 // Constructor
00055 SamStatus::SamStatus(ErrorHandler::HandlingType handleType)
00056     : myHandlingType(handleType)
00057 {
00058     reset();
00059 }
00060 
00061    
00062 // Destructor
00063 SamStatus::~SamStatus()
00064 {
00065 }
00066 
00067 
00068 // Resets this status.
00069 void SamStatus::reset()
00070 {
00071     myType = UNKNOWN;
00072     myMessage.clear();
00073 }
00074 
00075 
00076 void SamStatus::setHandlingType(ErrorHandler::HandlingType handleType)
00077 {
00078     myHandlingType = handleType;
00079 }
00080 
00081 
00082 // Set the status with the specified values.
00083 void SamStatus::setStatus(Status newStatus, const char* newMessage)
00084 {
00085     myType = newStatus;
00086     myMessage = getStatusString(newStatus);
00087     myMessage += ": ";
00088     myMessage += newMessage;
00089 
00090     if(newStatus != SUCCESS)
00091     {
00092         handleError(newStatus, newMessage);
00093     }
00094 }
00095 
00096 
00097 // Adds the specified error message to the status message.
00098 // Sets the status to newStatus if the current status is SUCCESS.
00099 void SamStatus::addError(Status newStatus, const char* newMessage)
00100 {
00101     if(myType == SamStatus::SUCCESS)
00102     {
00103         myType = newStatus;
00104     }
00105     else
00106     {
00107         myMessage += "\n";
00108     }
00109     myMessage += getStatusString(newStatus);
00110     myMessage += ": ";
00111     myMessage += newMessage;
00112 
00113     if(newStatus != SUCCESS)
00114     {
00115         handleError(newStatus, newMessage);
00116     }
00117 }
00118 
00119 
00120 // Adds the specified status to the status message.
00121 // Sets the status to newStatus if the current status is SUCCESS.
00122 void SamStatus::addError(SamStatus newStatus)
00123 {
00124     if(myType == SamStatus::SUCCESS)
00125     {
00126         myType = newStatus.myType;
00127     }
00128     else
00129     {
00130         myMessage += "\n";
00131     }
00132     myMessage += newStatus.myMessage;
00133 
00134     if(newStatus != SUCCESS)
00135     {
00136         handleError(newStatus.myType, newStatus.myMessage.c_str());
00137     }
00138 }
00139 
00140 
00141 // Return the enum for this status.
00142 SamStatus::Status SamStatus::getStatus() const
00143 {
00144     return(myType);
00145 }
00146 
00147 
00148 // Return the status message.
00149 const char* SamStatus::getStatusMessage() const
00150 {
00151     return(myMessage.c_str());
00152 }
00153 
00154 
00155 // Overload operator = to set the sam status type to the
00156 // passed in status and to clear the message string.
00157 SamStatus & SamStatus::operator = (SamStatus::Status newStatus)
00158 {
00159     reset();
00160     myType = newStatus;
00161 
00162     if(newStatus != SUCCESS)
00163     {
00164         handleError(newStatus, "");
00165     }
00166     return(*this);
00167 }
00168 
00169 
00170 // Overload operator != to determine if the passed in type is not equal
00171 // to this status's type.
00172 bool SamStatus::operator != (const SamStatus::Status& compStatus) const
00173 {
00174     return(compStatus != myType);
00175 }
00176 
00177 
00178 // Overload operator != to determine if the passed in type is equal
00179 // to this status's type.
00180 bool SamStatus::operator == (const SamStatus::Status& compStatus) const
00181 {
00182     return(compStatus == myType);
00183 }
00184 
00185 
00186 void SamStatus::handleError(Status newStatus, const char* newMessage)
00187 {
00188     // If the status is not success and not NO_MORE_RECS, handle
00189     // the error  (SUCCESS & NO_MORE_RECS are not real errors.)
00190     if((newStatus != SUCCESS) && (newStatus != NO_MORE_RECS))
00191     {
00192         std::string message = getStatusString(newStatus);
00193         message += ": ";
00194         message += newMessage;
00195  
00196         ErrorHandler::handleError(message.c_str(), myHandlingType);
00197     }
00198 }
Generated on Tue Sep 6 17:51:59 2011 for libStatGen Software by  doxygen 1.6.3