📄 dvgenc.c
字号:
/* DataDraw Copyright(C) 1992-2006 Bill Cox <bill@billrocks.org> This program can be distributed under the terms of the GNU Library GPL. See the file COPYING.LIB.*/#include "dv.h"static char *dvPrefix;/*-------------------------------------------------------------------------------------------------- Write a function to resize a hash table.--------------------------------------------------------------------------------------------------*/static void writeHashTableResizeFunction( dvRelationship relationship){ dvClass parentClass = dvRelationshipGetParentClass(relationship); dvClass childClass = dvRelationshipGetChildClass(relationship); char *propName = utSprintf("%s%sTable", dvRelationshipGetChildLabel(relationship), dvClassGetName(childClass)); utSym propSym = utSymCreate(propName); dvProperty tablesProp = dvClassFindProperty(parentClass, propSym); utAssert(tablesProp != dvPropertyNull); dvWrtemp(dvFile, "static void add%1%2%3ToHashTable(%4%1 %1, %5%3 _%3);\n" "/*----------------------------------------------------------------------------------------\n" " Increase the size of the hash table.\n" "----------------------------------------------------------------------------------------*/\n" "static void resize%1%2%3HashTable(\n" " %4%1 %1)\n" "{\n" " %5%3 _%3;\n" " %5%3 *%3s;\n" " uint32 num%3s = %0%1GetNum%2%3Table(%1) << 1;\n" "\n" " if(num%3s == 0) {\n" " num%3s = 2;\n" " %0%1Alloc%2%3Tables(%1, 2);\n" " } else {\n" " %0%1Resize%2%3Tables(%1, num%3s);\n" " }\n" " %3s = %0%1Get%2%3Tables(%1);\n", dvPrefix, dvClassGetName(parentClass), dvRelationshipGetChildLabel(relationship), dvClassGetName(childClass), dvClassGetPrefix(parentClass), dvClassGetPrefix(childClass)); if(dvClassUndo(parentClass)) { dvWrtemp(dvFile, " utRecordArray(%0ModuleID, %5, %0%1Get%2%3Tables(%1) - %0%1s.%4, %0%1GetNum%2%3Table(%1), true);\n", dvPrefix, dvClassGetName(parentClass), dvRelationshipGetChildLabel(relationship), dvClassGetName(childClass), dvPropertyGetName(tablesProp), dvPropertyGetID(tablesProp)); } dvWrtemp(dvFile, " /* Zero out the table */\n" " while(num%0s-- != 0) {\n" " *%0s++ = %1%0Null;\n" " }\n", dvClassGetName(childClass), dvClassGetPrefix(childClass)); if(dvClassRedo(parentClass)) { dvWrtemp(dvFile, " utRecordArray(%0ModuleID, %5, %0%1Get%2%3Tables(%1) - %0%1s.%4, %0%1GetNum%2%3Table(%1), false);\n", dvPrefix, dvClassGetName(parentClass), dvRelationshipGetChildLabel(relationship), dvClassGetName(childClass), dvPropertyGetName(tablesProp), dvPropertyGetID(tablesProp)); } dvWrtemp(dvFile, " %0%1SetNum%2%3(%1, 0);\n" " %0Foreach%1%2%3(%1, _%3) {\n" " if(%0%3Get%2Sym(_%3) != utSymNull) {\n" " add%1%2%3ToHashTable(%1, _%3);\n" " }\n" " } %0EndForeach%1%2%3;\n" "}\n\n", dvPrefix, dvClassGetName(parentClass), dvRelationshipGetChildLabel(relationship), dvClassGetName(childClass));}/*-------------------------------------------------------------------------------------------------- Write a function to add an element to a hash table.--------------------------------------------------------------------------------------------------*/static void writeHashTableAddFunction( dvRelationship relationship){ dvClass parentClass = dvRelationshipGetParentClass(relationship); dvClass childClass = dvRelationshipGetChildClass(relationship); dvWrtemp(dvFile, "/*----------------------------------------------------------------------------------------\n" " Add an %2 to the %1. If the table is near full, build a new one twice\n" " as big, delete the old one, and return the new one.\n" "----------------------------------------------------------------------------------------*/\n" "static void add%1%2%3ToHashTable(\n" " %4%1 %1,\n" " %5%3 _%3)\n" "{\n" " %5%3 next%3;\n" " uint32 index;\n" "\n" " if(%0%1GetNum%2%3(%1) >= %0%1GetNum%2%3Table(%1)) {\n" " resize%1%2%3HashTable(%1);\n" /* This adds _%3 to the hash table */ " return;\n" " }\n" " index = (%0%1GetNum%2%3Table(%1) - 1) & utSymGetHashValue(%0%3Get%2Sym(_%3));\n" " next%3 = %0%1Geti%2%3Table(%1, index);\n" " %0%3SetNextTable%1%2%3(_%3, next%3);\n" " %0%1Seti%2%3Table(%1, index, _%3);\n" " %0%1SetNum%2%3(%1, %0%1GetNum%2%3(%1) + 1);\n" "}\n\n", dvPrefix, dvClassGetName(parentClass), dvRelationshipGetChildLabel(relationship), dvClassGetName(childClass), dvClassGetPrefix(parentClass), dvClassGetPrefix(childClass));}/*-------------------------------------------------------------------------------------------------- Write a function to remove an element from a hash table.--------------------------------------------------------------------------------------------------*/static void writeHashTableRemoveFunction( dvRelationship relationship){ dvClass parentClass = dvRelationshipGetParentClass(relationship); dvClass childClass = dvRelationshipGetChildClass(relationship); dvWrtemp(dvFile, "/*----------------------------------------------------------------------------------------\n" " Remove the %2%3 from the hash table.\n" "----------------------------------------------------------------------------------------*/\n" "static void remove%1%2%3FromHashTable(\n" " %4%1 %1,\n" " %5%3 _%3)\n" "{\n" " uint32 index = (%0%1GetNum%2%3Table(%1) - 1) & utSymGetHashValue(%0%3Get%2Sym(_%3));\n" " %5%3 prev%3, next%3;\n" " \n" " next%3 = %0%1Geti%2%3Table(%1, index);\n" " if(next%3 == _%3) {\n" " %0%1Seti%2%3Table(%1, index, %0%3GetNextTable%1%2%3(next%3));\n" " } else {\n" " do {\n" " prev%3 = next%3;\n" " next%3 = %0%3GetNextTable%1%2%3(next%3);\n" " } while(next%3 != _%3);\n" " %0%3SetNextTable%1%2%3(prev%3, %0%3GetNextTable%1%2%3(_%3));\n" " }\n" " %0%1SetNum%2%3(%1, %0%1GetNum%2%3(%1) - 1);\n" " %0%3SetNextTable%1%2%3(_%3, %5%3Null);\n" "}\n\n", dvPrefix, dvClassGetName(parentClass), dvRelationshipGetChildLabel(relationship), dvClassGetName(childClass), dvClassGetPrefix(parentClass), dvClassGetPrefix(childClass));}/*-------------------------------------------------------------------------------------------------- Write a query by symbol lookup function for a hash-table relationship.--------------------------------------------------------------------------------------------------*/static void writeFindFunction( dvRelationship relationship){ dvClass parentClass = dvRelationshipGetParentClass(relationship); dvClass childClass = dvRelationshipGetChildClass(relationship); dvWrtemp(dvFile, "/*----------------------------------------------------------------------------------------\n" " Find the %2%3 from the %1 and its name.\n" "----------------------------------------------------------------------------------------*/\n" "%5%3 %0%1Find%2%3(\n" " %4%1 %1,\n" " utSym sym)\n" "{\n" " uint32 mask = %0%1GetNum%2%3Table(%1) - 1;\n" " %0%2%3 _%3;\n" "\n" " if(mask + 1 != 0) {\n" " _%3 = %0%1Geti%2%3Table(%1, utSymGetHashValue(sym) & mask);\n" " while(_%3 != %5%3Null) {\n" " if(%0%2%3GetSym(_%3) == sym) {\n" " return _%3;\n" " }\n" " _%3 = %0%2%3GetNextTable%1%2%3(_%3);\n" " }\n" " }\n" " return %5%3Null;\n" "}\n\n", dvPrefix, dvClassGetName(parentClass), dvRelationshipGetChildLabel(relationship), dvClassGetName(childClass), dvClassGetPrefix(parentClass), dvClassGetPrefix(childClass));}/*-------------------------------------------------------------------------------------------------- Write a function to rename the object.--------------------------------------------------------------------------------------------------*/static void writeRenameFunction( dvRelationship relationship){ dvClass parentClass = dvRelationshipGetParentClass(relationship); dvClass childClass = dvRelationshipGetChildClass(relationship); dvWrtemp(dvFile, "/*----------------------------------------------------------------------------------------\n" " Find the %2%3 from the %1 and its name.\n" "----------------------------------------------------------------------------------------*/\n" "void %0%1Rename%2%3(\n" " %4%1 %1,\n" " %5%3 _%3,\n" " utSym sym)\n" "{\n" " if(%0%3Get%2Sym(_%3) != utSymNull) {\n" " remove%1%2%3FromHashTable(%1, _%3);\n" " }\n" " %0%3Set%2Sym(_%3, sym);\n" " if(sym != utSymNull) {\n" " add%1%2%3ToHashTable(%1, _%3);\n" " }\n" "}\n\n", dvPrefix, dvClassGetName(parentClass), dvRelationshipGetChildLabel(relationship), dvClassGetName(childClass), dvClassGetPrefix(parentClass), dvClassGetPrefix(childClass), dvRelationshipGetParentLabel(relationship));}/*-------------------------------------------------------------------------------------------------- Write an add relationship routine for theClass.--------------------------------------------------------------------------------------------------*/static void writeClassInsertFunction( dvClass theClass, dvRelationship relationship){ char *parentName = dvClassGetName(theClass); dvClass childClass = dvRelationshipGetChildClass(relationship); char *childName = dvClassGetName(childClass); dvRelationshipType type = dvRelationshipGetType(relationship); dvWrtemp(dvFile, "/*----------------------------------------------------------------------------------------\n" " Add the %2%3 to the head of the list on the %1.\n" "----------------------------------------------------------------------------------------*/\n" "void %0%1Insert%2%3(\n" " %4%1 %1,\n" " %5%3 _%3)\n" "{\n" "#if defined(DD_DEBUG)\n" " if(%1 == %4%1Null) {\n" " utExit(\"Non-existent %1\");\n" " }\n" " if(_%3 == %5%3Null) {\n" " utExit(\"Non-existent %3\");\n" " }\n", dvPrefix, parentName, dvRelationshipGetChildLabel(relationship), childName, dvClassGetPrefix(theClass), dvClassGetPrefix(childClass)); if(dvRelationshipAccessParent(relationship)) { dvWrtemp(dvFile, " if(%0%2Get%4%1(_%2) != %3%1Null) {\n" " utExit(\"Attempting to add %3 to %1 twice\");\n" " }\n", dvPrefix, parentName, childName, dvClassGetPrefix(theClass), dvRelationshipGetParentLabel(relationship)); } dvWrtemp(dvFile, "#endif\n"); if(type == REL_DOUBLY_LINKED || type == REL_HASHED) { dvWrtemp(dvFile, " %0%3SetNext%1%2%3(_%3, %0%1GetFirst%2%3(%1));\n" " if(%0%1GetFirst%2%3(%1) != %5%3Null) {\n" " %0%3SetPrev%1%2%3(%0%1GetFirst%2%3(%1), _%3);\n" " }\n" " %0%1SetFirst%2%3(%1, _%3);\n" " %0%3SetPrev%1%2%3(_%3, %5%3Null);\n" " if(%0%1GetLast%2%3(%1) == %5%3Null) {\n" " %0%1SetLast%2%3(%1, _%3);\n" " }\n", dvPrefix, parentName, dvRelationshipGetChildLabel(relationship), childName, dvClassGetPrefix(theClass), dvClassGetPrefix(childClass)); } else { dvWrtemp(dvFile, " %0%3SetNext%1%2%3(_%3, %0%1GetFirst%2%3(%1));\n" " %0%1SetFirst%2%3(%1, _%3);\n", dvPrefix, parentName, dvRelationshipGetChildLabel(relationship), childName, dvClassGetPrefix(theClass), dvClassGetPrefix(childClass)); if(type == REL_TAIL_LINKED) { dvWrtemp(dvFile, " if(%0%1GetLast%2%3(%1) == %5%3Null) {\n" " %0%1SetLast%2%3(%1, _%3);\n" " }\n", dvPrefix, parentName, dvRelationshipGetChildLabel(relationship), childName, dvClassGetPrefix(theClass), dvClassGetPrefix(childClass)); } } if(dvRelationshipAccessParent(relationship)) { dvWrtemp(dvFile, " %0%3Set%2%1(_%3, %1);\n", dvPrefix, parentName, dvRelationshipGetParentLabel(relationship), childName);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -