Annotation of libaitio/src/tools.c, revision 1.2

1.2     ! misho       1: /*************************************************************************
        !             2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
        !             3: *  by Michael Pounov <misho@openbsd-bg.org>
        !             4: *
        !             5: * $Author: misho $
        !             6: * $Id: tools.c,v 1.1.2.3 2010/04/05 08:51:34 misho Exp $
        !             7: *
        !             8: *************************************************************************/
        !             9: #include "global.h"
        !            10: #include "aitio.h"
        !            11: 
        !            12: 
        !            13: /*
        !            14:  * io_LTrimStr() Remove left whitespaces from text string
        !            15:  * @psLine = Text string
        !            16:  * return: 0 nothing to do; !=0 Removed bytes
        !            17: */
        !            18: inline int io_LTrimStr(u_char *psLine)
        !            19: {
        !            20:        int pos = 0;
        !            21:        u_char *s;
        !            22: 
        !            23:        if (!psLine || !*psLine)
        !            24:                return 0;
        !            25: 
        !            26:        for (s = psLine; isspace(*s); s++);
        !            27:        pos = s - psLine;
        !            28: 
        !            29:        memmove(psLine, s, (strlen((char*) psLine) - pos) + 1);
        !            30:        return pos;
        !            31: }
        !            32: 
        !            33: /*
        !            34:  * io_RTrimStr() Remove right whitespaces from text string
        !            35:  * @psLine = Text string
        !            36:  * return: 0 nothing to do; !=0 Removed bytes
        !            37: */
        !            38: inline int io_RTrimStr(u_char *psLine)
        !            39: {
        !            40:        u_char *t, *pos;
        !            41: 
        !            42:        if (!psLine || !*psLine)
        !            43:                return 0;
        !            44: 
        !            45:        pos = psLine + strlen((char*) psLine);
        !            46:        for (t = pos - 1; t > psLine && isspace(*t); t--);
        !            47:        *++t = 0;
        !            48: 
        !            49:        return pos - t;
        !            50: }
        !            51: 
        !            52: /*
        !            53:  * io_TrimStr() Remove left and right whitespaces from text string
        !            54:  * @psLine = Text string
        !            55:  * return: 0 nothing to do; !=0 Removed bytes
        !            56: */
        !            57: inline int io_TrimStr(u_char *psLine)
        !            58: {
        !            59:        int ret = 0;
        !            60: 
        !            61:        ret = io_LTrimStr(psLine);
        !            62:        ret += io_RTrimStr(psLine);
        !            63: 
        !            64:        return ret;
        !            65: }
        !            66: 
        !            67: /*
        !            68:  * io_UnquotStr() Remove quots from input text string 
        !            69:  * @psLine = Text string
        !            70:  * return: 0 nothing to do; 1 successful unquoted string
        !            71: */
        !            72: inline int io_UnquotStr(u_char *psLine)
        !            73: {
        !            74:        char *pos, *str = NULL;
        !            75:        int flg;
        !            76: 
        !            77:        if (!psLine)
        !            78:                return 0;
        !            79: 
        !            80:        switch (*psLine) {
        !            81:                case '`':
        !            82:                case '"':
        !            83:                case '\'':
        !            84:                        str = strdup((char*) psLine + 1);
        !            85:                        for (pos = str, flg = 0; *pos; flg = ('\\' == *pos), pos++) {
        !            86:                                if (!flg && *pos == *psLine) {
        !            87:                                        *pos = 0;
        !            88:                                        strlcpy((char*) psLine, str, strlen((char*) psLine) + 1);
        !            89:                                        break;
        !            90:                                }
        !            91:                        }
        !            92:                        free(str);
        !            93:                        return 1;
        !            94:        }
        !            95: 
        !            96:        return 0;
        !            97: }
        !            98: 
        !            99: /*
        !           100:  * io_Char2Hex() Convert from Char string to Hex string
        !           101:  * @psLine = Text string
        !           102:  * @lineLen = Length of Text string
        !           103:  * return: NULL nothing to do or error; !=0 Allocated new converted string(must be free)
        !           104: */
        !           105: inline char *io_Char2Hex(u_char *psLine, int lineLen)
        !           106: {
        !           107:        register int i;
        !           108:        char szWork[3], *str;
        !           109: 
        !           110:        if (!psLine || !*psLine || !lineLen)
        !           111:                return NULL;
        !           112: 
        !           113:        str = malloc(lineLen * 2 + 1);
        !           114:        if (!str) {
        !           115:                LOGERR;
        !           116:                return NULL;
        !           117:        } else
        !           118:                memset(str, 0, lineLen * 2 + 1);
        !           119: 
        !           120:        for (i = 0; i < lineLen && psLine[i]; i++) {
        !           121:                memset(szWork, 0, 3);
        !           122:                snprintf(szWork, 3, "%02X", (u_char) psLine[i]);
        !           123:                strncat(str, szWork, 2);
        !           124:        }
        !           125: 
        !           126:        return str;
        !           127: }
        !           128: 
        !           129: 
        !           130: /*
        !           131:  * io_Hex2Char() Convert from Hex string to Char string
        !           132:  * @psLine = Text string
        !           133:  * @lineLen = Length of Text string
        !           134:  * return: NULL nothing to do or error; !=0 Allocated new converted string(must be free)
        !           135: */
        !           136: inline char *io_Hex2Char(u_char *psLine, int lineLen)
        !           137: {
        !           138:        register int i;
        !           139:        char szWork[3], *str;
        !           140: 
        !           141:        if (!psLine || !*psLine || !lineLen)
        !           142:                return NULL;
        !           143: 
        !           144:        str = malloc(lineLen / 2 + 1);
        !           145:        if (!str) {
        !           146:                LOGERR;
        !           147:                return NULL;
        !           148:        } else
        !           149:                memset(str, 0, lineLen / 2 + 1);
        !           150: 
        !           151:        for (i = 0; i < lineLen && psLine[i * 2]; i++) {
        !           152:                strlcpy(szWork, (char*) &psLine[i * 2], 3);
        !           153:                str[i] = (char) strtol(szWork, NULL, 16);
        !           154:        }
        !           155: 
        !           156:        return str;
        !           157: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>