debug print formatting
[henge/apc.git] / src / ir.c
index 4cec206..a175ef3 100644 (file)
--- a/src/ir.c
+++ b/src/ir.c
 #include <unistd.h>   //u8_* functions\r
 #include <unitypes.h> //uint8_t as a char\r
 #include <unistr.h>   //u32_cpy\r
+#include <unistdio.h> //ulc_fprintf\r
 /* Local */\r
+#include "print.h"\r
 #include "apc.h"\r
 #include "ir.h"\r
+#undef  do_error\r
+#define do_error(...) exit(-1)\r
 /* Public */\r
 int      ir_init(void);\r
 void     ir_quit(void);\r
@@ -36,9 +40,13 @@ struct pagelist_t {
   struct pagenode_t* root, * head;\r
   size_t             pagesize;\r
 };\r
-/* Set link data */\r
+#define DATA_PAGESIZE    (sys_pagesize)\r
+#define NAME_PAGESIZE    (APC_NAME_MAX * 1024)\r
+#define PL_HEADERSIZE    (sizeof(struct pagenode_header_t))\r
+#define PL_HEADSIZE(_PL) (_PL.head->header.head - _PL.head->root)\r
+#define PL_HEADMEM(_PL)  (_PL.pagesize - PL_HEADERSIZE - PL_HEADSIZE(_PL))\r
+/* Set data mem */\r
 enum dtype { FSDAT, MSDAT, ADAT, LDAT, FBDAT };\r
-enum ltype { OLINK, MLINK, VLINK, ALINK };\r
 struct ir_namelist_t;\r
 struct ir_namelist_t\r
 { struct ir_namelist_t* nextsib;\r
@@ -53,7 +61,6 @@ struct ir_setld_t
   long long             ref;\r
   struct ir_namelist_t* namelist, * namelist_head;\r
 };\r
-/* Set data mem */\r
 struct ir_setdata_header_t\r
 { enum dtype          type;\r
   uint8_t*            src_filename, * data_name;\r
@@ -101,50 +108,26 @@ struct ir_set_t
 };\r
 /* Functions */\r
 static inline\r
-int                   init_pagelist(struct pagelist_t*,size_t);\r
+struct ir_framebox_t* ir_set_add_framebox(struct ir_set_t*,const uint8_t*);\r
+static inline\r
+union ir_setdata_t*   ir_framedata (enum dtype,const uint8_t*,apc_facing,int,int);\r
 static inline\r
-struct ir_framebox_t* ir_set_add_framebox(struct ir_set_t*, uint8_t*);\r
+int      init_pagelist(struct pagelist_t*,size_t);\r
 static\r
-void                  ir_free_pagenodes(struct pagenode_t*);\r
+void     ir_free_pagenodes(struct pagenode_t*);\r
 static inline\r
-int                   bytes_identical(uint8_t*,uint8_t*);\r
+int      bytes_identical(const uint8_t*,const uint8_t*);\r
+static inline\r
+int      classnames_identical(const uint8_t*,const uint8_t*);\r
 static\r
-void*                 stack_alloc(size_t);\r
+void*    stack_alloc(size_t);\r
+#define  struct_alloc(_T) ((struct _T*) stack_alloc(sizeof(struct _T)))\r
 static\r
-uint8_t*              name_alloc(uint8_t*);\r
-static inline\r
-union ir_setdata_t*   ir_framedata (enum dtype,uint8_t*,apc_facing,int,int);\r
-/* Function-Like Macros */\r
-#define do_warn() do {                         \\r
-  } while (0)\r
-#define wprint(str) do {                       \\r
-    fprintf(stderr, str);                      \\r
-    do_warn();                                 \\r
-  } while (0)\r
-#define wprintf(fmt,...) do {                  \\r
-    fprintf(stderr, fmt, __VA_ARGS__);         \\r
-    do_warn();                                 \\r
-  } while (0)\r
-#define do_error() do {                                \\r
-    exit(-1);                                  \\r
-  } while (0)\r
-#define eprint(str) do {                       \\r
-    fprintf(stderr, str);                      \\r
-    do_error();                                        \\r
-  } while (0)\r
-#define eprintf(fmt,...) do {                  \\r
-    fprintf(stderr, fmt, __VA_ARGS__);         \\r
-    do_error();                                        \\r
-  } while (0)\r
-#define struct_alloc(_T) ((struct _T*) stack_alloc(sizeof(struct _T)))\r
-#define DATA_PAGESIZE   (sys_pagesize)\r
-#define NAME_PAGESIZE   (APC_NAME_MAX * 1024)\r
-#define PL_HEADERSIZE   (sizeof(struct pagenode_header_t))\r
-#define PL_HEADSIZE(_PL) (_PL.head->header.head - _PL.head->root)\r
-#define PL_HEADMEM(_PL)  (_PL.pagesize - PL_HEADERSIZE - PL_HEADSIZE(_PL))\r
-/* Memory */\r
+uint8_t* name_alloc(const uint8_t*);\r
+static\r
+uint8_t* classname_alloc(const uint8_t*);\r
 extern //apc.c\r
