Fix problem with getting cstring into buffer ... was failing when it shouldn't

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@23114 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2006-06-29 13:32:56 +00:00
parent 5e5cca4bca
commit 5ee536df00
2 changed files with 48 additions and 17 deletions

View file

@ -1,6 +1,7 @@
2006-06-29 Richard Frith-Macdonald <rfm@gnu.org>
* Source/Tools/gdnc.m: Don't try to trap SIGPROF
* Source/GSString.m: fix bug getting Cstring.
2006-06-27 Richard Frith-Macdonald <rfm@gnu.org>

View file

@ -1704,6 +1704,10 @@ static inline BOOL
getCStringE_c(GSStr self, char *buffer, unsigned int maxLength,
NSStringEncoding enc)
{
if (buffer == 0)
{
return NO; // Can't fit in here
}
if (enc == NSUnicodeStringEncoding)
{
if (maxLength >= sizeof(unichar))
@ -1745,7 +1749,43 @@ getCStringE_c(GSStr self, char *buffer, unsigned int maxLength,
}
return YES;
}
else if (enc == NSASCIIStringEncoding
if (enc == NSUTF8StringEncoding
&& GSIsByteEncoding(internalEncoding))
{
unsigned i;
/*
* Maybe we actually contain ascii data, which can be
* copied out directly as a utf-8 string.
*/
if (bytes > self->_count)
{
bytes = self->_count;
}
for (i = 0; i < bytes; i++)
{
unsigned char c = self->_contents.c[i];
if (c > 127)
{
break;
}
buffer[i] = c;
}
if (i == bytes)
{
buffer[bytes] = '\0';
if (bytes < self->_count)
{
return NO;
}
return YES;
}
// Fall through to perform conversion to unicode and back
}
if (enc == NSASCIIStringEncoding
&& GSIsByteEncoding(internalEncoding))
{
unsigned i;
@ -1791,18 +1831,16 @@ getCStringE_c(GSStr self, char *buffer, unsigned int maxLength,
format: @"Can't convert to Unicode string."];
}
if (GSFromUnicode((unsigned char**)&c, &bytes, u, l, enc,
NSDefaultMallocZone(), GSUniTerminate|GSUniStrict) == NO)
0, GSUniTerminate|GSUniStrict) == NO)
{
NSZoneFree(NSDefaultMallocZone(), u);
[NSException raise: NSCharacterConversionException
format: @"Can't convert from Unicode string."];
c = 0; // Unable to convert
}
NSZoneFree(NSDefaultMallocZone(), u);
if (c == (unsigned char*)buffer)
{
return YES; // Fitted in original buffer
}
else
else if (c != 0)
{
NSZoneFree(NSDefaultMallocZone(), c);
}
@ -1898,20 +1936,12 @@ getCStringE_u(GSStr self, char *buffer, unsigned int maxLength,
if (GSFromUnicode((unsigned char**)&c, &maxLength,
self->_contents.u, self->_count, enc,
NSDefaultMallocZone(), GSUniTerminate|GSUniStrict) == NO)
0, GSUniTerminate|GSUniStrict) == NO)
{
[NSException raise: NSCharacterConversionException
format: @"Can't convert to/from Unicode string."];
return NO;
}
if (c == (unsigned char*)buffer)
{
return YES;
}
else
{
NSZoneFree(NSDefaultMallocZone(), c);
}
}
}
return NO;
}