X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fcommon%2Fgentype.c;h=1600a7c2f5b1e72fafee38e34b97f1f63cd4d401;hb=3a6430b13d5b13de264ae33751ff9c69728498d2;hp=7bdddaf33dd83b51a3202cd3ba98cf197bcf26b6;hpb=6583320b79cf3f1dda36f51b29163f18fd9144c0;p=cc65 diff --git a/src/common/gentype.c b/src/common/gentype.c index 7bdddaf33..1600a7c2f 100644 --- a/src/common/gentype.c +++ b/src/common/gentype.c @@ -34,6 +34,7 @@ /* common */ +#include "check.h" #include "gentype.h" #include "strbuf.h" @@ -47,47 +48,66 @@ void GT_AddArray (StrBuf* Type, unsigned ArraySize) /* Add an array with the given size to the type string in Type. This will - * NOT add the element type! - */ +** NOT add the element type! +*/ { - unsigned I; + unsigned SizeBytes; + + /* Remember the current position */ + unsigned Pos = SB_GetLen (Type); - /* Add the array token */ + /* Add a dummy array token */ SB_AppendChar (Type, GT_TYPE_ARRAY); /* Add the size. */ - for (I = 0; I < 4; ++I) { + SizeBytes = 0; + do { SB_AppendChar (Type, ArraySize & 0xFF); ArraySize >>= 8; - } + ++SizeBytes; + } while (ArraySize); + + /* Write the correct array token */ + SB_GetBuf (Type)[Pos] = GT_ARRAY (SizeBytes); } -unsigned GT_GetArraySize (StrBuf* Type) -/* Retrieve the size of an array stored in Type at the current index position. - * The index position will get moved past the array size. - */ +unsigned GT_GetElementCount (StrBuf* Type) +/* Retrieve the element count of an array stored in Type at the current index +** position. Note: Index must point to the array token itself, since the size +** of the element count is encoded there. The index position will get moved +** past the array. +*/ { - unsigned Size; - Size = (unsigned)SB_Get (Type); - Size |= (unsigned)SB_Get (Type) << 8; - Size |= (unsigned)SB_Get (Type) << 16; - Size |= (unsigned)SB_Get (Type) << 24; + /* Get the number of bytes for the element count */ + unsigned SizeBytes = GT_GET_SIZE (SB_Get (Type)); + + /* Read the size */ + unsigned Size = 0; + const char* Buf = SB_GetConstBuf (Type) + SB_GetLen (Type); + while (SizeBytes--) { + Size <<= 8; + Size |= Buf[SizeBytes]; + } + + /* Return it */ return Size; } -void GT_AsString (const StrBuf* Type, StrBuf* String) -/* Convert the type into a readable representation */ +const char* GT_AsString (const StrBuf* Type, StrBuf* String) +/* Convert the type into a readable representation. The target string buffer +** will be zero terminated and a pointer to the contents are returned. +*/ { static const char HexTab[16] = "0123456789ABCDEF"; unsigned I; /* Convert Type into readable hex. String will have twice then length - * plus a terminator. - */ + ** plus a terminator. + */ SB_Realloc (String, 2 * SB_GetLen (Type) + 1); SB_Clear (String); @@ -99,10 +119,7 @@ void GT_AsString (const StrBuf* Type, StrBuf* String) /* Terminate the string so it can be used with string functions */ SB_Terminate (String); -} - - - - - + /* Return the contents of String */ + return SB_GetConstBuf (String); +}