/* common */
+#include "check.h"
#include "gentype.h"
#include "strbuf.h"
-#include "xmalloc.h"
-gt_string GT_FromStrBuf (const struct StrBuf* S)
-/* Create a dynamically allocated type string from a string buffer. */
+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!
+*/
{
- /* To avoid silly mistakes, check if the last character in S is a
- * terminator. If not, don't rely on S being terminated.
- */
- unsigned Len = SB_GetLen (S);
- if (Len > 0 && SB_LookAtLast (S) == '\0') {
- /* String is terminated - allocate memory */
- gt_string Type = xmalloc (Len);
- /* Copy the data and return the result */
- return memcpy (Type, SB_GetConstBuf (S), Len);
- } else {
- /* String not terminated - allocate memory */
- gt_string Type = xmalloc (Len + 1);
- /* Copy the data */
- memcpy (Type, SB_GetConstBuf (S), Len);
- /* Terminate the string */
- Type[Len] = GT_END;
- /* Return the copy */
- return Type;
+ unsigned SizeBytes;
+
+ /* Remember the current position */
+ unsigned Pos = SB_GetLen (Type);
+
+ /* Add a dummy array token */
+ SB_AppendChar (Type, GT_TYPE_ARRAY);
+
+ /* Add the size. */
+ 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_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.
+*/
+{
+ /* 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;
}
+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.
+ */
+ SB_Realloc (String, 2 * SB_GetLen (Type) + 1);
+ SB_Clear (String);
+
+ for (I = 0; I < SB_GetLen (Type); ++I) {
+ unsigned char C = SB_AtUnchecked (Type, I);
+ SB_AppendChar (String, HexTab[(C & 0xF0) >> 4]);
+ SB_AppendChar (String, HexTab[(C & 0x0F) >> 0]);
+ }
+
+ /* Terminate the string so it can be used with string functions */
+ SB_Terminate (String);
+
+ /* Return the contents of String */
+ return SB_GetConstBuf (String);
+}