Make first moves towards handling the fact that unicode and cString

representations of the same string may contain different numbers of
characters.  In particular, UTF8 of course.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@10643 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-Macdonald 2001-08-03 12:24:25 +00:00
parent 80eb611458
commit bf97ef27e6
5 changed files with 504 additions and 425 deletions

View file

@ -1,3 +1,12 @@
2001-08-03 Richard Frith-Macdonald <rfm@gnu.org>
* Headers/Foundation/Unicode.h: API Change
* Source/Unicode.m: API Change ... remove old string conversion
functions and replace with two functions, both of which differentiate
between lengths of the source strings and sizes of destination buffers.
* Source/NSString.m: Update for changes to Unicode.[hm]
* Source/GSString.m: Update for changes to Unicode.[hm]
2001-08-02 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSPortNameServer.m: Log the host we are connecting to

View file

@ -40,14 +40,11 @@ GS_EXPORT char unitochar(unichar u);
GS_EXPORT unichar encode_chartouni(char c, NSStringEncoding enc);
GS_EXPORT char encode_unitochar(unichar u, NSStringEncoding enc);
GS_EXPORT unsigned encode_unitochar_strict(unichar u, NSStringEncoding enc);
GS_EXPORT int strtoustr(unichar * u1,const char *s1,int size);
GS_EXPORT int ustrtostr(char *s2,unichar *u1,int size);
GS_EXPORT int encode_strtoustr(unichar* u1,const char*s1,int size,
NSStringEncoding enc);
GS_EXPORT int encode_ustrtostr(char *s2, unichar *u1, int size,
NSStringEncoding enc);
GS_EXPORT int encode_ustrtostr_strict(char *s2, unichar *u1, int size,
NSStringEncoding enc);
GS_EXPORT int encode_ustrtocstr(char *dst, int dl, const unichar *src, int sl,
NSStringEncoding enc, BOOL strict);
GS_EXPORT int encode_cstrtoustr(unichar *dst, int dl, const char *str, int sl,
NSStringEncoding enc);
GS_EXPORT unichar uni_tolower(unichar ch);
GS_EXPORT unichar uni_toupper(unichar ch);

View file