-long sys_pagesize;\r
+long     sys_pagesize;\r
 static\r
 struct pagelist_t datapages, namepages;\r
 static\r
@@ -154,9 +137,9 @@ struct ir_class_t root_class = { .name = (uint8_t*)"." };
 int ir_init\r
 ( void )\r
 { if (init_pagelist(&datapages, (size_t)DATA_PAGESIZE))\r
-    eprint("Memory allocation error\n");\r
+    eprintf("Memory allocation error\n");\r
   if (init_pagelist(&namepages, (size_t)NAME_PAGESIZE))\r
-    eprint("Memory allocation error\n");\r
+    eprintf("Memory allocation error\n");\r
   return 0;\r
 }\r
 \r
@@ -203,6 +186,11 @@ int ir_condenser
 ( void )\r
 { return 0; }\r
 \r
+/* Return the class's name string */\r
+uint8_t* ir_class_name\r
+( struct ir_class_t* class )\r
+{ return class->name; }\r
+\r
 /* Return a pointer to the root class */\r
 struct ir_class_t* ir_class_root\r
 ( void )\r
@@ -214,22 +202,27 @@ struct ir_class_t* ir_class_root
 */\r
 struct ir_class_t* ir_class_addchild\r
 ( struct ir_class_t* class,\r
-  uint8_t*           name\r
+  const uint8_t*     name\r
 )\r
 { struct ir_class_t* iter;\r
   if (class->nextchild == NULL)\r
-    return class->nextchild = struct_alloc(ir_class_t);\r
+    goto alloc;\r
   iter = class->nextchild;\r
+  if (iter->name == NULL)\r
+    eprintf("Null name pointer in class %p\n", iter);\r
+  if (name == NULL)\r
+    eprintf("Null child added to class %s\n", iter->name);\r
  check:\r
-  if (bytes_identical(iter->name, name))\r
+  if (classnames_identical(iter->name, name))\r
     return iter;\r
   if (iter->nextsib != NULL)\r
     { iter = iter->nextsib;\r
       goto check;\r
     }\r
+ alloc:\r
   iter = struct_alloc(ir_class_t);\r
   iter->nextsib = class->nextchild;\r
-  iter->name = name_alloc(name);\r
+  iter->name = classname_alloc(name);\r
   return class->nextchild = iter;\r
 }\r
 \r
