📄 _punct.cc
字号:
/****************************************************************************
*
* _punct.cc - Internal classes for numeric formatting and parsing.
*
* $Id: _punct.cc,v 1.2 2004/01/30 22:33:05 sdouglas Exp $
*
***************************************************************************
*
* Copyright (c) 1994-2001 Rogue Wave Software, Inc. All Rights Reserved.
*
* This computer software is owned by Rogue Wave Software, Inc. and is
* protected by U.S. copyright laws and other laws and by international
* treaties. This computer software is furnished by Rogue Wave Software,
* Inc. pursuant to a written license agreement and may be used, copied,
* transmitted, and stored only in accordance with the terms of such
* license and with the inclusion of the above copyright notice. This
* computer software or any other copies thereof may not be provided or
* otherwise made available to any other person.
*
* U.S. Government Restricted Rights. This computer software is provided
* with Restricted Rights. Use, duplication, or disclosure by the
* Government is subject to restrictions as set forth in subparagraph (c)
* (1) (ii) of The Rights in Technical Data and Computer Software clause
* at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the
* Commercial Computer Software--Restricted Rights at 48 CFR 52.227-19,
* as applicable. Manufacturer is Rogue Wave Software, Inc., 5500
* Flatiron Parkway, Boulder, Colorado 80301 USA.
*
***************************************************************************/
#include <rw/_locimp.h>
#include <rw/_ctype.h>
#include <rw/_defs.h>
#include _RWSTD_CLOCALE
_RWSTD_NAMESPACE_BEGIN (__rw)
_USING (namespace std);
// -----------------------------------------------------
// Template __rw_timepunct_impl_data<_CharT> member templates.
// -----------------------------------------------------
template <class _CharT>
__rw_timepunct_impl_data<_CharT>::__rw_timepunct_impl_data
(const __rw_timepunct_init<_CharT> &__p)
{
int __i;
for (__i=0; __i<7; __i++) {
_C_day_nms[__i][0]=__p._C_get_day (__i, false);
_C_day_nms[__i][1]=__p._C_get_day (__i, true);
}
for (__i=0; __i<12; __i++) {
_C_month_nms[__i][0]=__p._C_get_month (__i, false);
_C_month_nms[__i][1]=__p._C_get_month (__i, true);
}
_C_ampm_nms[0]=__p._C_get_ampm (false);
_C_ampm_nms[1]=__p._C_get_ampm (true);
_C_bad_nm=__p._C_get_bad ();
for (__i=0; __i<100; __i++)
_C_ord_nms[__i]=__p._C_get_ord (__i);
for (size_t __j=0; __j<sizeof _C_dt_tm_pats/sizeof _C_dt_tm_pats[0]; __j++)
_C_dt_tm_pats[__j]=__p._C_get_pattern (int(__j));
}
// struct for storing formatting information in map
struct __rwstd_fmt {
__rwstd_fmt () {}
__rwstd_fmt (size_t __pos, size_t __len, char __fmtc)
: _C_pos(__pos), _C_len(__len), _C_fmt_char(__fmtc) {}
size_t _C_pos;
size_t _C_len;
char _C_fmt_char;
bool operator < (const __rwstd_fmt& __fmt) {
return _C_pos < __fmt._C_pos;
}
};
// initialize data member pat_ which holds formatting strings
template <class _CharT>
void __rw_timepunct_impl_data<_CharT>::_C_initpat (const locale &__loc)
{
static const char __tm_single_data_patts[] = "YyjSMIHmBbAapdZUWw";
tm __tmb;
char __buf[256];
__rwstd_fmt __fmtstruct_array[20];
// set up tmp
__tmb.tm_sec = 56;
__tmb.tm_min = 54;
__tmb.tm_hour = 22;
__tmb.tm_mday = 30;
__tmb.tm_mon = 10;
__tmb.tm_year = 90;
__tmb.tm_wday = 5;
__tmb.tm_yday = -1;
__tmb.tm_isdst = -1;
(void) mktime (&__tmb);
typedef ctype<_CharT> _Ctype;
typedef __rw_facet_maker<_Ctype> _FacetMaker;
// bypass the mutex in std::use_facet<>() by directly calling
// the implementation locale member function to prevent a deadlock
const _Ctype &__ctp =
_RWSTD_STATIC_CAST (const _Ctype&,
__loc._C_unsafe_use_facet (_Ctype::id,
_Ctype::_C_ok_implicit,
_Ctype::_C_facet_cat,
_FacetMaker::_C_maker_func));
// Set up passed in locale
string __oldloc = setlocale (LC_ALL, 0);
#ifdef __BORLANDC__
string::size_type __posd = __loc->name ().find ('=')+1;
setlocale (LC_ALL, string (__loc->name (), __posd, __loc->name ().find ('\n')-__posd).c_str ());
#else
setlocale (LC_ALL, __loc.name ().c_str ());
#endif // __BORLANDC__
// For each composite format character create format string
static const char __tm_mult_data_patts[] = "xXcDrT";
for (unsigned int __j = 0; __j != sizeof __tm_mult_data_patts - 1; ++__j) {
// create format string to parse
char __tmfmt_pat[] = "% ";
__tmfmt_pat[1] = __tm_mult_data_patts[__j];
strftime (__buf, sizeof (__buf), __tmfmt_pat, &__tmb);
string __tmfmt_result (__buf);
// starting at the beginning of the formatted string find each component
// and save in an ordered structure
size_t __pos;
int __fmtstruct_index = 0;
for (unsigned int __i=0; __i < sizeof (__tm_single_data_patts) - 1; ++__i) {
char __tmp_fmt_arg[] = "% ";
__tmp_fmt_arg[1] = __tm_single_data_patts[__i];
strftime (__buf, sizeof (__buf), __tmp_fmt_arg, &__tmb);
// Some locales don't have am/pm, in which case strftime (...%p...)
// returns a space. Exclude this case to ensure valid substring searches.
if (!(__tm_single_data_patts[__i]=='p' && strcmp (__buf, " ")==0)) {
if ((__pos = __tmfmt_result.find (__buf)) != size_t (-1)) {
if (strlen (__buf) > 0) {
__fmtstruct_array[__fmtstruct_index++] =
__rwstd_fmt (__pos, strlen (__buf),
__tm_single_data_patts[__i]);
__tmfmt_result.replace (__pos, strlen (__buf), strlen (__buf), ' ');
}
}
}
}
// Sort Format Items
for (int __top = __fmtstruct_index; __top > 0; --__top) {
int __large = 0;
for (int __k = 0; __k < __top; ++__k) {
if (__fmtstruct_array[__k] < __fmtstruct_array[__large])
__large = __k;
}
__rwstd_fmt __temp = __fmtstruct_array[__large];
__fmtstruct_array[__large] = __fmtstruct_array[__top-1];
__fmtstruct_array[__top-1] = __temp;
}
// Attempt to deal with implementations of strftime that use '%e'
{
string __alt_tmfmt (__tmfmt_result); // Alternate timepunct format string
char __compare_buf[256]; // Buffer to compare with strftime
// put format characters into formatting string
for (__rwstd_fmt *__tmpfmt = __fmtstruct_array;
__tmpfmt < (__fmtstruct_array + __fmtstruct_index);
++__tmpfmt) {
__tmfmt_result.replace ((*__tmpfmt)._C_pos, (*__tmpfmt)._C_len,
(string ("%").append (1, (*__tmpfmt)._C_fmt_char)));
// create an alternate formatting string that uses "%e" instead of "%d"
if ((*__tmpfmt)._C_fmt_char != 'd') {
__alt_tmfmt.replace ((*__tmpfmt)._C_pos, (*__tmpfmt)._C_len,
(string ("%").append (1, (*__tmpfmt)._C_fmt_char)));
} else {
__alt_tmfmt.replace ((*__tmpfmt)._C_pos, (*__tmpfmt)._C_len,
(string ("%e")));
}
}
// We need a single-digit day to see if spaces or zeroes are used to pad
__tmb.tm_mday=3;
// See if our usual format string is correct
strftime (__buf, sizeof (__buf), __tmfmt_result.c_str (), &__tmb);
strftime (__compare_buf, sizeof (__compare_buf), __tmfmt_pat, &__tmb);
// Make a best-effort guess ... we can't verify that a "%e" format
// string will do the trick, since the 'e' modifier is not specified by
// the C standard, and hence the result of strftime would be undefined.
if (strcmp (__buf, __compare_buf)) {
__tmfmt_result = __alt_tmfmt; // use the alternate format string
}
// And restore the "safe" day-of-month for next time.
__tmb.tm_mday=30;
}
// convert to string_type and insert in array _C_dt_tm_pats
string_type __str;
for (_TYPENAME string_type::size_type __k = 0;
__k < __tmfmt_result.length (); ++__k)
__str += __ctp.widen (__tmfmt_result[__k]);
_C_dt_tm_pats[__j] = __str;
}
setlocale (LC_ALL, __oldloc.c_str ());
}
template <class _CharT>
void __rw_timepunct_impl_data<_CharT>::_C_initfacet (const locale &__loc) {
int __i;
for (__i=0; __i<7; ++__i) {
_C_dn_defs[__i][0]._C_name=_C_day_nms[__i][0].c_str ();
_C_dn_defs[__i][0]._C_indx=__i;
_C_dn_defs[__i][1]._C_name=_C_day_nms[__i][1].c_str ();
_C_dn_defs[__i][1]._C_indx=__i;
}
for (__i=0; __i<12; ++__i) {
_C_mn_defs[__i][0]._C_name=_C_month_nms[__i][0].c_str ();
_C_mn_defs[__i][0]._C_indx=__i;
_C_mn_defs[__i][1]._C_name=_C_month_nms[__i][1].c_str ();
_C_mn_defs[__i][1]._C_indx=__i;
}
_C_ampm_defs[0]._C_name=_C_ampm_nms[0].c_str ();
_C_ampm_defs[0]._C_indx= 0;
_C_ampm_defs[1]._C_name=_C_ampm_nms[1].c_str ();
_C_ampm_defs[1]._C_indx= 1;
_C_dn_defsmap._C_numdefs=7*2;
_C_dn_defsmap._C_defs=&(_C_dn_defs[0][0]);
_C_mn_defsmap._C_numdefs=12*2;
_C_mn_defsmap._C_defs=&(_C_mn_defs[0][0]);
_C_ampm_defsmap._C_numdefs=2;
_C_ampm_defsmap._C_defs=&(_C_ampm_defs[0]);
_C_initpat (__loc);
}
template <class _CharT>
const _CharT __rw_bool_name<_CharT>::_C_name[][6] = {
{ 'f', 'a', 'l', 's', 'e', '\0' },
{ 't', 'r', 'u', 'e', '\0' }
};
template <class _CharT>
const __rw_numpunct_init_data<_CharT>
__rw_numpunct_impl<_CharT>::_C_ivals = {
false, // don't delete
'.', // decimal point
#if !defined (_WIN32) && !defined (_WIN64)
'\0', // standard "C" locale thousands separator
#else
',', // Windows "C" locale thousands separator
#endif
"", // Digit grouping rule
__rw_bool_name<_CharT>::_C_name [true], // Boolean true value as string
__rw_bool_name<_CharT>::_C_name [false] // Boolean false value as string
};
// ----------------------------------------------------
// Facet rwstd::__rw_timepunct<_CharT> member templates.
// ----------------------------------------------------
template <class _CharT>
locale::id __rw_timepunct<_CharT>::id;
template <class _CharT>
void __rw_timepunct<_CharT>::_C_initfacet (const locale &__loc) {
__rw_timepunct_impl<_CharT>::_C_initfacet (__loc);
}
template <class _CharT>
_TYPENAME __rw_timepunct<_CharT>::string_type
__rw_timepunct<_CharT>::do_dayname (int __day, bool __abbr) const {
return this->_C_day_nms[__day][__abbr];
}
template <class _CharT>
_TYPENAME __rw_timepunct<_CharT>::string_type
__rw_timepunct<_CharT>::do_monthname (int __mon, bool __abbr) const {
return this->_C_month_nms[__mon][__abbr];
}
template <class _CharT>
_TYPENAME __rw_timepunct<_CharT>::string_type
__rw_timepunct<_CharT>::do_ordinal (int __num) const {
return (__num<0 || __num>99) ? this->_C_bad_nm : this->_C_ord_nms[__num];
}
// ---------------------------------------
// Template __rw_digit_reader members
// ---------------------------------------
template <class _CharT, class _InputIter>
char* __rw_digit_reader<_CharT, _InputIter>::
_C_get_digit_string (char *__first, bool __discard /* = false */) {
char *__last = __first;
// get a set of punctuator characters
const _CharT *__punct = this->_C_dmap._C_get_punct ();
// convert a sequence starting at iterator 'in' into
// a binary (not character) string of digits
while (!eof ()) {
_CharT __c = *_C_in; // extracted character
int __digit = this->_C_dmap._C_eval (__c); // binary equivalent
// validate and break out on unknown char
if ( __digit >= this->_C_base || __digit < 0
&& ( 16 != this->_C_base
|| __c != __punct [this->_C_x] && __c != __punct [this->_C_X]))
break;
// buffer full, reallocate
if (__last == this->_C_digits + this->_C_bufsize)
__last = this->_C_realloc ();
// append digit to the end of buffer
// leading zeros may be optionally discarded
if ( 0 < __digit
|| 0 == __digit && (!__discard || __first != __last))
*__last++ = char (__digit);
++_C_in;
}
return __last;
}
template <class _CharT, class _InputIter>
char* __rw_digit_reader<_CharT, _InputIter>::_C_get_digits (int)
{
char *__last = this->_C_digits;
const _CharT* __punct = this->_C_dmap._C_get_punct ();
if (!eof () && (this->_C_state & this->_C_signed)) {
_CharT __ch = *_C_in;
// If a sign is present, set negative if it's a minus sign.
if ( __ch == __punct [this->_C_plus]
|| __ch == __punct [this->_C_minus]) {
if (__ch == __punct [this->_C_minus])
this->_C_state |= this->_C_negative;
do
++_C_in;
while (!eof () && this->_C_ctyp.is (ctype_base::space, *_C_in));
this->_C_state |= this->_C_advanced;
}
}
if (!this->_C_base) {
this->_C_base = _C_io._C_base ();
if (0 == this->_C_base || (_C_io.flags () & ios_base::showbase)) {
// No radix was forced by the ios_base flags, so poke around
// for a radix specifier on the front of the input value.
if (0 == this->_C_base)
this->_C_base = 10;
if (!eof () && *_C_in == __punct[this->_C_zero]) {
this->_C_base = 8;
++_C_in;
*__last++ = 0;
this->_C_state |= this->_C_advanced;
_CharT __ch;
if ( !eof ()
&& ( (__ch = *_C_in) == __punct[this->_C_x]
|| __ch == __punct[this->_C_X])) {
this->_C_base = 16;
++_C_in;
__last--; // Leading 0 was not a digit after all.
}
}
}
}
if (!*(_C_grouping = this->_C_get_grouping ()))
__last = _C_get_digit_string (__last);
else {
_C_gpos = _C_groups;
thousands_sep = this->_C_get_thousands_sep ();
__last = _C_get_digit_groups (__last);
if (!this->_C_error && --_C_gpos > _C_groups) {
// Digit grouping is optional, but if present it must be right.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -