1
0
mirror of https://github.com/cc65/cc65.git synced 2025-02-07 04:31:38 +00:00

Add a gentype.c module that contains GT_FromStrBuf.

git-svn-id: svn://svn.cc65.org/cc65/trunk@5238 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
uz 2011-08-20 22:13:58 +00:00
parent 85584fb404
commit f3b49b57bd
4 changed files with 104 additions and 2 deletions

74
src/common/gentype.c Normal file
View File

@ -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;
}
}

View File

@ -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
@ -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

View File

@ -28,6 +28,7 @@ OBJS = abend.o \
filetype.o \
fname.o \
fp.o \
gentype.o \
hashfunc.o \
hashtab.o \
intstack.o \

View File

@ -70,6 +70,7 @@ OBJS = abend.obj \
filetype.obj \
fname.obj \
fp.obj \
gentype.obj \
hashfunc.obj \
hashtab.obj \
intstack.obj \