Added more property list support.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@14967 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
CaS 2002-11-10 10:20:05 +00:00
parent b8b64cb3e5
commit 963c232265
4 changed files with 158 additions and 51 deletions

View file

@ -6,7 +6,8 @@
* Source/NSArray.m: Use new plist code * Source/NSArray.m: Use new plist code
* Source/NSData.m: ditto * Source/NSData.m: ditto
* Source/NSDictionary.m: ditto * Source/NSDictionary.m: ditto
* Source/NSString.m: ditto * Source/NSString.m: ditto, plus implement extensions to old plist
support so we can encode NSNumber and NSDate values.
Remove GNUstep property list extensions from the api ... make more Remove GNUstep property list extensions from the api ... make more
like MacOS and OpenStep spec by having a central mechanism for like MacOS and OpenStep spec by having a central mechanism for
generating property lists rather than spreading the code across the generating property lists rather than spreading the code across the

View file

@ -482,7 +482,7 @@ OAppend(id obj, NSDictionary *loc, unsigned lev, unsigned step,
} }
else else
{ {
Append(@"YES", dest); Append(@"<*BY>", dest);
} }
} }
else if (val == 0.0) else if (val == 0.0)
@ -493,7 +493,7 @@ OAppend(id obj, NSDictionary *loc, unsigned lev, unsigned step,
} }
else else
{ {
Append(@"NO", dest); Append(@"<*BN>", dest);
} }
} }
else if (rint(val) == val) else if (rint(val) == val)
@ -506,7 +506,9 @@ OAppend(id obj, NSDictionary *loc, unsigned lev, unsigned step,
} }
else else
{ {
Append(@"<*I", dest);
PString([obj stringValue], dest); PString([obj stringValue], dest);
Append(@">", dest);
} }
} }
else else
@ -519,7 +521,9 @@ OAppend(id obj, NSDictionary *loc, unsigned lev, unsigned step,
} }
else else
{ {
Append(@"<*R", dest);
PString([obj stringValue], dest); PString([obj stringValue], dest);
Append(@">", dest);
} }
} }
} }
@ -571,14 +575,16 @@ OAppend(id obj, NSDictionary *loc, unsigned lev, unsigned step,
if (x == YES) if (x == YES)
{ {
Append(@"<date>", dest); Append(@"<date>", dest);
Append([obj descriptionWithCalendarFormat: @"%Y-%m-%d %H:%M:%S %z"], Append([obj descriptionWithCalendarFormat: @"%Y-%m-%d %H:%M:%S %z"
dest); timeZone: nil locale: nil], dest);
Append(@"</date>\n", dest); Append(@"</date>\n", dest);
} }
else else
{ {
PString([obj descriptionWithCalendarFormat: @"%Y-%m-%d %H:%M:%S %z"], Append(@"<*D", dest);
dest); Append([obj descriptionWithCalendarFormat: @"%Y-%m-%d %H:%M:%S %z"
timeZone: nil locale: nil], dest);
Append(@">", dest);
} }
} }
else if ([obj isKindOfClass: [NSArray class]]) else if ([obj isKindOfClass: [NSArray class]])

View file

@ -4770,6 +4770,93 @@ static id parsePlItem(pldata* pld)
break; break;
case '<': case '<':
pld->pos++;
if (pld->pos < pld->end && pld->ptr[pld->pos] == '*')
{
const unichar *ptr;
unsigned min;
unsigned len = 0;
int i;
pld->pos++;
min = pld->pos;
ptr = &(pld->ptr[min]);
while (pld->pos < pld->end && pld->ptr[pld->pos] != '>')
{
pld->pos++;
}
len = pld->pos - min;
if (len > 1)
{
unichar type = *ptr++;
len--;
if (type == 'I')
{
char buf[len+1];
for (i = 0; i < len; i++) buf[i] = (char)ptr[i];
buf[len] = '\0';
result = [[NSNumber alloc] initWithLong: atol(buf)];
}
else if (type == 'B')
{
if (ptr[0] == 'Y')
{
result = [[NSNumber alloc] initWithBool: YES];
}
else if (ptr[0] == 'N')
{
result = [[NSNumber alloc] initWithBool: NO];
}
else
{
pld->err = @"bad value for bool";
return nil;
}
}
else if (type == 'D')
{
NSString *str;
str = [[NSString alloc] initWithCharacters: ptr
length: len];
result = [[NSCalendarDate alloc] initWithString: str
calendarFormat: @"%Y-%m-%d %H:%M:%S %z"];
RELEASE(str);
}
else if (type == 'R')
{
char buf[len+1];
for (i = 0; i < len; i++) buf[i] = (char)ptr[i];
buf[len] = '\0';
result = [[NSNumber alloc] initWithDouble: atof(buf)];
}
else
{
pld->err = @"unrecognized type code after '<*'";
return nil;
}
}
else
{
pld->err = @"missing type code after '<*'";
return nil;
}
if (pld->pos >= pld->end)
{
pld->err = @"unexpected end of string when parsing data";
return nil;
}
if (pld->ptr[pld->pos] != '>')
{
pld->err = @"unexpected character in string";
return nil;
}
pld->pos++;
}
else
{ {
NSMutableData *data; NSMutableData *data;
unsigned max = pld->end - 1; unsigned max = pld->end - 1;
@ -4777,7 +4864,6 @@ static id parsePlItem(pldata* pld)
unsigned len = 0; unsigned len = 0;
data = [[NSMutableData alloc] initWithCapacity: 0]; data = [[NSMutableData alloc] initWithCapacity: 0];
pld->pos++;
skipSpace(pld); skipSpace(pld);
while (pld->pos < max while (pld->pos < max
&& GS_IS_HEXDIGIT(pld->ptr[pld->pos]) && GS_IS_HEXDIGIT(pld->ptr[pld->pos])

View file

@ -33,8 +33,22 @@ int main ()
{ {
id pool = [NSAutoreleasePool new]; id pool = [NSAutoreleasePool new];
id o = [NSObject new]; id o = [NSObject new];
id x;
NSString *s;
NSArray *a = [NSArray arrayWithObjects: @"a", @"b", nil]; NSArray *a = [NSArray arrayWithObjects: @"a", @"b", nil];
o = [NSDictionary dictionaryWithObjectsAndKeys:
@"test", @"one",
[NSNumber numberWithBool: YES], @"two",
[NSDate date], @"three",
[NSNumber numberWithInt: 33], @"four",
[NSNumber numberWithFloat: 4.5], @"five",
nil];
s = [o description];
NSLog(@"%@", s);
x = [s propertyList];
NSLog(@"%d", [o isEqual: x]);
test1(); test1();
test2(); test2();