home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / classsrc.pak / DATEIO.CPP < prev    next >
C/C++ Source or Header  |  1997-07-23  |  5KB  |  171 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  DATEIO.CPP                                                            */
  4. /*                                                                        */
  5. /*  Copyright (c) 1993, 1994 Borland International                        */
  6. /*  All Rights Reserved                                                   */
  7. /*                                                                        */
  8. /*------------------------------------------------------------------------*/
  9.  
  10. #if !defined( __STDIO_H )
  11. #include <stdio.h>
  12. #endif
  13.  
  14. #if !defined( __CTYPE_H )
  15. #include <ctype.h>
  16. #endif
  17.  
  18. #if !defined( __STRSTREA_H )
  19. #include <strstrea.h>
  20. #endif
  21.  
  22. #if !defined( __CSTRING_H )
  23. #include <cstring.h>
  24. #endif
  25.  
  26. #if !defined( CLASSLIB_DATE_H )
  27. #include <classlib/date.h>
  28. #endif
  29.  
  30. TDate::HowToPrint TDate::PrintOption = TDate::Normal;
  31.  
  32. string TDate::AsString() const
  33. {
  34.     char buf[80];
  35.     ostrstream strtemp(buf, sizeof(buf));
  36.     strtemp << (*this) << ends;
  37.     string temp(buf);
  38.     return temp;
  39. }
  40.  
  41. TDate::HowToPrint TDate::SetPrintOption( HowToPrint h )
  42. {
  43.     HowToPrint oldoption = PrintOption;
  44.     PrintOption = h;
  45.     return oldoption;
  46. }
  47.  
  48. // Skip any characters except alphanumeric characters
  49. static void _BIDSNEARFUNC SkipDelim( istream _BIDSFAR & strm )
  50. {
  51.     char c;
  52.     if( !strm.good() )
  53.         return;
  54.  
  55.     do  {
  56.         strm >> c;
  57.         } while (strm.good() && !isalnum(c)) ;
  58.  
  59.     if (strm.good())
  60.         strm.putback(c);
  61. }
  62.  
  63. // Parse the name of a month from input stream.
  64. static const char* _BIDSNEARFUNC ParseMonth( istream _BIDSFAR & s )
  65. {
  66.     static char month[12];
  67.     register char* p = month;
  68.     char c;
  69.     SkipDelim(s);
  70.     s.get(c);
  71.     while (s.good() && isalpha(c) && (p != &month[10]))
  72.         {
  73.         *p++ = c;
  74.         s.get(c);
  75.         }
  76.     if( s.good() )
  77.         s.putback(c);
  78.     *p = '\0';
  79.     return month;
  80. }
  81.  
  82. //  Parse a date from the specified input stream.  
  83. //    The date must be in one of the following forms: 
  84. //                dd-mmm-yy, mm/dd/yy, or mmm dd,yy
  85. //        e.g.: 10-MAR-86,  3/10/86, or March 10, 1986.  
  86. //  Any non-alphanumeric character may be used as a delimiter.
  87.  
  88.  
  89. void TDate::ParseFrom( istream _BIDSFAR & s )
  90. {
  91.     unsigned d,m,y;
  92.     Julnum = 0;                 // Assume failure
  93.  
  94.     if (s.good())
  95.         {
  96.         SkipDelim(s);
  97.         s >> m;                 // try to parse day or month number 
  98.         SkipDelim(s);
  99.         if (s.eof())
  100.             return;
  101.         if( s.fail() )          // parse <monthName><day><year> 
  102.             {
  103.             s.clear();
  104.             m = IndexOfMonth(ParseMonth(s)); // parse month name
  105.             SkipDelim(s);
  106.             s >> d;                 // parse day 
  107.             }
  108.         else                        // try to parse day number 
  109.             {
  110.             s >> d;
  111.             if (s.eof()) return;
  112.             if (s.fail())           // parse <day><monthName><year> 
  113.                 {
  114.                 d = m;
  115.                 s.clear();
  116.                 m = IndexOfMonth(ParseMonth(s)); // parse month name
  117.                 }
  118.             }
  119.         SkipDelim(s);
  120.         s >> y;
  121.         }
  122.     Julnum = s.good() ? Jday(m, d, y) : 0;
  123.     if(Julnum==0)
  124.         s.clear(ios::badbit);
  125. }
  126.  
  127. ostream _BIDSFAR & _BIDSFUNC operator << ( ostream _BIDSFAR & s, const TDate _BIDSFAR & d )
  128. {
  129.     char buf[80];
  130.  
  131.     // we use an ostrstream to format into buf so that
  132.     // we don't affect the ostream's width setting.
  133.     ostrstream out( buf, sizeof(buf) );
  134.   
  135.     switch ( TDate::PrintOption )
  136.         {
  137.         case TDate::Normal:
  138.             out << d.NameOfMonth() << " " 
  139.                 << d.DayOfMonth()  << ", "
  140.                 << d.Year() << ends;
  141.             break;
  142.         case TDate::Terse:
  143.             sprintf(buf,"%2u-%.3s-%.2u",
  144.                     d.DayOfMonth(),
  145.                     d.NameOfMonth(),
  146.                     d.Year() % 100);
  147.             break;
  148.         case TDate::Numbers:
  149.             out << d.Month() << "/"
  150.                 << d.DayOfMonth() << "/"
  151.                 << (d.Year() % 100) << ends;
  152.             break;
  153.         case TDate::EuropeanNumbers:
  154.             out << d.DayOfMonth()   << "/"
  155.                 << d.Month() <<"/"
  156.                 << (d.Year() % 100) << ends;
  157.             break;
  158.         case TDate::European:
  159.             out << d.DayOfMonth() << " "
  160.                 << d.NameOfMonth() << " " 
  161.                 << d.Year() << ends;
  162.             break;
  163.         };
  164.  
  165.     // now we write out the formatted buffer, and the ostream's
  166.     // width setting will control the actual width of the field.
  167.     s << buf;
  168.     return s;
  169. }
  170.  
  171.