home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / stlport / stl / _time_facets.h < prev    next >
C/C++ Source or Header  |  2001-08-28  |  11KB  |  316 lines

  1. /*
  2.  * Copyright (c) 1999
  3.  * Silicon Graphics Computer Systems, Inc.
  4.  *
  5.  * Copyright (c) 1999 
  6.  * Boris Fomitchev
  7.  *
  8.  * This material is provided "as is", with absolutely no warranty expressed
  9.  * or implied. Any use is at your own risk.
  10.  *
  11.  * Permission to use or copy this software for any purpose is hereby granted 
  12.  * without fee, provided the above notices are retained on all copies.
  13.  * Permission to modify the code and to distribute modified code is granted,
  14.  * provided the above notices are retained, and a notice that the code was
  15.  * modified is included with the above copyright notice.
  16.  *
  17.  */ 
  18. // WARNING: This is an internal header file, included by other C++
  19. // standard library headers.  You should not attempt to use this header
  20. // file directly.
  21.  
  22.  
  23. #ifndef _STLP_INTERNAL_TIME_FACETS_H
  24. #define _STLP_INTERNAL_TIME_FACETS_H
  25.  
  26. #ifndef _STLP_CTIME
  27. # include <ctime>                // Needed (for struct tm) by time facets
  28. #endif
  29.  
  30. #include <stl/c_locale.h>
  31. #include <stl/_ios_base.h>
  32.  
  33. _STLP_BEGIN_NAMESPACE
  34.  
  35. // Template functions used by time_get
  36.  
  37. // Matching input against a list of names
  38.  
  39. // Alphabetic input of the names of months and the names
  40. // of weekdays requires matching input against a list of names.
  41. // We use a simple generic algorithm to accomplish this.  This
  42. // algorithm is not very efficient, especially for longer lists
  43. // of names, but it probably does not matter for the initial
  44. // implementation and it may never matter, since we do not expect
  45. // this kind of input to be used very often.  The algorithm
  46. // could be improved fairly simply by creating a new list of
  47. // names still in the running at each iteration.  A more sophisticated
  48. // approach would be to build a trie to do the matching.
  49. //
  50. // We compare each character of the input to the corresponding
  51. // character of each name on the list that has not been eliminated,
  52. // either because every character in the name has already been
  53. // matched, or because some character has not been matched.  We
  54. // continue only as long as there are some names that have not been
  55. // eliminated.
  56.  
  57. // We do not really need a random access iterator (a forward iterator
  58. // would do), but the extra generality makes the notation clumsier,
  59. // and we don't really need it.
  60.  
  61. // We can recognize a failed match by the fact that the second
  62. // component of the return value will be __name_end.
  63.  
  64. #define _MAXNAMES        64
  65. #define _MAX_NAME_LENGTH 64
  66.  
  67. // Both time_get and time_put need a structure of type _Time_Info
  68. // to provide names and abbreviated names for months and days,
  69. // as well as the am/pm designator.  The month and weekday tables
  70. // have the all the abbreviated names before all the full names.
  71. // The _Time_Info tables are initialized using the non-template
  72. // function _Init_timeinfo, which has two overloadings:  one
  73. // with a single reference parameter for the table to be initialized,
  74. // and one with a second _Locale_time * parameter.  The first form
  75. // is called by the default constructor and the second by a special
  76. // constructor invoked from the _byname subclass constructor to
  77. // construct the base class.
  78.  
  79. class _STLP_CLASS_DECLSPEC _Time_Info {
  80. public:
  81.   string _M_dayname[14];
  82.   string _M_monthname[24];
  83.   string _M_am_pm[2];
  84.   string _M_time_format;
  85.   string _M_date_format;
  86.   string _M_date_time_format;
  87.   string _M_long_date_format;
  88.   string _M_long_date_time_format;
  89. };
  90.  
  91. void _STLP_CALL _Init_timeinfo(_Time_Info&);
  92. void _STLP_CALL _Init_timeinfo(_Time_Info&, _Locale_time*);
  93.  
  94. class _STLP_CLASS_DECLSPEC time_base {
  95. public:
  96.   enum dateorder {no_order, dmy, mdy, ymd, ydm};
  97. };
  98.  
  99.  
  100. template <class _Ch, __DFL_TMPL_PARAM( _InIt , istreambuf_iterator<_Ch>) >
  101. class time_get : public locale::facet, public time_base 
  102. {
  103.   friend class _Locale;
  104.  
  105. public:
  106.   typedef _Ch   char_type;
  107.   typedef _InIt iter_type;
  108.  
  109.   explicit time_get(size_t __refs = 0)   : _BaseFacet(__refs) {
  110.       _Init_timeinfo(_M_timeinfo);
  111.   }
  112.   dateorder date_order() const { return do_date_order(); }
  113.   iter_type get_time(iter_type __s, iter_type  __end, ios_base&  __str,
  114.                      ios_base::iostate&  __err, tm* __t) const
  115.     { return do_get_time(__s,  __end,  __str,  __err, __t); }
  116.   iter_type get_date(iter_type __s, iter_type  __end, ios_base&  __str,
  117.                      ios_base::iostate&  __err, tm* __t) const
  118.     { return do_get_date(__s,  __end,  __str,  __err, __t); }
  119.   iter_type get_weekday(iter_type __s, iter_type  __end, ios_base&  __str,
  120.                         ios_base::iostate&  __err, tm* __t) const
  121.     { return do_get_weekday(__s,  __end,  __str,  __err, __t); }
  122.   iter_type get_monthname(iter_type __s, iter_type  __end, ios_base&  __str,
  123.                           ios_base::iostate&  __err, tm* __t) const
  124.     { return do_get_monthname(__s,  __end,  __str,  __err, __t); }
  125.   iter_type get_year(iter_type __s, iter_type  __end, ios_base&  __str,
  126.                      ios_base::iostate&  __err, tm* __t) const
  127.     { return do_get_year(__s,  __end,  __str,  __err, __t); }
  128.  
  129.   _STLP_STATIC_MEMBER_DECLSPEC static locale::id id;
  130.  
  131. protected:
  132.   _Time_Info _M_timeinfo;
  133.  
  134.   time_get(_Locale_time *, size_t __refs) : _BaseFacet(__refs) {}
  135.  
  136.   ~time_get() {}
  137.  
  138.   virtual dateorder do_date_order() const {return no_order;}
  139.     
  140.   virtual iter_type do_get_time(iter_type __s, iter_type  __end,
  141.                                 ios_base&, ios_base::iostate&  __err,
  142.                                 tm* __t) const;
  143.     
  144.   virtual iter_type do_get_date(iter_type __s, iter_type  __end,
  145.                                 ios_base&, ios_base::iostate& __err,
  146.                                 tm* __t) const;
  147.  
  148.   virtual iter_type do_get_weekday(iter_type __s, iter_type  __end,
  149.                                    ios_base&,
  150.                                    ios_base::iostate& __err,
  151.                                    tm* __t) const;
  152.   virtual iter_type do_get_monthname(iter_type __s, iter_type  __end,
  153.                                      ios_base&,
  154.                                      ios_base::iostate& __err,
  155.                                      tm* __t) const;
  156.   
  157.   virtual iter_type do_get_year(iter_type __s, iter_type  __end,
  158.                                 ios_base&, ios_base::iostate& __err,
  159.                                 tm* __t) const;
  160. };
  161.  
  162. time_base::dateorder _STLP_CALL
  163. __get_date_order(_Locale_time*);
  164. _Locale_time* _STLP_CALL __acquire_time(const char* __name);
  165. void          _STLP_CALL __release_time(_Locale_time* __time);
  166.  
  167. template <class _Ch, __DFL_TMPL_PARAM( _InIt , istreambuf_iterator<_Ch>) >
  168. class time_get_byname : public time_get<_Ch, _InIt> 
  169. {
  170. public:
  171.   typedef  time_base::dateorder dateorder;
  172.   typedef _InIt                 iter_type;
  173.  
  174.   explicit time_get_byname(const char* __name, size_t __refs = 0)
  175.     : time_get<_Ch, _InIt>((_Locale_time*) 0, __refs),
  176.       _M_time(__acquire_time(__name))
  177.     { _Init_timeinfo(this->_M_timeinfo, this->_M_time); }
  178.  
  179. protected:
  180.   ~time_get_byname() { __release_time(_M_time); }
  181.   dateorder do_date_order() const { return __get_date_order(_M_time); }
  182. private:
  183.   _Locale_time* _M_time;
  184. };
  185.  
  186. // time_put facet
  187.  
  188. // For the formats 'x, 'X', and 'c', do_put calls the first form of
  189. // put with the pattern obtained from _M_timeinfo._M_date_format or
  190. // _M_timeinfo._M_time_format.
  191.  
  192. // Helper function:  __  takes a single-character
  193. // format.  As indicated by the foregoing remark, this will never be
  194. // 'x', 'X', or 'c'.
  195.  
  196. char * _STLP_CALL
  197. __write_formatted_time(char * __buf, char __format, char __modifier,
  198.                        const _Time_Info& __table, const tm* __t);
  199.  
  200. template <class _OuIt>
  201. inline _OuIt _STLP_CALL __put_time(char * __first, char * __last, _OuIt __out,
  202.                                    const ios_base& /* __loc */, char) {
  203.     return copy(__first, __last, __out);
  204. }
  205.  
  206. # ifndef _STLP_NO_WCHAR_T
  207. template <class _OuIt>
  208. _OuIt _STLP_CALL __put_time(char * __first, char * __last, _OuIt __out,
  209.                             const ios_base& __s, wchar_t);
  210. # endif
  211.  
  212. template<class _Ch, __DFL_TMPL_PARAM( _OutputIter , ostreambuf_iterator<_Ch> ) >
  213. class time_put : public locale::facet, public time_base
  214. {
  215.   friend class _Locale;
  216. public:
  217.   typedef _Ch      char_type;
  218.   typedef _OutputIter iter_type;
  219.  
  220.   explicit time_put(size_t __refs = 0) : _BaseFacet(__refs) {
  221.     _Init_timeinfo(_M_timeinfo);
  222.   }
  223.  
  224.   _OutputIter put(iter_type __s, ios_base& __f, _Ch __fill,
  225.           const tm* __tmb,
  226.           const _Ch* __pat, const _Ch* __pat_end) const;
  227.   
  228.   _OutputIter put(iter_type __s, ios_base& __f, _Ch  __fill,
  229.           const tm* __tmb, char __format, char __modifier = 0) const { 
  230.     return do_put(__s, __f,  __fill, __tmb, __format, __modifier); 
  231.   }
  232.   
  233.   _STLP_STATIC_MEMBER_DECLSPEC static locale::id id;
  234.   
  235. protected:
  236.   _Time_Info _M_timeinfo;
  237.  
  238.   time_put(_Locale_time* /*__time*/, size_t __refs) : _BaseFacet(__refs) {
  239.     //    _Init_timeinfo(_M_timeinfo, __time);
  240.   }
  241.  
  242.   ~time_put() {}
  243.   virtual iter_type do_put(iter_type __s, ios_base& __f,
  244.                            char_type  /* __fill */, const tm* __tmb,
  245.                            char __format, char /* __modifier */) const;
  246. };
  247.  
  248. template <class _Ch, __DFL_TMPL_PARAM( _InIt , ostreambuf_iterator<_Ch> ) >
  249. class time_put_byname : public time_put<_Ch, _InIt> 
  250. {
  251.   friend class _Locale;
  252. public:
  253.   typedef time_base::dateorder dateorder;
  254.   typedef _InIt iter_type;
  255.   typedef _Ch   char_type;
  256.  
  257.   explicit time_put_byname(const char * __name, size_t __refs = 0)
  258.     : time_put<_Ch, _InIt>((_Locale_time*) 0, __refs),
  259.     _M_time(__acquire_time(__name))
  260.   { _Init_timeinfo(this->_M_timeinfo, this->_M_time); }
  261.   
  262. protected:
  263.   ~time_put_byname() { __release_time(_M_time); }
  264.  
  265. private:
  266.   _Locale_time* _M_time;
  267. };
  268.  
  269. # ifdef _STLP_USE_TEMPLATE_EXPORT
  270. _STLP_EXPORT_TEMPLATE_CLASS time_get<char, istreambuf_iterator<char, char_traits<char> > >;
  271. _STLP_EXPORT_TEMPLATE_CLASS time_put<char, ostreambuf_iterator<char, char_traits<char> > >;
  272. // _STLP_EXPORT_TEMPLATE_CLASS time_get<char, const char*>;
  273. // _STLP_EXPORT_TEMPLATE_CLASS time_put<char, char*>;
  274. #  ifndef _STLP_NO_WCHAR_T
  275. _STLP_EXPORT_TEMPLATE_CLASS time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >;
  276. _STLP_EXPORT_TEMPLATE_CLASS time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >;
  277. // _STLP_EXPORT_TEMPLATE_CLASS time_get<wchar_t, const wchar_t*>;
  278. // _STLP_EXPORT_TEMPLATE_CLASS time_put<wchar_t, wchar_t*>;
  279. #  endif /* INSTANTIATE_WIDE_STREAMS */
  280.  
  281. # endif
  282.  
  283. # if defined (__BORLANDC__) && defined (_RTLDLL)
  284. inline void _Stl_loc_init_time_facets() {
  285.   
  286.   time_get<char, istreambuf_iterator<char, char_traits<char> > >::id._M_index                      = 16;
  287.   time_get<char, const char*>::id._M_index         = 17;
  288.   time_put<char, ostreambuf_iterator<char, char_traits<char> > >::id._M_index                      = 18;
  289.   time_put<char, char*>::id._M_index               = 19;
  290.   
  291. # ifndef _STLP_NO_WCHAR_T
  292.   
  293.   time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id._M_index                   = 35;
  294.   time_get<wchar_t, const wchar_t*>::id._M_index   = 36;
  295.   time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id._M_index                   = 37;
  296.   time_put<wchar_t, wchar_t*>::id._M_index         = 38;
  297.   
  298. # endif
  299.   
  300. }
  301. # endif
  302.  
  303. _STLP_END_NAMESPACE
  304.  
  305. #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
  306. #  include <stl/_time_facets.c>
  307. # endif
  308.  
  309. #endif /* _STLP_INTERNAL_TIME_FACETS_H */
  310.  
  311. // Local Variables:
  312. // mode:C++
  313. // End:
  314.  
  315.  
  316.