2 * Portions Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
5 * Silicon Graphics Computer Systems, Inc.
10 * This material is provided "as is", with absolutely no warranty expressed
11 * or implied. Any use is at your own risk.
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.
20 // WARNING: This is an internal header file, included by other C++
21 // standard library headers. You should not attempt to use this header
25 #ifndef _STLP_INTERNAL_TIME_FACETS_H
26 #define _STLP_INTERNAL_TIME_FACETS_H
28 #ifndef _STLP_INTERNAL_CTIME
29 # include <stl/_ctime.h> // Needed (for struct tm) by time facets
32 #ifndef _STLP_C_LOCALE_H
33 # include <stl/c_locale.h>
36 #ifndef _STLP_IOS_BASE_H
37 # include <stl/_ios_base.h>
42 _STLP_MOVE_TO_PRIV_NAMESPACE
44 // Template functions used by time_get
46 // Matching input against a list of names
48 // Alphabetic input of the names of months and the names
49 // of weekdays requires matching input against a list of names.
50 // We use a simple generic algorithm to accomplish this. This
51 // algorithm is not very efficient, especially for longer lists
52 // of names, but it probably does not matter for the initial
53 // implementation and it may never matter, since we do not expect
54 // this kind of input to be used very often. The algorithm
55 // could be improved fairly simply by creating a new list of
56 // names still in the running at each iteration. A more sophisticated
57 // approach would be to build a trie to do the matching.
59 // We compare each character of the input to the corresponding
60 // character of each name on the list that has not been eliminated,
61 // either because every character in the name has already been
62 // matched, or because some character has not been matched. We
63 // continue only as long as there are some names that have not been
66 // We do not really need a random access iterator (a forward iterator
67 // would do), but the extra generality makes the notation clumsier,
68 // and we don't really need it.
70 // We can recognize a failed match by the fact that the second
71 // component of the return value will be __name_end.
74 #define _MAX_NAME_LENGTH 64
76 // Both time_get and time_put need a structure of type _Time_Info
77 // to provide names and abbreviated names for months and days,
78 // as well as the am/pm designator. The month and weekday tables
79 // have the all the abbreviated names before all the full names.
80 // The _Time_Info tables are initialized using the non-template
81 // function _Init_timeinfo, which has two overloadings: one
82 // with a single reference parameter for the table to be initialized,
83 // and one with a second _Locale_time * parameter. The first form
84 // is called by the default constructor and the second by a special
85 // constructor invoked from the _byname subclass constructor to
86 // construct the base class.
88 class _STLP_CLASS_DECLSPEC _Time_Info {
90 string _M_dayname[14];
91 string _M_monthname[24];
93 string _M_time_format;
94 string _M_date_format;
95 string _M_date_time_format;
96 string _M_long_date_format;
97 string _M_long_date_time_format;
100 _STLP_DECLSPEC void _STLP_CALL _Init_timeinfo(_Time_Info&);
101 _STLP_DECLSPEC void _STLP_CALL _Init_timeinfo(_Time_Info&, _Locale_time*);
103 _STLP_MOVE_TO_STD_NAMESPACE
105 class _STLP_CLASS_DECLSPEC time_base {
107 enum dateorder {no_order, dmy, mdy, ymd, ydm};
110 #if defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
111 template <class _Ch, class _InIt>
113 template <class _Ch, class _InIt = istreambuf_iterator<_Ch, char_traits<_Ch> > >
115 class time_get : public locale::facet, public time_base {
116 friend class _Locale_impl;
119 typedef _Ch char_type;
120 typedef _InIt iter_type;
122 explicit time_get(size_t __refs = 0) : locale::facet(__refs)
123 { _STLP_PRIV _Init_timeinfo(_M_timeinfo); }
124 dateorder date_order() const { return do_date_order(); }
125 iter_type get_time(iter_type __s, iter_type __end, ios_base& __str,
126 ios_base::iostate& __err, tm* __t) const
127 { return do_get_time(__s, __end, __str, __err, __t); }
128 iter_type get_date(iter_type __s, iter_type __end, ios_base& __str,
129 ios_base::iostate& __err, tm* __t) const
130 { return do_get_date(__s, __end, __str, __err, __t); }
131 iter_type get_weekday(iter_type __s, iter_type __end, ios_base& __str,
132 ios_base::iostate& __err, tm* __t) const
133 { return do_get_weekday(__s, __end, __str, __err, __t); }
134 iter_type get_monthname(iter_type __s, iter_type __end, ios_base& __str,
135 ios_base::iostate& __err, tm* __t) const
136 { return do_get_monthname(__s, __end, __str, __err, __t); }
137 iter_type get_year(iter_type __s, iter_type __end, ios_base& __str,
138 ios_base::iostate& __err, tm* __t) const
139 { return do_get_year(__s, __end, __str, __err, __t); }
141 #if defined(__SYMBIAN32__WSD__)
142 static _STLP_STATIC_MEMBER_DECLSPEC locale::id& GetFacetLocaleId();
143 #elif defined (__SYMBIAN32__NO_STATIC_IMPORTS__)
144 static _STLP_STATIC_MEMBER_DECLSPEC locale::id& GetFacetLocaleId();
145 static locale::id id;
147 // NOTE: Symbian doesn't support exporting static data.
148 // Users of this class should use GetFacetLocaleId() to access the data member id
149 static _STLP_STATIC_MEMBER_DECLSPEC locale::id id;
153 time_get(_Locale_time *, size_t __refs) : locale::facet(__refs) {}
157 virtual dateorder do_date_order() const {return no_order;}
159 virtual iter_type do_get_time(iter_type __s, iter_type __end,
160 ios_base&, ios_base::iostate& __err,
163 virtual iter_type do_get_date(iter_type __s, iter_type __end,
164 ios_base&, ios_base::iostate& __err,
167 virtual iter_type do_get_weekday(iter_type __s, iter_type __end,
169 ios_base::iostate& __err,
171 virtual iter_type do_get_monthname(iter_type __s, iter_type __end,
173 ios_base::iostate& __err,
176 virtual iter_type do_get_year(iter_type __s, iter_type __end,
177 ios_base&, ios_base::iostate& __err,
180 _STLP_PRIV _Time_Info _M_timeinfo;
183 _STLP_MOVE_TO_PRIV_NAMESPACE
185 _STLP_DECLSPEC time_base::dateorder _STLP_CALL __get_date_order(_Locale_time*);
186 _STLP_DECLSPEC _Locale_time* _STLP_CALL __acquire_time(const char* __name, _Locale_name_hint*);
187 _STLP_DECLSPEC void _STLP_CALL __release_time(_Locale_time* __time);
189 _STLP_MOVE_TO_STD_NAMESPACE
191 template <class _Ch, class _InIt>
192 class time_get_byname;
194 #if defined (__GNUC__) && (__GNUC__ < 3)
195 template <class _Ch, class _InIt>
196 _Locale_name_hint* _Locale_time_extract_hint(time_get_byname<_Ch, _InIt>*);
198 _Locale_name_hint* _Locale_time_extract_hint(time_get_byname<char, istreambuf_iterator<char, char_traits<char> > >*);
201 #if defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
202 template <class _Ch, class _InIt>
204 template <class _Ch, class _InIt = istreambuf_iterator<_Ch, char_traits<_Ch> > >
206 class time_get_byname : public time_get<_Ch, _InIt> {
208 typedef time_base::dateorder dateorder;
209 typedef _InIt iter_type;
211 explicit time_get_byname(const char* __name, size_t __refs = 0, _Locale_name_hint* __hint = 0)
212 : time_get<_Ch, _InIt>((_Locale_time*) 0, __refs),
213 _M_time(_STLP_PRIV __acquire_time(__name, __hint))
214 { _STLP_PRIV _Init_timeinfo(this->_M_timeinfo, this->_M_time); }
217 ~time_get_byname() { _STLP_PRIV __release_time(_M_time); }
218 dateorder do_date_order() const { return _STLP_PRIV __get_date_order(_M_time); }
221 _Locale_time* _M_time;
223 typedef time_get_byname<_Ch, _InIt> _Self;
224 //explicitely defined as private to avoid warnings:
225 time_get_byname(_Self const&);
226 _Self& operator = (_Self const&);
227 #if defined (__GNUC__) && (__GNUC__ < 3)
228 friend _Locale_name_hint* _Locale_time_extract_hint<>(_Self*);
230 friend _Locale_name_hint* _Locale_time_extract_hint(time_get_byname<char, istreambuf_iterator<char, char_traits<char> > >*);
236 // For the formats 'x, 'X', and 'c', do_put calls the first form of
237 // put with the pattern obtained from _M_timeinfo._M_date_format or
238 // _M_timeinfo._M_time_format.
240 // Helper function: __ takes a single-character
241 // format. As indicated by the foregoing remark, this will never be
244 _STLP_MOVE_TO_PRIV_NAMESPACE
246 _STLP_DECLSPEC char * _STLP_CALL
247 __write_formatted_time(char *__buf, size_t __buf_size, char __format, char __modifier,
248 const _Time_Info& __table, const tm* __t);
250 template <class _OuIt>
251 inline _OuIt _STLP_CALL __put_time(char * __first, char * __last, _OuIt __out_ite,
252 const ios_base& /* __loc */, char)
253 { return copy(__first, __last, __out_ite); }
255 #if !defined (_STLP_NO_WCHAR_T)
256 template <class _OuIt>
257 _OuIt _STLP_CALL __put_time(char * __first, char * __last, _OuIt __out_ite,
258 const ios_base& __s, wchar_t);
261 _STLP_MOVE_TO_STD_NAMESPACE
263 #if defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
264 template <class _Ch, class _OutIt>
266 template <class _Ch, class _OutIt = ostreambuf_iterator<_Ch, char_traits<_Ch> > >
268 class time_put : public locale::facet, public time_base {
269 friend class _Locale_impl;
271 typedef _Ch char_type;
272 typedef _OutIt iter_type;
274 explicit time_put(size_t __refs = 0) : locale::facet(__refs)
275 { _STLP_PRIV _Init_timeinfo(_M_timeinfo); }
277 _OutIt put(iter_type __s, ios_base& __f, _Ch __fill,
279 const _Ch* __pat, const _Ch* __pat_end) const;
281 _OutIt put(iter_type __s, ios_base& __f, _Ch __fill,
282 const tm* __tmb, char __format, char __modifier = 0) const
283 { return do_put(__s, __f, __fill, __tmb, __format, __modifier); }
285 #if defined(__SYMBIAN32__WSD__)
286 static _STLP_STATIC_MEMBER_DECLSPEC locale::id& GetFacetLocaleId();
287 #elif defined (__SYMBIAN32__NO_STATIC_IMPORTS__)
288 static _STLP_STATIC_MEMBER_DECLSPEC locale::id& GetFacetLocaleId();
289 static locale::id id;
291 // NOTE: Symbian doesn't support exporting static data.
292 // Users of this class should use GetFacetLocaleId() to access the data member id
293 static _STLP_STATIC_MEMBER_DECLSPEC locale::id id;
297 time_put(_Locale_time* /*__time*/, size_t __refs) : locale::facet(__refs)
298 {} //_STLP_PRIV _Init_timeinfo(_M_timeinfo, __time); }
301 virtual iter_type do_put(iter_type __s, ios_base& __f,
302 char_type /* __fill */, const tm* __tmb,
303 char __format, char /* __modifier */) const;
305 _STLP_PRIV _Time_Info _M_timeinfo;
308 #if defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
309 template <class _Ch, class _OutIt>
311 template <class _Ch, class _OutIt = ostreambuf_iterator<_Ch, char_traits<_Ch> > >
313 class time_put_byname : public time_put<_Ch, _OutIt> {
314 friend class _Locale_impl;
316 typedef time_base::dateorder dateorder;
317 typedef _OutIt iter_type;
318 typedef _Ch char_type;
320 explicit time_put_byname(const char * __name, size_t __refs = 0, _Locale_name_hint* __hint = 0)
321 : time_put<_Ch, _OutIt>((_Locale_time*) 0, __refs),
322 _M_time(_STLP_PRIV __acquire_time(__name, __hint))
323 { _STLP_PRIV _Init_timeinfo(this->_M_timeinfo, this->_M_time); }
326 ~time_put_byname() { _STLP_PRIV __release_time(_M_time); }
329 _Locale_time* _M_time;
331 typedef time_put_byname<_Ch, _OutIt> _Self;
332 //explicitely defined as private to avoid warnings:
333 time_put_byname(_Self const&);
334 _Self& operator = (_Self const&);
337 #if defined (_STLP_USE_TEMPLATE_EXPORT)
338 _STLP_EXPORT_TEMPLATE_CLASS time_get<char, istreambuf_iterator<char, char_traits<char> > >;
339 _STLP_EXPORT_TEMPLATE_CLASS time_put<char, ostreambuf_iterator<char, char_traits<char> > >;
340 // _STLP_EXPORT_TEMPLATE_CLASS time_get<char, const char*>;
341 // _STLP_EXPORT_TEMPLATE_CLASS time_put<char, char*>;
342 # if !defined (_STLP_NO_WCHAR_T)
343 _STLP_EXPORT_TEMPLATE_CLASS time_get<wchar_t, istreambuf_iterator<wchar_t, char_traits<wchar_t> > >;
344 _STLP_EXPORT_TEMPLATE_CLASS time_put<wchar_t, ostreambuf_iterator<wchar_t, char_traits<wchar_t> > >;
345 // _STLP_EXPORT_TEMPLATE_CLASS time_get<wchar_t, const wchar_t*>;
346 // _STLP_EXPORT_TEMPLATE_CLASS time_put<wchar_t, wchar_t*>;
353 #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
354 # include <stl/_time_facets.c>
357 #endif /* _STLP_INTERNAL_TIME_FACETS_H */