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

📄 strtools.c

📁 Open DMT Client C Source code
💻 C
📖 第 1 页 / 共 2 页
字号:
            }        }    }    return s;}// ----------------------------------------------------------------------------/* return boolean value of string */utBool strParseBoolean(const char *s, utBool dft){        /* return default if string is null */    if (!s || !*s) {        return dft;    }    /* skip prefixing spaces and return default if string is empty */    while (*s && isspace(*s)) { s++; }    if (!*s) {        return dft;    }    /* parse */    // Warning: This will also attempt to parse 'Hex' values as well.    //   "0x01" - will return 'true'    //   "0x00" - will return 'false'    //   "0xAA" - will return 'true'    //   "0xZZ" - will return the default ('0xZZ' is an invalid hex value)    if (dft) {        // default is true, check for false        if (isdigit(*s)) {            return strParseUInt32(s,1L)? utTrue : utFalse;        } else {            return strStartsWithIgnoreCase(s,"false")? utFalse : utTrue;        }    } else {        // default is false, check for true        if (isdigit(*s)) {            return strParseUInt32(s,0L)? utTrue : utFalse;        } else {            return strStartsWithIgnoreCase(s,"true")? utTrue : utFalse;        }    }}// ----------------------------------------------------------------------------/* return signed 32-bit value of string */Int32 strParseInt32(const char *s, Int32 dft){        /* return default if string is null */    if (!s || !*s) {        return dft;    }    /* skip prefixing spaces and return default if string is empty */    while (*s && isspace(*s)) { s++; }    if (!*s) {        return dft;    }    /* Hex? */    if ((s[0] == '0') && ((s[1] == 'x') || (s[1] == 'X'))) {        return (Int32)strParseHex32(s+2, (UInt32)dft);    }        /* Boolean? (test for 'T'/'F' character first to exit quickly) */    if (((*s == 't') || (*s == 'T')) && strStartsWithIgnoreCase(s,"true" )) {        // Thus, "config.value=true" will set the property value to '1'        return 1L;    } else    if (((*s == 'f') || (*s == 'F')) && strStartsWithIgnoreCase(s,"false")) {        // Thus, "config.value=false" will set the property value to '0'        return 0L;    }        /* Integer */    if (*s == '+') { s++; }    if (isdigit(*s) || ((*s == '-') && isdigit(*(s+1)))) {        return (Int32)atol(s);    }        /* unparsable default */    return dft;}/* return unsigned 32-bit value of string */UInt32 strParseUInt32(const char *s, UInt32 dft){    return (UInt32)strParseInt32(s, (Int32)dft);}/* return unsigned 32-bit value of hex-encoded string */UInt32 strParseHex32(const char *s, UInt32 dft){        /* return default if string is null */    if (!s || !*s) {        return dft;    }        /* skip prefixing spaces and return default if string is empty */    while (*s && isspace(*s)) { s++; }    if ((s[0] == '0') && ((s[1] == 'x') || (s[1] == 'X'))) { s += 2; } // skip "0x"    if (!*s) {        return dft;    }        /* parse hex string */    UInt32 accum = 0L;    int i, maxNybbles = 8; // max 8 nybbles    for (i = 0; *s && (i < maxNybbles); i++, s++) {        UInt8 C = toupper(*s), N = 0;        if ((C >= '0') && (C <= '9')) {            N = C - '0';            // N == 0..9        } else        if ((C >= 'A') && (C <= 'F')) {            N = 10 + (C - 'A');     // N == 10..15        } else {            // non-hex character, return default if nothing was parsed            return (i > 0)? accum : dft;        }        accum = (accum << 4) | N; // 4 bits per nybble [N == 0..15]    }    return accum;}// ----------------------------------------------------------------------------#if defined(SUPPORT_UInt64)/* return signed 64-bit value of string */Int64 strParseInt64(const char *s, Int64 dft){        /* return default if string is null */    if (!s || !*s) {        return dft;    }    /* skip prefixing spaces and return default if string is empty */    while (*s && isspace(*s)) { s++; }    if (!*s) {        return dft;    }    /* Hex? */    if ((s[0] == '0') && ((s[1] == 'x') || (s[1] == 'X'))) {        return (Int64)strParseHex64(s+2, (UInt64)dft);    }    /* Decimal */    if (*s == '+') { s++; }    if (isdigit(*s) || ((*s == '-') && isdigit(*(s+1)))) {#if defined(TARGET_WINCE)        return (Int64)_atoi64(s);#else        return (Int64)atoll(s);#endif    }    /* unparsable default */    return dft;    }/* return unsigned 64-bit value of string */UInt64 strParseUInt64(const char *s, UInt64 dft){    return (UInt32)strParseInt64(s, (Int64)dft);}/* return unsigned 64-bit value of hex-encoded string */UInt64 strParseHex64(const char *s, UInt64 dft){        /* return default if string is null */    if (!s) {        return dft;    }        /* skip prefixing spaces and return default if string is empty */    while (*s && isspace(*s)) { s++; }    if ((s[0] == '0') && ((s[1] == 'x') || (s[1] == 'X'))) { s += 2; } // skip "0x"    if (!*s) {        return dft;    }        /* parse hex string */    UInt64 accum = (UInt64)0;    int i, maxNybbles = 16; // max 16 nybbles    for (i = 0; *s && (i < maxNybbles); i++, s++) {        UInt8 C = toupper(*s), N = 0;        if ((C >= '0') && (C <= '9')) {            N = C - '0';            // N == 0..9        } else        if ((C >= 'A') && (C <= 'F')) {            N = 10 + (C - 'A');     // N == 10..15        } else {            // non-hex character, return default if nothing was parsed            return (i > 0)? accum : dft;        }        accum = (accum << 4) | N; // 4 bits per nybble [N == 0..15]    }    return accum;}#endif// ----------------------------------------------------------------------------/* return double value of string */ double strParseDouble(const char *s, double dft){    if (s && *s) {        while (*s && isspace(*s)) { s++; }        if (*s == '+') { s++; }        if (isdigit(*s) || ((*s == '-') && isdigit(*(s+1)))) {            return (double)atof(s);        }    }    return dft;}// ----------------------------------------------------------------------------static int _hexNybble(UInt8 ch){    if ((ch >= '0') && (ch <= '9')) {        return (int)(ch - '0') & 0xF;    } else    if ((ch >= 'a') && (ch <= 'f')) {        return ((int)(ch - 'a') & 0xF) + 10;    } else    if ((ch >= 'A') && (ch <= 'F')) {        return ((int)(ch - 'A') & 0xF) + 10;    } else {        return -1;    }}/* return true if character is a hex digit */utBool strIsHexDigit(char ch){    return (_hexNybble(ch) >= 0)? utTrue : utFalse;}/* decode hex-encoded string into byte array */// returns the length of the parse hex value (or '0' if unparsable)int strParseHex(const char *hex, int hexLen, UInt8 *data, int dataLen){    // The number of valid nybbles is assumed to be even.      // If not even, the last byte will not be parsed.    if (!hex) {        return 0;    } else {        int len = (hexLen >= 0)? strLength(hex, hexLen) : strlen(hex);        const char *h = hex;        if ((len >= 2) && (*h == '0') && ((*(h+1) == 'x') || (*(h+1) == 'X'))) {            h += 2;            len -= 2;        }        if (len & 1) { len--; } // make sure length is even        if (len >= 2) {            int i;            for (i = 0; (i < len) && ((i/2) < dataLen); i += 2) {                int c1 = _hexNybble(h[i]);                if (c1 < 0) {                    // Invalid Hex char (the first of the pair)                    break; // stop parsing                }                int c2 = _hexNybble(h[i+1]);                if (c2 < 0) {                     // Invalid Hex char (the second of the pair)                    break; // stop parsing (last byte ignored                }                data[i/2] = (UInt8)(((c1 << 4) & 0xF0) | (c2 & 0x0F));            }            return i/2;        } else {            return 0;        }    }}/* encode byte array into hex-encoded string */char *strEncodeHex(char *hex, int hexLen, const UInt8 *data, int dataLen){    if (!hex || (hexLen == 0)) {        return (char*)0;    }    *hex = 0;    if ((hexLen < 0) || (hexLen >= ((dataLen * 2) + 1))) {        if (data && (dataLen > 0)) {            int i;            char *h = hex;            for (i = 0; i < dataLen; i++) {                sprintf(h, "%02X", (unsigned int)data[i] & 0xFF);                h += strlen(h);            }        }        return hex;    } else {        return (char*)0;    }}// ----------------------------------------------------------------------------/* parse (1,2,"hello","hi") in place (',' separator assummed) */// return the number of parsed fieldsint strParseArray(char *s, char *arry[], int maxSize){    return strParseArray_sep(s, arry, maxSize, ',');}/* parse (1,2,"hello","hi") in place (separator specified) */// return the number of parsed fieldsint strParseArray_sep(char *s, char *arry[], int maxSize, char separator){        /* null string specified? */    if (!s) {        return 0;    }        /* skip leading spaces */    while (*s && isspace(*s)) { s++; }    if (*s == '(') { s++; } // skip start of array (if specified)        /* read until end of string */    int size = 0;    for (;(size < maxSize) && (*s != 0);) {                /* skip white space */        while (*s && isspace(*s)) { s++; }                /* find data start */        utBool quoted = utFalse;        if (*s == '\"') {            quoted = utTrue;            s++;        }            /* look for end of string [",)] */        char *start = s, *end = s;        while (utTrue) {            if (*s == 0) {                // end of string                end = s; // already terminated (premature end of array)                break;            } else            if (*s == ')') {                // end of array ('quoted' should be false)                end = s;                *s = 0; // terminate, end of array                break;            } else            if ((*s == '\"') && quoted) {                // end of quote                end = s;                *s++ = 0; // terminate and skip quote                while (*s && isspace(*s)) { s++; } // skip spaces                if (*s == separator) {                    // ready for next element                    s++; // skip separator                } else                 if (*s == ')') {                    // end of array                    *s = 0; // terminate                }                break;            } else            if ((*s == separator) && !quoted) {                // end of element                end = s;                *s++ = 0; // terminate and skip separator for next element                break;            } else {                // advance to next character in element                s++;             }        }                /* remove ending space (only if not quoted) */        if (!quoted) {            while ((end > start) && isspace(*(end-1))) { *(--end) = 0; }        }                /* set array element */        arry[size++] = start;            }        /* return size */    return size;    }// ----------------------------------------------------------------------------

⌨️ 快捷键说明

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