xref: /openbsd-src/gnu/lib/libstdc++/libstdc++/config/io/c_io_libio_codecvt.c (revision c2fb321235c8dbfba0909bc43913400e90646ae7)
1 /* Copyright (C) 2000 Free Software Foundation, Inc.
2    This file is part of the GNU IO Library.
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2, or (at
7    your option) any later version.
8 
9    This library is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this library; see the file COPYING.  If not, write to
16    the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
17    MA 02111-1307, USA.
18 
19    As a special exception, if you link this library with files
20    compiled with a GNU compiler to produce an executable, this does
21    not cause the resulting executable to be covered by the GNU General
22    Public License.  This exception does not however invalidate any
23    other reasons why the executable file might be covered by the GNU
24    General Public License.  */
25 
26 // As a special exception, you may use this file as part of a free software
27 // library without restriction.  Specifically, if other files instantiate
28 // templates or use macros or inline functions from this file, or you compile
29 // this file and link it with other files to produce an executable, this
30 // file does not by itself cause the resulting executable to be covered by
31 // the GNU General Public License.  This exception does not however
32 // invalidate any other reasons why the executable file might be covered by
33 // the GNU General Public License.
34 /* Slightly modified from glibc/libio/iofwide.c */
35 
36 #include <libio.h>
37 
38 #if defined(_GLIBCPP_USE_WCHAR_T) || defined(_GLIBCPP_USE_TYPE_WCHAR_T)
39 
40 /* Prototypes of libio's codecvt functions.  */
41 static enum __codecvt_result
42 do_out(struct _IO_codecvt *codecvt, __c_mbstate_t *statep,
43        const wchar_t *from_start, const wchar_t *from_end,
44        const wchar_t **from_stop, char *to_start, char *to_end,
45        char **to_stop);
46 
47 static enum __codecvt_result
48 do_unshift(struct _IO_codecvt *codecvt, __c_mbstate_t *statep, char *to_start,
49 	   char *to_end, char **to_stop);
50 
51 static enum __codecvt_result
52 do_in(struct _IO_codecvt *codecvt, __c_mbstate_t *statep,
53       const char *from_start, const char *from_end, const char **from_stop,
54       wchar_t *to_start, wchar_t *to_end, wchar_t **to_stop);
55 
56 static int
57 do_encoding(struct _IO_codecvt *codecvt);
58 
59 static int
60 do_length(struct _IO_codecvt *codecvt, __c_mbstate_t *statep,
61 	  const char *from_start, const char *from_end, _IO_size_t max);
62 
63 static int
64 do_max_length(struct _IO_codecvt *codecvt);
65 
66 static int
67 do_always_noconv(struct _IO_codecvt *codecvt);
68 
69 
70 /* The functions used in `codecvt' for libio are always the same.  */
71 struct _IO_codecvt __c_libio_codecvt =
72 {
73   .__codecvt_destr = NULL,		/* Destructor, never used.  */
74   .__codecvt_do_out = do_out,
75   .__codecvt_do_unshift = do_unshift,
76   .__codecvt_do_in = do_in,
77   .__codecvt_do_encoding = do_encoding,
78   .__codecvt_do_always_noconv = do_always_noconv,
79   .__codecvt_do_length = do_length,
80   .__codecvt_do_max_length = do_max_length
81 };
82 
83 static enum __codecvt_result
do_out(struct _IO_codecvt * codecvt,__c_mbstate_t * statep,const wchar_t * from_start,const wchar_t * from_end,const wchar_t ** from_stop,char * to_start,char * to_end,char ** to_stop)84 do_out(struct _IO_codecvt *codecvt, __c_mbstate_t *statep,
85        const wchar_t *from_start, const wchar_t *from_end,
86        const wchar_t **from_stop, char *to_start, char *to_end,
87        char **to_stop)
88 {
89   enum __codecvt_result res = __codecvt_ok;
90 
91   while (from_start < from_end)
92     {
93       if (to_start >= to_end)
94 	{
95 	  res = __codecvt_partial;
96 	  break;
97 	}
98       *to_start++ = (char) *from_start++;
99     }
100 
101   *from_stop = from_start;
102   *to_stop = to_start;
103 
104   return res;
105 }
106 
107 
108 static enum __codecvt_result
do_unshift(struct _IO_codecvt * codecvt,__c_mbstate_t * statep,char * to_start,char * to_end,char ** to_stop)109 do_unshift(struct _IO_codecvt *codecvt, __c_mbstate_t *statep,
110 	   char *to_start, char *to_end, char **to_stop)
111 {
112   *to_stop = to_start;
113   return __codecvt_ok;
114 }
115 
116 
117 static enum __codecvt_result
do_in(struct _IO_codecvt * codecvt,__c_mbstate_t * statep,const char * from_start,const char * from_end,const char ** from_stop,wchar_t * to_start,wchar_t * to_end,wchar_t ** to_stop)118 do_in(struct _IO_codecvt *codecvt, __c_mbstate_t *statep,
119       const char *from_start, const char *from_end, const char **from_stop,
120       wchar_t *to_start, wchar_t *to_end, wchar_t **to_stop)
121 {
122   enum __codecvt_result res = __codecvt_ok;
123 
124   while (from_start < from_end)
125     {
126       if (to_start >= to_end)
127 	{
128 	  res = __codecvt_partial;
129 	  break;
130 	}
131       *to_start++ = (wchar_t) *from_start++;
132     }
133 
134   *from_stop = from_start;
135   *to_stop = to_start;
136 
137   return res;
138 }
139 
140 
141 static int
do_encoding(struct _IO_codecvt * codecvt)142 do_encoding(struct _IO_codecvt *codecvt)
143 { return 1; }
144 
145 
146 static int
do_always_noconv(struct _IO_codecvt * codecvt)147 do_always_noconv(struct _IO_codecvt *codecvt)
148 { return 0; }
149 
150 
151 static int
do_length(struct _IO_codecvt * codecvt,__c_mbstate_t * statep,const char * from_start,const char * from_end,_IO_size_t max)152 do_length(struct _IO_codecvt *codecvt, __c_mbstate_t *statep,
153 	  const char *from_start, const char *from_end, _IO_size_t max)
154 { return from_end - from_start; }
155 
156 
157 static int
do_max_length(struct _IO_codecvt * codecvt)158 do_max_length(struct _IO_codecvt *codecvt)
159 { return 1; }
160 
161 #endif /* _GLIBCPP_USE_WCHAR_T */
162