_codecvt.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. #ifndef _STLP_INTERNAL_CODECVT_H
  22. #define _STLP_INTERNAL_CODECVT_H
  23. #ifndef _STLP_C_LOCALE_H
  24. # include <stl/c_locale.h>
  25. #endif
  26. #ifndef _STLP_INTERNAL_LOCALE_H
  27. # include <stl/_locale.h>
  28. #endif
  29. #ifndef _STLP_INTERNAL_ALGOBASE_H
  30. # include <stl/_algobase.h>
  31. #endif
  32. _STLP_BEGIN_NAMESPACE
  33. class _STLP_CLASS_DECLSPEC codecvt_base {
  34. public:
  35. enum result {ok, partial, error, noconv};
  36. };
  37. template <class _InternT, class _ExternT, class _StateT>
  38. class codecvt : public locale::facet, public codecvt_base {
  39. public:
  40. typedef _InternT intern_type;
  41. typedef _ExternT extern_type;
  42. typedef _StateT state_type;
  43. #if defined (_STLP_MSVC) && (_STLP_MSVC < 1300)
  44. /* For the moment VC6 do not support this facet default implementation
  45. * because of the static locale::id instance. When VC6 see this definition
  46. * it goes crasy with locale::id static instances and all the has_facet tests
  47. * unit tests are failing.
  48. */
  49. };
  50. #else
  51. explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
  52. result out(state_type& __state,
  53. const intern_type* __from,
  54. const intern_type* __from_end,
  55. const intern_type*& __from_next,
  56. extern_type* __to,
  57. extern_type* __to_limit,
  58. extern_type*& __to_next) const {
  59. _STLP_VERBOSE_ASSERT(__from <= __from_end, _StlMsg_INVALID_ARGUMENT)
  60. _STLP_VERBOSE_ASSERT(__to <= __to_limit, _StlMsg_INVALID_ARGUMENT)
  61. return do_out(__state,
  62. __from, __from_end, __from_next,
  63. __to, __to_limit, __to_next);
  64. }
  65. result unshift(state_type& __state,
  66. extern_type* __to,
  67. extern_type* __to_limit,
  68. extern_type*& __to_next) const {
  69. _STLP_VERBOSE_ASSERT(__to <= __to_limit, _StlMsg_INVALID_ARGUMENT)
  70. return do_unshift(__state, __to, __to_limit, __to_next);
  71. }
  72. result in(state_type& __state,
  73. const extern_type* __from,
  74. const extern_type* __from_end,
  75. const extern_type*& __from_next,
  76. intern_type* __to,
  77. intern_type* __to_limit,
  78. intern_type*& __to_next) const {
  79. _STLP_VERBOSE_ASSERT(__from <= __from_end, _StlMsg_INVALID_ARGUMENT)
  80. _STLP_VERBOSE_ASSERT(__to <= __to_limit, _StlMsg_INVALID_ARGUMENT)
  81. return do_in(__state,
  82. __from, __from_end, __from_next,
  83. __to, __to_limit, __to_next);
  84. }
  85. int encoding() const _STLP_NOTHROW { return do_encoding(); }
  86. bool always_noconv() const _STLP_NOTHROW { return do_always_noconv(); }
  87. int length(state_type& __state,
  88. const extern_type* __from,
  89. const extern_type* __from_end,
  90. size_t __max) const {
  91. _STLP_VERBOSE_ASSERT(__from <= __from_end, _StlMsg_INVALID_ARGUMENT)
  92. return do_length(__state, __from, __from_end, __max);
  93. }
  94. int max_length() const _STLP_NOTHROW { return do_max_length(); }
  95. static locale::id id;
  96. protected:
  97. ~codecvt() {}
  98. virtual result do_out(state_type&,
  99. const intern_type* __from,
  100. const intern_type*,
  101. const intern_type*& __from_next,
  102. extern_type* __to,
  103. extern_type*,
  104. extern_type*& __to_next) const
  105. { __from_next = __from; __to_next = __to; return noconv; }
  106. virtual result do_in (state_type&,
  107. const extern_type* __from,
  108. const extern_type*,
  109. const extern_type*& __from_next,
  110. intern_type* __to,
  111. intern_type*,
  112. intern_type*& __to_next) const
  113. { __from_next = __from; __to_next = __to; return noconv; }
  114. virtual result do_unshift(state_type&,
  115. extern_type* __to,
  116. extern_type*,
  117. extern_type*& __to_next) const
  118. { __to_next = __to; return noconv; }
  119. virtual int do_encoding() const _STLP_NOTHROW
  120. { return 1; }
  121. virtual bool do_always_noconv() const _STLP_NOTHROW
  122. { return true; }
  123. virtual int do_length(state_type&,
  124. const extern_type* __from,
  125. const extern_type* __end,
  126. size_t __max) const
  127. { return (int)(min) ( __STATIC_CAST(size_t, (__end - __from)), __max); }
  128. virtual int do_max_length() const _STLP_NOTHROW
  129. { return 1; }
  130. private:
  131. codecvt(const codecvt<intern_type, extern_type, state_type>&);
  132. codecvt<intern_type, extern_type, state_type>& operator = (const codecvt<intern_type, extern_type, state_type>&);
  133. };
  134. # if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
  135. # if !defined (__BORLANDC__) || (__BORLANDC__ >= 0x590)
  136. template <class _InternT, class _ExternT, class _StateT>
  137. locale::id codecvt<_InternT, _ExternT, _StateT>::id;
  138. # endif
  139. # endif
  140. #endif
  141. template <class _InternT, class _ExternT, class _StateT>
  142. class codecvt_byname : public codecvt<_InternT, _ExternT, _StateT> {};
  143. _STLP_TEMPLATE_NULL
  144. class _STLP_CLASS_DECLSPEC codecvt<char, char, mbstate_t>
  145. : public locale::facet, public codecvt_base {
  146. public:
  147. typedef char intern_type;
  148. typedef char extern_type;
  149. typedef mbstate_t state_type;
  150. explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
  151. result out(state_type& __state,
  152. const char* __from,
  153. const char* __from_end,
  154. const char*& __from_next,
  155. char* __to,
  156. char* __to_limit,
  157. char*& __to_next) const {
  158. _STLP_VERBOSE_ASSERT(__from <= __from_end, _StlMsg_INVALID_ARGUMENT)
  159. _STLP_VERBOSE_ASSERT(__to <= __to_limit, _StlMsg_INVALID_ARGUMENT)
  160. return do_out(__state,
  161. __from, __from_end, __from_next,
  162. __to, __to_limit, __to_next);
  163. }
  164. result unshift(state_type& __state,
  165. char* __to, char* __to_limit, char*& __to_next) const {
  166. _STLP_VERBOSE_ASSERT(__to <= __to_limit, _StlMsg_INVALID_ARGUMENT)
  167. return do_unshift(__state, __to, __to_limit, __to_next);
  168. }
  169. result in(state_type& __state,
  170. const char* __from,
  171. const char* __from_end,
  172. const char*& __from_next,
  173. char* __to,
  174. char* __to_limit,
  175. char*& __to_next) const {
  176. _STLP_VERBOSE_ASSERT(__from <= __from_end, _StlMsg_INVALID_ARGUMENT)
  177. _STLP_VERBOSE_ASSERT(__to <= __to_limit, _StlMsg_INVALID_ARGUMENT)
  178. return do_in(__state,
  179. __from, __from_end, __from_next,
  180. __to, __to_limit, __to_next);
  181. }
  182. int encoding() const _STLP_NOTHROW { return do_encoding(); }
  183. bool always_noconv() const _STLP_NOTHROW { return do_always_noconv(); }
  184. int length(state_type& __state,
  185. const char* __from, const char* __from_end,
  186. size_t __max) const {
  187. _STLP_VERBOSE_ASSERT(__from <= __from_end, _StlMsg_INVALID_ARGUMENT)
  188. return do_length(__state, __from, __from_end, __max);
  189. }
  190. int max_length() const _STLP_NOTHROW { return do_max_length(); }
  191. static _STLP_STATIC_DECLSPEC locale::id id;
  192. protected:
  193. ~codecvt();
  194. virtual result do_out(state_type& /* __state */,
  195. const char* __from,
  196. const char* /* __from_end */,
  197. const char*& __from_next,
  198. char* __to,
  199. char* /* __to_limit */,
  200. char*& __to_next) const;
  201. virtual result do_in (state_type& /* __state */ ,
  202. const char* __from,
  203. const char* /* __from_end */,
  204. const char*& __from_next,
  205. char* __to,
  206. char* /* __to_end */,
  207. char*& __to_next) const;
  208. virtual result do_unshift(state_type& /* __state */,
  209. char* __to,
  210. char* /* __to_limit */,
  211. char*& __to_next) const;
  212. virtual int do_encoding() const _STLP_NOTHROW;
  213. virtual bool do_always_noconv() const _STLP_NOTHROW;
  214. virtual int do_length(state_type& __state,
  215. const char* __from,
  216. const char* __end,
  217. size_t __max) const;
  218. virtual int do_max_length() const _STLP_NOTHROW;
  219. private:
  220. codecvt(const codecvt<char, char, mbstate_t>&);
  221. codecvt<char, char, mbstate_t>& operator =(const codecvt<char, char, mbstate_t>&);
  222. };
  223. # ifndef _STLP_NO_WCHAR_T
  224. _STLP_TEMPLATE_NULL
  225. class _STLP_CLASS_DECLSPEC codecvt<wchar_t, char, mbstate_t>
  226. : public locale::facet, public codecvt_base {
  227. public:
  228. typedef wchar_t intern_type;
  229. typedef char extern_type;
  230. typedef mbstate_t state_type;
  231. explicit codecvt(size_t __refs = 0) : locale::facet(__refs) {}
  232. result out(state_type& __state,
  233. const wchar_t* __from,
  234. const wchar_t* __from_end,
  235. const wchar_t*& __from_next,
  236. char* __to,
  237. char* __to_limit,
  238. char*& __to_next) const {
  239. _STLP_VERBOSE_ASSERT(__from <= __from_end, _StlMsg_INVALID_ARGUMENT)
  240. _STLP_VERBOSE_ASSERT(__to <= __to_limit, _StlMsg_INVALID_ARGUMENT)
  241. return do_out(__state,
  242. __from, __from_end, __from_next,
  243. __to, __to_limit, __to_next);
  244. }
  245. result unshift(state_type& __state,
  246. char* __to, char* __to_limit, char*& __to_next) const {
  247. _STLP_VERBOSE_ASSERT(__to <= __to_limit, _StlMsg_INVALID_ARGUMENT)
  248. return do_unshift(__state, __to, __to_limit, __to_next);
  249. }
  250. result in(state_type& __state,
  251. const char* __from,
  252. const char* __from_end,
  253. const char*& __from_next,
  254. wchar_t* __to,
  255. wchar_t* __to_limit,
  256. wchar_t*& __to_next) const {
  257. _STLP_VERBOSE_ASSERT(__from <= __from_end, _StlMsg_INVALID_ARGUMENT)
  258. _STLP_VERBOSE_ASSERT(__to <= __to_limit, _StlMsg_INVALID_ARGUMENT)
  259. return do_in(__state,
  260. __from, __from_end, __from_next,
  261. __to, __to_limit, __to_next);
  262. }
  263. int encoding() const _STLP_NOTHROW { return do_encoding(); }
  264. bool always_noconv() const _STLP_NOTHROW { return do_always_noconv(); }
  265. int length(state_type& __state,
  266. const char* __from, const char* __from_end,
  267. size_t __max) const {
  268. _STLP_VERBOSE_ASSERT(__from <= __from_end, _StlMsg_INVALID_ARGUMENT)
  269. return do_length(__state, __from, __from_end, __max);
  270. }
  271. int max_length() const _STLP_NOTHROW { return do_max_length(); }
  272. static _STLP_STATIC_DECLSPEC locale::id id;
  273. protected:
  274. ~codecvt();
  275. virtual result do_out(state_type& __state,
  276. const wchar_t* __from,
  277. const wchar_t* __from_end,
  278. const wchar_t*& __from_next,
  279. char* __to,
  280. char* __to_limit,
  281. char*& __to_next) const;
  282. virtual result do_in (state_type& __state,
  283. const char* __from,
  284. const char* __from_end,
  285. const char*& __from_next,
  286. wchar_t* __to,
  287. wchar_t* __to_limit,
  288. wchar_t*& __to_next) const;
  289. virtual result do_unshift(state_type& __state,
  290. char* __to,
  291. char* __to_limit,
  292. char*& __to_next) const;
  293. virtual int do_encoding() const _STLP_NOTHROW;
  294. virtual bool do_always_noconv() const _STLP_NOTHROW;
  295. virtual int do_length(state_type& __state,
  296. const char* __from,
  297. const char* __end,
  298. size_t __max) const;
  299. virtual int do_max_length() const _STLP_NOTHROW;
  300. private:
  301. codecvt(const codecvt<wchar_t, char, mbstate_t>&);
  302. codecvt<wchar_t, char, mbstate_t>& operator = (const codecvt<wchar_t, char, mbstate_t>&);
  303. };
  304. # endif
  305. _STLP_TEMPLATE_NULL
  306. class _STLP_CLASS_DECLSPEC codecvt_byname<char, char, mbstate_t>
  307. : public codecvt<char, char, mbstate_t> {
  308. public:
  309. explicit codecvt_byname(const char* __name, size_t __refs = 0);
  310. ~codecvt_byname();
  311. private:
  312. codecvt_byname(const codecvt_byname<char, char, mbstate_t>&);
  313. codecvt_byname<char, char, mbstate_t>& operator =(const codecvt_byname<char, char, mbstate_t>&);
  314. };
  315. # ifndef _STLP_NO_WCHAR_T
  316. _STLP_TEMPLATE_NULL
  317. class _STLP_CLASS_DECLSPEC codecvt_byname<wchar_t, char, mbstate_t>
  318. : public codecvt<wchar_t, char, mbstate_t> {
  319. friend class _Locale_impl;
  320. public:
  321. explicit codecvt_byname(const char * __name, size_t __refs = 0);
  322. protected:
  323. ~codecvt_byname();
  324. virtual result do_out(state_type& __state,
  325. const wchar_t* __from,
  326. const wchar_t* __from_end,
  327. const wchar_t*& __from_next,
  328. char* __to,
  329. char* __to_limit,
  330. char*& __to_next) const;
  331. virtual result do_in (state_type& __state,
  332. const char* __from,
  333. const char* __from_end,
  334. const char*& __from_next,
  335. wchar_t* __to,
  336. wchar_t* __to_limit,
  337. wchar_t*& __to_next) const;
  338. virtual result do_unshift(state_type& __state,
  339. char* __to,
  340. char* __to_limit,
  341. char*& __to_next) const;
  342. virtual int do_encoding() const _STLP_NOTHROW;
  343. virtual bool do_always_noconv() const _STLP_NOTHROW;
  344. virtual int do_length(state_type& __state,
  345. const char* __from,
  346. const char* __end,
  347. size_t __max) const;
  348. virtual int do_max_length() const _STLP_NOTHROW;
  349. private:
  350. codecvt_byname(_Locale_codecvt* __cvt)
  351. : _M_codecvt(__cvt) {}
  352. codecvt_byname(const codecvt_byname<wchar_t, char, mbstate_t>&);
  353. codecvt_byname<wchar_t, char, mbstate_t>& operator =(const codecvt_byname<wchar_t, char, mbstate_t>&);
  354. _Locale_codecvt* _M_codecvt;
  355. };
  356. # endif
  357. _STLP_END_NAMESPACE
  358. #endif /* _STLP_INTERNAL_CODECVT_H */
  359. // Local Variables:
  360. // mode:C++
  361. // End: