libStatGen Software
1
|
00001 /* 00002 * Copyright (C) 2010-2011 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 "StatGenStatus.h" 00019 00020 const char* StatGenStatus::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* StatGenStatus::getStatusString(StatGenStatus::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 StatGenStatus::isContinuableStatus(StatGenStatus::Status status) 00042 { 00043 if(status == StatGenStatus::SUCCESS || status == StatGenStatus::FAIL_PARSE || 00044 status == StatGenStatus::INVALID_SORT || status == StatGenStatus::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 StatGenStatus::StatGenStatus(ErrorHandler::HandlingType handleType) 00056 : myHandlingType(handleType) 00057 { 00058 reset(); 00059 } 00060 00061 00062 // Destructor 00063 StatGenStatus::~StatGenStatus() 00064 { 00065 } 00066 00067 00068 // Resets this status. 00069 void StatGenStatus::reset() 00070 { 00071 myType = UNKNOWN; 00072 myMessage.clear(); 00073 } 00074 00075 00076 void StatGenStatus::setHandlingType(ErrorHandler::HandlingType handleType) 00077 { 00078 myHandlingType = handleType; 00079 } 00080 00081 00082 // Set the status with the specified values. 00083 void StatGenStatus::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 StatGenStatus::addError(Status newStatus, const char* newMessage) 00100 { 00101 if(myType == StatGenStatus::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 StatGenStatus::addError(StatGenStatus newStatus) 00123 { 00124 if(myType == StatGenStatus::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 StatGenStatus::Status StatGenStatus::getStatus() const 00143 { 00144 return(myType); 00145 } 00146 00147 00148 // Return the status message. 00149 const char* StatGenStatus::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 StatGenStatus & StatGenStatus::operator = (StatGenStatus::Status newStatus) 00158 { 00159 myType = newStatus; 00160 myMessage.clear(); 00161 00162 if(newStatus != SUCCESS) 00163 { 00164 handleError(newStatus, ""); 00165 } 00166 return(*this); 00167 } 00168 00169 00170 // Overload operator = to copy the specified status object to this one. 00171 StatGenStatus & StatGenStatus::operator = (StatGenStatus newStatus) 00172 { 00173 myType = newStatus.myType; 00174 myMessage = newStatus.myMessage; 00175 myHandlingType = newStatus.myHandlingType; 00176 return(*this); 00177 } 00178 00179 00180 // Overload operator != to determine if the passed in type is not equal 00181 // to this status's type. 00182 bool StatGenStatus::operator != (const StatGenStatus::Status& compStatus) const 00183 { 00184 return(compStatus != myType); 00185 } 00186 00187 00188 // Overload operator != to determine if the passed in type is equal 00189 // to this status's type. 00190 bool StatGenStatus::operator == (const StatGenStatus::Status& compStatus) const 00191 { 00192 return(compStatus == myType); 00193 } 00194 00195 00196 void StatGenStatus::handleError(Status newStatus, const char* newMessage) 00197 { 00198 // If the status is not success and not NO_MORE_RECS, handle 00199 // the error (SUCCESS & NO_MORE_RECS are not real errors.) 00200 if((newStatus != SUCCESS) && (newStatus != NO_MORE_RECS)) 00201 { 00202 std::string message = getStatusString(newStatus); 00203 message += ": "; 00204 message += newMessage; 00205 00206 ErrorHandler::handleError(message.c_str(), myHandlingType); 00207 } 00208 }