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

📄 dvutil.c

📁 DataDraw is an ultra-fast persistent database for high performance programs written in C. It s so fa
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) 2006 Bill Cox * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License  * along with this program; if not, write to the Free Software  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA *//*--------------------------------------------------------------------------------------------------  Random support functions.--------------------------------------------------------------------------------------------------*/#include <ctype.h>#include "dv.h"/*--------------------------------------------------------------------------------------------------  Find the module from the prefix.--------------------------------------------------------------------------------------------------*/dvModule dvFindModuleFromPrefix(    utSym prefix){    dvModule module;    dvForeachRootModule(dvTheRoot, module) {        if(dvModuleGetPrefixSym(module) == prefix) {            return module;        }    } dvEndForeachRootModule;    return dvModuleNull;}/*--------------------------------------------------------------------------------------------------  Find out how many args are used in a template.--------------------------------------------------------------------------------------------------*/static uint32 countArgs(    char *temp){    char c;    uint32 maxArg = 0, xArg;    while (*temp) {        c = *temp++;        if (c == '%') {            c = *temp;            if (c == 'l' || c == 'u' || c == 'c') {                temp++;                c = *temp;            }            if(isdigit(c)) {                temp++;                xArg = c - '0';                if (xArg >= maxArg) {                    maxArg = xArg + 1;                }            } else if (c == '%') {                temp++;            }        }    }    return maxArg;}/*--------------------------------------------------------------------------------------------------  This code manages a simple string buffer.--------------------------------------------------------------------------------------------------*/static char *prStringBuffer;static uint32 prStringBufferSize;static uint32 prStringBufferPosition;/*--------------------------------------------------------------------------------------------------  Append a string to the string buffer.--------------------------------------------------------------------------------------------------*/static void appendString(    char *string){    uint32 length = strlen(string);    if(prStringBufferPosition + length + 1 >= prStringBufferSize) {        prStringBufferSize = ((prStringBufferPosition + length + 1)*3) >> 1;        utResizeArray(prStringBuffer, prStringBufferSize);    }    strcpy(prStringBuffer + prStringBufferPosition, string);    prStringBufferPosition += length;}/*--------------------------------------------------------------------------------------------------  Append a character to the string buffer.--------------------------------------------------------------------------------------------------*/static void appendChar(    char c){    char string[2];    string[0] = c;    string[1] = '\0';    appendString(string);}/*--------------------------------------------------------------------------------------------------  Initialize the utility module, allocating buffers.--------------------------------------------------------------------------------------------------*/void prUtilStart(void){    prStringBufferSize = 42;    prStringBuffer = utNewA(char, prStringBufferSize);    prStringBufferPosition = 0;}/*--------------------------------------------------------------------------------------------------  Free memory used by the utility module.--------------------------------------------------------------------------------------------------*/void prUtilStop(void){    utFree(prStringBuffer);}/*--------------------------------------------------------------------------------------------------  Write a template to a string.--------------------------------------------------------------------------------------------------*/static void wrtemp(   char *temp,   va_list ap){    uint32 sArg = countArgs(temp), xArg;    char *(args[10]);    char *string, *arg;    char c;    bool lowerCase = false, upperCase = false, caps = false;    prStringBufferPosition = 0;    prStringBuffer[0] = '\0';    for (xArg = 0; xArg < sArg; xArg++) {        args[xArg] = va_arg(ap, char *);    }    string = temp;    while (*string) {        c = *string++;        if (c == '%') {            c = *string;            if(c == 'l') {                lowerCase = true;                c = *++string;            } else if(c == 'u') {                upperCase = true;                c = *++string;            } else if(c == 'c') {                caps = true;                c = *++string;            }            if(isdigit(c)) {                string++;                xArg = c - '0';                if(xArg >= sArg) {                    utExit("exWrtemp: not enough args");                }                if (*args[xArg]) {                    if(lowerCase) {                        appendChar(tolower(*(args[xArg])));                        appendString((args[xArg]) + 1);                        lowerCase = false;                    } else if(upperCase) {                        appendChar(toupper(*(args[xArg])));                        appendString((args[xArg]) + 1);                        upperCase = false;                    } else if(caps) {                        arg = args[xArg];                        while(*arg) {                            appendChar(toupper(*arg));                            arg++;                        }                        caps = false;                    } else {                        appendString(args[xArg]);                    }                }            } else if (c == '%') {                string++;                appendChar('%');            }        } else {            appendChar(c);        }    }}/*--------------------------------------------------------------------------------------------------  Write a template to a string.--------------------------------------------------------------------------------------------------*/char *dvSwrtemp(    char *temp,    ...){    va_list ap;    va_start(ap, temp);    wrtemp(temp, ap);    va_end(ap);    return utCopyString(prStringBuffer);}/*--------------------------------------------------------------------------------------------------  Write a template to a string.--------------------------------------------------------------------------------------------------*/void dvWrtemp(    FILE *file,    char *temp,    ...){    va_list ap;    va_start(ap, temp);    wrtemp(temp, ap);    va_end(ap);    fputs(prStringBuffer, file);}/*--------------------------------------------------------------------------------------------------  Get the property type name.--------------------------------------------------------------------------------------------------*/char *dvPropertyGetTypeName(    dvProperty property){    dvClass theClass;    dvEnum theEnum;    dvTypedef theTypedef;    switch(dvPropertyGetType(property)) {    case PROP_INT: return utSprintf("int%u", dvPropertyGetWidth(property));    case PROP_UINT: return utSprintf("uint%u", dvPropertyGetWidth(property));    case PROP_FLOAT: return "float";    case PROP_DOUBLE: return "double";    case PROP_BIT: case PROP_BOOL:        return "uint8";    case PROP_CHAR: return "char";    case PROP_SYM: return "utSym";    case PROP_ENUM:        theEnum = dvPropertyGetEnumProp(property);        return utSprintf("%s%s", dvModuleGetPrefix(dvEnumGetModule(theEnum)),            dvEnumGetName(theEnum));    case PROP_TYPEDEF: return dvTypedefGetName(dvPropertyGetTypedefProp(property));        theTypedef = dvPropertyGetTypedefProp(property);        return utSprintf("%s%s", dvModuleGetPrefix(dvTypedefGetModule(theTypedef)),            dvTypedefGetName(theTypedef));    case PROP_POINTER:        theClass = dvPropertyGetClassProp(property);        return utSprintf("%s%s", dvClassGetPrefix(theClass), dvClassGetName(theClass));    default:        utExit("Unknown property type");    }    return NULL; /* Dummy return */}/*--------------------------------------------------------------------------------------------------  Get the property type name as a utility library field type.--------------------------------------------------------------------------------------------------*/char *dvPropertyGetFieldTypeName(    dvProperty property){    switch(dvPropertyGetType(property)) {    case PROP_INT: return "UT_INT";    case PROP_UINT: return "UT_UINT";    case PROP_FLOAT: return "UT_FLOAT";    case PROP_DOUBLE: return "UT_DOUBLE";

⌨️ 快捷键说明

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