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 /*****************************************************************************/
43 /*****************************************************************************/
45 /*****************************************************************************/
49 void GT_AddArray (StrBuf* Type, unsigned ArraySize)
50 /* Add an array with the given size to the type string in Type. This will
51 ** NOT add the element type!
56 /* Remember the current position */
57 unsigned Pos = SB_GetLen (Type);
59 /* Add a dummy array token */
60 SB_AppendChar (Type, GT_TYPE_ARRAY);
65 SB_AppendChar (Type, ArraySize & 0xFF);
70 /* Write the correct array token */
71 SB_GetBuf (Type)[Pos] = GT_ARRAY (SizeBytes);
76 unsigned GT_GetElementCount (StrBuf* Type)
77 /* Retrieve the element count of an array stored in Type at the current index
78 ** position. Note: Index must point to the array token itself, since the size
79 ** of the element count is encoded there. The index position will get moved
83 /* Get the number of bytes for the element count */
84 unsigned SizeBytes = GT_GET_SIZE (SB_Get (Type));
88 const char* Buf = SB_GetConstBuf (Type) + SB_GetLen (Type);
91 Size |= Buf[SizeBytes];
100 const char* GT_AsString (const StrBuf* Type, StrBuf* String)
101 /* Convert the type into a readable representation. The target string buffer
102 ** will be zero terminated and a pointer to the contents are returned.
105 static const char HexTab[16] = "0123456789ABCDEF";
108 /* Convert Type into readable hex. String will have twice then length
109 ** plus a terminator.
111 SB_Realloc (String, 2 * SB_GetLen (Type) + 1);
114 for (I = 0; I < SB_GetLen (Type); ++I) {
115 unsigned char C = SB_AtUnchecked (Type, I);
116 SB_AppendChar (String, HexTab[(C & 0xF0) >> 4]);
117 SB_AppendChar (String, HexTab[(C & 0x0F) >> 0]);
120 /* Terminate the string so it can be used with string functions */
121 SB_Terminate (String);
123 /* Return the contents of String */
124 return SB_GetConstBuf (String);