@ -513,7 +513,7 @@ boolValue_u(ivars self)
unsigned len = self->_count < 10 ? self->_count : 9;
char buf[len+1];
encode_ustrtostr(buf, self->_contents.u, len, defEnc);
len = encode_ustrtocstr(buf, len, self->_contents.u, len, defEnc, NO);
buf[len] = '\0';
if (len == 3
&& (buf[0] == 'Y' || buf[0] == 'y')
@ -634,18 +634,18 @@ cString_c(ivars self)
static inline char*
cString_u(ivars self)
{
char *r = (char*)_fastMallocBuffer(self->_count+1);
int l = self->_count;
char *r = (char*)_fastMallocBuffer(l + 1);
if (self->_count > 0)
if (l > 0)
{
if (encode_ustrtostr_strict(r, self->_contents.u, self->_count, defEnc)
== 0)
if (encode_ustrtocstr(r, l, self->_contents.u, l, defEnc, YES) == 0)
{
[NSException raise: NSCharacterConversionException
format: @"Can't get cString from Unicode string."];
}
}
r[self->_count] = '\0';
r[l] = '\0';
return r;
}
@ -660,19 +660,20 @@ static inline unsigned int
cStringLength_u(ivars self)
{
unsigned c;
unsigned l = self->_count;
if (self->_count > 0)
if (l > 0)
{
char *r;
r = (char*)NSZoneMalloc(NSDefaultMallocZone(), self->_count+1);
if (encode_ustrtostr(r, self->_contents.u, self->_count, defEnc) == 0)
r = (char*)NSZoneMalloc(NSDefaultMallocZone(), l + 1);
if (encode_ustrtocstr(r, l, self->_contents.u, l, defEnc, NO) == 0)
{
NSZoneFree(NSDefaultMallocZone(), r);
[NSException raise: NSCharacterConversionException
format: @"Can't get cStringLength from Unicode string."];
}
r[self->_count] = '\0';
r[l] = '\0';
c = strlen(r);
NSZoneFree(NSDefaultMallocZone(), r);
}
@ -714,31 +715,45 @@ dataUsingEncoding_c(ivars self, NSStringEncoding encoding, BOOL flag)
buff = (unichar*)NSZoneMalloc(NSDefaultMallocZone(),
sizeof(unichar)*(len+1));
buff[0] = 0xFEFF;
t = encode_strtoustr(buff+1, self->_contents.c, len, defEnc);
t = encode_cstrtoustr(buff+1, len, self->_contents.c, len, defEnc);
return [NSDataClass dataWithBytesNoCopy: buff
length: sizeof(unichar)*(t+1)];
}
else
{
int t;
int bsiz;
unichar *ubuff;
unsigned char *buff;
ubuff = (unichar*)NSZoneMalloc(NSDefaultMallocZone(),
sizeof(unichar)*len);
t = encode_strtoustr(ubuff, self->_contents.c, len, defEnc);
buff = (unsigned char*)NSZoneMalloc(NSDefaultMallocZone(), t);
if (flag)
t = encode_ustrtostr(buff, ubuff, t, encoding);
else
t = encode_ustrtostr_strict(buff, ubuff, t, encoding);
t = encode_cstrtoustr(ubuff, len, self->_contents.c, len, defEnc);
if (encoding == NSUTF8StringEncoding)
{
bsiz = t*4;
}
else
{
bsiz = t;
}
buff = (unsigned char*)NSZoneMalloc(NSDefaultMallocZone(), bsiz);
flag = (flag == YES) ? NO : YES;
t = encode_ustrtocstr(buff, bsiz, ubuff, t, encoding, flag);
NSZoneFree(NSDefaultMallocZone(), ubuff);
if (t == 0)
{
NSZoneFree(NSDefaultMallocZone(), buff);
return nil;
}
return [NSDataClass dataWithBytesNoCopy: buff length: t];
else
{
if (t != bsiz)
{
buff = NSZoneRealloc(NSDefaultMallocZone(), buff, t);
}
return [NSDataClass dataWithBytesNoCopy: buff length: t];
}
}
}
@ -766,19 +781,33 @@ dataUsingEncoding_u(ivars self, NSStringEncoding encoding, BOOL flag)
else
{
int t;
int bsiz;
unsigned char *buff;
buff = (unsigned char*)NSZoneMalloc(NSDefaultMallocZone(), len);
if (flag == YES)
t = encode_ustrtostr(buff, self->_contents.u, len, encoding);
else
t = encode_ustrtostr_strict(buff, self->_contents.u, len, encoding);
if (!t)
if (encoding == NSUTF8StringEncoding)
{
bsiz = len*4;
}
else
{
bsiz = len;
}
buff = (unsigned char*)NSZoneMalloc(NSDefaultMallocZone(), bsiz);
flag = (flag == YES) ? NO : YES;
t = encode_ustrtocstr(buff, bsiz, self->_contents.u, len, encoding, flag);
if (t == 0)
{
NSZoneFree(NSDefaultMallocZone(), buff);
return nil;
}
return [NSDataClass dataWithBytesNoCopy: buff length: t];
else
{
if (t != bsiz)
{
buff = NSZoneRealloc(NSDefaultMallocZone(), buff, t);
}
return [NSDataClass dataWithBytesNoCopy: buff length: t];
}
}
}
@ -812,7 +841,7 @@ doubleValue_u(ivars self)
unsigned len = self->_count < 32 ? self->_count : 31;
char buf[len+1];
encode_ustrtostr(buf, self->_contents.u, len, defEnc);
len = encode_ustrtocstr(buf, len, self->_contents.u, len, defEnc, NO);
buf[len] = '\0';
return atof(buf);
}
@ -863,7 +892,7 @@ fillHole(ivars self, unsigned index, unsigned size)
static inline void
getCharacters_c(ivars self, unichar *buffer, NSRange aRange)
{
encode_strtoustr(buffer, self->_contents.c + aRange.location,
encode_cstrtoustr(buffer, aRange.length, self->_contents.c + aRange.location,
aRange.length, defEnc);
}
@ -937,8 +966,8 @@ getCString_u(ivars self, char *buffer, unsigned int maxLength,
}
}
result = encode_ustrtostr_strict(buffer, &self->_contents.u[aRange.location],
len, defEnc);
result = encode_ustrtocstr(buffer, len, &self->_contents.u[aRange.location],
len, defEnc, YES);
if (result != len)
{
[NSException raise: NSCharacterConversionException
@ -977,7 +1006,7 @@ intValue_u(ivars self)
unsigned len = self->_count < 32 ? self->_count : 31;
char buf[len+1];
encode_ustrtostr(buf, self->_contents.u, len, defEnc);
len = encode_ustrtocstr(buf, len, self->_contents.u, len, defEnc, NO);
buf[len] = '\0';
return atol(buf);
}
@ -1130,10 +1159,11 @@ lossyCString_c(ivars self)
static inline const char*
lossyCString_u(ivars self)
{
unsigned char *r = (unsigned char*)_fastMallocBuffer(self->_count+1);
unsigned l = self->_count;
unsigned char *r = (unsigned char*)_fastMallocBuffer(l + 1);
encode_ustrtostr(r, self->_contents.u, self->_count, defEnc);
r[self->_count] = '\0';
encode_ustrtocstr(r, l, self->_contents.u, l, defEnc, NO);
r[l] = '\0';
return (const char*)r;
}
@ -1433,10 +1463,10 @@ transmute(ivars self, NSString *aString)
if (transmute == YES)
{
unichar *tmp;
int len;
int len = self->_count;
tmp = NSZoneMalloc(self->_zone, self->_capacity * sizeof(unichar));
len = encode_strtoustr(tmp, self->_contents.c, self->_count, defEnc);
len = encode_cstrtoustr(tmp, len, self->_contents.c, len, defEnc);
if (self->_flags.free == 1)
{
NSZoneFree(self->_zone, self->_contents.c);

View file

@ -601,7 +601,8 @@ handle_printf_atsign (FILE *stream,
unichar *buf;
buf = (unichar*)NSZoneMalloc(GSObjCZone(self), sizeof(unichar)*length);
length = encode_strtoustr(buf, byteString, length, _DefaultStringEncoding);
length = encode_cstrtoustr(buf, length, byteString, length,
_DefaultStringEncoding);
if (flag == YES && byteString != 0)
{
NSZoneFree(NSZoneFromPointer(byteString), byteString);
@ -694,7 +695,8 @@ handle_printf_atsign (FILE *stream,
unichar *s;
s = NSZoneMalloc(GSObjCZone(self), sizeof(unichar)*length);
length = encode_strtoustr(s, bytes, length+1, NSUTF8StringEncoding);
length = encode_cstrtoustr(s, length, bytes, length,
NSUTF8StringEncoding);
self = [self initWithCharactersNoCopy: s
length: length
freeWhenDone: YES];
@ -1101,7 +1103,8 @@ handle_printf_atsign (FILE *stream,
unichar *u;
u = NSZoneMalloc(GSObjCZone(self), sizeof(unichar)*length);
length = encode_strtoustr(u, bytes, length+1, NSUTF8StringEncoding);
length = encode_cstrtoustr(u, length, bytes, length,
NSUTF8StringEncoding);
self = [self initWithCharactersNoCopy: u
length: length
freeWhenDone: YES];
@ -1141,7 +1144,7 @@ handle_printf_atsign (FILE *stream,
}
else
{
count = encode_strtoustr(u, b, len, encoding);
count = encode_cstrtoustr(u, len, b, len, encoding);
}
self = [self initWithCharactersNoCopy: u length: count freeWhenDone: YES];
@ -2307,23 +2310,37 @@ handle_printf_atsign (FILE *stream,
else
{
int t;
int bsiz;
unichar *u;
unsigned char *buff;
u = (unichar*)NSZoneMalloc(NSDefaultMallocZone(), len*sizeof(unichar));
[self getCharacters: u];
buff = (unsigned char*)NSZoneMalloc(NSDefaultMallocZone(), len);
if (flag == YES)
t = encode_ustrtostr(buff, u, len, encoding);
else
t = encode_ustrtostr_strict(buff, u, len, encoding);
if (encoding == NSUTF8StringEncoding)
{
bsiz = len * 4;
}
else
{
bsiz = len;
}
buff = (unsigned char*)NSZoneMalloc(NSDefaultMallocZone(), bsiz);
flag = (flag == YES) ? NO : YES;
t = encode_ustrtocstr(buff, bsiz, u, len, encoding, flag);
NSZoneFree(NSDefaultMallocZone(), u);
if (t == 0)
{
NSZoneFree(NSDefaultMallocZone(), buff);
return nil;
}
return [NSDataClass dataWithBytesNoCopy: buff length: t];
else
{
if (bsiz != t)
{
buff = NSZoneRealloc(NSDefaultMallocZone(), buff, t);
}
return [NSDataClass dataWithBytesNoCopy: buff length: t];
}
}
return nil;
}

View file

@ -360,7 +360,7 @@ iconv_strtoustr(unichar *u2, int size2, const char *s1, int size1, NSStringEncod
}
int
iconv_ustrtostr(char *s2, int size2, unichar *u1, int size1,
iconv_ustrtostr(char *s2, int size2, const unichar *u1, int size1,
NSStringEncoding enc)
{
iconv_t conv;
@ -643,378 +643,12 @@ unitochar(unichar u)
return encode_unitochar(u, defEnc);
}
int
strtoustr(unichar *u1, const char *s1, int size)
{
if (defEnc == GSUndefinedEncoding)
{
defEnc = GetDefEncoding();
}
return encode_strtoustr(u1, s1, size, defEnc);
}
int
ustrtostr(char *s2, unichar *u1, int size)
{
if (defEnc == GSUndefinedEncoding)
{
defEnc = GetDefEncoding();
}
return encode_ustrtostr(s2, u1, size, defEnc);
}
int
encode_strtoustr(unichar *u1, const char *s1, int size, NSStringEncoding enc)
{
int count;
switch (enc)
{
case NSNonLossyASCIIStringEncoding:
case NSASCIIStringEncoding:
case NSISOLatin1StringEncoding:
case NSUnicodeStringEncoding:
for (count = 0; count < size; count++)
{
u1[count] = (unichar)((unc)s1[count]);
}
return count;
case NSNEXTSTEPStringEncoding:
for (count = 0; count < size; count++)
{
unc c = (unc)s1[count];
if (c < Next_conv_base)
u1[count] = (unichar)c;
else
u1[count] = Next_char_to_uni_table[c - Next_conv_base];
}
return count;
case NSISOCyrillicStringEncoding:
for (count = 0; count < size; count++)
{
unc c = (unc)s1[count];
if (c < Cyrillic_conv_base)
u1[count] = (unichar)c;
else
u1[count] = Cyrillic_char_to_uni_table[c - Cyrillic_conv_base];
}
return count;
case NSISOLatin2StringEncoding:
for (count = 0; count < size; count++)
{
unc c = (unc)s1[count];
if (c < Latin2_conv_base)
u1[count] = (unichar)c;
else
u1[count] = Latin2_char_to_uni_table[c - Latin2_conv_base];
}
return count;
#if 0
case NSSymbolStringEncoding:
for (count = 0; count < size; count++)
{
unc c = (unc)s1[count];
if (c < Symbol_conv_base)
u1[count] = (unichar)c;
else
u1[count] = Symbol_char_to_uni_table[c - Symbol_conv_base];
}
return count;
#endif
default:
#ifdef HAVE_ICONV
return iconv_strtoustr(u1, size, s1, size, enc);
#else
return 0;
#endif
}
/*
for (count = 0; count < size; count++)
{
u1[count] = encode_chartouni(s1[count], enc);
}
return count;
*/
}
int
encode_ustrtostr(char *s2, unichar *u1, int size, NSStringEncoding enc)
{
int count;
unichar u;
switch (enc)
{
case NSNonLossyASCIIStringEncoding:
for (count = 0; count < size; count++)
{
u = u1[count];
if (u < 128)
s2[count] = (char)u;
else
s2[count] = '*';
}
return count;
case NSASCIIStringEncoding:
for (count = 0; count < size; count++)
{
u = u1[count];
if (u < 128)
s2[count] = (char)u;
else
s2[count] = '*';
}
return count;
case NSISOLatin1StringEncoding:
case NSUnicodeStringEncoding:
for (count = 0; count < size; count++)
{
u = u1[count];
if (u < 256)
s2[count] = (char)u;
else
s2[count] = '*';
}
return count;
case NSNEXTSTEPStringEncoding:
for (count = 0; count < size; count++)
{
u = u1[count];
if (u < (unichar)Next_conv_base)
s2[count] = (char)u;
else
{
int res;
int i = 0;
while (((res = u - Next_uni_to_char_table[i++].from) > 0)
&& (i < Next_uni_to_char_table_size));
s2[count] = res ? '*' : Next_uni_to_char_table[--i].to;
}
}
return count;
case NSISOCyrillicStringEncoding:
for (count = 0; count < size; count++)
{
u = u1[count];
if (u < (unichar)Cyrillic_conv_base)
s2[count] = (char)u;
else
{
int res;
int i = 0;
while (((res = u - Cyrillic_uni_to_char_table[i++].from) > 0)
&& (i < Cyrillic_uni_to_char_table_size));
s2[count] = res ? '*' : Cyrillic_uni_to_char_table[--i].to;
}
}
return count;
case NSISOLatin2StringEncoding:
for (count = 0; count < size; count++)
{
u = u1[count];
if (u < (unichar)Latin2_conv_base)
s2[count] = (char)u;
else
{
int res;
int i = 0;
while (((res = u - Latin2_uni_to_char_table[i++].from) > 0)
&& (i < Latin2_uni_to_char_table_size));
s2[count] = res ? '*' : Latin2_uni_to_char_table[--i].to;
}
}
return count;
#if 0
case NSSymbolStringEncoding:
for (count = 0; count < size; count++)
{
u = u1[count];
if (u < (unichar)Symbol_conv_base)
s2[count] = (char)u;
else
{
int res;
int i = 0;
while (((res = u - Symbol_uni_to_char_table[i++].from) > 0)
&& (i < Symbol_uni_to_char_table_size));
s2[count] = res ? '*' : Symbol_uni_to_char_table[--i].to;
}
}
return count;
#endif
default:
#ifdef HAVE_ICONV
// FIXME: The non-strict encoding is still missing
return iconv_ustrtostr(s2, size, u1, size, enc);
#else
return 0;
#endif
}
}
int
encode_ustrtostr_strict(char *s2, unichar *u1, int size, NSStringEncoding enc)
{
int count;
unichar u;
switch (enc)
{
case NSNonLossyASCIIStringEncoding:
for (count = 0; count < size; count++)
{
u = u1[count];
if (u < 128)
s2[count] = (char)u;
else
return 0;
}
return count;
case NSASCIIStringEncoding:
for (count = 0; count < size; count++)
{
u = u1[count];
if (u < 128)
s2[count] = (char)u;
else
return 0;
}
return count;
case NSISOLatin1StringEncoding:
case NSUnicodeStringEncoding:
for (count = 0; count < size; count++)
{
u = u1[count];
if (u < 256)
s2[count] = (char)u;
else
return 0;
}
return count;
case NSNEXTSTEPStringEncoding:
for (count = 0; count < size; count++)
{
u = u1[count];
if (u < (unichar)Next_conv_base)
s2[count] = (char)u;
else
{
int res;
int i = 0;
while (((res = u - Next_uni_to_char_table[i++].from) > 0)
&& (i < Next_uni_to_char_table_size));
if (!res)
s2[count] = Next_uni_to_char_table[--i].to;
else
return 0;
}
}
return count;
case NSISOCyrillicStringEncoding:
for (count = 0; count < size; count++)
{
u = u1[count];
if (u < (unichar)Cyrillic_conv_base)
s2[count] = (char)u;
else
{
int res;
int i = 0;
while (((res = u - Cyrillic_uni_to_char_table[i++].from) > 0)
&& (i < Cyrillic_uni_to_char_table_size));
if (!res)
s2[count] = Cyrillic_uni_to_char_table[--i].to;
else
return 0;
}
}
return count;
case NSISOLatin2StringEncoding:
for (count = 0; count < size; count++)
{
u = u1[count];
if (u < (unichar)Latin2_conv_base)
s2[count] = (char)u;
else
{
int res;
int i = 0;
while (((res = u - Latin2_uni_to_char_table[i++].from) > 0)
&& (i < Latin2_uni_to_char_table_size));
if (!res)
s2[count] = Latin2_uni_to_char_table[--i].to;
else
return 0;
}
}
return count;
#if 0
case NSSymbolStringEncoding:
for (count = 0; count < size; count++)
{
u = u1[count];
if (u < (unichar)Symbol_conv_base)
s2[count] = (char)u;
else
{
int res;
int i = 0;
while (((res = u - Symbol_uni_to_char_table[i++].from) > 0)
&& (i < Symbol_uni_to_char_table_size));
if (!res)
s2[count] = Symbol_uni_to_char_table[--i].to;
else
return 0;
}
}
return count;
#endif
default:
#ifdef HAVE_ICONV
return iconv_ustrtostr(s2, size, u1, size, enc);
#else
return 0;
#endif
}
}
// These two functions use direct access into a two-level table to map cases.
// The two-level table method is less space efficient (but still not bad) than
// a single table and a linear search, but it reduces the number of
// conditional statements to just one.
* These two functions use direct access into a two-level table to map cases.
* The two-level table method is less space efficient (but still not bad) than
* a single table and a linear search, but it reduces the number of
* conditional statements to just one.
*/
unichar
uni_tolower(unichar ch)
{
@ -1127,3 +761,395 @@ uni_is_decomp(unichar u)
return 0;
}
int encode_ustrtocstr(char *dst, int dl, const unichar *src, int sl,
NSStringEncoding enc, BOOL strict)
{
if (strict == YES)
{
int count;
unichar u;
switch (enc)
{
case NSNonLossyASCIIStringEncoding:
for (count = 0; count < sl && count < dl; count++)
{
u = src[count];
if (u < 128)
dst[count] = (char)u;
else
return 0;
}
if (count < sl)
return 0; // Not all characters converted.
return count;
case NSASCIIStringEncoding:
for (count = 0; count < sl && count < dl; count++)
{
u = src[count];
if (u < 128)
dst[count] = (char)u;
else
return 0;
}
if (count < sl)
return 0; // Not all characters converted.
return count;
case NSISOLatin1StringEncoding:
case NSUnicodeStringEncoding:
for (count = 0; count < sl && count < dl; count++)
{
u = src[count];
if (u < 256)
dst[count] = (char)u;
else
return 0;
}
if (count < sl)
return 0; // Not all characters converted.
return count;
case NSNEXTSTEPStringEncoding:
for (count = 0; count < sl && count < dl; count++)
{
u = src[count];
if (u < (unichar)Next_conv_base)
{
dst[count] = (char)u;
}
else
{
int res;
int i = 0;
while (((res = u - Next_uni_to_char_table[i++].from) > 0)
&& (i < Next_uni_to_char_table_size));
if (!res)
dst[count] = Next_uni_to_char_table[--i].to;
else
return 0;
}
}
if (count < sl)
return 0; // Not all characters converted.
return count;
case NSISOCyrillicStringEncoding:
for (count = 0; count < sl && count < dl; count++)
{
u = src[count];
if (u < (unichar)Cyrillic_conv_base)
dst[count] = (char)u;
else
{
int res;
int i = 0;
while (((res = u - Cyrillic_uni_to_char_table[i++].from)
> 0) && (i < Cyrillic_uni_to_char_table_size));
if (!res)
dst[count] = Cyrillic_uni_to_char_table[--i].to;
else
return 0;
}
}
if (count < sl)
return 0; // Not all characters converted.
return count;
case NSISOLatin2StringEncoding:
for (count = 0; count < sl && count < dl; count++)
{
u = src[count];
if (u < (unichar)Latin2_conv_base)
{
dst[count] = (char)u;
}
else
{
int res;
int i = 0;
while (((res = u - Latin2_uni_to_char_table[i++].from) > 0)
&& (i < Latin2_uni_to_char_table_size));
if (!res)
dst[count] = Latin2_uni_to_char_table[--i].to;
else
return 0;
}
}
if (count < sl)
return 0; // Not all characters converted.
return count;
#if 0
case NSSymbolStringEncoding:
for (count = 0; count < sl && count < dl; count++)
{
u = src[count];
if (u < (unichar)Symbol_conv_base)
dst[count] = (char)u;
else
{
int res;
int i = 0;
while (((res = u - Symbol_uni_to_char_table[i++].from) > 0)
&& (i < Symbol_uni_to_char_table_size));
if (!res)
dst[count] = Symbol_uni_to_char_table[--i].to;
else
return 0;
}
}
if (count < sl)
return 0; // Not all characters converted.
return count;
#endif
default:
#ifdef HAVE_ICONV
return iconv_ustrtostr(dst, dl, src, sl, enc);
#else
return 0;
#endif
}
}
else
{
int count;
unichar u;
switch (enc)
{
case NSNonLossyASCIIStringEncoding:
for (count = 0; count < sl && count < dl; count++)
{
u = src[count];
if (u < 128)
dst[count] = (char)u;
else
dst[count] = '*';
}
if (count < sl)
return 0; // Not all characters converted.
return count;
case NSASCIIStringEncoding:
for (count = 0; count < sl && count < dl; count++)
{
u = src[count];
if (u < 128)
dst[count] = (char)u;
else
dst[count] = '*';
}
if (count < sl)
return 0; // Not all characters converted.
return count;
case NSISOLatin1StringEncoding:
case NSUnicodeStringEncoding:
for (count = 0; count < sl && count < dl; count++)
{
u = src[count];
if (u < 256)
dst[count] = (char)u;
else
dst[count] = '*';
}
if (count < sl)
return 0; // Not all characters converted.
return count;
case NSNEXTSTEPStringEncoding:
for (count = 0; count < sl && count < dl; count++)
{
u = src[count];
if (u < (unichar)Next_conv_base)
dst[count] = (char)u;
else
{
int res;
int i = 0;
while (((res = u - Next_uni_to_char_table[i++].from) > 0)
&& (i < Next_uni_to_char_table_size));
dst[count] = res ? '*' : Next_uni_to_char_table[--i].to;
}
}
if (count < sl)
return 0; // Not all characters converted.
return count;
case NSISOCyrillicStringEncoding:
for (count = 0; count < sl && count < dl; count++)
{
u = src[count];
if (u < (unichar)Cyrillic_conv_base)
dst[count] = (char)u;
else
{
int res;
int i = 0;
while (((res = u - Cyrillic_uni_to_char_table[i++].from)
> 0) && (i < Cyrillic_uni_to_char_table_size));
dst[count] = res ? '*' : Cyrillic_uni_to_char_table[--i].to;
}
}
if (count < sl)
return 0; // Not all characters converted.
return count;
case NSISOLatin2StringEncoding:
for (count = 0; count < sl && count < dl; count++)
{
u = src[count];
if (u < (unichar)Latin2_conv_base)
dst[count] = (char)u;
else
{
int res;
int i = 0;
while (((res = u - Latin2_uni_to_char_table[i++].from) > 0)
&& (i < Latin2_uni_to_char_table_size));
dst[count] = res ? '*' : Latin2_uni_to_char_table[--i].to;
}
}
if (count < sl)
return 0; // Not all characters converted.
return count;
#if 0
case NSSymbolStringEncoding:
for (count = 0; count < sl && count < dl; count++)
{
u = src[count];
if (u < (unichar)Symbol_conv_base)
dst[count] = (char)u;
else
{
int res;
int i = 0;
while (((res = u - Symbol_uni_to_char_table[i++].from) > 0)
&& (i < Symbol_uni_to_char_table_size));
dst[count] = res ? '*' : Symbol_uni_to_char_table[--i].to;
}
}
if (count < sl)
return 0; // Not all characters converted.
return count;
#endif
default:
#ifdef HAVE_ICONV
// FIXME: The non-strict encoding is still missing
return iconv_ustrtostr(dst, dl, src, sl, enc);
#else
return 0;
#endif
}
}
}
int encode_cstrtoustr(unichar *dst, int dl, const char *src, int sl,
NSStringEncoding enc)
{
int count;
switch (enc)
{
case NSNonLossyASCIIStringEncoding:
case NSASCIIStringEncoding:
case NSISOLatin1StringEncoding:
case NSUnicodeStringEncoding:
for (count = 0; count < sl && count < dl; count++)
{
dst[count] = (unichar)((unc)src[count]);
}
if (count < sl)
return 0; // Not all characters converted.
return count;
case NSNEXTSTEPStringEncoding:
for (count = 0; count < sl && count < dl; count++)
{
unc c = (unc)src[count];
if (c < Next_conv_base)
dst[count] = (unichar)c;
else
dst[count] = Next_char_to_uni_table[c - Next_conv_base];
}
if (count < sl)
return 0; // Not all characters converted.
return count;
case NSISOCyrillicStringEncoding:
for (count = 0; count < sl && count < dl; count++)
{
unc c = (unc)src[count];
if (c < Cyrillic_conv_base)
dst[count] = (unichar)c;
else
dst[count] = Cyrillic_char_to_uni_table[c - Cyrillic_conv_base];
}
if (count < sl)
return 0; // Not all characters converted.
return count;
case NSISOLatin2StringEncoding:
for (count = 0; count < sl && count < dl; count++)
{
unc c = (unc)src[count];
if (c < Latin2_conv_base)
dst[count] = (unichar)c;
else
dst[count] = Latin2_char_to_uni_table[c - Latin2_conv_base];
}
if (count < sl)
return 0; // Not all characters converted.
return count;
#if 0
case NSSymbolStringEncoding:
for (count = 0; count < sl && count < dl; count++)
{
unc c = (unc)src[count];
if (c < Symbol_conv_base)
dst[count] = (unichar)c;
else
dst[count] = Symbol_char_to_uni_table[c - Symbol_conv_base];
}
if (count < sl)
return 0; // Not all characters converted.
return count;
#endif
default:
#ifdef HAVE_ICONV
return iconv_strtoustr(dst, dl, src, sl, enc);
#else
return 0;
#endif
}
/*
for (count = 0; count < sl && count < dl; count++)
{
dst[count] = encode_chartouni(src[count], enc);
}
if (count < sl)
return 0; // Not all characters converted.
return count;
*/
}