1 /*****************************************************************************/
5 /* Generic data type encoding */
9 /* (C) 2011, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
42 /*****************************************************************************/
44 /*****************************************************************************/
48 void GT_AddArray (StrBuf* Type, unsigned ArraySize)
49 /* Add an array with the given size to the type string in Type. This will
50 * NOT add the element type!
55 /* Add the array token */
56 SB_AppendChar (Type, GT_TYPE_ARRAY);
59 for (I = 0; I < 4; ++I) {
60 SB_AppendChar (Type, ArraySize & 0xFF);
67 unsigned GT_GetArraySize (StrBuf* Type)
68 /* Retrieve the size of an array stored in Type at the current index position.
69 * The index position will get moved past the array size.
73 Size = (unsigned)SB_Get (Type);
74 Size |= (unsigned)SB_Get (Type) << 8;
75 Size |= (unsigned)SB_Get (Type) << 16;
76 Size |= (unsigned)SB_Get (Type) << 24;
82 const char* GT_AsString (const StrBuf* Type, StrBuf* String)
83 /* Convert the type into a readable representation. The target string buffer
84 * will be zero terminated and a pointer to the contents are returned.
87 static const char HexTab[16] = "0123456789ABCDEF";
90 /* Convert Type into readable hex. String will have twice then length
93 SB_Realloc (String, 2 * SB_GetLen (Type) + 1);
96 for (I = 0; I < SB_GetLen (Type); ++I) {
97 unsigned char C = SB_AtUnchecked (Type, I);
98 SB_AppendChar (String, HexTab[(C & 0xF0) >> 4]);
99 SB_AppendChar (String, HexTab[(C & 0x0F) >> 0]);
102 /* Terminate the string so it can be used with string functions */
103 SB_Terminate (String);
105 /* Return the contents of String */
106 return SB_GetConstBuf (String);