⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util.c

📁 DataDraw is an ultra-fast persistent database for high performance programs written in C. It s so fa
💻 C
📖 第 1 页 / 共 3 页
字号:
}/*--------------------------------------------------------------------------------------------------  Find the lower-case version of the symbol, and create it if it does not exist.--------------------------------------------------------------------------------------------------*/utSym utSymGetLowerSym(    utSym sym){    return utSymCreate(utStringToLowerCase(utSymGetName(sym)));}/*--------------------------------------------------------------------------------------------------  Allocate a buffer for generic use.  There is only a queue of UT_MAX_BUFFERS which can be in use  at any time.  This is primarily useful for returning strings from subroutines.--------------------------------------------------------------------------------------------------*/void *utMakeBuffer_(    uint32 length){    void *buffer;    if(length > utBufferSizes[utNextBuffer]) {        utBufferSizes[utNextBuffer] = length + length/2;        utBuffers[utNextBuffer] = (char *)realloc(utBuffers[utNextBuffer],                utBufferSizes[utNextBuffer]*sizeof(char));    }    buffer = utBuffers[utNextBuffer];    if(++utNextBuffer == UT_MAX_BUFFERS) {        utNextBuffer = 0;    }    return buffer;}/*--------------------------------------------------------------------------------------------------  Print a formated string to the file.--------------------------------------------------------------------------------------------------*/char *utVsprintf(   char *format,   va_list ap){    char buffer[UTSTRLEN];    char *returnBuffer;    vsprintf((char *)buffer, (char *)format, ap);    returnBuffer = utMakeString(strlen(buffer) + 1);    strcpy(returnBuffer, buffer);    return returnBuffer;}/*--------------------------------------------------------------------------------------------------  Just replace the file suffix.--------------------------------------------------------------------------------------------------*/char *utReplaceSuffix(    char *originalName,    char *newSuffix){    uint32 length = strlen(originalName);    char *buffer = utMakeBuffer_(length + strlen(newSuffix) + 1);    char *endPtr;    strcpy(buffer, originalName);    endPtr = buffer + length;    while(endPtr > buffer && *endPtr != '.' && *endPtr != UTDIRSEP) {        endPtr--;    }    if(*endPtr != '.') {        endPtr = buffer + length;    }    strcpy(endPtr, newSuffix);    return buffer;}/*--------------------------------------------------------------------------------------------------  Return a temporary copy of a string.--------------------------------------------------------------------------------------------------*/char *utCopyString(    char *string){    char *buffer;    if(string == NULL) {        return NULL;    }    buffer = utMakeString(strlen(string) + 1);    strcpy(buffer, string);    return buffer;}/*--------------------------------------------------------------------------------------------------  Make a new string by concatenating the two old ones.--------------------------------------------------------------------------------------------------*/char *utCatStrings(    char *string1,    char *string2){    return utSprintf("%s%s", string1, string2);}/*--------------------------------------------------------------------------------------------------  Convert a string to upper case.--------------------------------------------------------------------------------------------------*/char *utStringToUpperCase(    char *string){    char *buffer = utCopyString(string);    char *p = buffer;        while(*p != '\0') {        *p = toupper(*p);        p++;    }    return buffer;}/*--------------------------------------------------------------------------------------------------  Convert a string to lower case.--------------------------------------------------------------------------------------------------*/char *utStringToLowerCase(    char *string){    char *buffer = utCopyString(string);    char *p = buffer;        while(*p != '\0') {        *p = tolower(*p);        p++;    }    return buffer;}/*--------------------------------------------------------------------------------------------------  Return the compile time for the executable. This is in one place only for consistancy.--------------------------------------------------------------------------------------------------*/char *utGetCompileTime(void){    return __DATE__ " " __TIME__;}/*--------------------------------------------------------------------------------------------------  Return the date and time as an ASCII string.--------------------------------------------------------------------------------------------------*/char *utGetDateAndTime(void){    time_t timeval = time(NULL);    struct tm *theTime = localtime(&timeval);    return utSprintf("%02u/%02u/%02u %02u:%02u:%02u", theTime->tm_mon + 1, theTime->tm_mday,        theTime->tm_year % 100, theTime->tm_hour, theTime->tm_min, theTime->tm_sec);}/*--------------------------------------------------------------------------------------------------  Return the base name of the path name.--------------------------------------------------------------------------------------------------*/char *utBaseName(    char *name){    char *left = strrchr(name, UTDIRSEP);    char *right = name + strlen(name);    char *buffer;    if(left == NULL) {        return utCopyString(name);    }    left++;    buffer = utMakeString(right - left + 1);    strncpy(buffer, left, right - left);    buffer[right - left] = '\0';    return buffer;}/*--------------------------------------------------------------------------------------------------  Return the suffix of the filename.--------------------------------------------------------------------------------------------------*/char *utSuffix(    char *name){    uint32 i = 0;    uint32 start = UINT32_MAX;    char c = name[i];    while(c != 0) {        i++;        if(c == '.') {            start = i;        }        c = name[i];    }    if(start == UINT32_MAX) {        return NULL;    }    return utCopyString(name + start);}/*--------------------------------------------------------------------------------------------------  Return the directory name of the path name.--------------------------------------------------------------------------------------------------*/char *utDirName(    char *name){    char *end = strrchr(name, UTDIRSEP);    char *buffer;    if(end == NULL) {        return ".";    }    buffer = utMakeString(end - name + 1);    strncpy(buffer, name, end - name);    buffer[end - name] = '\0';    return buffer;}/*--------------------------------------------------------------------------------------------------  Just determine if the file exists.--------------------------------------------------------------------------------------------------*/bool utFileExists(    char *fileName){    FILE *file;    if(fileName == NULL || *fileName == '\0') {        return false;    }    file = fopen(fileName, "r");    if(file != NULL) {        fclose(file);        return true;    }    return false;}/*--------------------------------------------------------------------------------------------------  Find a file name in the directory and return the full path if it exists.  Otherwise return NULL.--------------------------------------------------------------------------------------------------*/static char *findFileInDirectory(    char *fileName,    char *dirName){    char *name = utSprintf("%s%c%s", dirName, UTDIRSEP, fileName);    if(utAccess(name, NULL)) {        return name;    }    return NULL;}/*--------------------------------------------------------------------------------------------------  Find a file in the path that has the mode.--------------------------------------------------------------------------------------------------*/char *utFindInPath(    char *name,    char *path){    char *buf = utCopyString(path);    char *p = buf;    char *next, *fileName, *directory;    while(p != '\0') {        next = strchr(p, ':');        if(next != NULL) {            *next++ = '\0';        }        directory = utExpandEnvVariables(p);        fileName = findFileInDirectory(name, directory);        if(fileName != NULL) {            return fileName;        }        p = next;    }    return NULL;}/*--------------------------------------------------------------------------------------------------  Find the matching '}' in the string.--------------------------------------------------------------------------------------------------*/static char *findMatchingBracket(    char *string){    uint32 numBraces = 0;    char c;    utDo {        c = *string;        if(c == '{') {            numBraces++;        } else if(c == '}') {            numBraces--;        }    } utWhile(numBraces != 0) {        string++;    } utRepeat;    return string;}/*--------------------------------------------------------------------------------------------------  Find the first non alpha-numeric character.--------------------------------------------------------------------------------------------------*/static char *findFirstNonAlnumChar(    char *string){    while(isalnum(*string)) {        string++;    }    return string;}/*--------------------------------------------------------------------------------------------------  Expand the string's environment variable.--------------------------------------------------------------------------------------------------*/static char *expandString(    char *string,    char *varStart){    char *ending;    char *p;    *varStart++ = '\0';    if(*varStart == '{') {        ending = findMatchingBracket(varStart);        varStart++;        if(ending == NULL) {            utWarning("Variable %s does not have a matching '}'" , varStart);            return string;        }        *ending++ = '\0';    } else {        ending = findFirstNonAlnumChar(varStart);        if(ending == NULL) {            ending = "";        } else {            p = ending;            ending = utCopyString(ending);            *p = '\0'; /* To terminate varStart */        }    }    return utSprintf("%s%s%s", string, getenv(varStart), ending);}/*--------------------------------------------------------------------------------------------------  Expand the string to replace environment variables with their values.--------------------------------------------------------------------------------------------------*/char *utExpandEnvVariables(    char *string){    char *p;    bool changed;    string = utCopyString(string); /* To make a writable copy */    if(string == NULL) {        return NULL;    }    do {        changed = false;        p = strchr(string, '$');        if(p != NULL) {            changed = true;            string = expandString(string, p);        }    } while(changed);    return string;}/*--------------------------------------------------------------------------------------------------  Create a dynamic array.--------------------------------------------------------------------------------------------------*/utDynarray utDynarrayCreate_(    uint16 valueSize){    utDynarray dynarray = utDynarrayAlloc();    utDynarraySetSize(dynarray, valueSize);    return dynarray;}/*--------------------------------------------------------------------------------------------------  Expand the string to replace environment variables with their values.--------------------------------------------------------------------------------------------------*/void utDynarrayResize(    utDynarray dynarray,    uint32 newSize){    uint32 numValues = newSize*utDynarrayGetSize(dynarray);    if(utDynarrayGetNumValue(dynarray) == 0) {        utDynarrayAllocValues(dynarray, numValues);    } else {        utDynarrayResizeValues(dynarray, numValues);    }}/*--------------------------------------------------------------------------------------------------  Build a new symbol, and add it to the symbol table.--------------------------------------------------------------------------------------------------*/utSym utUniqueSymCreate(    char *name,    char *suffix){    utSym baseSym, sym;    char buf[UTSTRLEN];    char *tail;    uint32 count;    uint32 hashValue;    strcpy(buf, name);    tail = buf + strlen(buf) - 1;    while(isdigit(*tail)) {        tail--;    }    tail++;    strcpy(tail, suffix);    hashValue = hashSymName(buf);    baseSym = symtabFindSym(buf, hashValue);    if(baseSym == utSymNull) {        return utSymCreate(buf);    }    tail += strlen(suffix);    count = utSymNextIndex;    do {        sprintf(tail, "%u", count);        count++;        sym = symtabFindSym(buf, hashValue);    } while (sym != utSymNull);    utSymNextIndex = count;    return utSymCreate(buf);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -