Skip to content

Commit 128ba71

Browse files
author
Howard Hinnant
committed
patch by Jeffrey Yasskin for porting to Ubuntu Hardy. Everything was accepted except there were some bug fixes needed in <locale> for the __nolocale_* series. For the apple branch I ended up using templates instead of the var_args solution because it seemed both safer and more efficient.
llvm-svn: 104516
1 parent 8a57aec commit 128ba71

21 files changed

+307
-119
lines changed

libcxx/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ DESTDIR = $(DSTROOT)
77

88
OBJROOT=.
99
SYMROOT=.
10+
TRIPLE=-apple-
1011

1112
installsrc:: $(SRCROOT)
1213

libcxx/include/__config

+10-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,16 @@
3737
#endif
3838

3939
#if !defined(_LIBCPP_LITTLE_ENDIAN) || !defined(_LIBCPP_BIG_ENDIAN)
40-
#error unable to determine endian
40+
# include <endian.h>
41+
# if __BYTE_ORDER == __LITTLE_ENDIAN
42+
# define _LIBCPP_LITTLE_ENDIAN 1
43+
# define _LIBCPP_BIG_ENDIAN 0
44+
# elif __BYTE_ORDER == __BIG_ENDIAN
45+
# define _LIBCPP_LITTLE_ENDIAN 0
46+
# define _LIBCPP_BIG_ENDIAN 1
47+
# else
48+
# error unable to determine endian
49+
# endif
4150
#endif
4251

4352
#ifndef _LIBCPP_VISIBILITY_TAG

libcxx/include/__locale

+18
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <mutex>
1919
#include <cstdint>
2020
#include <cctype>
21+
#include <locale.h>
2122
#include <xlocale.h>
2223