@@ -239,12 +232,16 @@ struct ir_class_t* ir_class_addchild
 */\r
 struct ir_set_t* ir_class_addset\r
 ( struct ir_class_t* class,\r
-  uint8_t*           name\r
+  const uint8_t*     name\r
 )\r
 { struct ir_set_t* iter;\r
   if (class->root_set == NULL)\r
-    return class->root_set = struct_alloc(ir_set_t);\r
+    goto alloc;\r
   iter = class->root_set;\r
+  if (iter->name == NULL)\r
+    eprintf("Null name pointer in class %p\n", iter);\r
+  if (name == NULL)\r
+    eprintf("Null set added to class %U\n", iter->name);\r
  check:\r
   if (bytes_identical(iter->name, name))\r
     return iter;\r
@@ -252,6 +249,7 @@ struct ir_set_t* ir_class_addset
     { iter = iter->nextsib;\r
       goto check;\r
     }\r
+ alloc:\r
   iter = struct_alloc(ir_set_t);\r
   iter->nextsib = class->root_set;\r
   iter->name = name_alloc(name);\r
@@ -264,12 +262,16 @@ struct ir_set_t* ir_class_addset
 */\r
 struct ir_set_t* ir_set_addchild\r
 ( struct ir_set_t* set,\r
-  uint8_t*         name\r
+  const uint8_t*   name\r
 )\r
 { struct ir_set_t* iter;\r
   if (set->nextchild == NULL)\r
-    return set->nextchild = struct_alloc(ir_set_t);\r
+    goto alloc;\r
   iter = set->nextchild;\r
+  if (iter->name == NULL)\r
+    eprintf("Null name pointer in set %p\n", iter);\r
+  if (name == NULL)\r
+    eprintf("Null child added to set %s\n", iter->name);\r
  check:\r
   if (bytes_identical(iter->name, name))\r
     return iter;\r
@@ -277,6 +279,7 @@ struct ir_set_t* ir_set_addchild
     { iter = iter->nextsib;\r
       goto check;\r
     }\r
+ alloc:\r
   iter = struct_alloc(ir_set_t);\r
   iter->nextsib = set->nextchild;\r
   iter->name = name_alloc(name);\r
@@ -290,11 +293,11 @@ struct ir_set_t* ir_set_addchild
 static inline\r
 struct ir_framebox_t* ir_set_add_framebox\r
 ( struct ir_set_t* set,\r
-  uint8_t*         name\r
+  const uint8_t*   name\r
 )\r
 { struct ir_framebox_t* iter;\r
   if (set->frameboxes == NULL)\r
-    return set->frameboxes = struct_alloc(ir_framebox_t);\r
+    goto alloc;\r
   iter = set->frameboxes;\r
  check:\r
   if (bytes_identical(iter->header.data_name, name))\r
@@ -303,6 +306,7 @@ struct ir_framebox_t* ir_set_add_framebox
     { iter = (struct ir_framebox_t*) iter->header.nextsib;\r
       goto check;\r
     }\r
+ alloc:\r
   iter = struct_alloc(ir_framebox_t);\r
   iter->header.nextsib = (union ir_setdata_t*) set->frameboxes;\r
   iter->header.data_name = name_alloc(name);\r
@@ -314,8 +318,21 @@ struct ir_framebox_t* ir_set_add_framebox
 */\r
 static inline\r
 int bytes_identical\r
-( uint8_t* stra,\r
-  uint8_t* strb\r
+( const uint8_t* stra,\r
+  const uint8_t* strb\r
+)\r
+{ int ca, cb;\r
+  do {\r
+    ca = *stra++;\r
+    cb = *strb++;\r
+  } while (ca && ca != '_' && ca == cb);\r
+  return (ca == cb);\r
+}\r
+\r
+static inline\r
+int classnames_identical\r
+( const uint8_t* stra,\r
+  const uint8_t* strb\r
 )\r
 { int ca, cb;\r
   do {\r
@@ -390,39 +407,43 @@ void ir_set_assign_ref
 \r
 void ir_data_assign_path\r
 ( union ir_setdata_t* setdata,\r
-  uint8_t*            path\r
+  const uint8_t*      path\r
 )\r
-{ if (setdata->header.src_filename != NULL)\r
+{ if (path == NULL)\r
+    eprintf("Null path in data %s\n", setdata->header.data_name);\r
+  if (setdata->header.src_filename != NULL)\r
     wprintf("Path override: %s -> %s for setdata %s\n",\r
            setdata->header.src_filename, path, setdata->header.data_name);\r
   setdata->header.src_filename = name_alloc(path);\r
 }\r
 \r
 union ir_setdata_t* ir_framesheet\r
-( uint8_t*   name,\r
-  apc_facing d,\r
-  int        width,\r
-  int        height\r
+( const uint8_t* name,\r
+  apc_facing     d,\r
+  int            width,\r
+  int            height\r
 )\r
 { return ir_framedata(FSDAT, name, d, width, height); }\r
 \r
 union ir_setdata_t* ir_mapsheet\r
-( uint8_t*   name,\r
-  apc_facing d,\r
-  int        width,\r
-  int        height\r
+( const uint8_t* name,\r
+  apc_facing     d,\r
+  int            width,\r
+  int            height\r
 )\r
 { return ir_framedata(MSDAT, name, d, width, height); }\r
 \r
 static inline\r
 union ir_setdata_t* ir_framedata\r
-( enum dtype type,\r
-  uint8_t*   name,\r
-  apc_facing d,\r
-  int        width,\r
-  int        height\r
+( enum dtype     type,\r
+  const uint8_t* name,\r
+  apc_facing     d,\r
+  int            width,\r
+  int            height\r
 )\r
 { struct ir_framedata_t* framedata = struct_alloc(ir_framedata_t);\r
+  if (name == NULL)\r
+    eprintf("Null name in set allocation\n");\r
   framedata->header.type = type;\r
   framedata->header.data_name = name_alloc(name);\r
   framedata->frameinfo.facing = d;\r
@@ -432,8 +453,10 @@ union ir_setdata_t* ir_framedata
 }\r
 \r
 union ir_setdata_t* ir_audio\r
-( uint8_t* name )\r
+( const uint8_t* name )\r
 { struct ir_simplex_t* audio = struct_alloc(ir_simplex_t);\r
+  if (name == NULL)\r
+    eprintf("Null audio\n");\r
   audio->header.type = ADAT;\r
   audio->header.data_name = name_alloc(name);\r
   return (union ir_setdata_t*) audio;\r
@@ -444,6 +467,8 @@ union ir_setdata_t* ir_audio
 struct ir_classld_t* ir_classld_from_class\r
 ( struct ir_class_t* class )\r
 { struct ir_classld_t* classld;\r
+  if (class == NULL)\r
+    eprintf("Null class in classld\n");\r
   classld = struct_alloc(ir_classld_t);\r
   classld->root_class = class;\r
   return classld;\r
@@ -459,7 +484,7 @@ struct ir_setld_t* ir_setld_from_ref
 \r
 struct ir_setld_t* ir_setld_from_classld\r
 ( struct ir_classld_t* classld,\r
-  uint8_t*             name\r
+  const uint8_t*       name\r
 )\r
 { struct ir_setld_t* setld;\r
   setld = struct_alloc(ir_setld_t);\r
@@ -472,7 +497,7 @@ struct ir_setld_t* ir_setld_from_classld
 \r
 struct ir_setld_t* ir_setld_addchild\r
 ( struct ir_setld_t* setld,\r
-  uint8_t*           name\r
+  const uint8_t*     name\r
 )\r
 { if (setld->namelist == NULL)\r
     { setld->namelist = struct_alloc(ir_namelist_t);\r
@@ -489,7 +514,7 @@ struct ir_setld_t* ir_setld_addchild
 union ir_setdata_t* ir_link\r
 ( enum ltype         link_type,\r
   struct ir_setld_t* setld,\r
-  uint8_t*           name\r
+  const uint8_t*     name\r
 )\r
 { struct ir_link_t* link;\r
   link = struct_alloc(ir_link_t);\r
@@ -511,7 +536,7 @@ void* stack_alloc
   if (PL_HEADMEM(datapages) < bytes) \r
     { datapages.head->header.next = (struct pagenode_t*) calloc(datapages.pagesize,1);\r
       if (datapages.head->header.next == NULL)\r
-       eprint("Memory allocation error \n");\r
+       eprintf("Memory allocation error \n");\r
       datapages.head = datapages.head->header.next; \r
       datapages.head->header.head = datapages.head->root;\r
     }\r
@@ -521,9 +546,33 @@ void* stack_alloc
 \r
 static\r
 uint8_t* name_alloc\r
-( uint8_t* name_src )\r
-{ uint8_t* iter, * name;\r
-  int      head_mem;\r
+( const uint8_t* name_src )\r
+{ const uint8_t* iter;\r
+  uint8_t*       name;\r
+  int            head_mem;\r
+ copy:\r
+  name = (uint8_t*)namepages.head->header.head;\r
+  iter = name_src;\r
+  for (head_mem = PL_HEADMEM(namepages); *iter && *iter != '_' && head_mem; head_mem--)\r
+    *(namepages.head->header.head)++ = *iter++;\r
+  if (head_mem == 0) //not enough room\r
+    { namepages.head->header.next = (struct pagenode_t*) calloc(namepages.pagesize,1);\r
+      if (namepages.head->header.next == NULL)\r
+       eprintf("Memory allocation error\n");\r
+      namepages.head = namepages.head->header.next;\r
+      namepages.head->header.head = namepages.head->root;\r
+      goto copy;\r
+    }\r
+  *(namepages.head->header.head)++ = '\0';\r
+  return name;\r
+}\r
+\r
+static\r
+uint8_t* classname_alloc\r
+( const uint8_t* name_src )\r
+{ const uint8_t* iter;\r
+  uint8_t*       name;\r
+  int            head_mem;\r
  copy:\r
   name = (uint8_t*)namepages.head->header.head;\r
   iter = name_src;\r
@@ -532,7 +581,7 @@ uint8_t* name_alloc
   if (head_mem == 0) //not enough room\r
     { namepages.head->header.next = (struct pagenode_t*) calloc(namepages.pagesize,1);\r
       if (namepages.head->header.next == NULL)\r
-       eprint("Memory allocation error\n");\r
+       eprintf("Memory allocation error\n");\r
       namepages.head = namepages.head->header.next;\r
       namepages.head->header.head = namepages.head->root;\r
       goto copy;\r