]> git.sur5r.net Git - cc65/blob - src/common/gentype.c
Added GT_AsString().
[cc65] / src / common / gentype.c
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 "gentype.h"
38 #include "strbuf.h"
39
40
41
42 /*****************************************************************************/
43 /*                                   Code                                    */
44 /*****************************************************************************/
45
46
47
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!
51  */
52 {
53     unsigned I;
54
55     /* Add the array token */
56     SB_AppendChar (Type, GT_TYPE_ARRAY);
57
58     /* Add the size. */
59     for (I = 0; I < 4; ++I) {
60         SB_AppendChar (Type, ArraySize & 0xFF);
61         ArraySize >>= 8;
62     }
63 }
64
65
66
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.
70  */
71 {
72     unsigned 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;
77     return Size;
78 }
79
80
81
82 void GT_AsString (const StrBuf* Type, StrBuf* String)
83 /* Convert the type into a readable representation */
84 {
85     static const char HexTab[16] = "0123456789ABCDEF";
86     unsigned I;
87
88     /* Convert Type into readable hex. String will have twice then length
89      * plus a terminator.
90      */
91     SB_Realloc (String, 2 * SB_GetLen (Type) + 1);
92     SB_Clear (String);
93
94     for (I = 0; I < SB_GetLen (Type); ++I) {
95         unsigned char C = SB_AtUnchecked (Type, I);
96         SB_AppendChar (String, HexTab[(C & 0xF0) >> 4]);
97         SB_AppendChar (String, HexTab[(C & 0x0F) >> 0]);
98     }
99
100     /* Terminate the string so it can be used with string functions */
101     SB_Terminate (String);
102 }
103
104
105
106
107
108