summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerdgeist <>2013-07-15 19:05:54 +0000
committererdgeist <>2013-07-15 19:05:54 +0000
commit959d7d76aec53f03463223ee64afb805e1865464 (patch)
tree8f98dc0ca01ac9804e9cf0f23e069b9abf8dfd50
parentf4f1db9d616de9281ebac66e2755bebbf21b7dae (diff)
Work around mbrlen return code stupidity.
-rwxr-xr-xvchat-ui.c32
1 files changed, 23 insertions, 9 deletions
diff --git a/vchat-ui.c b/vchat-ui.c
index 7873268..f3e5af5 100755
--- a/vchat-ui.c
+++ b/vchat-ui.c
@@ -142,14 +142,22 @@ togglequery() {
142} 142}
143 143
144const char * skip_to_character( const char * string, size_t offset ) { 144const char * skip_to_character( const char * string, size_t offset ) {
145 size_t ch_size;
145 mbstate_t mbs; 146 mbstate_t mbs;
146 memset(&mbs, 0, sizeof(mbs)); 147 memset(&mbs, 0, sizeof(mbs));
147 148
148 while( offset-- > 0 ) { 149 while( offset-- > 0 ) {
149 size_t ch_size = mbrlen( string, MB_CUR_MAX, &mbs ); 150 switch( ch_size = mbrlen( string, MB_CUR_MAX, &mbs ) )
150 if( ch_size < 0 ) return NULL; 151 {
151 if( !ch_size ) break; 152 case (size_t)-1:
152 string += ch_size; 153 case (size_t)-2:
154 return NULL;
155 case 0:
156 return string;
157 default:
158 string += ch_size;
159 break;
160 }
153 } 161 }
154 return string; 162 return string;
155} 163}
@@ -158,13 +166,19 @@ size_t offset_to_character( const char * string, size_t offset ) {
158 mbstate_t mbs; 166 mbstate_t mbs;
159 memset(&mbs, 0, sizeof(mbs)); 167 memset(&mbs, 0, sizeof(mbs));
160 const char * string_offset = string + offset; 168 const char * string_offset = string + offset;
161 size_t nchars = 0; 169 size_t ch_size, nchars = 0;
162 170
163 while( string < string_offset ) { 171 while( string < string_offset ) {
164 size_t ch_size = mbrlen( string, MB_CUR_MAX, &mbs ); 172 switch( ch_size = mbrlen( string, MB_CUR_MAX, &mbs ) )
165 if( ch_size < 0 ) return -1; 173 {
166 if( !ch_size ) break; 174 case (size_t)-1:
167 string += ch_size; 175 case (size_t)-2:
176 return -1;
177 case 0:
178 return nchars;
179 default:
180 string += ch_size;
181 }
168 nchars++; 182 nchars++;
169 } 183 }
170 return nchars; 184 return nchars;