mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-23 17:10:48 +00:00
Initial revision
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@92 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
8de414570f
commit
eded938ac2
4 changed files with 310 additions and 0 deletions
100
Source/CString.m
Normal file
100
Source/CString.m
Normal file
|
@ -0,0 +1,100 @@
|
|||
/* Implementation for GNU Objective-C CString object
|
||||
Copyright (C) 1993,1994 Free Software Foundation, Inc.
|
||||
|
||||
Written by: R. Andrew McCallum <mccallum@gnu.ai.mit.edu>
|
||||
Date: July 1994
|
||||
|
||||
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 <objects/String.h>
|
||||
#include <objects/IndexedCollection.h>
|
||||
#include <objects/IndexedCollectionPrivate.h>
|
||||
/* memcpy(), strlen(), strcmp() are gcc builtin's */
|
||||
|
||||
@implementation CString
|
||||
|
||||
/* This is the designated initializer for this class */
|
||||
- initWithCString: (char*)aCharPtr range: (IndexRange)aRange
|
||||
{
|
||||
[super initWithType:@encode(char)];
|
||||
_count = aRange.end - aRange.start;
|
||||
OBJC_MALLOC(_contents_chars, char, _count+1);
|
||||
memcpy(_contents_chars, aCharPtr + aRange.start, _count);
|
||||
_contents_chars[_count] = '\0';
|
||||
return self;
|
||||
}
|
||||
|
||||
- (Class) classForConnectedCoder: aRmc
|
||||
{
|
||||
/* Make sure that Connection's always send us bycopy,
|
||||
i.e. as our own class, not a Proxy class. */
|
||||
return [self class];
|
||||
}
|
||||
|
||||
- (void) encodeWithCoder: aCoder
|
||||
{
|
||||
[aCoder encodeValueOfType:@encode(char*) at:&_contents_chars
|
||||
withName:"Concrete String content_chars"];
|
||||
}
|
||||
|
||||
+ newWithCoder: aCoder
|
||||
{
|
||||
CString *n = [super newWithCoder:aCoder];
|
||||
[aCoder decodeValueOfType:@encode(char*) at:&(n->_contents_chars)
|
||||
withName:NULL];
|
||||
n->_count = strlen(n->_contents_chars);
|
||||
return n;
|
||||
}
|
||||
|
||||
/* Empty copy must empty an allocCopy'ed version of self */
|
||||
- emptyCopy
|
||||
{
|
||||
CString *copy = [super emptyCopy];
|
||||
OBJC_MALLOC(copy->_contents_chars, char, _count+1);
|
||||
copy->_count = 0;
|
||||
copy->_contents_chars[0] = '\0';
|
||||
return copy;
|
||||
}
|
||||
|
||||
- (const char *) cString
|
||||
{
|
||||
return _contents_chars;
|
||||
}
|
||||
|
||||
- (unsigned) count
|
||||
{
|
||||
return _count;
|
||||
}
|
||||
|
||||
- (char) charAtIndex: (unsigned)index
|
||||
{
|
||||
CHECK_INDEX_RANGE_ERROR(index, _count);
|
||||
return _contents_chars[index];
|
||||
}
|
||||
|
||||
// FOR IndexedCollection SUPPORT;
|
||||
|
||||
- (elt) elementAtIndex: (unsigned)index
|
||||
{
|
||||
elt ret_elt;
|
||||
CHECK_INDEX_RANGE_ERROR(index, _count);
|
||||
ret_elt.char_u = _contents_chars[index];
|
||||
return ret_elt;
|
||||
}
|
||||
|
||||
@end
|
108
Source/NSAutoreleasePool.m
Normal file
108
Source/NSAutoreleasePool.m
Normal file
|
@ -0,0 +1,108 @@
|
|||
/* Implementation of auto release pool for delayed disposal
|
||||
Copyright (C) 1995 Free Software Foundation, Inc.
|
||||
|
||||
Written by: R. Andrew McCallum <mccallum@gnu.ai.mit.edu>
|
||||
Date: January 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 <objects/stdobjects.h>
|
||||
#include <foundation/NSAutoreleasePool.h>
|
||||
|
||||
#include <objects/collhash.h>
|
||||
#include <objects/eltfuncs.h>
|
||||
#include <objects/objc-malloc.h>
|
||||
|
||||
/* Doesn't handle multi-threaded stuff.
|
||||
Doesn't handle exceptions. */
|
||||
|
||||
/* Put the stuff from initialize into a runtime init function.
|
||||
This class should be made more efficient, especially:
|
||||
[[NSAutorelease alloc] init]
|
||||
[current_pool addObject:o] (REALLOC-case)
|
||||
*/
|
||||
|
||||
static NSAutoreleasePool *current_pool = nil;
|
||||
|
||||
#define DEFAULT_SIZE 64
|
||||
|
||||
@implementation NSAutoreleasePool
|
||||
|
||||
+ currentPool
|
||||
{
|
||||
return current_pool;
|
||||
}
|
||||
|
||||
+ (void) addObject: anObj
|
||||
{
|
||||
[current_pool addObject:anObj];
|
||||
}
|
||||
|
||||
- (void) addObject: anObj
|
||||
{
|
||||
released_count++;
|
||||
if (released_count == released_size)
|
||||
{
|
||||
released_size *= 2;
|
||||
OBJC_REALLOC(released, id, released_size);
|
||||
}
|
||||
released[released_count] = anObj;
|
||||
}
|
||||
|
||||
- init
|
||||
{
|
||||
parent = current_pool;
|
||||
current_pool = self;
|
||||
OBJC_MALLOC(released, id, DEFAULT_SIZE);
|
||||
released_size = DEFAULT_SIZE;
|
||||
released_count = 0;
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id) retain
|
||||
{
|
||||
[self error:"Don't call `-retain' on a NSAutoreleasePool"];
|
||||
return self;
|
||||
}
|
||||
|
||||
- (oneway void) release
|
||||
{
|
||||
[self dealloc];
|
||||
}
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
int i;
|
||||
|
||||
if (parent)
|
||||
current_pool = parent;
|
||||
else
|
||||
current_pool = [[NSAutoreleasePool alloc] init];
|
||||
for (i = 0; i < released_count; i++)
|
||||
[released[i] release];
|
||||
OBJC_FREE(released);
|
||||
NSDeallocateObject(self);
|
||||
}
|
||||
|
||||
- autorelease
|
||||
{
|
||||
[self error:"Don't call `-autorelease' on a NSAutoreleasePool"];
|
||||
return self;
|
||||
}
|
||||
|
||||
@end
|
10
Testing/string.m
Normal file
10
Testing/string.m
Normal file
|
@ -0,0 +1,10 @@
|
|||
#include "objects/String.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
id s = @"This is a test string";
|
||||
id s2;
|
||||
|
||||
printf("The string [%s], length %d\n", [s cString], [s length]);
|
||||
exit(0);
|
||||
}
|
92
Testing/textcoding.m
Normal file
92
Testing/textcoding.m
Normal file
|
@ -0,0 +1,92 @@
|
|||
#include <objects/TextCoder.h>
|
||||
#include <objects/StdioStream.h>
|
||||
#include <objects/Set.h>
|
||||
#include <objects/EltNodeCollector.h>
|
||||
#include <objects/LinkedList.h>
|
||||
#include <objects/LinkedListEltNode.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
#if 0
|
||||
id stream;
|
||||
int count = 0;
|
||||
int i = 0;
|
||||
float f = 0.0;
|
||||
double d = 0.0;
|
||||
unsigned u = 0;
|
||||
unsigned char uc = 0;
|
||||
unsigned ux = 0;
|
||||
char *cp = NULL;
|
||||
|
||||
stream = [[StdioStream alloc]
|
||||
initWithFilename:"./textcoding.txt"
|
||||
fmode:"w"];
|
||||
[stream writeFormat:"testing %d %u %f %f 0x%x \"cow\"\n",
|
||||
1234, 55, 3.14159, 1.23456789, 0xfeedface];
|
||||
[stream release];
|
||||
|
||||
stream = [[StdioStream alloc]
|
||||
initWithFilename:"./textcoding.txt"
|
||||
fmode:"r"];
|
||||
count = [stream readFormat:"testing %d %u %f %lf 0x%x \"%a[^\"]\"\n",
|
||||
&i, &u, &f, &d, &ux, &cp];
|
||||
uc = (unsigned char) ux;
|
||||
[stream release];
|
||||
printf("Read count=%d, int %d unsigned %u float %f double %f "
|
||||
"uchar %x cp %s\n",
|
||||
count, i, u, f, d, (unsigned)uc, cp);
|
||||
|
||||
#else
|
||||
id set, ll;
|
||||
id stream;
|
||||
id coder;
|
||||
const char *n;
|
||||
|
||||
set = [[Set alloc] initWithType:@encode(int)];
|
||||
ll = [[EltNodeCollector alloc] initWithType:@encode(float)
|
||||
nodeCollector:[[LinkedList alloc] init]
|
||||
nodeClass:[LinkedListEltNode class]];
|
||||
|
||||
[set addElement:1];
|
||||
[set addElement:2];
|
||||
[set addElement:3];
|
||||
[set addElement:4];
|
||||
[set addElement:5];
|
||||
[set printForDebugger];
|
||||
|
||||
[ll addElement:(float)1.2];
|
||||
[ll addElement:(float)3.4];
|
||||
[ll addElement:(float)5.6];
|
||||
[ll addElement:(float)7.8];
|
||||
[ll addElement:(float)9.0];
|
||||
[ll printForDebugger];
|
||||
|
||||
stream = [[StdioStream alloc]
|
||||
initWithFilename:"./textcoding.txt"
|
||||
fmode:"w"];
|
||||
coder = [[TextCoder alloc] initEncodingOnStream:stream];
|
||||
[coder encodeObject:set withName:"Test Set"];
|
||||
[coder encodeObject:ll withName:"Test EltNodeCollector LinkedList"];
|
||||
[coder release];
|
||||
[set release];
|
||||
[ll release];
|
||||
|
||||
stream = [[StdioStream alloc]
|
||||
initWithFilename:"./textcoding.txt"
|
||||
fmode:"r"];
|
||||
coder = [[TextCoder alloc] initDecodingOnStream:stream];
|
||||
[coder decodeObjectAt:&set withName:&n];
|
||||
printf("got object named %s\n", n);
|
||||
(*objc_free)((void*)n);
|
||||
[coder decodeObjectAt:&ll withName:&n];
|
||||
printf("got object named %s\n", n);
|
||||
(*objc_free)((void*)n);
|
||||
[set printForDebugger];
|
||||
[ll printForDebugger];
|
||||
[coder release];
|
||||
[set release];
|
||||
[ll release];
|
||||
#endif
|
||||
|
||||
exit(0);
|
||||
}
|
Loading…
Reference in a new issue