/home/ntakagi/work/STLport-5.1.5/stlport/stl/_time_facets.h

Go to the documentation of this file.
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_INTERNAL_CTIME
00027 #  include <stl/_ctime.h>                // Needed (for struct tm) by time facets
00028 #endif
00029 
00030 #ifndef _STLP_C_LOCALE_H
00031 #  include <stl/c_locale.h>
00032 #endif
00033 
00034 #ifndef _STLP_IOS_BASE_H
00035 #  include <stl/_ios_base.h>
00036 #endif
00037 
00038 _STLP_BEGIN_NAMESPACE
00039 
00040 _STLP_MOVE_TO_PRIV_NAMESPACE
00041 
00042 // Template functions used by time_get
00043 
00044 // Matching input against a list of names
00045 
00046 // Alphabetic input of the names of months and the names
00047 // of weekdays requires matching input against a list of names.
00048 // We use a simple generic algorithm to accomplish this.  This
00049 // algorithm is not very efficient, especially for longer lists
00050 // of names, but it probably does not matter for the initial
00051 // implementation and it may never matter, since we do not expect
00052 // this kind of input to be used very often.  The algorithm
00053 // could be improved fairly simply by creating a new list of
00054 // names still in the running at each iteration.  A more sophisticated
00055 // approach would be to build a trie to do the matching.
00056 //
00057 // We compare each character of the input to the corresponding
00058 // character of each name on the list that has not been eliminated,
00059 // either because every character in the name has already been
00060 // matched, or because some character has not been matched.  We
00061 // continue only as long as there are some names that have not been
00062 // eliminated.
00063 
00064 // We do not really need a random access iterator (a forward iterator
00065 // would do), but the extra generality makes the notation clumsier,
00066 // and we don't really need it.
00067 
00068 // We can recognize a failed match by the fact that the second
00069 // component of the return value will be __name_end.
00070 
00071 #define _MAXNAMES        64
00072 #define _MAX_NAME_LENGTH 64
00073 
00074 // Both time_get and time_put need a structure of type _Time_Info
00075 // to provide names and abbreviated names for months and days,
00076 // as well as the am/pm designator.  The month and weekday tables
00077 // have the all the abbreviated names before all the full names.
00078 // The _Time_Info tables are initialized using the non-template
00079 // function _Init_timeinfo, which has two overloadings:  one
00080 // with a single reference parameter for the table to be initialized,
00081 // and one with a second _Locale_time * parameter.  The first form
00082 // is called by the default constructor and the second by a special
00083 // constructor invoked from the _byname subclass constructor to
00084 // construct the base class.
00085 
00086 class _STLP_CLASS_DECLSPEC _Time_Info {
00087 public:
00088   string _M_dayname[14];
00089   string _M_monthname[24];
00090   string _M_am_pm[2];
00091   string _M_time_format;
00092   string _M_date_format;
00093   string _M_date_time_format;
00094   string _M_long_date_format;
00095   string _M_long_date_time_format;
00096 };
00097 
00098 void _STLP_CALL _Init_timeinfo(_Time_Info&);
00099 void _STLP_CALL _Init_timeinfo(_Time_Info&, _Locale_time*);
00100 
00101 _STLP_MOVE_TO_STD_NAMESPACE
00102 
00103 class _STLP_CLASS_DECLSPEC time_base {
00104 public:
00105   enum dateorder {no_order, dmy, mdy, ymd, ydm};
00106 };
00107 
00108 #if defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
00109 template <class _Ch, class _InIt>
00110 #else
00111 template <class _Ch, class _InIt = istreambuf_iterator<_Ch, char_traits<_Ch> > >
00112 #endif
00113 class time_get : public locale::facet, public time_base {
00114   friend class _Locale_impl;
00115 
00116 public:
00117   typedef _Ch   char_type;
00118   typedef _InIt iter_type;
00119 
00120   explicit time_get(size_t __refs = 0) : locale::facet(__refs)
00121   { _STLP_PRIV _Init_timeinfo(_M_timeinfo); }
00122   dateorder date_order() const { return do_date_order(); }
00123   iter_type get_time(iter_type __s, iter_type  __end, ios_base&  __str,
00124                      ios_base::iostate&  __err, tm* __t) const
00125   { return do_get_time(__s,  __end,  __str,  __err, __t); }
00126   iter_type get_date(iter_type __s, iter_type  __end, ios_base&  __str,
00127                      ios_base::iostate&  __err, tm* __t) const
00128   { return do_get_date(__s,  __end,  __str,  __err, __t); }
00129   iter_type get_weekday(iter_type __s, iter_type  __end, ios_base&  __str,
00130                         ios_base::iostate&  __err, tm* __t) const
00131   { return do_get_weekday(__s,  __end,  __str,  __err, __t); }
00132   iter_type get_monthname(iter_type __s, iter_type  __end, ios_base&  __str,
00133                           ios_base::iostate&  __err, tm* __t) const
00134   { return do_get_monthname(__s,  __end,  __str,  __err, __t); }
00135   iter_type get_year(iter_type __s, iter_type  __end, ios_base&  __str,
00136                      ios_base::iostate&  __err, tm* __t) const
00137   { return do_get_year(__s,  __end,  __str,  __err, __t); }
00138 
00139   static _STLP_STATIC_MEMBER_DECLSPEC locale::id id;
00140 
00141 protected:
00142   time_get(_Locale_time *, size_t __refs) : locale::facet(__refs) {}
00143 
00144   ~time_get() {}
00145 
00146   virtual dateorder do_date_order() const {return no_order;}
00147 
00148   virtual iter_type do_get_time(iter_type __s, iter_type  __end,
00149                                 ios_base&, ios_base::iostate&  __err,
00150                                 tm* __t) const;
00151 
00152   virtual iter_type do_get_date(iter_type __s, iter_type  __end,
00153                                 ios_base&, ios_base::iostate& __err,
00154                                 tm* __t) const;
00155 
00156   virtual iter_type do_get_weekday(iter_type __s, iter_type  __end,
00157                                    ios_base&,
00158                                    ios_base::iostate& __err,
00159                                    tm* __t) const;
00160   virtual iter_type do_get_monthname(iter_type __s, iter_type  __end,
00161                                      ios_base&,
00162                                      ios_base::iostate& __err,
00163                                      tm* __t) const;
00164 
00165   virtual iter_type do_get_year(iter_type __s, iter_type  __end,
00166                                 ios_base&, ios_base::iostate& __err,
00167                                 tm* __t) const;
00168 
00169   _STLP_PRIV _Time_Info _M_timeinfo;
00170 };
00171 
00172 _STLP_MOVE_TO_PRIV_NAMESPACE
00173 
00174 time_base::dateorder _STLP_CALL __get_date_order(_Locale_time*);
00175 _Locale_time* _STLP_CALL __acquire_time(const char* __name, _Locale_name_hint*);
00176 void _STLP_CALL __release_time(_Locale_time* __time);
00177 
00178 _STLP_MOVE_TO_STD_NAMESPACE
00179 
00180 template <class _Ch, class _InIt>
00181 class time_get_byname;
00182 
00183 #if defined (__GNUC__) && (__GNUC__ < 3)
00184 template <class _Ch, class _InIt>
00185 _Locale_name_hint* _Locale_time_extract_hint(time_get_byname<_Ch, _InIt>*);
00186 #else
00187 _Locale_name_hint* _Locale_time_extract_hint(time_get_byname<char, istreambuf_iterator<char, char_traits<char> > >*);
00188 #endif
00189 
00190 #if defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
00191 template <class _Ch, class _InIt>
00192 #else
00193 template <class _Ch, class _InIt = istreambuf_iterator<_Ch, char_traits<_Ch> > >
00194 #endif
00195 class time_get_byname : public time_get<_Ch, _InIt> {
00196 public:
00197   typedef  time_base::dateorder dateorder;
00198   typedef _InIt                 iter_type;
00199 
00200   explicit time_get_byname(const char* __name, size_t __refs = 0, _Locale_name_hint* __hint = 0)
00201     : time_get<_Ch, _InIt>((_Locale_time*) 0, __refs),
00202       _M_time(_STLP_PRIV __acquire_time(__name, __hint))
00203   { _STLP_PRIV _Init_timeinfo(this->_M_timeinfo, this->_M_time); }
00204 
00205 protected:
00206   ~time_get_byname() { _STLP_PRIV __release_time(_M_time); }
00207   dateorder do_date_order() const { return _STLP_PRIV __get_date_order(_M_time); }
00208 
00209 private:
00210   _Locale_time* _M_time;
00211 
00212   typedef time_get_byname<_Ch, _InIt> _Self;
00213   //explicitely defined as private to avoid warnings:
00214   time_get_byname(_Self const&);
00215   _Self& operator = (_Self const&);
00216 #if defined (__GNUC__) && (__GNUC__ < 3)
00217   friend _Locale_name_hint* _Locale_time_extract_hint<>(_Self*);
00218 #else
00219   friend _Locale_name_hint* _Locale_time_extract_hint(time_get_byname<char, istreambuf_iterator<char, char_traits<char> > >*);
00220 #endif
00221 };
00222 
00223 // time_put facet
00224 
00225 // For the formats 'x, 'X', and 'c', do_put calls the first form of
00226 // put with the pattern obtained from _M_timeinfo._M_date_format or
00227 // _M_timeinfo._M_time_format.
00228 
00229 // Helper function:  __  takes a single-character
00230 // format.  As indicated by the foregoing remark, this will never be
00231 // 'x', 'X', or 'c'.
00232 
00233 _STLP_MOVE_TO_PRIV_NAMESPACE
00234 
00235 char * _STLP_CALL
00236 __write_formatted_time(char *__buf, size_t __buf_size, char __format, char __modifier,
00237                        const _Time_Info& __table, const tm* __t);
00238 
00239 template <class _OuIt>
00240 inline _OuIt _STLP_CALL __put_time(char * __first, char * __last, _OuIt __out_ite,
00241                                    const ios_base& /* __loc */, char)
00242 { return copy(__first, __last, __out_ite); }
00243 
00244 #if !defined (_STLP_NO_WCHAR_T)
00245 template <class _OuIt>
00246 _OuIt _STLP_CALL __put_time(char * __first, char * __last, _OuIt __out_ite,
00247                             const ios_base& __s, wchar_t);
00248 #endif
00249 
00250 _STLP_MOVE_TO_STD_NAMESPACE
00251 
00252 #if defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
00253 template <class _Ch, class _OutIt>
00254 #else
00255 template <class _Ch, class _OutIt = ostreambuf_iterator<_Ch, char_traits<_Ch> > >
00256 #endif
00257 class time_put : public locale::facet, public time_base {
00258   friend class _Locale_impl;
00259 public:
00260   typedef _Ch      char_type;
00261   typedef _OutIt iter_type;
00262 
00263   explicit time_put(size_t __refs = 0) : locale::facet(__refs)
00264   { _STLP_PRIV _Init_timeinfo(_M_timeinfo); }
00265 
00266   _OutIt put(iter_type __s, ios_base& __f, _Ch __fill,
00267                   const tm* __tmb,
00268                   const _Ch* __pat, const _Ch* __pat_end) const;
00269 
00270   _OutIt put(iter_type __s, ios_base& __f, _Ch  __fill,
00271                   const tm* __tmb, char __format, char __modifier = 0) const
00272   { return do_put(__s, __f,  __fill, __tmb, __format, __modifier); }
00273 
00274   static _STLP_STATIC_MEMBER_DECLSPEC locale::id id;
00275 
00276 protected:
00277   time_put(_Locale_time* /*__time*/, size_t __refs) : locale::facet(__refs)
00278   {} //_STLP_PRIV _Init_timeinfo(_M_timeinfo, __time); }
00279 
00280   ~time_put() {}
00281   virtual iter_type do_put(iter_type __s, ios_base& __f,
00282                            char_type  /* __fill */, const tm* __tmb,
00283                            char __format, char /* __modifier */) const;
00284 
00285   _STLP_PRIV _Time_Info _M_timeinfo;
00286 };
00287 
00288 #if defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
00289 template <class _Ch, class _OutIt>
00290 #else
00291 template <class _Ch, class _OutIt = ostreambuf_iterator<_Ch, char_traits<_Ch> > >
00292 #endif
00293 class time_put_byname : public time_put<_Ch, _OutIt> {
00294   friend class _Locale_impl;
00295 public:
00296   typedef time_base::dateorder dateorder;
00297   typedef _OutIt iter_type;
00298   typedef _Ch   char_type;
00299 
00300   explicit time_put_byname(const char * __name, size_t __refs = 0, _Locale_name_hint* __hint = 0)
00301     : time_put<_Ch, _OutIt>((_Locale_time*) 0, __refs),
00302     _M_time(_STLP_PRIV __acquire_time(__name, __hint))
00303   { _STLP_PRIV _Init_timeinfo(this->_M_timeinfo, this->_M_time); }
00304 
00305 protected:
00306   ~time_put_byname() { _STLP_PRIV __release_time(_M_time); }
00307 
00308 private:
00309   _Locale_time* _M_time;
00310 
00311   typedef time_put_byname<_Ch, _OutIt> _Self;
00312   //explicitely defined as private to avoid warnings:
00313   time_put_byname(_Self const&);
00314   _Self& operator = (_Self const&);
00315 };
00316 
00317 #if defined (_STLP_USE_TEMPLATE_EXPORT)
00318 _STLP_EXPORT_TEMPLATE_CLASS time_get<char, istreambuf_iterator<char, char_traits<char> > >;
00319 _STLP_EXPORT_TEMPLATE_CLASS time_put<char, ostreambuf_iterator<char, char_traits<char> > >;
00320 // _STLP_EXPORT_TEMPLATE_CLASS time_get<char, const char*>;
00321 // _STLP_EXPORT_TEMPLATE_CLASS time_put<char, char*>;
00322 #  if !defined (_STLP_NO_WCHAR_T)
00323 _STLP_EXPORT_TEMPLATE_CLASS time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >;
00324 _STLP_EXPORT_TEMPLATE_CLASS time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >;
00325 // _STLP_EXPORT_TEMPLATE_CLASS time_get<wchar_t, const wchar_t*>;
00326 // _STLP_EXPORT_TEMPLATE_CLASS time_put<wchar_t, wchar_t*>;
00327 #  endif
00328 
00329 #endif
00330 
00331 _STLP_END_NAMESPACE
00332 
00333 #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
00334 #  include <stl/_time_facets.c>
00335 #endif
00336 
00337 #endif /* _STLP_INTERNAL_TIME_FACETS_H */
00338 
00339 // Local Variables:
00340 // mode:C++
00341 // End:



Generated on Mon Mar 10 15:32:41 2008 by  doxygen 1.5.1