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

1.1.2.1   misho       1: /*************************************************************************
                      2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
                      3: *  by Michael Pounov <misho@openbsd-bg.org>
                      4: *
                      5: * $Author: misho $
1.1.2.2 ! misho       6: * $Id: tools.c,v 1.1.2.1 2010/03/22 18:25:07 misho Exp $
1.1.2.1   misho       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: }
1.1.2.2 ! misho      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);
        !           114:        if (!str) {
        !           115:                LOGERR;
        !           116:                return NULL;
        !           117:        } else
        !           118:                memset(str, 0, lineLen * 2);
        !           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: }

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