00001 #ifndef UTILITIES_H 00002 #define UTILITIES_H 00003 00004 //-------------------------------------------------------------------- 00005 // 00006 // This file is part of PEACE. 00007 // 00008 // PEACE is free software: you can redistribute it and/or modify it 00009 // under the terms of the GNU General Public License as published by 00010 // the Free Software Foundation, either version 3 of the License, or 00011 // (at your option) any later version. 00012 // 00013 // PEACE is distributed in the hope that it will be useful, but 00014 // WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 // General Public License for more details. 00017 // 00018 // You should have received a copy of the GNU General Public License 00019 // along with PEACE. If not, see <http://www.gnu.org/licenses/>. 00020 // 00021 // Miami University makes no representations or warranties about the 00022 // suitability of the software, either express or implied, including 00023 // but not limited to the implied warranties of merchantability, 00024 // fitness for a particular purpose, or non-infringement. Miami 00025 // University shall not be liable for any damages suffered by licensee 00026 // as a result of using, result of using, modifying or distributing 00027 // this software or its derivatives. 00028 // 00029 // By using or copying this Software, Licensee agrees to abide by the 00030 // intellectual property laws, and all other applicable laws of the 00031 // U.S., and the terms of GNU General Public License (version 3). 00032 // 00033 // Authors: Dhananjai M. Rao raodm@muohio.edu 00034 // 00035 //--------------------------------------------------------------------- 00036 00037 #include <iostream> 00038 00039 #ifdef _WINDOWS 00040 00041 /** A global not-equal-to function (not an member function). 00042 00043 This function provides a plug-in replacement for the corresponding 00044 string comparison function present in Linux version of STL 00045 implementation but is missing in Windows version. Note that this 00046 method is defined only under Windows. On Linux the default 00047 operator!=() provided by basic_string is used instead. 00048 */ 00049 inline bool 00050 operator!=(const std::string& s1, const std::string& s2) { 00051 return (s1.compare(s2) != 0); 00052 } 00053 00054 /** A global less-than function (not an member function). 00055 00056 This function provides a plug-in replacement for the corresponding 00057 string comparison function present in Linux version of STL 00058 implementation but is missing in Windows version. Note that this 00059 method is defined only under Windows. On Linux the default 00060 operator<() provided by basic_string is used instead. 00061 */ 00062 inline bool 00063 operator<(const std::string& s1, const std::string& s2) { 00064 return (s1.compare(s2) < 0); 00065 } 00066 00067 /** A global equal-to function (not an member function). 00068 00069 This function provides a plug-in replacement for the corresponding 00070 string comparison function present in Linux version of STL 00071 implementation but is missing in Windows version. Note that this 00072 method is defined only under Windows. On Linux the default 00073 operator==() provided by basic_string is used instead. 00074 */ 00075 inline bool 00076 operator==(const std::string& s1, const std::string& s2) { 00077 return (s1.compare(s2) == 0); 00078 } 00079 00080 /** A global insertion operator for string (not an member function). 00081 00082 This function provides a plug-in replacement for the corresponding 00083 insertion operator for std::string present in Linux version of STL 00084 implementation but is missing in Windows version. Note that this 00085 method is defined only under Windows. On Linux the default 00086 operator<<() provided by std::basic_string is used instead. 00087 */ 00088 inline std::ostream& 00089 operator<<(std::ostream& os, const std::string& str) { 00090 return (os << str.c_str()); 00091 } 00092 00093 #endif // _WINDOWS 00094 00095 #if !defined(_WINDOWS) && !defined(ICC) && !defined(GCC) 00096 00097 /** \def UNREFERENCED_PARAMETER(param) Workaround warning C4100 in VS 2005 00098 and remark #869 in icc. 00099 00100 This macro is a simple work around to supress the C4100 warning 00101 (unreferenced parameter). This warning is generated at Level 4 00102 under visual studio 2005. This warning has not been observed 00103 under gcc (4.2) and therefore on Linux/gcc this macro resolves to 00104 nothing. 00105 */ 00106 #ifndef UNREFERENCED_PARAMETER 00107 #define UNREFERENCED_PARAMETER(param) param 00108 #endif 00109 00110 #else 00111 #define UNREFERENCED_PARAMETER(param) 00112 #endif 00113 00114 #ifdef _WINDOWS 00115 /** \def fmax Macro to return the maximum of 2 values. 00116 00117 This macro provides a replacement for the fmax() function 00118 defined in math.h under Linux. However, VS 2005 does not 00119 have this method and this macro serves as a replacement. 00120 */ 00121 #define fmax(x, y) ((x < y) ? y : x) 00122 #endif 00123 00124 #ifdef _WINDOWS 00125 /** \def fmin Macro to return the minimum of 2 values. 00126 00127 This macro provides a replacement for the fmin() function 00128 defined in math.h under Linux. However, VS 2005 does not 00129 have this method and this macro serves as a replacement. 00130 */ 00131 #define fmin(x, y) ((x < y) ? x : y) 00132 #endif 00133 00134 #if (!defined(_WINDOWS) && !defined(vsnprintf_s)) 00135 /** \def vsnprintf_s(buffer, bufsize, count, format, argptr) 00136 00137 \brief Macro to define vsnprintf_s if not defined. 00138 00139 This macro provides a replacement for the vsnprintf_s function 00140 defined in Windows but is absent in Unix/Linux. This macro 00141 simply defines vsnprintf_s as vsnprinf in Unix and Linux. 00142 */ 00143 #define vsnprintf_s(buffer, bufSize, count, format, argptr) vsnprintf(buffer, count, format, argptr) 00144 #endif 00145 00146 #if (!defined(_WINDOWS) && !defined(_fileno)) 00147 /** \def _fileno 00148 00149 \brief Macro to define _fileno if not defined. 00150 00151 This macro provides a replacement for the \c _fileno function 00152 defined in Windows but is absent in Unix/Linux. This macro 00153 simply defines \c _fileno as \c fileno in Unix and Linux. 00154 */ 00155 #define _fileno fileno 00156 #endif 00157 00158 #if (!defined(_WINDOWS) && !defined(ctime_s)) 00159 /** \def ctime_s(buffer, size, time) 00160 00161 \brief Macro to define ctime_s if not defined. 00162 00163 This macro provides a replacement for the \c ctime_s function 00164 defined in Windows but is absent in Unix/Linux. This macro 00165 simply defines \c ctime_s as \c ctime_r in Unix and Linux. 00166 */ 00167 #define ctime_s(buffer, size, time) ctime_r(time, buffer); 00168 #endif 00169 00170 /** \def ASSERT(x) 00171 00172 \brief Define a convenient macro for using c asserts. 00173 00174 Define a custom macro ASSERT (note the all caps) method to be used 00175 instead of the c \c assert method to enable compiling ASSERT 00176 usages in and out of the source code. When the compiler flag \c 00177 DEVELOPER_ASSERTIONS is specified then the ASSERT call defaults 00178 to the normal \c assert method. For example ASSERT (<<em>boolean 00179 expression</em>>)} will be mapped to \c assert (<<em>boolean 00180 expression</em>>). If the compiler flag \c DEVELOPER_ASSERTIONS 00181 is not specified then the ASSERT simply gets compiled out. 00182 */ 00183 #ifndef ASSERT 00184 #ifdef DEVELOPER_ASSERTIONS 00185 #include <assert.h> 00186 00187 #define ASSERT(x) assert(x) 00188 00189 #else // !DEVELOPER_ASSERTIONS 00190 00191 #define ASSERT(x) 00192 00193 #endif 00194 #endif 00195 00196 /** \def VALIDATE(x) 00197 00198 \brief Define a convenient macro for extra checks that can be 00199 easily compiled out to improve performance. 00200 00201 Define a custom macro VALIDATE (note the all caps) to be used 00202 around additional extra checks that can be easily compiled out to 00203 improve performance. When the compiler flag \c ENABLE_VALIDATES is 00204 specified then the VALIDATE call defaults to the normal operation 00205 and are included in the source code. If the compiler flag \c 00206 ENABLE_VALIDATES is not specified then the VALIDATE code simply 00207 gets compiled out. 00208 */ 00209 #ifndef VALIDATE 00210 #ifdef ENABLE_VALIDATES 00211 00212 #define VALIDATE(x) x 00213 00214 #else // !ENABLE_VALIDATES 00215 00216 #define VALIDATE(x) 00217 00218 #endif 00219 #endif 00220 00221 00222 /** \def getTimeStamp 00223 00224 \brief Get's the file modification timestamp for a given file 00225 name. 00226 00227 This method provides a portable (to Windows and Linux/Unix) 00228 implementation for a helper method to obtain the modification 00229 timestamp for a given file. 00230 00231 \param[in] fileName The file name (with full path) for which the 00232 modification time stamp is desired. If the fileName is NULL then 00233 this method simply returns the buffer without any modifications. 00234 00235 \param[out] buffer The buffer into which the time stamp is to be 00236 written. 00237 00238 \return A pointer to the buffer. 00239 */ 00240 char* getTimeStamp(const char *fileName, char *buffer); 00241 00242 /** \def getTime 00243 00244 \brief Returns the string representation of the supplied time data 00245 structure. 00246 00247 This method provides a portable (to Windows and Linux/Unix) 00248 implementation for a helper method to obtain the string 00249 representation of a given encoded time_t 00250 datastructure. 00251 00252 \param[out] buffer The buffer into which the string representation 00253 of the supplied time is to be written. This pointer must be 00254 capable of holding at least 128 characters. If this pointer is 00255 NULL, then this method exits immediately. 00256 00257 \param[in] codedTime The encoded time_t data structure to be 00258 converted to a string representation. If this parameter is NULL, 00259 then the current system time is converted to a string and filled 00260 into the supplied buffer. 00261 00262 \return The pointer to the buffer passed in. 00263 */ 00264 char* getTime(char *buffer, const time_t *codedTime = NULL); 00265 00266 #endif