Tidied api a little

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@13114 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-Macdonald 2002-03-14 15:27:12 +00:00
parent f1f97ad614
commit af6f96f586
2 changed files with 102 additions and 33 deletions

View file

@ -31,6 +31,11 @@
#include <Foundation/NSString.h> /* For string encodings */ #include <Foundation/NSString.h> /* For string encodings */
/*
* Private API used internally by NSString etc.
*/
#ifndef NO_GNUSTEP
GS_EXPORT NSStringEncoding *GetAvailableEncodings(); GS_EXPORT NSStringEncoding *GetAvailableEncodings();
GS_EXPORT NSStringEncoding GetDefEncoding(); GS_EXPORT NSStringEncoding GetDefEncoding();
GS_EXPORT NSString* GetEncodingName(NSStringEncoding encoding); GS_EXPORT NSString* GetEncodingName(NSStringEncoding encoding);
@ -52,4 +57,20 @@ GS_EXPORT unsigned char uni_cop(unichar u);
GS_EXPORT BOOL uni_isnonsp(unichar u); GS_EXPORT BOOL uni_isnonsp(unichar u);
GS_EXPORT unichar *uni_is_decomp(unichar u); GS_EXPORT unichar *uni_is_decomp(unichar u);
/*
* Options when converting strings.
*/
#define GSUniTerminate 0x01
#define GSUniTemporary 0x02
#define GSUniStrict 0x04
GS_EXPORT BOOL GSFromUnicode(unsigned char **dst, unsigned int *size,
const unichar *src, unsigned int slen, NSStringEncoding enc, NSZone *zone,
unsigned int options);
GS_EXPORT BOOL GSToUnicode(unichar **dst, unsigned int *size,
const unsigned char *src, unsigned int slen, NSStringEncoding enc,
NSZone *zone, unsigned int options);
#endif
#endif /* __Unicode_h_OBJECTS_INCLUDE */ #endif /* __Unicode_h_OBJECTS_INCLUDE */

View file

