*** empty log message ***

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@837 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Andrew McCallum 1996-01-26 15:44:29 +00:00
parent af1c3b79a5
commit 8a669fe04c
4 changed files with 0 additions and 696 deletions

View file

@ -1,331 +0,0 @@
/* Implementation of GNU Objective-C binary coder object for use serializing
Copyright (C) 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/stdobjects.h>
#include <objects/BinaryCoder.h>
#include <objects/MemoryStream.h>
#include <assert.h>
#include <objects/StdioStream.h>
#include <objects/TextCoder.h>
#define CONCRETE_FORMAT_VERSION 0
static BOOL debug_binary_coder = NO;
@implementation BinaryCoder
+ setDebugging: (BOOL)f
{
debug_binary_coder = f;
return self;
}
+ (TextCoder*) debugStderrCoder
{
static TextCoder* c = nil;
if (!c)
c = [[TextCoder alloc] initEncodingOnStream:[StdioStream standardError]];
return c;
}
+ (int) coderConcreteFormatVersion
{
return CONCRETE_FORMAT_VERSION;
}
/* Careful, this shouldn't contain newlines */
+ (const char *) coderSignature
{
return "GNU Objective C Class Library BinaryCoder";
}
- doInitOnStream: (Stream *)s isDecoding: (BOOL)f
{
[super doInitOnStream:s isDecoding:f];
return self;
}
- (void) encodeValueOfCType: (const char*)type
at: (const void*)d
withName: (const char *)name
{
unsigned char size;
if (debug_binary_coder)
{
[[BinaryCoder debugStderrCoder]
encodeValueOfCType:type
at:d
withName:name];
}
assert(type);
assert(*type != '@');
assert(*type != '^');
assert(*type != ':');
assert(*type != '{');
assert(*type != '[');
/* A fairly stupid, inefficient binary encoding. This could use
some improvement. For instance, we could compress the sign
information and the type information.
It could probably also use some portability fixes. */
[stream writeByte:*type];
size = objc_sizeof_type(type);
[stream writeByte:size];
switch (*type)
{
case _C_CHARPTR:
{
int length = strlen(*(char**)d);
[self encodeValueOfCType:@encode(int)
at:&length withName:@"BinaryCoder char* length"];
[stream writeBytes:*(char**)d length:length];
break;
}
case _C_CHR:
#ifndef __CHAR_UNSIGNED__
if (*(char*)d < 0)
[stream writeByte:1];
else
#endif
[stream writeByte:0];
case _C_UCHR:
[stream writeByte:*(unsigned char*)d];
break;
case _C_SHT:
if (*(short*)d < 0)
[stream writeByte:1];
else
[stream writeByte:0];
case _C_USHT:
{
unsigned char *buf = alloca(size);
short s = *(short*)d;
int count = size;
if (s < 0) s = -s;
for (; count--; s >>= 8)
buf[count] = (char) (s % 0x100);
[stream writeBytes:buf length:size];
break;
}
case _C_INT:
if (*(int*)d < 0)
[stream writeByte:1];
else
[stream writeByte:0];
case _C_UINT:
{
unsigned char *buf = alloca(size);
int s = *(int*)d;
int count = size;
if (s < 0) s = -s;
for (; count--; s >>= 8)
buf[count] = (char) (s % 0x100);
[stream writeBytes:buf length:size];
break;
}
case _C_LNG:
if (*(long*)d < 0)
[stream writeByte:1];
else
[stream writeByte:0];
case _C_ULNG:
{
unsigned char *buf = alloca(size);
long s = *(long*)d;
int count = size;
if (s < 0) s = -s;
for (; count--; s >>= 8)
buf[count] = (char) (s % 0x100);
[stream writeBytes:buf length:size];
break;
}
/* Two quickie kludges to make archiving of floats and doubles work */
case _C_FLT:
{
char buf[64];
char *s = buf;
sprintf(buf, "%f", *(float*)d);
[self encodeValueOfCType:@encode(char*)
at:&s withName:@"BinaryCoder float"];
break;
}
case _C_DBL:
{
char buf[64];
char *s = buf;
sprintf(buf, "%f", *(double*)d);
[self encodeValueOfCType:@encode(char*)
at:&s withName:@"BinaryCoder double"];
break;
}
default:
[self error:"Unrecognized Type %s", type];
}
}
- (void) decodeValueOfCType: (const char*)type
at: (void*)d
withName: (const char **)namePtr
{
char encoded_type;
unsigned char encoded_size;
unsigned char encoded_sign = 0;
assert(type);
assert(*type != '@');
assert(*type != '^');
assert(*type != ':');
assert(*type != '{');
assert(*type != '[');
[stream readByte:&encoded_type];
if (encoded_type != *type
&& !((encoded_type=='c' || encoded_type=='C')
&& (*type=='c' || *type=='C')))
[self error:"Expected type \"%c\", got type \"%c\"", *type, encoded_type];
[stream readByte:&encoded_size];
switch (encoded_type)
{
case _C_CHARPTR:
{
int length;
[self decodeValueOfCType:@encode(int)
at:&length withName:NULL];
OBJC_MALLOC(*(char**)d, char, length+1);
[stream readBytes:*(char**)d length:length];
(*(char**)d)[length] = '\0';
break;
}
case _C_CHR:
[stream readByte:&encoded_sign];
case _C_UCHR:
[stream readByte:(unsigned char*)d];
if (encoded_sign)
*(char*)d = *(char*)d * -1;
break;
case _C_SHT:
[stream readByte:&encoded_sign];
case _C_USHT:
{
unsigned char *buf = alloca(encoded_size);
int i;
short s = 0;
[stream readBytes:buf length:encoded_size];
for (i = 0; i < sizeof(short); i++)
{
s <<= 8;
s += buf[i];
}
if (encoded_sign)
s = -s;
*(short*)d = s;
break;
}
case _C_INT:
[stream readByte:&encoded_sign];
case _C_UINT:
{
unsigned char *buf = alloca(encoded_size);
int i;
int s = 0;
[stream readBytes:buf length:encoded_size];
for (i = 0; i < sizeof(int); i++)
{
s <<= 8;
s += buf[i];
}
if (encoded_sign)
s = -s;
*(int*)d = s;
break;
}
case _C_LNG:
[stream readByte:&encoded_sign];
case _C_ULNG:
{
unsigned char *buf = alloca(encoded_size);
int i;
long s = 0;
[stream readBytes:buf length:encoded_size];
for (i = 0; i < sizeof(long); i++)
{
s <<= 8;
s += buf[i];
}
if (encoded_sign)
s = -s;
*(long*)d = s;
break;
}
/* Two quickie kludges to make archiving of floats and doubles work */
case _C_FLT:
{
char *buf;
[self decodeValueOfCType:@encode(char*) at:&buf withName:NULL];
if (sscanf(buf, "%f", (float*)d) != 1)
[self error:"expected float, got %s", buf];
(*objc_free)(buf);
break;
}
case _C_DBL:
{
char *buf;
[self decodeValueOfCType:@encode(char*) at:&buf withName:NULL];
if (sscanf(buf, "%lf", (double*)d) != 1)
[self error:"expected double, got %s", buf];
(*objc_free)(buf);
break;
}
default:
[self error:"Unrecognized Type %s", type];
}
if (debug_binary_coder)
{
[[BinaryCoder debugStderrCoder]
encodeValueOfCType:type
at:d
withName:@"decoding unnamed"];
}
}
- (void) encodeName: (const char *)name
{
if (debug_binary_coder)
[[BinaryCoder debugStderrCoder]
encodeName:name];
}
@end