2324
#pragma GCC system_header
@@ -294,6 +295,7 @@ class ctype_base {
294295
public:
295296
typedef __uint32_t mask;
296297

298+
#if __APPLE__
297299
static const mask space = _CTYPE_S;
298300
static const mask print = _CTYPE_R;
299301
static const mask cntrl = _CTYPE_C;
@@ -304,6 +306,18 @@ public:
304306
static const mask punct = _CTYPE_P;
305307
static const mask xdigit = _CTYPE_X;
306308
static const mask blank = _CTYPE_B;
309+
#else /* !__APPLE__ */
310+
static const mask space = _ISspace;
311+
static const mask print = _ISprint;
312+
static const mask cntrl = _IScntrl;
313+
static const mask upper = _ISupper;
314+
static const mask lower = _ISlower;
315+
static const mask alpha = _ISalpha;
316+
static const mask digit = _ISdigit;
317+
static const mask punct = _ISpunct;
318+
static const mask xdigit = _ISxdigit;
319+
static const mask blank = _ISblank;
320+
#endif /* __APPLE__ */
307321
static const mask alnum = alpha | digit;
308322
static const mask graph = alnum | punct;
309323

@@ -507,7 +521,11 @@ public:
507521

508522
static locale::id id;
509523

524+
#ifdef _CACHED_RUNES
510525
static const size_t table_size = _CACHED_RUNES;
526+
#else
527+
static const size_t table_size = 256; // FIXME: Don't hardcode this.
528+
#endif
511529
const mask* table() const throw() {return __tab_;}
512530
static const mask* classic_table() throw();
513531

libcxx/include/algorithm

+1-5
Original file line numberDiff line numberDiff line change
@@ -552,16 +552,12 @@ template <class BidirectionalIterator, class Compare>
552552
#ifdef _LIBCPP_DEBUG
553553
#include <cassert>
554554
#endif
555-
//#include <cstdlib>
556-
#define RAND_MAX 0x7fffffff // #include <cstdlib>
557-
extern "C" int rand(void); // #include <cstdlib>
555+
#include <cstdlib>
558556

559557
#pragma GCC system_header
560558

561559
_LIBCPP_BEGIN_NAMESPACE_STD
562560

563-
using ::rand; // #include <cstdlib>
564-
565561
template <class _T1, class _T2 = _T1>
566562
struct __equal_to
567563
{

libcxx/include/cstddef

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ Types:
3434
*/
3535

3636
#include <__config>
37+
#define __need_ptrdiff_t
38+
#define __need_size_t
3739
#include <stddef.h>
3840

3941
#pragma GCC system_header

libcxx/include/iosfwd

+5-5
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ typedef fpos<char_traits<wchar_t>::state_type> wstreampos;
8686
*/
8787

8888
#include <__config>
89-
#include <_types.h> // for __darwin_mbstate_t
89+
#include <wchar.h> // for mbstate_t
9090

9191
#pragma GCC system_header
9292

@@ -153,11 +153,11 @@ typedef basic_ofstream<wchar_t> wofstream;
153153
typedef basic_fstream<wchar_t> wfstream;
154154

155155
template <class _State> class fpos;
156-
typedef fpos<__darwin_mbstate_t> streampos;
157-
typedef fpos<__darwin_mbstate_t> wstreampos;
156+
typedef fpos<mbstate_t> streampos;
157+
typedef fpos<mbstate_t> wstreampos;
158158
#ifndef _LIBCPP_HAS_NO_UNICODE_CHARS
159-
typedef fpos<__darwin_mbstate_t> u16streampos;
160-
typedef fpos<__darwin_mbstate_t> u32streampos;
159+
typedef fpos<mbstate_t> u16streampos;
160+
typedef fpos<mbstate_t> u32streampos;
161161
#endif
162162

163163
typedef long long streamoff; // for char_traits in <string>

libcxx/include/locale

+149-17
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ template <class charT> class messages_byname;
137137
#include <streambuf>
138138
#include <iterator>
139139
#include <limits>
140+
#if !__APPLE__
141+
#include <cstdarg>
142+
#endif
140143
#include <cstdlib>
141144
#include <ctime>
142145
#include <nl_types.h>
@@ -145,6 +148,131 @@ template <class charT> class messages_byname;
145148

146149
_LIBCPP_BEGIN_NAMESPACE_STD
147150

151+
// OSX has nice foo_l() functions that let you turn off use of the global
152+
// locale. Linux, not so much. The following functions avoid the locale when
153+
// that's possible and otherwise do the wrong thing. FIXME.
154+
#if __APPLE__
155+
156+
template <class _Tp>
157+
inline
158+
int
159+
__nolocale_sprintf(char* __restrict __str,
160+
const char* __restrict __format, _Tp __v)
161+
{
162+
return sprintf_l(__str, 0, __format, __v);
163+
}
164+
165+
template <class _Tp>
166+
inline
167+
int
168+
__nolocale_snprintf(char* __restrict __str, size_t __size,
169+
const char* __restrict __format, _Tp __v)
170+
{
171+
return snprintf_l(__str, __size, 0, __format, __v);
172+
}
173+
174+
template <class _Tp>
175+
inline
176+
int
177+
__nolocale_snprintf(char* __restrict __str, size_t __size,
178+
const char* __restrict __format, int __prec, _Tp __v)
179+
{
180+
return snprintf_l(__str, __size, 0, __format, __prec, __v);
181+
}
182+
183+
template <class _Tp>
184+
inline
185+
int
186+
__nolocale_asprintf(char** __ret, const char* __restrict __format, _Tp __v)
187+
{
188+
return asprintf_l(__ret, 0, __format, __v);
189+
}
190+
191+
template <class _Tp>
192+
inline
193+
int
194+
__nolocale_asprintf(char** __ret, const char* __restrict __format, int __prec,
195+
_Tp __v)
196+
{
197+
return asprintf_l(__ret, 0, __format, __prec, __v);
198+
}
199+
200+
template <class _Tp>
201+
inline
202+
int
203+
__nolocale_sscanf(const char* __restrict __str,
204+
const char* __restrict __format, _Tp* __v)
205+
{
206+
return sscanf_l(__str, 0, __format, __v);
207+
}
208+
209+
inline
210+
int
211+
__nolocale_isxdigit(int __c)
212+
{
213+
return isxdigit_l(__c, 0);
214+
}
215+
216+
inline
217+
int
218+
__nolocale_isdigit(int __c)
219+
{
220+
return isdigit_l(__c, 0);
221+
}
222+
223+
#else /* !__APPLE__ */
224+
inline int
225+
__nolocale_sprintf(char* __restrict __str,
226+
const char* __restrict __format, ...)
227+
{
228+
va_list __ap;
229+
va_start(__ap, __format);
230+
int __result = vsprintf(__str, __format, __ap);
231+
va_end(__ap);
232+
return __result;
233+
}
234+
inline int
235+
__nolocale_snprintf(char* __restrict __str, size_t __size,
236+
const char* __restrict __format, ...)
237+
{
238+
va_list __ap;
239+
va_start(__ap, __format);
240+
int __result = vsnprintf(__str, __size, __format, __ap);
241+
va_end(__ap);
242+
return __result;
243+
}
244+
inline int
245+
__nolocale_asprintf(char** __ret,
246+
const char* __restrict __format, ...)
247+
{
248+
va_list __ap;
249+
va_start(__ap, __format);
250+
int __result = vasprintf(__ret, __format, __ap);
251+
va_end(__ap);
252+
return __result;
253+
}
254+
inline int
255+
__nolocale_sscanf(const char* __restrict __str,
256+
const char* __restrict __format, ...)
257+
{
258+
va_list __ap;
259+
va_start(__ap, __format);
260+
int __result = vsscanf(__str, __format, __ap);
261+
va_end(__ap);
262+
return __result;
263+
}
264+
inline int
265+
__nolocale_isxdigit(int __c)
266+
{
267+
return isxdigit(__c);
268+
}
269+
inline int
270+
__nolocale_isdigit(int __c)
271+
{
272+
return isdigit(__c);
273+
}
274+
#endif /* __APPLE__ */
275+
148276
// __scan_keyword
149277
// Scans [__b, __e) until a match is found in the basic_strings range
150278
// [__kb, __ke) or until it can be shown that there is no match in [__kb, __ke).
@@ -1002,7 +1130,7 @@ num_get<_CharT, _InputIterator>::do_get(iter_type __b, iter_type __e,
10021130
break;
10031131
// Stage 3
10041132
__a[sizeof(__a)-1] = 0;
1005-
if (sscanf_l(__a, 0, "%p", &__v) != 1)
1133+
if (__nolocale_sscanf(__a, "%p", &__v) != 1)
10061134
__err = ios_base::failbit;
10071135
// EOF checked
10081136
if (__b == __e)
@@ -1107,13 +1235,13 @@ __num_put<_CharT>::__widen_and_group_float(char* __nb, char* __np, char* __ne,
11071235
*__oe++ = __ct.widen(*__nf++);
11081236
*__oe++ = __ct.widen(*__nf++);
11091237
for (__ns = __nf; __ns < __ne; ++__ns)
1110-
if (!isxdigit_l(*__ns, 0))
1238+
if (!__nolocale_isxdigit(*__ns))
11111239
break;
11121240
}
11131241
else
11141242
{
11151243
for (__ns = __nf; __ns < __ne; ++__ns)
1116-
if (!isdigit_l(*__ns, 0))
1244+
if (!__nolocale_isdigit(*__ns))
11171245
break;
11181246
}
11191247
if (__grouping.empty())
@@ -1310,7 +1438,7 @@ num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
13101438
+ ((numeric_limits<long>::digits % 3) != 0)
13111439
+ 1;
13121440
char __nar[__nbuf];
1313-
int __nc = sprintf_l(__nar, 0, __fmt, __v);
1441+
int __nc = __nolocale_sprintf(__nar, __fmt, __v);
13141442
char* __ne = __nar + __nc;
13151443
char* __np = this->__identify_padding(__nar, __ne, __iob);
13161444
// Stage 2 - Widen __nar while adding thousands separators
@@ -1336,7 +1464,7 @@ num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
13361464
+ ((numeric_limits<long long>::digits % 3) != 0)
13371465
+ 1;
13381466
char __nar[__nbuf];
1339-
int __nc = sprintf_l(__nar, 0, __fmt, __v);
1467+
int __nc = __nolocale_sprintf(__nar, __fmt, __v);
13401468
char* __ne = __nar + __nc;
13411469
char* __np = this->__identify_padding(__nar, __ne, __iob);
13421470
// Stage 2 - Widen __nar while adding thousands separators
@@ -1362,7 +1490,7 @@ num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
13621490
+ ((numeric_limits<unsigned long>::digits % 3) != 0)
13631491
+ 1;
13641492
char __nar[__nbuf];
1365-
int __nc = sprintf_l(__nar, 0, __fmt, __v);
1493+
int __nc = __nolocale_sprintf(__nar, __fmt, __v);
13661494
char* __ne = __nar + __nc;
13671495
char* __np = this->__identify_padding(__nar, __ne, __iob);
13681496
// Stage 2 - Widen __nar while adding thousands separators
@@ -1388,7 +1516,7 @@ num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
13881516
+ ((numeric_limits<unsigned long long>::digits % 3) != 0)
13891517
+ 1;
13901518
char __nar[__nbuf];
1391-
int __nc = sprintf_l(__nar, 0, __fmt, __v);
1519+
int __nc = __nolocale_sprintf(__nar, __fmt, __v);
13921520
char* __ne = __nar + __nc;
13931521
char* __np = this->__identify_padding(__nar, __ne, __iob);
13941522
// Stage 2 - Widen __nar while adding thousands separators
@@ -1415,16 +1543,18 @@ num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
14151543
char* __nb = __nar;
14161544
int __nc;
14171545
if (__specify_precision)
1418-
__nc = snprintf_l(__nb, __nbuf, 0, __fmt, (int)__iob.precision(), __v);
1546+
__nc = __nolocale_snprintf(__nb, __nbuf, __fmt,
1547+
(int)__iob.precision(), __v);
14191548
else
1420-
__nc = snprintf_l(__nb, __nbuf, 0, __fmt, __v);
1549+
__nc = __nolocale_snprintf(__nb, __nbuf, __fmt, __v);
14211550
unique_ptr<char, void(*)(void*)> __nbh(0, free);
14221551
if (__nc > static_cast<int>(__nbuf-1))
14231552
{
14241553
if (__specify_precision)
1425-
__nc = asprintf_l(&__nb, 0, __fmt, (int)__iob.precision(), __v);
1554+
__nc = __nolocale_asprintf(&__nb, __fmt, (int)__iob.precision(),
1555+
__v);
14261556
else
1427-
__nc = asprintf_l(&__nb, 0, __fmt, __v);
1557+
__nc = __nolocale_asprintf(&__nb, __fmt, __v);
14281558
if (__nb == 0)
14291559
__throw_bad_alloc();
14301560
__nbh.reset(__nb);
@@ -1465,16 +1595,18 @@ num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
14651595
char* __nb = __nar;
14661596
int __nc;
14671597
if (__specify_precision)
1468-
__nc = snprintf_l(__nb, __nbuf, 0, __fmt, (int)__iob.precision(), __v);
1598+
__nc = __nolocale_snprintf(__nb, __nbuf, __fmt,
1599+
(int)__iob.precision(), __v);
14691600
else
1470-
__nc = snprintf_l(__nb, __nbuf, 0, __fmt, __v);
1601+
__nc = __nolocale_snprintf(__nb, __nbuf, __fmt, __v);
14711602
unique_ptr<char, void(*)(void*)> __nbh(0, free);
14721603
if (__nc > static_cast<int>(__nbuf-1))
14731604
{
14741605
if (__specify_precision)
1475-
__nc = asprintf_l(&__nb, 0, __fmt, (int)__iob.precision(), __v);
1606+
__nc = __nolocale_asprintf(&__nb, __fmt, (int)__iob.precision(),
1607+
__v);
14761608
else
1477-
__nc = asprintf_l(&__nb, 0, __fmt, __v);
1609+
__nc = __nolocale_asprintf(&__nb, __fmt, __v);
14781610
if (__nb == 0)
14791611
__throw_bad_alloc();
14801612
__nbh.reset(__nb);
@@ -1510,7 +1642,7 @@ num_put<_CharT, _OutputIterator>::do_put(iter_type __s, ios_base& __iob,
15101642
char __fmt[6] = "%p";
15111643
const unsigned __nbuf = 20;
15121644
char __nar[__nbuf];
1513-
int __nc = sprintf_l(__nar, 0, __fmt, __v);
1645+
int __nc = __nolocale_sprintf(__nar, __fmt, __v);
15141646
char* __ne = __nar + __nc;
15151647
char* __np = this->__identify_padding(__nar, __ne, __iob);
15161648
// Stage 2 - Widen __nar
@@ -3162,7 +3294,7 @@ money_put<_CharT, _OutputIterator>::do_put(iter_type __s, bool __intl,
31623294
// secure memory for digit storage
31633295
if (__n > __bs-1)
31643296
{
3165-
__n = asprintf_l(&__bb, 0, "%.0Lf", __units);
3297+
__n = __nolocale_asprintf(&__bb, "%.0Lf", __units);
31663298
if (__bb == 0)
31673299
__throw_bad_alloc();
31683300
__hn.reset(__bb);

libcxx/include/string

+1
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ template <> struct hash<wstring>;
358358
#include <__config>
359359
#include <iosfwd>
360360
#include <cstring>
361+
#include <cstdio> // For EOF.
361362
#include <cwchar>
362363
#include <algorithm>
363364
#include <iterator>

0 commit comments

Comments
 (0)