00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #if !defined(__GNUC__) || ((__GNUC__ == 2) && (__GNUC_MINOR__ > 7))
00011 #include <time.h>
00012 #endif
00013
00014 #if defined(__GNUC__)
00015 #pragma implementation
00016
00017 #endif
00018
00019 #include "myenv.h"
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <stdarg.h>
00024 #include <ctype.h>
00025 #include <iostream.h>
00026 #include <fstream.h>
00027 extern "C"
00028 {
00029 #if defined(macintosh)
00030 #include <types.h>
00031 #include <stat.h>
00032 #elif defined(WIN32)
00033 #include <sys/types.h>
00034 #include <sys/stat.h>
00035 #define stat _stat
00036 #define STDERR_FILENO 2
00037 #else
00038 #include <sys/types.h>
00039 #include <sys/stat.h>
00040 #include <unistd.h>
00041 #endif
00042 }
00043
00044 namespace linalg
00045 {
00046 using namespace linalg;
00047 using namespace std;
00048
00049 extern "C" {
00050
00051
00052 int linalg::stat(const char * path, struct stat * buf);
00053 }
00054
00055
00056
00057
00058
00059
00060
00061 const char _Minuses [] = "\
00062 -------------------------------------------------------------------------------";
00063
00064 const char _Asteriscs [] = "\
00065 *******************************************************************************";
00066
00067 const char _Equals [] = "\
00068 ===============================================================================";
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 const char * xgetenv(const char * name, const char * default_value)
00083 {
00084 const char * env_value = ::getenv(name);
00085 if( env_value != 0 )
00086 return env_value;
00087 else if( default_value != 0 )
00088 return default_value;
00089 else
00090 return _error("xgetenv: env variable '%s' wasn't found, but was required",
00091 name), (const char *)0;
00092 }
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 void _error(const char * message,...)
00107 {
00108 static char smsg[2048];
00109 va_list args;
00110 va_start(args,message);
00111
00112
00113
00114
00115
00116
00117
00118 sprintf(smsg,"\n_error:\n");
00119 int p = strlen(smsg);
00120 sprintf(&smsg[p],message,args);
00121 cout <<" _error: throwing exception..." << endl;
00122
00123 throw smsg;
00124
00125 return;
00126
00127 #ifdef __MWERKS__
00128 exit(4);
00129 #else
00130 abort();
00131 #endif
00132 }
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 void message(const char * text,...)
00145 {
00146 va_list args;
00147 va_start(args,text);
00148
00149 vfprintf(stderr,text,args);
00150 }
00151
00152
00153
00154
00155
00156
00157
00158 #if 0
00159 ostream_withassign Logger::log_stream(new filebuf(STDERR_FILENO));
00160
00161
00162 void Logger::set_log(const char log_file_name [])
00163 {
00164 filebuf * const new_filebuf = new filebuf();
00165 if( new_filebuf->open(log_file_name,ios::out|ios::ate) == 0 )
00166 perror("Log file open error"),
00167 _error("Failed to open the log file '%s' because of the error above",
00168 log_file_name);
00169 log_stream = new_filebuf;
00170 }
00171 #endif
00172
00173
00174
00175
00176
00177
00178
00179 size_t GFS_Default::operator () (const char * file_name)
00180 {
00181 perror("getting file status");
00182 _error("Failed to get status of the file <%s> because of the error "
00183 "above",file_name);
00184 return (size_t)EOF;
00185 }
00186
00187 GFS_Default GFS_default;
00188
00189 size_t get_file_size(const char * file_name, GFS_Default& on_error)
00190 {
00191 struct stat file_status;
00192 if( linalg::stat(file_name,&file_status) != 0 )
00193 return on_error(file_name);
00194 return file_status.st_size;
00195 }
00196
00197
00198
00199
00200
00201
00202 char * xstrncpy(char * dest, const char * src, const int len)
00203 {
00204 strncpy(dest,src,len);
00205 dest[len] = '\0';
00206 return dest;
00207 }
00208
00209
00210
00211
00212
00213 static inline int to_lower(const char c)
00214 {
00215 return isupper(c) ? (c - 'A') + 'a' : c;
00216 }
00217
00218
00219
00220
00221 bool does_start_with_ci(const char * s1, const char * s2)
00222 {
00223 while( *s2 != '\0' )
00224 if( *s1 == '\0' )
00225 return false;
00226 else if( to_lower(*s1++) != to_lower(*s2++) )
00227 return false;
00228 return true;
00229 }
00230
00231
00232
00233
00234
00235
00236 double pow(long x, long y)
00237 {
00238 if( x == 0 )
00239 return 0;
00240
00241 if( y == 0 )
00242 return 1;
00243
00244 if( x == 1 )
00245 return 1;
00246
00247 double multiplier = y > 0 ? x : (1.0/x);
00248 if( y < 0 )
00249 y = -y;
00250
00251 while( (y & 1) == 0 )
00252 {
00253 multiplier *= multiplier;
00254 y >>= 1;
00255 }
00256
00257 double accum = multiplier;
00258 y >>= 1;
00259
00260 while( y != 0 )
00261 {
00262 multiplier *= multiplier;
00263 if( (y & 1) == 1)
00264 accum *= multiplier;
00265 y >>= 1;
00266 }
00267
00268 return accum;
00269 }
00270
00271 double pow(double x, long y)
00272 {
00273 if( x == 0 )
00274 return 0;
00275
00276 if( y == 0 )
00277 return 1;
00278
00279 if( x == 1 )
00280 return 1;
00281
00282 if( y < 0 )
00283 y = -y, x = 1.0/x;
00284
00285 while( (y & 1) == 0 )
00286 {
00287 x *= x;
00288 y >>= 1;
00289 }
00290
00291 double accum = x;
00292 y >>= 1;
00293
00294 while( y != 0 )
00295 {
00296 x *= x;
00297 if( (y & 1) == 1)
00298 accum *= x;
00299 y >>= 1;
00300 }
00301
00302 return accum;
00303 }
00304
00305 #if !defined(__GNUC__) || ((__GNUC__ == 2) && (__GNUC_MINOR__ > 7))
00306
00307
00308
00309
00310 static time_t time_set;
00311
00312 double start_timer(void)
00313 {
00314 return time_set = time(0);
00315 }
00316
00317
00318
00319
00320 double return_elapsed_time(const double last_time)
00321 {
00322 time_t new_time = time(0);
00323 if( time_set == 0 )
00324 return -1;
00325 return new_time - (last_time == 0.0 ? time_set : last_time);
00326 }
00327
00328 #endif
00329 }