Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

BdbTimeInput.cc

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id: BdbTimeInput.cc,v 1.2 2004/08/06 05:48:02 bartoldu Exp $
00004 //
00005 // Description:
00006 //      BdbTimeInput implementation
00007 //
00008 // Environment:
00009 //      Software developed for the BaBar Detector at the SLAC B-Factory.
00010 //
00011 // Author List:
00012 //      Gregory Dubois-Felsmann  (originator)
00013 //
00014 // Copyright Information:
00015 //      Copyright (C) 2003   California Institute of Technology
00016 //
00017 //------------------------------------------------------------------------
00018 
00019 // Get the "non-zeroing" behavior of strptime() on Solaris.
00020 // It's the default on Linux.
00021 // 
00022 // Note that Unix98 says that one should not rely on one or the other
00023 // behavior, but that portable applications should do conversions as
00024 // single unit and make no assumptions.  If this proves to be a problem
00025 // in the future we could rewrite this code in a more complicated way
00026 // to avoid relying on non-zeroing behavior.  In the mean time we do
00027 // provide a regression test that checks this.                        FIXME
00028 #ifdef __SUNPRO_CC
00029 #define _STRPTIME_DONTZERO
00030 #endif
00031 
00032 #include "BaBar/BaBar.hh"
00033 
00034 //-----------------------
00035 // This Class's Header --
00036 //-----------------------
00037 #include "BdbTime/BdbTimeInput.hh"
00038 
00039 //-----------------
00040 // C/C++ Headers --
00041 //-----------------
00042 #include <string.h>
00043 #include <iostream>
00044 using std::cerr;
00045 using std::endl;
00046 
00047 //-------------------------------
00048 // Collaborating Class Headers --
00049 //-------------------------------
00050 
00051 //-----------------------------------------------------------------------
00052 // Local Macros, Typedefs, Structures, Unions and Forward Declarations --
00053 //-----------------------------------------------------------------------
00054 
00055 //              ----------------------------------------
00056 //              -- Public Function Member Definitions --
00057 //              ----------------------------------------
00058 
00059 //----------------
00060 // Constructors --
00061 //----------------
00062 
00063 //--------------
00064 // Destructor --
00065 //--------------
00066 
00067 //-------------
00068 // Methods   --
00069 //-------------
00070     
00071 //-------------
00072 // Operators --
00073 //-------------
00074     
00075 //-------------
00076 // Selectors --
00077 //-------------
00078     
00079 //-------------
00080 // Modifiers --
00081 //-------------
00082 
00083 //              -----------------------------------------------
00084 //              -- Static Data & Function Member Definitions --
00085 //              -----------------------------------------------
00086 
00087 BdbTimeInput::Status
00088 BdbTimeInput::parseTime( const char* string, 
00089                          struct tm&  result )
00090 {
00091   return BdbTimeInput::parseString( string, timeFormats_, result, debug_ );
00092 }
00093 
00094 
00095 BdbTimeInput::Status
00096 BdbTimeInput::parseDate( const char* string, 
00097                          struct tm&  result )
00098 {
00099   return BdbTimeInput::parseString( string, dateFormats_, result, debug_ );
00100 }
00101 
00102 
00103 BdbTimeInput::Status
00104 BdbTimeInput::parseString( const char* string, 
00105                            const char* const formats[], 
00106                            struct tm&  result,
00107                            bool debug )
00108 {
00109   // Find the end of the input string.  This is needed in order to be
00110   // sure that strptime has parsed all of it.
00111   if ( debug ) {
00112     cerr << "BdbTimeInput::parseString: strlen(\"" << string << "\") is "
00113          << strlen(string) << endl;
00114   }
00115   const char* const stringEnd = string + strlen(string);
00116 
00117   BdbTimeInput::Status status = BdbTimeInput::NoMatch;
00118   for ( const char* const* fmt = formats; *fmt != 0; ++fmt ) {
00119     if ( debug ) {
00120       cerr << "Trying format \"" << *fmt << "\":";
00121     }
00122     const char* last = strptime( string, *fmt, &result );
00123     if ( last == stringEnd ) {
00124       status = BdbTimeInput::Success;
00125       if ( debug ) {
00126         cerr << " OK!" << endl;
00127       }
00128       break;
00129     }
00130     else if ( debug ) {
00131       if ( last == 0 ) {
00132         cerr << " returned null pointer; failure." << endl;
00133       }
00134       else {
00135         cerr << " stopped at position " << (last-string);
00136         if ( *last == '\0' ) {
00137           cerr << " = '\0'; strange!" << endl;
00138         }
00139         else {
00140           cerr << " = '" << *last << "'; failure." << endl;
00141         }
00142       }
00143     }
00144   }
00145   if ( debug && status != BdbTimeInput::Success ) {
00146     cerr << "Tried all patterns without success." << endl;
00147   }
00148 
00149   return status;
00150 }
00151 
00152 void
00153 BdbTimeInput::setDebugging( bool debug ) 
00154 {
00155   debug_ = debug;
00156 }
00157 
00158 const char*
00159 BdbTimeInput::defaultTimeFormats_[] = 
00160   {
00161     "%T"     // hh:mm:ss
00162     , "%R"   // hh:mm
00163     , 0
00164   };
00165 
00166 const char* const *
00167 BdbTimeInput::timeFormats_ = BdbTimeInput::defaultTimeFormats_;
00168 
00169 const char*
00170 BdbTimeInput::defaultDateFormats_[] = 
00171   {
00172     "%D"     // mm/dd/yy
00173     , "%d%b%Y" // ddMmmyyyy
00174     , 0
00175   };
00176 
00177 const char* const *
00178 BdbTimeInput::dateFormats_ = BdbTimeInput::defaultDateFormats_;
00179 
00180 bool
00181 BdbTimeInput::debug_ = false;
00182 
00183 //              -------------------------------------------
00184 //              -- Protected Function Member Definitions --
00185 //              -------------------------------------------
00186 
00187 //              -----------------------------------------
00188 //              -- Private Function Member Definitions --
00189 //              -----------------------------------------
00190 
00191 //              -----------------------------------
00192 //              -- Internal Function Definitions --
00193 //              -----------------------------------

Generated on Mon Dec 5 18:21:58 2005 for CDB by doxygen1.3-rc3