Annotation of libaitcfg/src/tools.c, revision 1.4

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 $
1.4     ! misho       6: * $Id: tools.c,v 1.2.4.4 2010/03/22 15:05:14 misho Exp $
1.2       misho       7: *
                      8: *************************************************************************/
1.1       misho       9: #include "global.h"
                     10: #include "aitcfg.h"
                     11: #include "tools.h"
                     12: 
                     13: #pragma GCC visibility push(hidden)
                     14: 
                     15: // ltrim() Left trim whitespaces
                     16: inline int ltrim(u_char *psLine)
                     17: {
                     18:        int pos = 0;
                     19: 
                     20:        if (!psLine)
                     21:                return 0;
                     22:        pos = strspn((char*) psLine, " \t\r\n");
                     23:        if (!pos)
                     24:                return 0;
                     25: 
1.4     ! misho      26:        memmove(psLine, psLine + pos, (strlen((char*) psLine) - pos) + 1);
1.1       misho      27:        return pos;
                     28: }
                     29: 
                     30: // rtrim() Right trim whitespaces
                     31: inline int rtrim(u_char *psLine)
                     32: {
1.4     ! misho      33:        register int i;
        !            34:        int pos = 0;
1.1       misho      35: 
                     36:        if (!psLine)
                     37:                return 0;
1.4     ! misho      38:        for (i = strlen((char*) psLine) - 1; i; i--) {
        !            39:                switch (psLine[i]) {
1.1       misho      40:                        case ' ':
                     41:                        case '\t':
                     42:                        case '\r':
                     43:                        case '\n':
1.4     ! misho      44:                                psLine[i] = 0;
1.1       misho      45:                                pos++;
                     46:                                continue;
                     47:                }
                     48:                break;
                     49:        }
                     50: 
                     51:        return pos;
                     52: }
                     53: 
                     54: #pragma GCC visibility pop
                     55: 
                     56: // trim() Triming all whitespaces both left/right
                     57: inline int trim(u_char *psLine)
                     58: {
                     59:        int ret = 0;
                     60: 
                     61:        ret = ltrim(psLine);
                     62:        ret += rtrim(psLine);
                     63: 
                     64:        return ret;
                     65: }
1.4     ! misho      66: 
        !            67: // unquot() Unquoted string
        !            68: inline int unquot(u_char *psLine)
        !            69: {
        !            70:        char *pos, *str = NULL;
        !            71:        int flg;
        !            72: 
        !            73:        if (!psLine)
        !            74:                return 0;
        !            75: 
        !            76:        switch (*psLine) {
        !            77:                case '`':
        !            78:                case '"':
        !            79:                case '\'':
        !            80:                        str = strdup((char*) psLine + 1);
        !            81:                        for (pos = str, flg = 0; *pos; flg = ('\\' == *pos), pos++) {
        !            82:                                if (!flg && *pos == *psLine) {
        !            83:                                        *pos = 0;
        !            84:                                        strlcpy((char*) psLine, str, strlen((char*) psLine) + 1);
        !            85:                                        break;
        !            86:                                }
        !            87:                        }
        !            88:                        free(str);
        !            89:                        return 1;
        !            90:        }
        !            91: 
        !            92:        return 0;
        !            93: }

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