Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

myenv.cpp

Go to the documentation of this file.
00001 // This may look like C code, but it is really -*- C++ -*-
00002 /*
00003  ************************************************************************
00004  *                      Service C++ functions 
00005  *           that support the standard environment for me
00006  *
00007  * $Id: myenv.cpp,v 1.1 2004/05/21 21:02:52 maxx Exp $
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 //#pragma implementation "Logger.h"
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 //#include "Logger.h"
00055 
00056 /*
00057  *-----------------------------------------------------------------------
00058  *              Some global constant pertaining to input/output
00059  */
00060 
00061 const char _Minuses [] = "\
00062 -------------------------------------------------------------------------------";
00063 
00064 const char _Asteriscs [] = "\
00065 *******************************************************************************";
00066 
00067 const char _Equals [] = "\
00068 ===============================================================================";
00069 
00070 //------------------------------------------------------------------------
00071 //                              A better getenv
00072 //
00073 // It works just as a regular getenv: searches the process' environment for
00074 // a string of the form name=value and, if the string is present, returns
00075 // a pointer to the 'value' part of it.
00076 // If the string is not present, the default_value is returned, unless it
00077 // is nil.
00078 // If the default_value was nil and the string wasn't found,
00079 // the function prints the message that the name wasn't
00080 // found, and aborts the program
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  *              Print an error message at stderr and abort
00099  * Synopsis
00100  *      volatile void _error(const char * message,... );
00101  *      Message may contain format control sequences %x. Items to print 
00102  *      with the control sequences are to be passed as additional arguments to
00103  *      the function call.
00104  */
00105 
00106 void _error(const char * message,...)
00107 {
00108     static char smsg[2048];
00109     va_list args;
00110     va_start(args,message);             /* Init 'args' to the beginning of */
00111                                         /* the variable length list of args*/
00112     /*
00113       fprintf(stderr,"\n_error:\n");    
00114       vfprintf(stderr,message,args);
00115       fputs("\n",stderr);
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;     //SB!!
00126     
00127 #ifdef __MWERKS__
00128     exit(4);
00129 #else
00130     abort();
00131 #endif
00132 }
00133 
00134 
00135 /*
00136  *------------------------------------------------------------------------
00137  *                       Print a message at stderr
00138  * Synopsis
00139  *      void message(const char * text,... );
00140  * It looks and acts like printf(), only prints on stderr
00141  * (which is usually unbuffered...)
00142  */
00143 
00144 void message(const char * text,...)
00145 {
00146   va_list args;
00147   va_start(args,text);          /* Init 'args' to the beginning of */
00148                                         /* the variable length list of args*/
00149   vfprintf(stderr,text,args);
00150 }
00151 
00152 //------------------------------------------------------------------------
00153 //                      A logging service
00154 //
00155 // It filters and logs various system activities onto stderr
00156 // (unless it is set to log to something different)
00157 
00158 #if 0
00159 ostream_withassign Logger::log_stream(new filebuf(STDERR_FILENO));
00160 
00161                         // Set the log to append to a specified file
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;             // set new streambuf and delete the old one
00170 }
00171 #endif
00172 
00173 
00174 //------------------------------------------------------------------------
00175 //                      Obtaining the size of a file
00176 
00177                 // Default action when the file wasn't found/can't be
00178                 // accessed
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 //                  Patches to the standard environment
00199 
00200                                 // Like strncpy(), but ALWAYS terminates
00201                                 // the destination string
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                                 // Convert char c to lower case
00210                                 // Unlike traditional tolower(), it
00211                                 // applies conversion only if c is a letter
00212                                 // in uppercase
00213 static inline int to_lower(const char c)
00214 {
00215   return isupper(c) ? (c - 'A') + 'a' : c;
00216 }
00217 
00218                                 // Return TRUE if string s1 starts with s2
00219                                 // i.e., s2 is the prefix of s1
00220                                 // Case doesn't matter
00221 bool does_start_with_ci(const char * s1, const char * s2)
00222 {
00223   while( *s2 != '\0' )          // Alas, stricmp is not standard
00224     if( *s1 == '\0' )
00225       return false;                     // s1 is shorter than s2
00226     else if( to_lower(*s1++) != to_lower(*s2++) )
00227       return false;
00228   return true;
00229 }
00230 
00231 
00232                 // Convenient pow functions that are often missing
00233                 // They both compute x^y where y is an integer
00234                 // This code is based on target.c of GNU's F77 code, and sets
00235                 // the results in controversial cases as that code.
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                                 // libg++ nifty timing functions
00308                                 // Very rough and dirty implementation for
00309                                 // platforms w/o libg++
00310 static time_t time_set;
00311 
00312 double start_timer(void)
00313 {
00314   return time_set = time(0);
00315 }
00316                                 // return_elapsed_time(last_time) returns
00317                                 // process time (in secs) since Last_Time
00318                                 // If Last_time == 0.0, return time since
00319                                 // the last call to start_timer()
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;                          // timer wasn't started
00325   return new_time - (last_time == 0.0 ? time_set : last_time);
00326 }
00327 
00328 #endif
00329 }

Generated on Wed Dec 15 21:20:29 2004 for vuVolume by  doxygen 1.3.9.1