View file

@ -1,290 +0,0 @@
/* Implementation of GNU Objective-C text coder object for use serializing
Copyright (C) 1994, 1995 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/stdobjects.h>
#include <objects/TextCoder.h>
#include <objects/MemoryStream.h>
#include <objects/StdioStream.h>
#include <objects/objc-malloc.h>
#include <assert.h>
#define CONCRETE_FORMAT_VERSION 0
static BOOL debug_textcoder = NO;
@implementation TextCoder
+ (int) coderConcreteFormatVersion
{
return CONCRETE_FORMAT_VERSION;
}
/* Careful, this shouldn't contain newlines */
+ (const char *) coderSignature
{
return "GNU Objective C Class Library TextCoder";
}
- doInitOnStream: (Stream *)s isDecoding: (BOOL)f
{
[super doInitOnStream:s isDecoding:f];
indentation = 0;
return self;
}
#define XSTR(s) STR(s)
#define STR(s) #s
#define ENCODER_FORMAT(TYPE, CONVERSION) \
"%*s<%s> (" XSTR(TYPE) ") = %" XSTR(CONVERSION) "\n"
- (void) encodeValueOfCType: (const char*)type
at: (const void*)d
withName: (const char *)name
{
if (!name)
name = "";
switch (*type)
{
case _C_LNG:
[stream writeFormat:"%*s<%s> (long) = %ld\n",
indentation, "", name, *(long*)d];
break;
case _C_ULNG:
[stream writeFormat:"%*s<%s> (unsigned long) = %lu\n",
indentation, "", name, *(unsigned long*)d];
break;
case _C_INT:
[stream writeFormat:"%*s<%s> (int) = %d\n",
indentation, "", name, *(int*)d];
break;
case _C_UINT:
[stream writeFormat:"%*s<%s> (unsigned int) = %u\n",
indentation, "", name, *(unsigned int*)d];
break;
case _C_SHT:
[stream writeFormat:"%*s<%s> (short) = %d\n",
indentation, "", name, (int)*(short*)d];
break;
case _C_USHT:
[stream writeFormat:"%*s<%s> (unsigned short) = %u\n",
indentation, "", name, (unsigned)*(unsigned short*)d];
break;
case _C_CHR:
[stream writeFormat:"%*s<%s> (char) = %c (0x%x)\n",
indentation, "", name, *(char*)d, (unsigned)*(char*)d];
break;
case _C_UCHR:
[stream writeFormat:"%*s<%s> (unsigned char) = 0x%x\n",
indentation, "", name, (unsigned)*(unsigned char*)d];
break;
case _C_FLT:
[stream writeFormat:"%*s<%s> (float) = %g\n",
indentation, "", name, *(float*)d];
break;
case _C_DBL:
[stream writeFormat:"%*s<%s> (double) = %g\n",
indentation, "", name, *(double*)d];
break;
case _C_CHARPTR:
[stream writeFormat:"%*s<%s> (char*) = \"%s\"\n",
indentation, "", name, *(char**)d];
break;
default:
[self error:"type %s not yet implemented", type];
}
}
#define DECODER_FORMAT(TYPE, CONVERSION) \
" <%a[^>]> (" XSTR(TYPE) ") = %" XSTR(CONVERSION) " \n"
#define DECODE_ERROR(TYPE) [self error:"bad format decoding " XSTR(TYPE)]
#define DECODE_DEBUG(TYPE, CONVERSION) \
if (debug_textcoder) \
[[StdioStream standardError] writeFormat:"got <%s> (%s) %" \
XSTR(CONVERSION) "\n", \
tmpname, \
XSTR(TYPE), *(TYPE*)d];
- (void) decodeValueOfCType: (const char*)type
at: (void*)d
withName: (const char **)name
{
char *tmpname;
assert (d);
switch (*type)
{
case _C_LNG:
if ([stream readFormat:DECODER_FORMAT(long,l),
&tmpname, (long*)d] != 2)
DECODE_ERROR(long);
DECODE_DEBUG(long, l);
break;
case _C_ULNG:
if ([stream readFormat:DECODER_FORMAT(unsigned long, lu),
&tmpname, (unsigned long*)d] != 2)
DECODE_ERROR(unsigned long);
DECODE_DEBUG(unsigned long, lu);
break;
case _C_INT:
if ([stream readFormat:DECODER_FORMAT(int, d),
&tmpname, (int*)d] != 2)
DECODE_ERROR(int);
DECODE_DEBUG(int, d);
break;
case _C_UINT:
if ([stream readFormat:DECODER_FORMAT(unsigned int,u),
&tmpname, (unsigned int*)d] != 2)
DECODE_ERROR(unsigned int);
DECODE_DEBUG(unsigned int, u);
break;
case _C_SHT:
if ([stream readFormat:DECODER_FORMAT(short,hd),
&tmpname, (short*)d] != 2)
DECODE_ERROR(short);
DECODE_DEBUG(short, d);
break;
case _C_USHT:
if ([stream readFormat:DECODER_FORMAT(unsigned short,hu),
&tmpname, (unsigned short*)d] != 2)
DECODE_ERROR(unsigned short);
DECODE_DEBUG(unsigned short, u);
break;
case _C_CHR:
{
unsigned tmp;
if ([stream readFormat:" <%a[^>]> (char) = %*c (%x) \n",
&tmpname, &tmp] != 2)
DECODE_ERROR(char);
*(char*)d = (char)tmp;
DECODE_DEBUG(char, c);
break;
}
case _C_UCHR:
{
unsigned tmp;
if ([stream readFormat:DECODER_FORMAT(unsigned char,x),
&tmpname, &tmp] != 2)
DECODE_ERROR(unsigned char);
*(unsigned char*)d = (unsigned char)tmp;
DECODE_DEBUG(unsigned char, c);
break;
}
case _C_FLT:
if ([stream readFormat:DECODER_FORMAT(float,f),
&tmpname, (float*)d] != 2)
DECODE_ERROR(float);
DECODE_DEBUG(float, f);
break;
case _C_DBL:
if ([stream readFormat:DECODER_FORMAT(double,lf),
&tmpname, (double*)d] != 2)
DECODE_ERROR(double);
DECODE_DEBUG(double, f);
break;
case _C_CHARPTR:
if ([stream readFormat:" <%a[^>]> (char*) = \"%a[^\"]\" \n",
&tmpname, (char**)d] != 2)
DECODE_ERROR(char*);
DECODE_DEBUG(char*, s);
break;
default:
[self error:"type %s not yet implemented", type];
}
if (name && *name)
*name = tmpname;
else
(*objc_free)(tmpname);
}
- (void) encodeIndent
{
[stream writeFormat:"%*s {\n", indentation, ""];
indentation += 2;
}
- (void) encodeUnindent
{
indentation -= 2;
[stream writeFormat:"%*s }\n", indentation, ""];
}
- (void) decodeIndent
{
char *line;
char *lp;
lp = line = [stream readLine];
while (*lp == ' ') lp++;
if (*lp != '{')
[self error:"bad indent format, got \"%s\"", line];
}
- (void) decodeUnindent
{
char *line;
char *lp;
lp = line = [stream readLine];
while (*lp == ' ') lp++;
if (*lp != '}')
[self error:"bad unindent format, got \"%s\"", line];
}
- (void) encodeName: (const char*)n
{
if (n)
[stream writeFormat:"%*s<%s>\n", indentation, "", n];
else
[stream writeFormat:"%*s<NULL>\n", indentation, "", n];
}
/* Buffer is malloc'ed */
- (void) decodeName: (const char**)n
{
if (n)
{
if ([stream readFormat:" <%a[^>]> \n", n] != 1)
[self error:"bad format"];
if (debug_textcoder)
fprintf(stderr, "got name <%s>\n", *n);
}
else
{
[stream readFormat:" <%*[^>]> \n"];
}
}
- (void) encodeWithCoder: anEncoder
{
[self notImplemented:_cmd];
}
+ newWithCoder: aDecoder
{
[self notImplemented:_cmd];
return self;
}
@end

View file

@ -1,38 +0,0 @@
/* Interface for GNU Objective-C binary coder object for use serializing
Copyright (C) 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.
*/
#ifndef __BinaryCoder_h
#define __BinaryCoder_h
#include <objects/stdobjects.h>
#include <objects/Coder.h>
@class Stream;
@interface BinaryCoder : Coder
{
}
@end
#endif /* __BinaryCoder_h */

View file

@ -1,37 +0,0 @@
/* Interface for GNU Objective-C text coder object for use serializing
Copyright (C) 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.
*/
#ifndef __TextCoder_h_OBJECTS_INCLUDE
#define __TextCoder_h_OBJECTS_INCLUDE
#include <objects/stdobjects.h>
#include <objects/Coder.h>
@interface TextCoder : Coder
{
int indentation;
}
@end
#endif /* __TextCoder_h_OBJECTS_INCLUDE */