home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / c++advio 2.3 / Advanced i⁄o / myenv.cc < prev    next >
Encoding:
Text File  |  1997-03-05  |  5.2 KB  |  198 lines  |  [TEXT/ALFA]

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /*
  3.  ************************************************************************
  4.  *            Service C++ functions 
  5.  *         that support the standard environment for me
  6.  *
  7.  * $Id: myenv.cc,v 2.0 1997/03/04 20:27:43 oleg Exp oleg $
  8.  */
  9.  
  10. #ifdef __GNUC__
  11. #pragma implementation
  12. #endif
  13.  
  14. #include "myenv.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <stdarg.h>
  19. #include <ctype.h>
  20. extern "C"
  21. {
  22. #ifdef macintosh
  23. #include <types.h>
  24. #include <stat.h>
  25. #else
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #endif
  29. int stat(const char * path, struct stat * buf);
  30. }
  31.  
  32. /*
  33.  *-----------------------------------------------------------------------
  34.  *        Some global constant pertaining to input/output
  35.  */
  36.  
  37. const char _Minuses [] = "\
  38. -------------------------------------------------------------------------------";
  39.  
  40. const char _Asteriscs [] = "\
  41. *******************************************************************************";
  42.  
  43. const char _Equals [] = "\
  44. ===============================================================================";
  45.  
  46. //------------------------------------------------------------------------
  47. //                A better getenv
  48. //
  49. // It works just as a regular getenv: searches the process' environment for
  50. // a string of the form name=value and, if the string is present, returns
  51. // a pointer to the 'value' part of it.
  52. // If the string is not present, the default_value is returned, unless it
  53. // is nil.
  54. // If the default_value was nil and the string wasn't found,
  55. // the function prints the message that the name wasn't
  56. // found, and aborts the program
  57.  
  58. const char * xgetenv(const char * name, const char * default_value)
  59. {
  60.   const char * env_value = ::getenv(name);
  61.   if( env_value != 0 )
  62.     return env_value;
  63.   else if( default_value != 0 )
  64.     return default_value;
  65.   else
  66.     return _error("xgetenv: env variable '%s' wasn't found, but was required",
  67.                   name), (const char *)0;
  68. }
  69.  
  70.  
  71.  
  72. /*
  73.  *------------------------------------------------------------------------
  74.  *            Print an error message at stderr and abort
  75.  * Synopsis
  76.  *    volatile void _error(const char * message,... );
  77.  *    Message may contain format control sequences %x. Items to print 
  78.  *    with the control sequences are to be passed as additional arguments to
  79.  *    the function call.
  80.  */
  81.  
  82. void _error(const char * message,...)
  83. {
  84.   va_list args;
  85.   va_start(args,message);        /* Init 'args' to the beginning of */
  86.                     /* the variable length list of args*/
  87.   fprintf(stderr,"\n_error:\n");     
  88.   vfprintf(stderr,message,args);
  89.   fputs("\n",stderr);
  90. #ifdef __MWERKS__
  91.   exit(4);
  92. #else  
  93.   abort();
  94. #endif
  95. }
  96.  
  97.  
  98. /*
  99.  *------------------------------------------------------------------------
  100.  *                    Print a message at stderr
  101.  * Synopsis
  102.  *    void message(const char * text,... );
  103.  * It looks and acts like printf(), only prints on stderr
  104.  * (which is usually unbuffered...)
  105.  */
  106.  
  107. void message(const char * text,...)
  108. {
  109.   va_list args;
  110.   va_start(args,text);        /* Init 'args' to the beginning of */
  111.                     /* the variable length list of args*/
  112.   vfprintf(stderr,text,args);
  113. }
  114.  
  115. //------------------------------------------------------------------------
  116. //            Obtaining the size of a file
  117.  
  118.         // Default action when the file wasn't found/can't be
  119.         // accessed
  120. size_t GFS_Default::operator () (const char * file_name)
  121. {
  122.   perror("getting file status");
  123.   _error("Failed to get status of the file <%s> because of the error "
  124.          "above",file_name);
  125.   return (size_t)EOF;
  126. }
  127.  
  128. GFS_Default GFS_default;
  129.  
  130. size_t get_file_size(const char * file_name, GFS_Default& on_error)
  131. {
  132.   struct stat file_status;
  133.   if( stat(file_name,&file_status) != 0 )
  134.     return on_error(file_name);
  135.   return file_status.st_size;
  136. }
  137.  
  138. //------------------------------------------------------------------------
  139. //            Patches to the standard environment
  140.  
  141.                                 // Like strncpy(), but ALWAYS terminates
  142.                                 // the destination string
  143. char * xstrncpy(char * dest, const char * src, const int len)
  144. {
  145.   strncpy(dest,src,len);
  146.   dest[len] = '\0';
  147.   return dest;
  148. }
  149.  
  150.                 // Convert char c to lower case
  151.                 // Unlike traditional tolower(), it
  152.                 // applies conversion only if c is a letter
  153.                 // in uppercase
  154. static inline int to_lower(const char c)
  155. {
  156.   return isupper(c) ? (c - 'A') + 'a' : c;
  157. }
  158.  
  159.                 // Return TRUE if string s1 starts with s2
  160.                 // i.e., s2 is the prefix of s1
  161.                 // Case doesn't matter
  162. bool does_start_with_ci(const char * s1, const char * s2)
  163. {
  164.   while( *s2 != '\0' )        // Alas, stricmp is not standard
  165.     if( *s1 == '\0' )
  166.       return false;            // s1 is shorter than s2
  167.     else if( to_lower(*s1++) != to_lower(*s2++) )
  168.       return false;
  169.   return true;
  170. }
  171.  
  172.  
  173. #ifndef __GNUC__
  174. #include <time.h>
  175.  
  176.                 // libg++ nifty timing functions
  177.                 // Very rough and dirty implementation for
  178.                 // platforms w/o libg++
  179. static time_t time_set;
  180.  
  181. double start_timer(void)
  182. {
  183.   return time_set = time(0);
  184. }
  185.                 // return_elapsed_time(last_time) returns
  186.                 // process time (in secs) since Last_Time
  187.                 // If Last_time == 0.0, return time since
  188.                 // the last call to start_timer()
  189. double return_elapsed_time(const double last_time)
  190. {
  191.   time_t new_time = time(0);
  192.   if( time_set == 0 )
  193.     return -1;                // timer wasn't started
  194.   return new_time - (last_time == 0.0 ? time_set : last_time);
  195. }
  196.  
  197. #endif
  198.