A lot of property list tidyups

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@19669 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
CaS 2004-07-02 10:37:54 +00:00
parent 0fe2082695
commit 32418a87f9
5 changed files with 165 additions and 2030 deletions

View file

@ -1,3 +1,14 @@
2004-07-02 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSHost.m: Use UTF8String rather than cString throughout
as cString is deprecated.
* Source/NSPropertyList.m: Output property list unicode escapes
as \U rather than \u for MacOS-X compatibility. Fix a few bugs
in old style plists.
* Source/GSCompatibility.m: Remove plist stuff which duplicated code
in NSPropertyList.m, so we only have one version to worry about.
* Source/NSString.m ditto.
2004-07-01 David Ayers <d.ayers@inode.at> 2004-07-01 David Ayers <d.ayers@inode.at>
* Headers/Foundation/NSMethodSignature.h * Headers/Foundation/NSMethodSignature.h

View file

@ -55,935 +55,3 @@ BOOL GSMacOSXCompatiblePropertyLists(void)
return GSUserDefaultsFlag(GSMacOSXCompatible); return GSUserDefaultsFlag(GSMacOSXCompatible);
} }
#include <math.h>
#include "Foundation/NSValue.h"
#include "Foundation/NSString.h"
static char base64[]
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static NSString*
encodeBase64(NSData *source)
{
int length = [source length];
int enclen = length / 3;
int remlen = length - 3 * enclen;
int destlen = 4 * ((length - 1) / 3) + 5;
unsigned char *sBuf;
unsigned char *dBuf;
int sIndex = 0;
int dIndex = 0;
if (length == 0)
{
return @"";
}
sBuf = (unsigned char*)[source bytes];
dBuf = NSZoneMalloc(NSDefaultMallocZone(), destlen);
dBuf[destlen - 1] = '\0';
for (sIndex = 0; sIndex < length - 2; sIndex += 3, dIndex += 4)
{
dBuf[dIndex] = base64[sBuf[sIndex] >> 2];
dBuf[dIndex + 1]
= base64[((sBuf[sIndex] << 4) | (sBuf[sIndex + 1] >> 4)) & 0x3f];
dBuf[dIndex + 2]
= base64[((sBuf[sIndex + 1] << 2) | (sBuf[sIndex + 2] >> 6)) & 0x3f];
dBuf[dIndex + 3] = base64[sBuf[sIndex + 2] & 0x3f];
}
if (remlen == 1)
{
dBuf[dIndex] = base64[sBuf[sIndex] >> 2];
dBuf[dIndex + 1] = (sBuf[sIndex] << 4) & 0x30;
dBuf[dIndex + 1] = base64[dBuf[dIndex + 1]];
dBuf[dIndex + 2] = '=';
dBuf[dIndex + 3] = '=';
}
else if (remlen == 2)
{
dBuf[dIndex] = base64[sBuf[sIndex] >> 2];
dBuf[dIndex + 1] = (sBuf[sIndex] << 4) & 0x30;
dBuf[dIndex + 1] |= sBuf[sIndex + 1] >> 4;
dBuf[dIndex + 1] = base64[dBuf[dIndex + 1]];
dBuf[dIndex + 2] = (sBuf[sIndex + 1] << 2) & 0x3c;
dBuf[dIndex + 2] = base64[dBuf[dIndex + 2]];
dBuf[dIndex + 3] = '=';
}
return [[NSString allocWithZone: NSDefaultMallocZone()]
initWithCStringNoCopy: dBuf length: destlen-1 freeWhenDone: YES];
}
static NSCharacterSet *xmlQuotables = nil;
static NSCharacterSet *plQuotables = nil;
static NSCharacterSet *oldPlQuotables = nil;
static unsigned const char *plQuotablesBitmapRep = NULL;
#define GS_IS_QUOTABLE(X) IS_BIT_SET(plQuotablesBitmapRep[(X)/8], (X) % 8)
static inline void Append(NSString *src, GSMutableString *dst)
{
[(NSMutableString*)dst appendString: src];
}
static void
PString(NSString *obj, GSMutableString *output)
{
unsigned length;
if ((length = [obj length]) == 0)
{
Append(@"\"\"", output);
return;
}
if ([obj rangeOfCharacterFromSet: oldPlQuotables].length > 0
|| [obj characterAtIndex: 0] == '/')
{
unichar tmp[length <= 1024 ? length : 0];
unichar *ustring;
unichar *from;
unichar *end;
int len = 0;
if (length <= 1024)
{
ustring = tmp;
}
else
{
ustring = NSZoneMalloc(NSDefaultMallocZone(), length*sizeof(unichar));
}
end = &ustring[length];
[obj getCharacters: ustring];
for (from = ustring; from < end; from++)
{
switch (*from)
{
case '\a':
case '\b':
case '\t':
case '\r':
case '\n':
case '\v':
case '\f':
case '\\':
case '\'' :
case '"' :
len += 2;
break;
default:
if (*from < 128)
{
if (isprint(*from) || *from == ' ')
{
len++;
}
else
{
len += 4;
}
}
else
{
len += 6;
}
break;
}
}
{
char buf[len+3];
char *ptr = buf;
*ptr++ = '"';
for (from = ustring; from < end; from++)
{
switch (*from)
{
case '\a': *ptr++ = '\\'; *ptr++ = 'a'; break;
case '\b': *ptr++ = '\\'; *ptr++ = 'b'; break;
case '\t': *ptr++ = '\\'; *ptr++ = 't'; break;
case '\r': *ptr++ = '\\'; *ptr++ = 'r'; break;
case '\n': *ptr++ = '\\'; *ptr++ = 'n'; break;
case '\v': *ptr++ = '\\'; *ptr++ = 'v'; break;
case '\f': *ptr++ = '\\'; *ptr++ = 'f'; break;
case '\\': *ptr++ = '\\'; *ptr++ = '\\'; break;
case '\'': *ptr++ = '\\'; *ptr++ = '\''; break;
case '"' : *ptr++ = '\\'; *ptr++ = '"'; break;
default:
if (*from < 128)
{
if (isprint(*from) || *from == ' ')
{
*ptr++ = *from;
}
else
{
unichar c = *from;
sprintf(ptr, "\\%03o", c);
ptr = &ptr[4];
}
}
else
{
sprintf(ptr, "\\u%04x", *from);
ptr = &ptr[6];
}
break;
}
}
*ptr++ = '"';
*ptr = '\0';
obj = [[NSString alloc] initWithCString: buf];
Append(obj, output);
RELEASE(obj);
}
if (length > 1024)
{
NSZoneFree(NSDefaultMallocZone(), ustring);
}
}
else
{
Append(obj, output);
}
}
static void
XString(NSString* obj, GSMutableString *output)
{
static char *hexdigits = "0123456789ABCDEF";
unsigned end;
end = [obj length];
if (end == 0)
{
return;
}
if ([obj rangeOfCharacterFromSet: xmlQuotables].length > 0)
{
unichar *base;
unichar *map;
unichar c;
unsigned len;
unsigned rpos;
unsigned wpos;
base = NSZoneMalloc(NSDefaultMallocZone(), end * sizeof(unichar));
[obj getCharacters: base];
for (len = rpos = 0; rpos < end; rpos++)
{
c = base[rpos];
switch (c)
{
case '&':
len += 5;
break;
case '<':
case '>':
len += 4;
break;
case '\'':
case '"':
len += 6;
break;
default:
if ((c < 0x20 && (c != 0x09 && c != 0x0A && c != 0x0D))
|| (c > 0xD7FF && c < 0xE000) || c > 0xFFFD)
{
len += 6;
}
else
{
len++;
}
break;
}
}
map = NSZoneMalloc(NSDefaultMallocZone(), len * sizeof(unichar));
for (wpos = rpos = 0; rpos < end; rpos++)
{
c = base[rpos];
switch (c)
{
case '&':
map[wpos++] = '&';
map[wpos++] = 'a';
map[wpos++] = 'm';
map[wpos++] = 'p';
map[wpos++] = ';';
break;
case '<':
map[wpos++] = '&';
map[wpos++] = 'l';
map[wpos++] = 't';
map[wpos++] = ';';
break;
case '>':
map[wpos++] = '&';
map[wpos++] = 'g';
map[wpos++] = 't';
map[wpos++] = ';';
break;
case '\'':
map[wpos++] = '&';
map[wpos++] = 'a';
map[wpos++] = 'p';
map[wpos++] = 'o';
map[wpos++] = 's';
map[wpos++] = ';';
break;
case '"':
map[wpos++] = '&';
map[wpos++] = 'q';
map[wpos++] = 'u';
map[wpos++] = 'o';
map[wpos++] = 't';
map[wpos++] = ';';
break;
default:
if ((c < 0x20 && (c != 0x09 && c != 0x0A && c != 0x0D))
|| (c > 0xD7FF && c < 0xE000) || c > 0xFFFD)
{
map[wpos++] = '\\';
map[wpos++] = 'U';
map[wpos++] = hexdigits[(c>>12) & 0xf];
map[wpos++] = hexdigits[(c>>8) & 0xf];
map[wpos++] = hexdigits[(c>>4) & 0xf];
map[wpos++] = hexdigits[c & 0xf];
}
else
{
map[wpos++] = c;
}
break;
}
}
NSZoneFree(NSDefaultMallocZone(), base);
obj = [[NSString alloc] initWithCharacters: map length: len];
Append(obj, output);
RELEASE(obj);
}
else
{
Append(obj, output);
}
}
static NSString *indentStrings[] = {
@"",
@" ",
@" ",
@" ",
@"\t",
@"\t ",
@"\t ",
@"\t ",
@"\t\t",
@"\t\t ",
@"\t\t ",
@"\t\t ",
@"\t\t\t",
@"\t\t\t ",
@"\t\t\t ",
@"\t\t\t ",
@"\t\t\t\t",
@"\t\t\t\t ",
@"\t\t\t\t ",
@"\t\t\t\t ",
@"\t\t\t\t\t",
@"\t\t\t\t\t ",
@"\t\t\t\t\t ",
@"\t\t\t\t\t ",
@"\t\t\t\t\t\t"
};
#define PLNEW 0 // New extended OpenStep property list
#define PLXML 1 // New MacOS-X XML property list
#define PLOLD 2 // Old (standard) OpenStep property list
#define PLDSC 3 // Just a description
/**
* obj is the object to be written out<br />
* loc is the locale for formatting (or nil to indicate no formatting)<br />
* lev is the level of indentation to use<br />
* step is the indentation step (0 == 0, 1 = 2, 2 = 4, 3 = 8)<br />
* x is an indicator for xml or old/new openstep property list format<br />
* dest is the output buffer.
*/
static void
OAppend(id obj, NSDictionary *loc, unsigned lev, unsigned step,
int x, GSMutableString *dest)
{
if ([obj isKindOfClass: [NSString class]])
{
if (x == PLXML)
{
Append(@"<string>", dest);
XString(obj, dest);
Append(@"</string>\n", dest);
}
else
{
PString(obj, dest);
}
}
else if ([obj isKindOfClass: [NSNumber class]])
{
const char *t = [obj objCType];
if (*t == 'c' || *t == 'C')
{
BOOL val = [obj boolValue];
if (val == YES)
{
if (x == PLXML)
{
Append(@"<true/>\n", dest);
}
else if (x == PLNEW)
{
Append(@"<*BY>", dest);
}
else
{
PString([obj description], dest);
}
}
else
{
if (x == PLXML)
{
Append(@"<false/>\n", dest);
}
else if (x == PLNEW)
{
Append(@"<*BN>", dest);
}
else
{
PString([obj description], dest);
}
}
}
else if (strchr("sSiIlLqQ", *t) != 0)
{
if (x == PLXML)
{
Append(@"<integer>", dest);
XString([obj stringValue], dest);
Append(@"</integer>\n", dest);
}
else if (x == PLNEW)
{
Append(@"<*I", dest);
PString([obj stringValue], dest);
Append(@">", dest);
}
else
{
PString([obj description], dest);
}
}
else
{
if (x == PLXML)
{
Append(@"<real>", dest);
XString([obj stringValue], dest);
Append(@"</real>\n", dest);
}
else if (x == PLNEW)
{
Append(@"<*R", dest);
PString([obj stringValue], dest);
Append(@">", dest);
}
else
{
PString([obj description], dest);
}
}
}
else if ([obj isKindOfClass: [NSData class]])
{
if (x == PLXML)
{
Append(@"<data>", dest);
Append(encodeBase64(obj), dest);
Append(@"</data>\n", dest);
}
else
{
NSString *str;
const char *src;
char *dst;
int length;
int i;
int j;
NSZone *z = NSDefaultMallocZone();
src = [obj bytes];
length = [obj length];
#define num2char(num) ((num) < 0xa ? ((num)+'0') : ((num)+0x57))
dst = (char*) NSZoneMalloc(z, 2*length+length/4+3);
dst[0] = '<';
for (i = 0, j = 1; i < length; i++, j++)
{
dst[j++] = num2char((src[i]>>4) & 0x0f);
dst[j] = num2char(src[i] & 0x0f);
if ((i&0x3) == 3 && i != length-1)
{
/* if we've just finished a 32-bit int, print a space */
dst[++j] = ' ';
}
}
dst[j++] = '>';
dst[j] = '\0';
str = [[NSString allocWithZone: z] initWithCStringNoCopy: dst
length: j
freeWhenDone: YES];
Append(str, dest);
RELEASE(str);
}
}
else if ([obj isKindOfClass: [NSDate class]])
{
if (x == PLXML)
{
Append(@"<date>", dest);
Append([obj descriptionWithCalendarFormat: @"%Y-%m-%d %H:%M:%S %z"
timeZone: nil locale: nil], dest);
Append(@"</date>\n", dest);
}
else if (x == PLNEW)
{
Append(@"<*D", dest);
Append([obj descriptionWithCalendarFormat: @"%Y-%m-%d %H:%M:%S %z"
timeZone: nil locale: nil], dest);
Append(@">", dest);
}
else
{
PString([obj description], dest);
}
}
else if ([obj isKindOfClass: [NSArray class]])
{
NSString *iBaseString;
NSString *iSizeString;
unsigned level = lev;
if (level*step < sizeof(indentStrings)/sizeof(id))
{
iBaseString = indentStrings[level*step];
}
else
{
iBaseString
= indentStrings[sizeof(indentStrings)/sizeof(id)-1];
}
level++;
if (level*step < sizeof(indentStrings)/sizeof(id))
{
iSizeString = indentStrings[level*step];
}
else
{
iSizeString
= indentStrings[sizeof(indentStrings)/sizeof(id)-1];
}
if (x == PLXML)
{
NSEnumerator *e;
Append(@"<array>\n", dest);
e = [obj objectEnumerator];
while ((obj = [e nextObject]))
{
Append(iSizeString, dest);
OAppend(obj, loc, level, step, x, dest);
}
Append(iBaseString, dest);
Append(@"</array>\n", dest);
}
else
{
unsigned count = [obj count];
unsigned last = count - 1;
NSString *plists[count];
unsigned i;
if ([obj isProxy] == YES)
{
for (i = 0; i < count; i++)
{
plists[i] = [obj objectAtIndex: i];
}
}
else
{
[obj getObjects: plists];
}
if (loc == nil)
{
Append(@"(", dest);
for (i = 0; i < count; i++)
{
id item = plists[i];
OAppend(item, nil, 0, step, x, dest);
if (i != last)
{
Append(@", ", dest);
}
}
Append(@")", dest);
}
else
{
Append(@"(\n", dest);
for (i = 0; i < count; i++)
{
id item = plists[i];
Append(iSizeString, dest);
OAppend(item, loc, level, step, x, dest);
if (i == last)
{
Append(@"\n", dest);
}
else
{
Append(@",\n", dest);
}
}
Append(iBaseString, dest);
Append(@")", dest);
}
}
}
else if ([obj isKindOfClass: [NSDictionary class]])
{
NSString *iBaseString;
NSString *iSizeString;
unsigned level = lev;
if (level*step < sizeof(indentStrings)/sizeof(id))
{
iBaseString = indentStrings[level*step];
}
else
{
iBaseString
= indentStrings[sizeof(indentStrings)/sizeof(id)-1];
}
level++;
if (level*step < sizeof(indentStrings)/sizeof(id))
{
iSizeString = indentStrings[level*step];
}
else
{
iSizeString
= indentStrings[sizeof(indentStrings)/sizeof(id)-1];
}
if (x == PLXML)
{
NSEnumerator *e;
id key;
Append(@"<dict>\n", dest);
e = [obj keyEnumerator];
while ((key = [e nextObject]))
{
id val;
val = [obj objectForKey: key];
Append(iSizeString, dest);
Append(@"<key>", dest);
XString(key, dest);
Append(@"</key>\n", dest);
Append(iSizeString, dest);
OAppend(val, loc, level, step, x, dest);
}
Append(iBaseString, dest);
Append(@"</dict>\n", dest);
}
else
{
unsigned i;
NSArray *keyArray = [obj allKeys];
unsigned numKeys = [keyArray count];
NSString *plists[numKeys];
NSString *keys[numKeys];
BOOL canCompare = YES;
Class lastClass = 0;
BOOL isProxy = [obj isProxy];
if (isProxy == YES)
{
for (i = 0; i < numKeys; i++)
{
keys[i] = [keyArray objectAtIndex: i];
}
}
else
{
[keyArray getObjects: keys];
}
for (i = 0; i < numKeys; i++)
{
if (GSObjCClass(keys[i]) == lastClass)
continue;
if ([keys[i] respondsToSelector: @selector(compare:)] == NO)
{
canCompare = NO;
break;
}
lastClass = GSObjCClass(keys[i]);
}
if (canCompare == YES)
{
#define STRIDE_FACTOR 3
unsigned c,d, stride;
BOOL found;
NSComparisonResult (*comp)(id, SEL, id) = 0;
unsigned int count = numKeys;
#ifdef GSWARN
BOOL badComparison = NO;
#endif
stride = 1;
while (stride <= count)
{
stride = stride * STRIDE_FACTOR + 1;
}
lastClass = 0;
while (stride > (STRIDE_FACTOR - 1))
{
// loop to sort for each value of stride
stride = stride / STRIDE_FACTOR;
for (c = stride; c < count; c++)
{
found = NO;
if (stride > c)
{
break;
}
d = c - stride;
while (!found)
{
id a = keys[d + stride];
id b = keys[d];
Class x;
NSComparisonResult r;
x = GSObjCClass(a);
if (x != lastClass)
{
lastClass = x;
comp = (NSComparisonResult (*)(id, SEL, id))
[a methodForSelector: @selector(compare:)];
}
r = (*comp)(a, @selector(compare:), b);
if (r < 0)
{
#ifdef GSWARN
if (r != NSOrderedAscending)
{
badComparison = YES;
}
#endif
keys[d + stride] = b;
keys[d] = a;
if (stride > d)
{
break;
}
d -= stride;
}
else
{
#ifdef GSWARN
if (r != NSOrderedDescending
&& r != NSOrderedSame)
{
badComparison = YES;
}
#endif
found = YES;
}
}
}
}
#ifdef GSWARN
if (badComparison == YES)
{
NSWarnFLog(@"Detected bad return value from comparison");
}
#endif
}
if (isProxy == YES)
{
for (i = 0; i < numKeys; i++)
{
plists[i] = [obj objectForKey: keys[i]];
}
}
else
{
SEL objSel = @selector(objectForKey:);
IMP myObj = [obj methodForSelector: objSel];
for (i = 0; i < numKeys; i++)
{
plists[i] = (*myObj)(obj, objSel, keys[i]);
}
}
if (loc == nil)
{
Append(@"{", dest);
for (i = 0; i < numKeys; i++)
{
OAppend(keys[i], nil, 0, step, x, dest);
Append(@" = ", dest);
OAppend(plists[i], nil, 0, step, x, dest);
Append(@"; ", dest);
}
Append(@"}", dest);
}
else
{
Append(@"{\n", dest);
for (i = 0; i < numKeys; i++)
{
Append(iSizeString, dest);
OAppend(keys[i], loc, level, step, x, dest);
Append(@" = ", dest);
OAppend(plists[i], loc, level, step, x, dest);
Append(@";\n", dest);
}
Append(iBaseString, dest);
Append(@"}", dest);
}
}
}
else
{
if (x == PLDSC)
{
Append([obj description], dest);
}
else if (x == PLXML)
{
NSDebugLog(@"Non-property-list class (%@) encoded as string",
NSStringFromClass([obj class]));
Append(@"<string>", dest);
XString([obj description], dest);
Append(@"</string>\n", dest);
}
else
{
NSDebugLog(@"Non-property-list class (%@) encoded as string",
NSStringFromClass([obj class]));
Append([obj description], dest);
}
}
}
void
GSPropertyListMake(id obj, NSDictionary *loc, BOOL xml,
BOOL forDescription, unsigned step, id *str)
{
GSMutableString *dest;
int style = PLNEW;
if (plQuotablesBitmapRep == NULL)
{
NSMutableCharacterSet *s;
NSData *bitmap;
s = [[NSCharacterSet characterSetWithCharactersInString:
@"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
@"abcdefghijklmnopqrstuvwxyz!#$%&*+-./:?@|~_^"]
mutableCopy];
[s invert];
plQuotables = [s copy];
RELEASE(s);
bitmap = RETAIN([plQuotables bitmapRepresentation]);
plQuotablesBitmapRep = [bitmap bytes];
s = [[NSCharacterSet characterSetWithCharactersInString:
@"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
@"abcdefghijklmnopqrstuvwxyz$./_"]
mutableCopy];
[s invert];
oldPlQuotables = [s copy];
RELEASE(s);
s = [[NSCharacterSet characterSetWithCharactersInString:
@"&<>'\\\""] mutableCopy];
[s addCharactersInRange: NSMakeRange(0x0001, 0x001f)];
[s removeCharactersInRange: NSMakeRange(0x0009, 0x0002)];
[s removeCharactersInRange: NSMakeRange(0x000D, 0x0001)];
[s addCharactersInRange: NSMakeRange(0xD800, 0x07FF)];
[s addCharactersInRange: NSMakeRange(0xFFFE, 0x0002)];
xmlQuotables = [s copy];
RELEASE(s);
}
if (*str == nil)
{
*str = AUTORELEASE([GSMutableString new]);
}
else if (GSObjCClass(*str) != [GSMutableString class])
{
[NSException raise: NSInvalidArgumentException
format: @"Illegal object (%@) at argument 0", *str];
}
dest = *str;
if (forDescription)
{
style = PLDSC;
}
else if (xml == YES)
{
Append([NSMutableString stringWithCString:
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist "
"PUBLIC \"-//GNUstep//DTD plist 0.9//EN\" "
"\"http://www.gnustep.org/plist-0_9.xml\">\n"
"<plist version=\"0.9\">\n"], dest);
style = PLXML;
}
else if (GSUserDefaultsFlag(NSWriteOldStylePropertyLists))
{
style = PLOLD;
}
else
{
style = PLNEW;
}
OAppend(obj, loc, 0, step > 3 ? 3 : step, style, dest);
if (style == PLXML)
{
Append(@"</plist>", dest);
}
}

