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