/** * Figure out where to insert an entry in a list of twins. */ LOCAL tDefEntry* findPlace(char* name, char const * pzIndex) { tDefEntry* pE = getEntry(); pE->pzDefName = name; if (pzIndex == NULL) pE->index = NO_INDEX; else if (IS_DEC_DIGIT_CHAR(*pzIndex) || (*pzIndex == '-')) pE->index = strtol(pzIndex, NULL, 0); else { pzIndex = getDefine(pzIndex, AG_TRUE); if (pzIndex != NULL) pE->index = strtol(pzIndex, NULL, 0); else pE->index = NO_INDEX; } strtransform(pE->pzDefName, pE->pzDefName); pE->valType = VALTYP_UNKNOWN; pE->pzSrcFile = (char*)pCurCtx->pzCtxFname; pE->srcLineNum = pCurCtx->lineNo; return (pCurrentEntry = insertDef(pE)); }
/* * assignIndex */ static char* assignIndex(char* pzOut, char* pzDef) { char* pzMatch; size_t len = strlen(pzDef); int idx; /* * Make the source text all lower case and map * '-', '^' and '_' characters to '_'. */ strtransform(pzDef, pzDef); /* * IF there is already an entry, * THEN put the index into the output. */ pzMatch = strstr(pzIndexText, pzDef); if (pzMatch != NULL) { pzMatch += len; while (isspace(*pzMatch)) pzMatch++; while ((*pzOut++ = *pzMatch++) != ']') ; return pzOut; } /* * We have a new entry. Make sure we have room for it * in our in-memory string */ if (((pzEndIndex - pzIndexText) + len + 64 ) > indexAlloc) { char* pz; indexAlloc += 0x1FFF; indexAlloc &= ~0x0FFF; pz = (char*)realloc((void*)pzIndexText, indexAlloc); if (pz == NULL) { fputs("Realloc of index text failed\n", stderr); exit(EXIT_FAILURE); } /* * IF the allocation moved, * THEN adjust all our pointers. */ if (pz != pzIndexText) { pzIndexEOF = pz + (pzIndexEOF - pzIndexText); pzEndIndex = pz + (pzEndIndex - pzIndexText); pzIndexText = pz; } } /* * IF there are no data in our text database, * THEN use default index. */ if (pzEndIndex == pzIndexText) idx = OPT_VALUE_FIRST_INDEX; else do { char* pz = strrchr(pzDef, ' '); *pz = NUL; len = strlen(pzDef); /* * Find the last entry for the current category of entries */ pzMatch = strstr(pzIndexText, pzDef); if (pzMatch == NULL) { /* * No entries for this category. Use default index. */ idx = OPT_VALUE_FIRST_INDEX; *pz = ' '; break; } for (;;) { char* pzn = strstr(pzMatch + len, pzDef); if (pzn == NULL) break; pzMatch = pzn; } /* * Skip forward to the '[' character and convert the * number that follows to a long. */ *pz = ' '; pzMatch = strchr(pzMatch + len, '['); idx = strtol(pzMatch+1, (char**)NULL, 0)+1; } while (0); /* * Add the new entry to our text database and * place a copy of the value into our output. */ pzEndIndex += sprintf(pzEndIndex, "%-40s [%d]\n", pzDef, idx); pzOut += sprintf(pzOut, "[%d]", idx); return pzOut; }