1/*****************************************************************************/
2/* */
3/* gentype.c */
4/* */
5/* Generic data type encoding */
6/* */
7/* */
8/* */
9/* (C) 2011, Ullrich von Bassewitz */
10/* Roemerstrasse 52 */
11/* D-70794 Filderstadt */
12/* EMail: uz@cc65.org */
13/* */
14/* */
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. */
18/* */
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: */
22/* */
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 */
30/* distribution. */
31/* */
32/*****************************************************************************/
33
34
35
36/* common */
37#include "check.h"
38#include "gentype.h"
39#include "strbuf.h"
40
41
42
43/*****************************************************************************/
44/* Code */
45/*****************************************************************************/
46
47
48
49void 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!
52*/
53{
54 unsigned SizeBytes;
55
56 /* Remember the current position */
57 unsigned Pos = SB_GetLen (Type);
58
59 /* Add a dummy array token */
60 SB_AppendChar (Type, GT_TYPE_ARRAY);
61
62 /* Add the size. */
63 SizeBytes = 0;
64 do {
65 SB_AppendChar (Type, ArraySize & 0xFF);
66 ArraySize >>= 8;
67 ++SizeBytes;
68 } while (ArraySize);
69
70 /* Write the correct array token */
71 SB_GetBuf (Type)[Pos] = GT_ARRAY (SizeBytes);
72}
73
74
75
76unsigned 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
80** past the array.
81*/
82{
83 /* Get the number of bytes for the element count */
84 unsigned SizeBytes = GT_GET_SIZE (SB_Get (Type));
85
86 /* Read the size */
87 unsigned Size = 0;
88 const char* Buf = SB_GetConstBuf (Type) + SB_GetLen (Type);
89 while (SizeBytes--) {
90 Size <<= 8;
91 Size |= Buf[SizeBytes];
92 }
93
94 /* Return it */
95 return Size;
96}
97
98
99
100const 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.
103*/
104{
105 static const char HexTab[16] = "0123456789ABCDEF";
106 unsigned I;
107
108 /* Convert Type into readable hex. String will have twice then length
109 ** plus a terminator.
110 */
111 SB_Realloc (String, 2 * SB_GetLen (Type) + 1);
112 SB_Clear (String);
113
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]);
118 }
119
120 /* Terminate the string so it can be used with string functions */
121 SB_Terminate (String);
122
123 /* Return the contents of String */
124 return SB_GetConstBuf (String);
125}
126