📄 util.c
字号:
utSetLogFile(fileName); file = fopen(fileName, "w"); if(file != NULL) { fclose(file); }}/*-------------------------------------------------------------------------------------------------- Just return the logi file name.--------------------------------------------------------------------------------------------------*/char *utGetLogFileName(void){ return utLogFileName;}/*-------------------------------------------------------------------------------------------------- Set the name of the logging file without resetting it.--------------------------------------------------------------------------------------------------*/void utSetLogFile( char *fileName){ if(utLogFileName != NULL) { utFree(utLogFileName); } utLogFileName = utNewA(char, strlen(fileName) + 1); strcpy(utLogFileName, fileName);}/*-------------------------------------------------------------------------------------------------- Set the name of the configuration file directory.--------------------------------------------------------------------------------------------------*/void utSetConfigDirectory( char *dirName){ if(utConfigDirectory != NULL) { utFree(utConfigDirectory); } utConfigDirectory = utNewA(char, strlen(dirName) + 1); strcpy(utConfigDirectory, dirName);}/*-------------------------------------------------------------------------------------------------- Set the name of the executable file directory.--------------------------------------------------------------------------------------------------*/void utSetExeDirectory( char *dirName){ if(utExeDirectory != NULL) { utFree(utExeDirectory); } utExeDirectory = utNewA(char, strlen(dirName) + 1); strcpy(utExeDirectory, dirName);}/*-------------------------------------------------------------------------------------------------- Return the exec directory.--------------------------------------------------------------------------------------------------*/char *utGetExeDirectory(void){ return utExeDirectory;}/*-------------------------------------------------------------------------------------------------- Return the config directory.--------------------------------------------------------------------------------------------------*/char *utGetConfigDirectory(void){ return utConfigDirectory;}/*-------------------------------------------------------------------------------------------------- Set the complete path and name of the executable file (argv[0])--------------------------------------------------------------------------------------------------*/void utSetExeFullPath( char *fullName){ if(utExeFullPath != NULL) { utFree(utExeFullPath); } utExeFullPath = utNewA(char, strlen(fullName) + 1); strcpy(utExeFullPath, fullName);}/*-------------------------------------------------------------------------------------------------- Return the full path of the executable file.--------------------------------------------------------------------------------------------------*/char *utGetExeFullPath(void){ return utExeFullPath;}/*-------------------------------------------------------------------------------------------------- Just set the version variable.--------------------------------------------------------------------------------------------------*/void utSetVersion( char *version){ if(utVersion != NULL) { utFree(utVersion); } utVersion = utNewA(char, strlen(version) + 1); strcpy(utVersion, version);}/*-------------------------------------------------------------------------------------------------- Return the version variable.--------------------------------------------------------------------------------------------------*/char *utGetVersion(void){ return utVersion;}/*-------------------------------------------------------------------------------------------------- Initialize generic buffer memory.--------------------------------------------------------------------------------------------------*/static void initBuffers(void){ uint16 xBuffer; utNextBuffer = 0; for(xBuffer = 0; xBuffer < UT_MAX_BUFFERS; xBuffer++) { utBufferSizes[xBuffer] = 42; utBuffers[xBuffer] = (char *)calloc(utBufferSizes[xBuffer], sizeof(char)); }}/*-------------------------------------------------------------------------------------------------- Free generic buffer memory.--------------------------------------------------------------------------------------------------*/static void freeBuffers(void){ uint16 xBuffer; utNextBuffer = 0; for(xBuffer = 0; xBuffer < UT_MAX_BUFFERS; xBuffer++) { free(utBuffers[xBuffer]); }}/*-------------------------------------------------------------------------------------------------- Free memory used by the utility module.--------------------------------------------------------------------------------------------------*/void utStop( bool reportTimeAndMemory){ if(utInitialized()) { if(utSetjmpDepth > 0 && utSetjmpDepth < UT_MAX_SETJMP_DEPTH) { utWarning("utClose: utSetjmpDepth != 0 (file %s, line %u)", utSetjmpFile[utSetjmpDepth - 1], utSetjmpLine[utSetjmpDepth - 1]); } else if(utSetjmpDepth != 0) { utWarning("utClose: utSetjmpDepth has an invalid value"); /* prevents crashes */ } if(utTimerDepth != 1) { utWarning("utClose: timer started, but never stopped"); } else if(reportTimeAndMemory) { utStopTimer(0, "Process completed in"); } utDatabaseManagerStop(); utDatabaseStop(); utFreePersistenceObjects(); utInitialized() = false; if(utLogFileName != NULL) { utFree(utLogFileName); utLogFileName = NULL; } if(utConfigDirectory != NULL) { utFree(utConfigDirectory); utConfigDirectory = NULL; } if(utExeDirectory != NULL) { utFree(utExeDirectory); utExeDirectory = NULL; } if(utExeFullPath != NULL) { utFree(utExeFullPath); utExeFullPath = NULL; } if(utVersion != NULL) { utFree(utVersion); utVersion = NULL; } utMemStop(reportTimeAndMemory); freeBuffers(); }}/*-------------------------------------------------------------------------------------------------- Initialize symbol table entries to utSymNull.--------------------------------------------------------------------------------------------------*/void utInitSymTable(void){ uint32 xSym; utTheSymtab = utSymtabAlloc(); utSymtabAllocTables(utTheSymtab, 2); for(xSym = 0; xSym < 2; xSym++) { utSymtabSetiTable(utTheSymtab, xSym, utSymNull); }}/*-------------------------------------------------------------------------------------------------- Initialize local memory.--------------------------------------------------------------------------------------------------*/void utStart(void){ if(!utInitialized()) { utMemStart(); utUserErrProc = 0; utUserExitProc = 0; utInitialized() = true; utSetjmpDepth = 0; utTimerDepth = 0; initBuffers(); utStartTimer(NULL); utInitSeed(4357); utAllocPersistenceObjects(); utDatabaseStart(); utInitSymTable(); utDatabaseManagerStart(); }}/*-------------------------------------------------------------------------------------------------- Just like mtRealloc, but pass file name and line number.--------------------------------------------------------------------------------------------------*/void *utReallocTrace( void *memPtr, size_t num, size_t size, char *fileName, uint32 line){ utMemRef mem; size_t newSize = (num + 1)*size; if(memPtr == NULL) { return utMallocTrace(num, size, fileName, line); } mem = utqMemPtr(memPtr); utMemCheckTrace(fileName, line); if(!uttMemExists(mem)) { utExit("utRealloc: Bad memory pointer in %s, line %u", fileName, line); } utdMem(mem, true, fileName, line); memPtr = realloc(memPtr, (num + 1)*size); if(memPtr != NULL) { utBuildMem(memPtr, newSize, true, fileName, line); } else { utLogMessage("utRealloc: unable to allocate memory %lu. Total used: %lu", num*(size+1), utUsedMem); } return memPtr;}/*-------------------------------------------------------------------------------------------------- Just like mtMalloc, but pass file name and line number.--------------------------------------------------------------------------------------------------*/void *utMallocTrace( size_t sStruct, size_t size, char *fileName, uint32 line){ size_t sByte = sStruct*size; void *memPtr = malloc(sByte + 1); utMemCheckTrace(fileName, line); if(memPtr != NULL) { utBuildMem(memPtr, sByte + 1, true, fileName, line); } else { utLogMessage("utRealloc: unable to allocate memory %lu. Total used: %lu", sByte, utUsedMem); } return memPtr;}/*-------------------------------------------------------------------------------------------------- Just like calloc, but pass file name and line number.--------------------------------------------------------------------------------------------------*/void *utCallocTrace( size_t sStruct, size_t size, char *fileName, uint32 line){ size_t sByte = sStruct*size; void *memPtr; memPtr = utMallocTrace(sStruct, size, fileName, line); if(memPtr) { memset((void *)memPtr, 0, sByte); } return memPtr;}/*-------------------------------------------------------------------------------------------------- Just like mtFree, but pass file name and line number.--------------------------------------------------------------------------------------------------*/void utFreeTrace( void *memPtr, char *fileName, uint32 line){ utMemRef mem = utqMemPtr(memPtr); utMemCheckTrace(fileName, line); if(!uttMemExists(mem)) { utExit("utFree: Bad memory pointer in %s, line %u", fileName, line); } utdMem(mem, true, fileName, line); free(memPtr);}/*-------------------------------------------------------------------------------------------------- Symbol table support.--------------------------------------------------------------------------------------------------*//*-------------------------------------------------------------------------------------------------- Just find a hash value for the symbol name.--------------------------------------------------------------------------------------------------*/uint32 hashSymName( char *name){ uint32 hashValue = 0; do { hashValue = (hashValue ^ *name) * 1103515245 + 12345; } while(*name++); return hashValue;}/*-------------------------------------------------------------------------------------------------- Find a symbol in the symbol table.--------------------------------------------------------------------------------------------------*/static utSym symtabFindSym( char *name, uint32 hashValue){ uint32 index = (utSymtabGetNumTable(utTheSymtab) - 1) & hashValue; utSym sym = utSymtabGetiTable(utTheSymtab, index); while(sym != utSymNull) { if(!strcmp(name, utSymGetName(sym))) { return sym; } sym = utSymGetNext(sym); } return utSymNull;}/*-------------------------------------------------------------------------------------------------- Build a new symbol, or return an old one with the same name.--------------------------------------------------------------------------------------------------*/static void addSymTableSym( utSym sym){ uint32 hashValue = utSymGetHashValue(sym); uint32 index = (utSymtabGetNumTable(utTheSymtab) - 1) & hashValue; utSym nextSym = utSymtabGetiTable(utTheSymtab, index); utSymSetNext(sym, nextSym); utSymtabSetiTable(utTheSymtab, index, sym); utSymtabSetNumSym(utTheSymtab, utSymtabGetNumSym(utTheSymtab) + 1);}/*-------------------------------------------------------------------------------------------------- Resize the symbol table. Make it twice as big.--------------------------------------------------------------------------------------------------*/static void resizeSymtab(void){ utSym sym; uint32 numSyms = utSymtabGetNumTable(utTheSymtab) << 1; uint32 xSym; utSymtabResizeTables(utTheSymtab, numSyms); for(xSym = 0; xSym < numSyms; xSym++) { utSymtabSetiTable(utTheSymtab, xSym, utSymNull); } utSymtabSetNumSym(utTheSymtab, 0); utForeachSym(sym) { addSymTableSym(sym); } utEndForeachSym;}/*-------------------------------------------------------------------------------------------------- Build a new symbol, or return an old one with the same name.--------------------------------------------------------------------------------------------------*/utSym utSymCreate( char *name){ uint32 hashValue = hashSymName(name); utSym sym = symtabFindSym(name, hashValue); uint32 length; if(sym != utSymNull) { return sym; } if(utSymtabGetNumSym(utTheSymtab) == utSymtabGetNumTable(utTheSymtab)) { resizeSymtab(); } sym = utSymAlloc(); length = strlen(name) + 1; utSymAllocNames(sym, length); utSymSetName(sym, name); utSymSetHashValue(sym, hashSymName(name)); addSymTableSym(sym); return sym;}/*-------------------------------------------------------------------------------------------------- Build a new symbol, and add it to the symbol table.--------------------------------------------------------------------------------------------------*/utSym utSymCreateFormatted(char *format, ...){ char *buff; va_list ap; va_start(ap, format); buff = utVsprintf(format, ap); va_end(ap); return utSymCreate(buff);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -