_time_facets.h

00001 /*
00002  * Copyright (c) 1999
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Copyright (c) 1999 
00006  * Boris Fomitchev
00007  *
00008  * This material is provided "as is", with absolutely no warranty expressed
00009  * or implied. Any use is at your own risk.
00010  *
00011  * Permission to use or copy this software for any purpose is hereby granted 
00012  * without fee, provided the above notices are retained on all copies.
00013  * Permission to modify the code and to distribute modified code is granted,
00014  * provided the above notices are retained, and a notice that the code was
00015  * modified is included with the above copyright notice.
00016  *
00017  */ 
00018 // WARNING: This is an internal header file, included by other C++
00019 // standard library headers.  You should not attempt to use this header
00020 // file directly.
00021 
00022 
00023 #ifndef _STLP_INTERNAL_TIME_FACETS_H
00024 #define _STLP_INTERNAL_TIME_FACETS_H
00025 
00026 #ifndef _STLP_CTIME
00027 # include <ctime>                // Needed (for struct tm) by time facets
00028 #endif
00029 
00030 #include <stl/c_locale.h>
00031 #include <stl/_ios_base.h>
00032 
00033 _STLP_BEGIN_NAMESPACE
00034 
00035 // Template functions used by time_get
00036 
00037 // Matching input against a list of names
00038 
00039 // Alphabetic input of the names of months and the names
00040 // of weekdays requires matching input against a list of names.
00041 // We use a simple generic algorithm to accomplish this.  This
00042 // algorithm is not very efficient, especially for longer lists
00043 // of names, but it probably does not matter for the initial
00044 // implementation and it may never matter, since we do not expect
00045 // this kind of input to be used very often.  The algorithm
00046 // could be improved fairly simply by creating a new list of
00047 // names still in the running at each iteration.  A more sophisticated
00048 // approach would be to build a trie to do the matching.
00049 //
00050 // We compare each character of the input to the corresponding
00051 // character of each name on the list that has not been eliminated,
00052 // either because every character in the name has already been
00053 // matched, or because some character has not been matched.  We
00054 // continue only as long as there are some names that have not been
00055 // eliminated.
00056 
00057 // We do not really need a random access iterator (a forward iterator
00058 // would do), but the extra generality makes the notation clumsier,
00059 // and we don't really need it.
00060 
00061 // We can recognize a failed match by the fact that the second
00062 // component of the return value will be __name_end.
00063 
00064 #define _MAXNAMES        64
00065 #define _MAX_NAME_LENGTH 64
00066 
00067 // Both time_get and time_put need a structure of type _Time_Info
00068 // to provide names and abbreviated names for months and days,
00069 // as well as the am/pm designator.  The month and weekday tables
00070 // have the all the abbreviated names before all the full names.
00071 // The _Time_Info tables are initialized using the non-template
00072 // function _Init_timeinfo, which has two overloadings:  one
00073 // with a single reference parameter for the table to be initialized,
00074 // and one with a second _Locale_time * parameter.  The first form
00075 // is called by the default constructor and the second by a special
00076 // constructor invoked from the _byname subclass constructor to
00077 // construct the base class.
00078 
00079 class _STLP_CLASS_DECLSPEC _Time_Info {
00080 public:
00081   string _M_dayname[14];
00082   string _M_monthname[24];
00083   string _M_am_pm[2];
00084   string _M_time_format;
00085   string _M_date_format;
00086   string _M_date_time_format;
00087   string _M_long_date_format;
00088   string _M_long_date_time_format;
00089 };
00090 
00091 void _STLP_CALL _Init_timeinfo(_Time_Info&);
00092 void _STLP_CALL _Init_timeinfo(_Time_Info&, _Locale_time*);
00093 
00094 class _STLP_CLASS_DECLSPEC time_base {
00095 public:
00096   enum dateorder {no_order, dmy, mdy, ymd, ydm};
00097 };
00098 
00099 
00100 template <class _Ch, __DFL_TMPL_PARAM( _InIt , istreambuf_iterator<_Ch>) >
00101 class time_get : public locale::facet, public time_base 
00102 {
00103   friend class _Locale;
00104 
00105 public:
00106   typedef _Ch   char_type;
00107   typedef _InIt iter_type;
00108 
00109   explicit time_get(size_t __refs = 0)   : _BaseFacet(__refs) {
00110       _Init_timeinfo(_M_timeinfo);
00111   }
00112   dateorder date_order() const { return do_date_order(); }
00113   iter_type get_time(iter_type __s, iter_type  __end, ios_base&  __str,
00114                      ios_base::iostate&  __err, tm* __t) const
00115     { return do_get_time(__s,  __end,  __str,  __err, __t); }
00116   iter_type get_date(iter_type __s, iter_type  __end, ios_base&  __str,
00117                      ios_base::iostate&  __err, tm* __t) const
00118     { return do_get_date(__s,  __end,  __str,  __err, __t); }
00119   iter_type get_weekday(iter_type __s, iter_type  __end, ios_base&  __str,
00120                         ios_base::iostate&  __err, tm* __t) const
00121     { return do_get_weekday(__s,  __end,  __str,  __err, __t); }
00122   iter_type get_monthname(iter_type __s, iter_type  __end, ios_base&  __str,
00123                           ios_base::iostate&  __err, tm* __t) const
00124     { return do_get_monthname(__s,  __end,  __str,  __err, __t); }
00125   iter_type get_year(iter_type __s, iter_type  __end, ios_base&  __str,
00126                      ios_base::iostate&  __err, tm* __t) const
00127     { return do_get_year(__s,  __end,  __str,  __err, __t); }
00128 
00129   _STLP_STATIC_MEMBER_DECLSPEC static locale::id id;
00130 
00131 protected:
00132   _Time_Info _M_timeinfo;
00133 
00134   time_get(_Locale_time *, size_t __refs) : _BaseFacet(__refs) {}
00135 
00136   ~time_get() {}
00137 
00138   virtual dateorder do_date_order() const {return no_order;}
00139     
00140   virtual iter_type do_get_time(iter_type __s, iter_type  __end,
00141                                 ios_base&, ios_base::iostate&  __err,
00142                                 tm* __t) const;
00143     
00144   virtual iter_type do_get_date(iter_type __s, iter_type  __end,
00145                                 ios_base&, ios_base::iostate& __err,
00146                                 tm* __t) const;
00147 
00148   virtual iter_type do_get_weekday(iter_type __s, iter_type  __end,
00149                                    ios_base&,
00150                                    ios_base::iostate& __err,
00151                                    tm* __t) const;
00152   virtual iter_type do_get_monthname(iter_type __s, iter_type  __end,
00153                                      ios_base&,
00154                                      ios_base::iostate& __err,
00155                                      tm* __t) const;
00156   
00157   virtual iter_type do_get_year(iter_type __s, iter_type  __end,
00158                                 ios_base&, ios_base::iostate& __err,
00159                                 tm* __t) const;
00160 };
00161 
00162 time_base::dateorder _STLP_CALL
00163 __get_date_order(_Locale_time*);
00164 _Locale_time* _STLP_CALL __acquire_time(const char* __name);
00165 void          _STLP_CALL __release_time(_Locale_time* __time);
00166 
00167 template <class _Ch, __DFL_TMPL_PARAM( _InIt , istreambuf_iterator<_Ch>) >
00168 class time_get_byname : public time_get<_Ch, _InIt> 
00169 {
00170 public:
00171   typedef  time_base::dateorder dateorder;
00172   typedef _InIt                 iter_type;
00173 
00174   explicit time_get_byname(const char* __name, size_t __refs = 0)
00175     : time_get<_Ch, _InIt>((_Locale_time*) 0, __refs),
00176       _M_time(__acquire_time(__name))
00177     { _Init_timeinfo(this->_M_timeinfo, this->_M_time); }
00178 
00179 protected:
00180   ~time_get_byname() { __release_time(_M_time); }
00181   dateorder do_date_order() const { return __get_date_order(_M_time); }
00182 private:
00183   _Locale_time* _M_time;
00184 };
00185 
00186 // time_put facet
00187 
00188 // For the formats 'x, 'X', and 'c', do_put calls the first form of
00189 // put with the pattern obtained from _M_timeinfo._M_date_format or
00190 // _M_timeinfo._M_time_format.
00191 
00192 // Helper function:  __  takes a single-character
00193 // format.  As indicated by the foregoing remark, this will never be
00194 // 'x', 'X', or 'c'.
00195 
00196 char * _STLP_CALL
00197 __write_formatted_time(char * __buf, char __format, char __modifier,
00198                        const _Time_Info& __table, const tm* __t);
00199 
00200 template <class _OuIt>
00201 inline _OuIt _STLP_CALL __put_time(char * __first, char * __last, _OuIt __out,
00202                                    const ios_base& /* __loc */, char) {
00203     return copy(__first, __last, __out);
00204 }
00205 
00206 # ifndef _STLP_NO_WCHAR_T
00207 template <class _OuIt>
00208 _OuIt _STLP_CALL __put_time(char * __first, char * __last, _OuIt __out,
00209                             const ios_base& __s, wchar_t);
00210 # endif
00211 
00212 template<class _Ch, __DFL_TMPL_PARAM( _OutputIter , ostreambuf_iterator<_Ch> ) >
00213 class time_put : public locale::facet, public time_base
00214 {
00215   friend class _Locale;
00216 public:
00217   typedef _Ch      char_type;
00218   typedef _OutputIter iter_type;
00219 
00220   explicit time_put(size_t __refs = 0) : _BaseFacet(__refs) {
00221     _Init_timeinfo(_M_timeinfo);
00222   }
00223 
00224   _OutputIter put(iter_type __s, ios_base& __f, _Ch __fill,
00225                   const tm* __tmb,
00226                   const _Ch* __pat, const _Ch* __pat_end) const;
00227   
00228   _OutputIter put(iter_type __s, ios_base& __f, _Ch  __fill,
00229                   const tm* __tmb, char __format, char __modifier = 0) const { 
00230     return do_put(__s, __f,  __fill, __tmb, __format, __modifier); 
00231   }
00232   
00233   _STLP_STATIC_MEMBER_DECLSPEC static locale::id id;
00234   
00235 protected:
00236   _Time_Info _M_timeinfo;
00237 
00238   time_put(_Locale_time* /*__time*/, size_t __refs) : _BaseFacet(__refs) {
00239     //    _Init_timeinfo(_M_timeinfo, __time);
00240   }
00241 
00242   ~time_put() {}
00243   virtual iter_type do_put(iter_type __s, ios_base& __f,
00244                            char_type  /* __fill */, const tm* __tmb,
00245                            char __format, char /* __modifier */) const;
00246 };
00247 
00248 template <class _Ch, __DFL_TMPL_PARAM( _InIt , ostreambuf_iterator<_Ch> ) >
00249 class time_put_byname : public time_put<_Ch, _InIt> 
00250 {
00251   friend class _Locale;
00252 public:
00253   typedef time_base::dateorder dateorder;
00254   typedef _InIt iter_type;
00255   typedef _Ch   char_type;
00256 
00257   explicit time_put_byname(const char * __name, size_t __refs = 0)
00258     : time_put<_Ch, _InIt>((_Locale_time*) 0, __refs),
00259     _M_time(__acquire_time(__name))
00260   { _Init_timeinfo(this->_M_timeinfo, this->_M_time); }
00261   
00262 protected:
00263   ~time_put_byname() { __release_time(_M_time); }
00264 
00265 private:
00266   _Locale_time* _M_time;
00267 };
00268 
00269 # ifdef _STLP_USE_TEMPLATE_EXPORT
00270 _STLP_EXPORT_TEMPLATE_CLASS time_get<char, istreambuf_iterator<char, char_traits<char> > >;
00271 _STLP_EXPORT_TEMPLATE_CLASS time_put<char, ostreambuf_iterator<char, char_traits<char> > >;
00272 // _STLP_EXPORT_TEMPLATE_CLASS time_get<char, const char*>;
00273 // _STLP_EXPORT_TEMPLATE_CLASS time_put<char, char*>;
00274 #  ifndef _STLP_NO_WCHAR_T
00275 _STLP_EXPORT_TEMPLATE_CLASS time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >;
00276 _STLP_EXPORT_TEMPLATE_CLASS time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >;
00277 // _STLP_EXPORT_TEMPLATE_CLASS time_get<wchar_t, const wchar_t*>;
00278 // _STLP_EXPORT_TEMPLATE_CLASS time_put<wchar_t, wchar_t*>;
00279 #  endif /* INSTANTIATE_WIDE_STREAMS */
00280 
00281 # endif
00282 
00283 # if defined (__BORLANDC__) && defined (_RTLDLL)
00284 inline void _Stl_loc_init_time_facets() {
00285   
00286   time_get<char, istreambuf_iterator<char, char_traits<char> > >::id._M_index                      = 16;
00287   time_get<char, const char*>::id._M_index         = 17;
00288   time_put<char, ostreambuf_iterator<char, char_traits<char> > >::id._M_index                      = 18;
00289   time_put<char, char*>::id._M_index               = 19;
00290   
00291 # ifndef _STLP_NO_WCHAR_T
00292   
00293   time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id._M_index                   = 35;
00294   time_get<wchar_t, const wchar_t*>::id._M_index   = 36;
00295   time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >::id._M_index                   = 37;
00296   time_put<wchar_t, wchar_t*>::id._M_index         = 38;
00297   
00298 # endif
00299   
00300 }
00301 # endif
00302 
00303 _STLP_END_NAMESPACE
00304 
00305 #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
00306 #  include <stl/_time_facets.c>
00307 # endif
00308 
00309 #endif /* _STLP_INTERNAL_TIME_FACETS_H */
00310 
00311 // Local Variables:
00312 // mode:C++
00313 // End:
00314 
00315 

Generated on Mon Jun 5 10:20:47 2006 for Intelligence.kdevelop by  doxygen 1.4.6