@ -1431,12 +1431,17 @@ else \
* is not big enough (unless dst is a nul pointer ... indicating that * is not big enough (unless dst is a nul pointer ... indicating that
* converted data is to be discarded). * converted data is to be discarded).
* </p> * </p>
* <p>The terminate argument indicates that the destination buffer size * The options argument controls some special behavior.
* supplied is actually one character shorter than the real buffer size, * <list>
* and that a nul terminator is to be placed at the end of the output string. * <item>If GSUniTerminate is set, the function is expected to nul terminate
* Also, if the function grows the buffer, it must allow for an extra * the output string, and will assume that it is safe to place the nul
* termination character. * just beyond the ned of the stated buffer size.
* </p> * Also, if the function grows the buffer, it will allow for an extra
* termination character.</item>
* <item>If GSUniTemporary is set, the function will return the results in
* an autoreleased buffer rather than in a buffer that the caller must
* release.</item>
* </list>
* <p>On return, the function result is a flag indicating success (YES) * <p>On return, the function result is a flag indicating success (YES)
* or failure (NO), and on success, the value stored in size is the number * or failure (NO), and on success, the value stored in size is the number
* of characters in the converted string. The converted string itsself is * of characters in the converted string. The converted string itsself is
@ -1447,15 +1452,16 @@ else \
* </p> * </p>
*/ */
BOOL BOOL
gsToUnicode(unichar **dst, unsigned *size, const char *src, unsigned slen, GSToUnicode(unichar **dst, unsigned int *size, const unsigned char *src,
NSStringEncoding enc, NSZone *zone, BOOL terminate) unsigned int slen, NSStringEncoding enc, NSZone *zone,
unsigned int options)
{ {
unichar buf[BUFSIZ]; unichar buf[BUFSIZ];
unichar *ptr; unichar *ptr;
unsigned bsize; unsigned bsize;
unsigned dpos = 0; // Offset into destination buffer. unsigned dpos = 0; // Offset into destination buffer.
unsigned spos = 0; // Offset into source buffer. unsigned spos = 0; // Offset into source buffer.
unsigned extra = (terminate == YES) ? sizeof(unichar) : 0; unsigned extra = (options & GSUniTerminate) ? sizeof(unichar) : 0;
unichar base = 0; unichar base = 0;
unichar *table = 0; unichar *table = 0;
@ -1470,7 +1476,7 @@ gsToUnicode(unichar **dst, unsigned *size, const char *src, unsigned slen,
if (dst == 0 || *size == 0) if (dst == 0 || *size == 0)
{ {
ptr = buf; ptr = buf;
bsize = (terminate == YES) ? BUFSIZ - 1 : BUFSIZ; bsize = (extra != 0) ? BUFSIZ - 1 : BUFSIZ;
} }
else else
{ {
@ -1618,21 +1624,37 @@ tables:
/* /*
* Post conversion ... set output values. * Post conversion ... set output values.
*/ */
if (terminate == YES) if (extra != 0)
{ {
ptr[dpos] = (unichar)0; ptr[dpos] = (unichar)0;
} }
*size = dpos; *size = dpos;
if (dst != 0) if (dst != 0)
{ {
/* if (options & GSUniTemporary)
* If resizing is permitted, try ensure we return a buffer which {
* is just big enough to hold the converted string. unsigned bytes = dpos * sizeof(unichar) + extra;
*/ void *r;
if (zone != 0 && bsize > dpos)
/*
* Temporary string was requested ... make one.
*/
r = _fastMallocBuffer(bytes);
memcpy(r, ptr, bytes);
if (ptr != buf && ptr != *dst)
{
NSZoneFree(zone, ptr);
}
ptr = r;
}
else if (zone != 0 && bsize > dpos)
{ {
unsigned bytes = dpos * sizeof(unichar) + extra; unsigned bytes = dpos * sizeof(unichar) + extra;
/*
* Resizing is permitted, try ensure we return a buffer which
* is just big enough to hold the converted string.
*/
if (ptr == buf || ptr == *dst) if (ptr == buf || ptr == *dst)
{ {
unichar *tmp; unichar *tmp;
@ -1736,12 +1758,21 @@ else \
* is not big enough (unless dst is a nul pointer ... indicating that * is not big enough (unless dst is a nul pointer ... indicating that
* converted data is to be discarded). * converted data is to be discarded).
* </p> * </p>
* <p>The terminate argument indicates that the destination buffer size * The options argument controls some special behavior.
* supplied is actually one character shorter than the real buffer size, * <list>
* and that a nul terminator is to be placed at the end of the output string. * <item>If GSUniStrict is set, the function will fail if a character is
* Also, if the function grows the buffer, it must allow for an extra * encountered which can't be displayed in the source. Otherwise, some
* termination character. * approximation or marker will be placed in the destination.</item>
* </p> * </list>
* <item>If GSUniTerminate is set, the function is expected to nul terminate
* the output string, and will assume that it is safe to place the nul
* just beyond the ned of the stated buffer size.
* Also, if the function grows the buffer, it will allow for an extra
* termination character.</item>
* <item>If GSUniTemporary is set, the function will return the results in
* an autoreleased buffer rather than in a buffer that the caller must
* release.</item>
* </list>
* <p>On return, the function result is a flag indicating success (YES) * <p>On return, the function result is a flag indicating success (YES)
* or failure (NO), and on success, the value stored in size is the number * or failure (NO), and on success, the value stored in size is the number
* of characters in the converted string. The converted string itsself is * of characters in the converted string. The converted string itsself is
@ -1752,16 +1783,17 @@ else \
* </p> * </p>
*/ */
BOOL BOOL
gsFromUnicode(unsigned char **dst, unsigned *size, const unichar *src, GSFromUnicode(unsigned char **dst, unsigned int *size, const unichar *src,
unsigned slen, NSStringEncoding enc, NSZone *zone, BOOL terminate, unsigned int slen, NSStringEncoding enc, NSZone *zone,
BOOL strict) unsigned int options)
{ {
unsigned char buf[BUFSIZ]; unsigned char buf[BUFSIZ];
unsigned char *ptr; unsigned char *ptr;
unsigned bsize; unsigned bsize;
unsigned dpos = 0; // Offset into destination buffer. unsigned dpos = 0; // Offset into destination buffer.
unsigned spos = 0; // Offset into source buffer. unsigned spos = 0; // Offset into source buffer.
unsigned extra = (terminate == YES) ? 1 : 0; unsigned extra = (options & GSUniTerminate) ? 1 : 0;
BOOL strict = (options & GSUniStrict) ? YES : NO;
unichar base = 0; unichar base = 0;
_ucc_ *table = 0; _ucc_ *table = 0;
unsigned tsize = 0; unsigned tsize = 0;
@ -1777,7 +1809,7 @@ gsFromUnicode(unsigned char **dst, unsigned *size, const unichar *src,
if (dst == 0 || *size == 0) if (dst == 0 || *size == 0)
{ {
ptr = buf; ptr = buf;
bsize = (terminate == YES) ? BUFSIZ - 1 : BUFSIZ; bsize = (extra != 0) ? BUFSIZ - 1 : BUFSIZ;
} }
else else
{ {
@ -2057,21 +2089,37 @@ tables:
/* /*
* Post conversion ... set output values. * Post conversion ... set output values.
*/ */
if (terminate == YES) if (extra != 0)
{ {
ptr[dpos] = (unsigned char)0; ptr[dpos] = (unsigned char)0;
} }
*size = dpos; *size = dpos;
if (dst != 0) if (dst != 0)
{ {
/* if (options & GSUniTemporary)
* If resizing is permitted, try ensure we return a buffer which {
* is just big enough to hold the converted string. unsigned bytes = dpos + extra;
*/ void *r;
if (zone != 0 && bsize > dpos)
/*
* Temporary string was requested ... make one.
*/
r = _fastMallocBuffer(bytes);
memcpy(r, ptr, bytes);
if (ptr != buf && ptr != *dst)
{
NSZoneFree(zone, ptr);
}
ptr = r;
}
else if (zone != 0 && bsize > dpos)
{ {
unsigned bytes = dpos + extra; unsigned bytes = dpos + extra;
/*
* Resizing is permitted - try ensure we return a buffer
* which is just big enough to hold the converted string.
*/
if (ptr == buf || ptr == *dst) if (ptr == buf || ptr == *dst)
{ {
unsigned char *tmp; unsigned char *tmp;