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