View file

@ -91,13 +91,13 @@ static NSMutableDictionary *_hostCache = nil;
struct in_addr hostaddr; struct in_addr hostaddr;
#ifndef HAVE_INET_ATON #ifndef HAVE_INET_ATON
hostaddr.s_addr = inet_addr([address cString]); hostaddr.s_addr = inet_addr([address UTF8String]);
if (hostaddr.s_addr == INADDR_NONE) if (hostaddr.s_addr == INADDR_NONE)
{ {
NSLog(@"Attempt to lookup host entry for bad IP address (%@)", address); NSLog(@"Attempt to lookup host entry for bad IP address (%@)", address);
} }
#else #else
if (inet_aton([address cString], (struct in_addr*)&hostaddr.s_addr) == 0) if (inet_aton([address UTF8String], (struct in_addr*)&hostaddr.s_addr) == 0)
{ {
NSLog(@"Attempt to lookup host entry for bad IP address (%@)", address); NSLog(@"Attempt to lookup host entry for bad IP address (%@)", address);
} }
@ -304,6 +304,7 @@ myHostName()
+ (NSHost*) hostWithName: (NSString*)name + (NSHost*) hostWithName: (NSString*)name
{ {
BOOL tryByAddress = NO;
NSHost *host = nil; NSHost *host = nil;
if (name == nil) if (name == nil)
@ -336,9 +337,14 @@ myHostName()
else else
{ {
struct hostent *h = 0; struct hostent *h = 0;
const char *n = [name UTF8String];
h = gethostbyname((char*)[name cString]); h = gethostbyname((char*)n);
if (h == 0) if (h == 0 && sscanf(n, "%*d.%*d.%*d.%*d") == 4)
{
tryByAddress = YES;
}
else if (h == 0)
{ {
if ([name isEqualToString: myHostName()] == YES) if ([name isEqualToString: myHostName()] == YES)
{ {
@ -365,6 +371,10 @@ myHostName()
} }
} }
[_hostCacheLock unlock]; [_hostCacheLock unlock];
if (tryByAddress == YES)
{
return [self hostWithAddress: name];
}
return host; return host;
} }
@ -400,13 +410,13 @@ myHostName()
BOOL badAddr = NO; BOOL badAddr = NO;
#ifndef HAVE_INET_ATON #ifndef HAVE_INET_ATON
hostaddr.s_addr = inet_addr([address cString]); hostaddr.s_addr = inet_addr([address UTF8String]);
if (hostaddr.s_addr == INADDR_NONE) if (hostaddr.s_addr == INADDR_NONE)
{ {
badAddr = YES; badAddr = YES;
} }
#else #else
if (inet_aton([address cString], if (inet_aton([address UTF8String],
(struct in_addr*)&hostaddr.s_addr) == 0) (struct in_addr*)&hostaddr.s_addr) == 0)
{ {
badAddr = YES; badAddr = YES;

View file

@ -42,6 +42,7 @@
#include "Foundation/NSValue.h" #include "Foundation/NSValue.h"
#include "Foundation/NSDebug.h" #include "Foundation/NSDebug.h"
#include "GSPrivate.h"
extern BOOL GSScanDouble(unichar*, unsigned, double*); extern BOOL GSScanDouble(unichar*, unsigned, double*);
@class GSString; @class GSString;
@ -168,9 +169,6 @@ static void setupWhitespace(void)
} }
} }
static id GSPropertyListFromStringsFormat(NSString *string);
#ifdef HAVE_LIBXML #ifdef HAVE_LIBXML
#include "GNUstepBase/GSXML.h" #include "GNUstepBase/GSXML.h"
static int XML_ELEMENT_NODE; static int XML_ELEMENT_NODE;
@ -733,7 +731,7 @@ static id parsePlItem(pldata* pld)
} }
if (pld->ptr[pld->pos] != '>') if (pld->ptr[pld->pos] != '>')
{ {
pld->err = @"unexpected character in string"; pld->err = @"unexpected character (wanted '>')";
return nil; return nil;
} }
pld->pos++; pld->pos++;
@ -773,7 +771,7 @@ static id parsePlItem(pldata* pld)
} }
if (pld->ptr[pld->pos] != '>') if (pld->ptr[pld->pos] != '>')
{ {
pld->err = @"unexpected character in string"; pld->err = @"unexpected character (wanted '>')";
RELEASE(data); RELEASE(data);
return nil; return nil;
} }
@ -1009,7 +1007,7 @@ nodeToObject(GSXMLNode* node, NSPropertyListMutabilityOptions o, NSString **e)
} }
#endif #endif
static id id
GSPropertyListFromStringsFormat(NSString *string) GSPropertyListFromStringsFormat(NSString *string)
{ {
NSMutableDictionary *dict; NSMutableDictionary *dict;
@ -1026,12 +1024,17 @@ GSPropertyListFromStringsFormat(NSString *string)
return nil; return nil;
} }
d = [string dataUsingEncoding: NSUnicodeStringEncoding]; d = [string dataUsingEncoding: NSASCIIStringEncoding];
if (d == nil)
{
[NSException raise: NSGenericException
format: @"Non-ascii data in string supposed to be property list"];
}
_pld.ptr = (unsigned char*)[d bytes]; _pld.ptr = (unsigned char*)[d bytes];
_pld.pos = 1; _pld.pos = 0;
_pld.end = length + 1; _pld.end = length;
_pld.err = nil; _pld.err = nil;
_pld.lin = 1; _pld.lin = 0;
_pld.opt = NSPropertyListImmutable; _pld.opt = NSPropertyListImmutable;
_pld.key = NO; _pld.key = NO;
_pld.old = YES; // OpenStep style _pld.old = YES; // OpenStep style
@ -1128,7 +1131,7 @@ GSPropertyListFromStringsFormat(NSString *string)
RELEASE(dict); RELEASE(dict);
[NSException raise: NSGenericException [NSException raise: NSGenericException
format: @"Parse failed at line %d (char %d) - %@", format: @"Parse failed at line %d (char %d) - %@",
_pld.lin, _pld.pos, _pld.err]; _pld.lin + 1, _pld.pos + 1, _pld.err];
} }
return AUTORELEASE(dict); return AUTORELEASE(dict);
} }
@ -1217,6 +1220,7 @@ PString(NSString *obj, NSMutableData *output)
unichar *from; unichar *from;
unichar *end; unichar *end;
unsigned char *ptr; unsigned char *ptr;
int base = [output length];
int len = 0; int len = 0;
if (length <= 1024) if (length <= 1024)
@ -1266,8 +1270,8 @@ PString(NSString *obj, NSMutableData *output)
} }
} }
[output setLength: [output length] + len + 2]; [output setLength: base + len + 2];
ptr = [output mutableBytes] + [output length]; ptr = [output mutableBytes] + base;
*ptr++ = '"'; *ptr++ = '"';
for (from = ustring; from < end; from++) for (from = ustring; from < end; from++)
{ {
@ -1295,14 +1299,29 @@ PString(NSString *obj, NSMutableData *output)
{ {
unichar c = *from; unichar c = *from;
sprintf(ptr, "\\%03o", c); *ptr++ = '\\';
ptr = &ptr[4]; ptr[2] = (c & 7) + '0';
c >>= 3;
ptr[1] = (c & 7) + '0';
c >>= 3;
ptr[0] = (c & 7) + '0';
ptr += 3;
} }
} }
else else
{ {
sprintf(ptr, "\\u%04x", *from); unichar c = *from;
ptr = &ptr[6];
*ptr++ = '\\';
*ptr++ = 'U';
ptr[3] = (c & 0xf) > 9 ? (c & 0xf) + 'A' : (c & 0xf) + '0';
c >>= 4;
ptr[2] = (c & 0xf) > 9 ? (c & 0xf) + 'A' : (c & 0xf) + '0';
c >>= 4;
ptr[1] = (c & 0xf) > 9 ? (c & 0xf) + 'A' : (c & 0xf) + '0';
c >>= 4;
ptr[0] = (c & 0xf) > 9 ? (c & 0xf) + 'A' : (c & 0xf) + '0';
ptr += 4;
} }
break; break;
} }
@ -1479,10 +1498,6 @@ static const char *indentStrings[] = {
"\t\t\t\t\t\t" "\t\t\t\t\t\t"
}; };
#define PLNEW 0 // New extended OpenStep property list
#define PLXML 1 // New MacOS-X XML property list
#define PLOLD 2 // Old (standard) OpenStep property list
#define PLDSC 3 // Just a description
/** /**
* obj is the object to be written out<br /> * obj is the object to be written out<br />
* loc is the locale for formatting (or nil to indicate no formatting)<br /> * loc is the locale for formatting (or nil to indicate no formatting)<br />
@ -1607,14 +1622,14 @@ OAppend(id obj, NSDictionary *loc, unsigned lev, unsigned step,
#define num2char(num) ((num) < 0xa ? ((num)+'0') : ((num)+0x57)) #define num2char(num) ((num) < 0xa ? ((num)+'0') : ((num)+0x57))
j = [dest length]; j = [dest length];
[dest setLength: j + 2*length+length/4+3]; [dest setLength: j + 2*length+(length > 4 ? (length-1)/4+2 : 2)];
dst = [dest mutableBytes]; dst = [dest mutableBytes];
dst[j++] = '<'; dst[j++] = '<';
for (i = 0; i < length; i++, j++) for (i = 0; i < length; i++, j++)
{ {
dst[j++] = num2char((src[i]>>4) & 0x0f); dst[j++] = num2char((src[i]>>4) & 0x0f);
dst[j] = num2char(src[i] & 0x0f); dst[j] = num2char(src[i] & 0x0f);
if ((i&0x3) == 3 && i != length-1) if ((i & 3) == 3 && i < length-1)
{ {
/* if we've just finished a 32-bit int, print a space */ /* if we've just finished a 32-bit int, print a space */
dst[++j] = ' '; dst[++j] = ' ';
@ -1970,13 +1985,13 @@ OAppend(id obj, NSDictionary *loc, unsigned lev, unsigned step,
@implementation NSPropertyListSerialization @implementation NSPropertyListSerialization
static BOOL classInitialized = NO;
+ (void) initialize + (void) initialize
{ {
static BOOL beenHere = NO; if (classInitialized == NO)
if (beenHere == NO)
{ {
beenHere = YES; classInitialized = YES;
#ifdef HAVE_LIBXML #ifdef HAVE_LIBXML
/* /*
@ -2040,6 +2055,69 @@ OAppend(id obj, NSDictionary *loc, unsigned lev, unsigned step,
return dest; return dest;
} }
void
GSPropertyListMake(id obj, NSDictionary *loc, BOOL xml,
BOOL forDescription, unsigned step, id *str)
{
NSString *tmp;
NSPropertyListFormat style;
NSMutableData *dest;
if (classInitialized == NO)
{
[NSPropertyListSerialization class];
}
if (*str == nil)
{
*str = AUTORELEASE([GSMutableString new]);
}
else if (GSObjCClass(*str) != [GSMutableString class])
{
[NSException raise: NSInvalidArgumentException
format: @"Illegal object (%@) at argument 0", *str];
}
if (forDescription)
{
style = NSPropertyListOpenStepFormat;
}
else if (xml == YES)
{
style = NSPropertyListXMLFormat_v1_0;
}
else if (GSUserDefaultsFlag(NSWriteOldStylePropertyLists))
{
style = NSPropertyListOpenStepFormat;
}
else
{
style = NSPropertyListGNUstepFormat;
}
dest = [NSMutableData dataWithCapacity: 1024];
if (style == NSPropertyListXMLFormat_v1_0)
{
const char *prefix =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist "
"PUBLIC \"-//GNUstep//DTD plist 0.9//EN\" "
"\"http://www.gnustep.org/plist-0_9.xml\">\n"
"<plist version=\"0.9\">\n";
[dest appendBytes: prefix length: strlen(prefix)];
OAppend(obj, loc, 0, step > 3 ? 3 : step, style, dest);
[dest appendBytes: "</plist>" length: 8];
}
else
{
OAppend(obj, loc, 0, step > 3 ? 3 : step, style, dest);
}
tmp = [[NSString alloc] initWithData: dest encoding: NSASCIIStringEncoding];
[*str appendString: tmp];
RELEASE(tmp);
}
+ (BOOL) propertyList: (id)aPropertyList + (BOOL) propertyList: (id)aPropertyList
isValidForFormat: (NSPropertyListFormat)aFormat isValidForFormat: (NSPropertyListFormat)aFormat
{ {
@ -2165,10 +2243,10 @@ OAppend(id obj, NSDictionary *loc, unsigned lev, unsigned step,
pldata _pld; pldata _pld;
_pld.ptr = bytes; _pld.ptr = bytes;
_pld.pos = 1; _pld.pos = 0;
_pld.end = length + 1; _pld.end = length;
_pld.err = nil; _pld.err = nil;
_pld.lin = 1; _pld.lin = 0;
_pld.opt = anOption; _pld.opt = anOption;
_pld.key = NO; _pld.key = NO;
_pld.old = YES; // OpenStep style _pld.old = YES; // OpenStep style
@ -2183,7 +2261,7 @@ OAppend(id obj, NSDictionary *loc, unsigned lev, unsigned step,
{ {
error = [NSString stringWithFormat: error = [NSString stringWithFormat:
@"Parse failed at line %d (char %d) - %@", @"Parse failed at line %d (char %d) - %@",
_pld.lin, _pld.pos, _pld.err]; _pld.lin + 1, _pld.pos + 1, _pld.err];
} }
} }
break; break;

File diff suppressed because it is too large Load diff