From 2a6015020fb92f1f42413de5e84f08236f9711e0 Mon Sep 17 00:00:00 2001 From: mccallum Date: Sat, 15 Apr 1995 19:39:49 +0000 Subject: [PATCH] Initial revision git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@361 72102866-910b-0410-8b05-ffd578937521 --- Headers/gnustep/base/NSGArchiver.h | 56 +++++++++++ Headers/gnustep/base/NSGData.h | 68 +++++++++++++ Source/NSGArchiver.m | 42 ++++++++ Source/NSGData.m | 136 +++++++++++++++++++++++++ Source/NSGUnarchiver.m | 42 ++++++++ Source/NSMutableData.m | 155 +++++++++++++++++++++++++++++ Source/NSUnarchiver.m | 108 ++++++++++++++++++++ Testing/nsarchiving.m | 18 ++++ 8 files changed, 625 insertions(+) create mode 100644 Headers/gnustep/base/NSGArchiver.h create mode 100644 Headers/gnustep/base/NSGData.h create mode 100644 Source/NSGArchiver.m create mode 100644 Source/NSGData.m create mode 100644 Source/NSGUnarchiver.m create mode 100644 Source/NSMutableData.m create mode 100644 Source/NSUnarchiver.m create mode 100644 Testing/nsarchiving.m diff --git a/Headers/gnustep/base/NSGArchiver.h b/Headers/gnustep/base/NSGArchiver.h new file mode 100644 index 000000000..439b4f7b4 --- /dev/null +++ b/Headers/gnustep/base/NSGArchiver.h @@ -0,0 +1,56 @@ +/* Interface to concrete implementation of NSArchiver + Copyright (C) 1995 Free Software Foundation, Inc. + + Written by: R. Andrew McCallum + Date: April 1995 + + This file is part of the GNU Objective C Class Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef __NSGArchiver_h_OBJECTS_INCLUDE +#define __NSGArchiver_h_OBJECTS_INCLUDE + +#include +#include +#include +#include +#include +#include + +@interface NSGArchiver : NSArchiver +{ + /* For now, these must match the instance variables in foundation/NSGCoder.h. + This will change. */ + int format_version; + int concrete_format_version; + Stream *stream; + BOOL is_decoding; + BOOL doing_root_object; + Dictionary *object_table; /* read/written objects */ + Dictionary *const_ptr_table; /* read/written const *'s */ + Stack *root_object_tables; /* Stack of Dicts for interconnt'd objs */ + Stack *forward_object_tables; /* Stack of Dictionaries for frwd refs */ + + NSZone *object_zone; +} + +@end + +@interface NSGArchiver (GNU) +@end + +#endif /* __NSGArchiver_h_OBJECTS_INCLUDE */ diff --git a/Headers/gnustep/base/NSGData.h b/Headers/gnustep/base/NSGData.h new file mode 100644 index 000000000..1ab5c28ef --- /dev/null +++ b/Headers/gnustep/base/NSGData.h @@ -0,0 +1,68 @@ +/* Interface to concrete implementation of NSData based on MemoryStream class + Copyright (C) 1995 Free Software Foundation, Inc. + + Written by: R. Andrew McCallum + Date: April 1995 + + This file is part of the GNU Objective C Class Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef __NSGData_h_OBJECTS_INCLUDE +#define __NSGData_h_OBJECTS_INCLUDE + +#include +#include +#include +#include + +@interface NSGData : NSData +{ + /* For now, these must match the instance variables in + objects/MemoryStream.h. + This will change. */ + int type; + char *buffer; + int size; + int eofPosition; + int prefix; + int position; +} + +@end + +@interface NSGData (GNU) +@end + +@interface NSGMutableData : NSMutableData +{ + /* For now, these must match the instance variables in + objects/MemoryStream.h. + This will change. */ + int type; + char *buffer; + int size; + int eofPosition; + int prefix; + int position; +} + +@end + +@interface NSGMutableData (GNU) +@end + +#endif /* __NSGData_h_OBJECTS_INCLUDE */ diff --git a/Source/NSGArchiver.m b/Source/NSGArchiver.m new file mode 100644 index 000000000..139534b51 --- /dev/null +++ b/Source/NSGArchiver.m @@ -0,0 +1,42 @@ +/* Concrete NSArchiver for GNUStep based on GNU Coder class + Copyright (C) 1995 Free Software Foundation, Inc. + + Written by: R. Andrew McCallum + Date: April 1995 + + This file is part of the GNU Objective C Class Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include + +@implementation NSGArchiver + ++ (void) initialize +{ + static int done = 0; + [self error:"This class not ready for business yet."]; + if (!done) + { + done = 1; + class_add_behavior([NSGArchiver class], [NSGCoder class]); + } +} + +@end diff --git a/Source/NSGData.m b/Source/NSGData.m new file mode 100644 index 000000000..5093ca13f --- /dev/null +++ b/Source/NSGData.m @@ -0,0 +1,136 @@ +/* Concrete NSData for GNUStep based on GNU MemoryStream class + Copyright (C) 1995 Free Software Foundation, Inc. + + Written by: R. Andrew McCallum + Date: April 1995 + + This file is part of the GNU Objective C Class Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include +#include +#include + +/* This from objects/MemoryStream.h */ +@interface NSGData (MemoryStream) +- _initOnMallocBuffer: (char*)b + size: (unsigned)s /* size of malloc'ed buffer */ + eofPosition: (unsigned)l /* length of buffer with data for reading */ + prefix: (unsigned)p /* reset for this position */ + position: (unsigned)i; /* current position for reading/writing */ +@end + +@implementation NSGData + ++ (void) initialize +{ + static int done = 0; + if (!done) + { + done = 1; + class_add_behavior([NSGData class], [MemoryStream class]); + } +} + +/* This is the designated initializer */ +- (id) initWithBytesNoCopy: (void*)bytes + length: (unsigned int)length +{ + [self _initOnMallocBuffer:bytes + size:length + eofPosition:length + prefix:0 + position:0]; + return self; +} + +- (const void*) bytes +{ + return buffer; +} + +- (unsigned int) length +{ + return eofPosition; +} + +// Storing Data + +- (BOOL) writeToFile: (NSString*)path + atomically: (BOOL)useAuxiliaryFile +{ + /* xxx This currently ignores useAuxiliaryFile. */ + int written; + FILE* fp = fopen([path cString], "w"); + assert (fp); /* This should raise NSException instead. */ + written = fwrite(buffer+prefix, 1, eofPosition, fp); + assert (eofPosition == written); + fclose(fp); + return YES; +} + +@end + + +@implementation NSGMutableData + ++ (void) initialize +{ + static int done = 0; + if (!done) + { + done = 1; + class_add_behavior([NSGMutableData class], [NSGData class]); + } +} + +/* Make sure we do this, and not what MemoryStream says. */ +- (id) initWithCapacity: (unsigned int)capacity +{ + return [self initWithBytesNoCopy:(*objc_malloc)(capacity) + length:capacity]; +} + +/* This is the designated initializer. The behavior comes from NSGData. + - (id) initWithBytesNoCopy: (void*)bytes + length: (unsigned int)length */ + +- (unsigned) capacity +{ + return size; +} + +- (void) setLength: (unsigned int)length +{ + [self setStreamBufferCapacity:length]; +} + +- (void*) mutableBytes +{ + return buffer; +} + +- (void) appendBytes: (const void*)bytes + length: (unsigned int)length +{ + [self writeBytes:bytes length:length]; +} + +@end diff --git a/Source/NSGUnarchiver.m b/Source/NSGUnarchiver.m new file mode 100644 index 000000000..6a5e771c8 --- /dev/null +++ b/Source/NSGUnarchiver.m @@ -0,0 +1,42 @@ +/* Concrete NSUnarchiver for GNUStep based on GNU Coder class + Copyright (C) 1995 Free Software Foundation, Inc. + + Written by: R. Andrew McCallum + Date: April 1995 + + This file is part of the GNU Objective C Class Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include + +@implementation NSGUnarchiver + ++ (void) initialize +{ + static int done = 0; + [self error:"This class not ready for business yet."]; + if (!done) + { + done = 1; + class_add_behavior([NSGUnarchiver class], [NSGCoder class]); + } +} + +@end diff --git a/Source/NSMutableData.m b/Source/NSMutableData.m new file mode 100644 index 000000000..995584104 --- /dev/null +++ b/Source/NSMutableData.m @@ -0,0 +1,155 @@ +/* Implementation of NSMutableData for GNUStep + Copyright (C) 1995 Free Software Foundation, Inc. + + Written by: R. Andrew McCallum + Date: April 1995 + + This file is part of the GNU Objective C Class Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +/* xxx Pretty messy. Needs work. */ + +@implementation NSMutableData + ++ (id) dataWithCapacity: (unsigned int)numBytes +{ + return [[[[NSGMutableData class] alloc] initWithCapacity:numBytes] + autorelease]; +} + ++ (id) dataWithLength: (unsigned int)length +{ + return [[[[NSGMutableData class] alloc] initWithLength:length] + autorelease]; +} + +- (id) initWithCapacity: (unsigned int)capacity +{ + return [self initWithBytesNoCopy:(*objc_malloc)(capacity) + length:capacity]; +} + +/* This is the designated initializer */ +- (id) initWithBytesNoCopy: (void*)bytes + length: (unsigned int)length +{ + /* xxx Eventually we'll have to be aware of malloc'ed memory + vs vm_allocate'd memory, etc. */ + [self notImplemented:_cmd]; + return nil; +} + +- (id) initWithLength: (unsigned int)length +{ + [self initWithCapacity:length]; + memset([self bytes], 0, length); + return self; +} + +/* This method not in OpenStep */ +- (unsigned) capacity +{ + [self notImplemented:_cmd]; + return 0; +} + +// Adjusting Capacity + +- (void) increaseLengthBy: (unsigned int)extraLength +{ + [self setLength:[self length]+extraLength]; +} + +- (void) setLength: (unsigned int)length +{ + [self notImplemented:_cmd]; +} + +- (void*) mutableBytes +{ + [self notImplemented:_cmd]; + return NULL; +} + +// Appending Data + +- (void) appendBytes: (const void*)bytes + length: (unsigned int)length +{ + [self notImplemented:_cmd]; +} + +- (void) appendData: (NSData*)other +{ + [self appendBytes:[other bytes] + length:[other length]]; +} + + +// Modifying Data + +- (void) replaceBytesInRange: (NSRange)aRange + withBytes: (const void*)bytes +{ + memcpy([self bytes] + aRange.location, bytes, aRange.length); +} + +- (void) resetBytesInRange: (NSRange)aRange +{ + memset([self bytes] + aRange.location, 0, aRange.length); +} + +// Serializing Data + +- (void) serializeAlignedBytesLength: (unsigned int)length +{ + [self notImplemented:_cmd]; +} + +- (void) serializeDataAt: (const void*)data + ofObjCType: (const char*)type + context: (id )callback +{ + [self notImplemented:_cmd]; +} + +- (void) serializeInt: (int)value +{ + [self notImplemented:_cmd]; +} + +- (void) serializeInt: (int)value + atIndex: (unsigned int)location +{ + [self notImplemented:_cmd]; +} + +- (void) serializeInts: (int*)intBuffer + count: (unsigned int)numInts +{ + [self notImplemented:_cmd]; +} + +- (void) serializeInts: (int*)intBuffer + count: (unsigned int)numInts + atIndex: (unsigned int)location +{ + [self notImplemented:_cmd]; +} + +@end + diff --git a/Source/NSUnarchiver.m b/Source/NSUnarchiver.m new file mode 100644 index 000000000..bf9d22aca --- /dev/null +++ b/Source/NSUnarchiver.m @@ -0,0 +1,108 @@ +/* Implementation of NSUnrchiver for GNUStep + Copyright (C) 1995 Free Software Foundation, Inc. + + Written by: R. Andrew McCallum + Date: March 1995 + + This file is part of the GNU Objective C Class Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include + +@implementation NSUnarchiver + +// Initializing an unarchiver + +- (id) initForReadingWithData: (NSData*)data +{ + [self notImplemented:_cmd]; + return nil; +} + +// Decoding objects + ++ (id) unarchiveObjectWithData: (NSData*)data +{ + [self notImplemented:_cmd]; + return nil; +} + ++ (id) unarchiveObjectWithFile: (NSString*)path +{ + [self notImplemented:_cmd]; + return nil; +} + +- (void) decodeArrayOfObjCType: (const char*)type + count: (unsigned int)count + at: (void*)array +{ + [self notImplemented:_cmd]; +} + + +// Managing + +- (BOOL) isAtEnd +{ + [self notImplemented:_cmd]; + return NO; +} + +- (NSZone*) objectZone +{ + [self notImplemented:_cmd]; + return NULL; +} + +- (void) setObjectZone: (NSZone*)zone +{ + [self notImplemented:_cmd]; +} + +- (unsigned int) systermVersion +{ + [self notImplemented:_cmd]; + return 0; +} + +// Substituting Classes + ++ (NSString*) classNameDecodedForArchiveClassName: (NSString*)nameInArchive +{ + [self notImplemented:_cmd]; + return nil; +} ++ (void) decodeClassName: (NSString*)nameInArchive + asClassName: (NSString*)trueName +{ + [self notImplemented:_cmd]; +} + +- (NSString*) classNameDecodedForArchiveClassName: (NSString*)nameInArchive +{ + [self notImplemented:_cmd]; + return nil; +} + +- (void) decodeClassName: (NSString*)nameInArchive + asClassName: (NSString*)trueName +{ + [self notImplemented:_cmd]; +} + +@end diff --git a/Testing/nsarchiving.m b/Testing/nsarchiving.m new file mode 100644 index 000000000..e9991c81f --- /dev/null +++ b/Testing/nsarchiving.m @@ -0,0 +1,18 @@ +#include +#include +#include + +int +main () +{ + id a = [NSArray arrayWithObjects: + [NSObject class], + [NSArray class], + nil]; + [NSArchiver archiveRootObject:a toFile:@"./nsarchiving.data"]; + [a release]; + + /* NSUnarchiver not available yet. */ + + exit (0); +}