From f3b49b57bd4d719029fd1930a64563cb35feec8e Mon Sep 17 00:00:00 2001 From: uz Date: Sat, 20 Aug 2011 22:13:58 +0000 Subject: [PATCH] Add a gentype.c module that contains GT_FromStrBuf. git-svn-id: svn://svn.cc65.org/cc65/trunk@5238 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/common/gentype.c | 74 ++++++++++++++++++++++++++++++++++++++ src/common/gentype.h | 30 ++++++++++++++-- src/common/make/gcc.mak | 1 + src/common/make/watcom.mak | 1 + 4 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 src/common/gentype.c diff --git a/src/common/gentype.c b/src/common/gentype.c new file mode 100644 index 000000000..fe682d17f --- /dev/null +++ b/src/common/gentype.c @@ -0,0 +1,74 @@ +/*****************************************************************************/ +/* */ +/* gentype.c */ +/* */ +/* Generic data type encoding */ +/* */ +/* */ +/* */ +/* (C) 2011, Ullrich von Bassewitz */ +/* Roemerstrasse 52 */ +/* D-70794 Filderstadt */ +/* EMail: uz@cc65.org */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +/* common */ +#include "gentype.h" +#include "strbuf.h" +#include "xmalloc.h" + + + +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +gt_string GT_FromStrBuf (const struct StrBuf* S) +/* Create a dynamically allocated type string from a string buffer. */ +{ + /* To avoid silly mistakes, check if the last character in S is a + * terminator. If not, don't rely on S being terminated. + */ + unsigned Len = SB_GetLen (S); + if (Len > 0 && SB_LookAtLast (S) == '\0') { + /* String is terminated - allocate memory */ + gt_string Type = xmalloc (Len); + /* Copy the data and return the result */ + return memcpy (Type, SB_GetConstBuf (S), Len); + } else { + /* String not terminated - allocate memory */ + gt_string Type = xmalloc (Len + 1); + /* Copy the data */ + memcpy (Type, SB_GetConstBuf (S), Len); + /* Terminate the string */ + Type[Len] = GT_END; + /* Return the copy */ + return Type; + } +} + + + diff --git a/src/common/gentype.h b/src/common/gentype.h index 4a01b409e..c229b5aa2 100644 --- a/src/common/gentype.h +++ b/src/common/gentype.h @@ -39,11 +39,26 @@ /*****************************************************************************/ -/* Data */ +/* Forwards */ /*****************************************************************************/ +struct StrBuf; + + + +/*****************************************************************************/ +/* Data */ +/*****************************************************************************/ + + + +/* The data type used to encode a generic type */ +typedef unsigned char* gt_string; + + + /* Termination, so we can use string functions to handle type strings */ #define GT_END 0x00U @@ -88,7 +103,7 @@ #define GT_IS_FUNCTION(x) (GT_GET_TYPE(x) == GT_FUNCTION) #define GT_IS_STRUCT(x) (GT_GET_TYPE(x) == GT_STRUCT) #define GT_IS_UNION(x) (GT_GET_TYPE(x) == GT_UNION) - + /* Combined values for the 6502 family */ #define GT_BYTE (GT_INTEGER | GT_LITTLE_ENDIAN | GT_UNSIGNED | GT_SIZE_1) #define GT_WORD (GT_INTEGER | GT_LITTLE_ENDIAN | GT_UNSIGNED | GT_SIZE_2) @@ -99,6 +114,17 @@ +/*****************************************************************************/ +/* Code */ +/*****************************************************************************/ + + + +gt_string GT_FromStrBuf (const struct StrBuf* S); +/* Create a dynamically allocated type string from a string buffer */ + + + /* End of gentype.h */ #endif diff --git a/src/common/make/gcc.mak b/src/common/make/gcc.mak index 558c97337..d0fc1904b 100644 --- a/src/common/make/gcc.mak +++ b/src/common/make/gcc.mak @@ -28,6 +28,7 @@ OBJS = abend.o \ filetype.o \ fname.o \ fp.o \ + gentype.o \ hashfunc.o \ hashtab.o \ intstack.o \ diff --git a/src/common/make/watcom.mak b/src/common/make/watcom.mak index 8b59787ba..e814115e5 100644 --- a/src/common/make/watcom.mak +++ b/src/common/make/watcom.mak @@ -70,6 +70,7 @@ OBJS = abend.obj \ filetype.obj \ fname.obj \ fp.obj \ + gentype.obj \ hashfunc.obj \ hashtab.obj \ intstack.obj \ -- 2.39.5