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

1.2       misho       1: /*************************************************************************
1.4     ! misho       2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
        !             3: *  by Michael Pounov <misho@elwix.org>
1.2       misho       4: *
                      5: * $Author: misho $
1.4     ! misho       6: * $Id: tools.c,v 1.3.8.2 2011/04/20 22:55:42 misho Exp $
1.2       misho       7: *
1.4     ! misho       8: **************************************************************************
        !             9: The ELWIX and AITNET software is distributed under the following
        !            10: terms:
        !            11: 
        !            12: All of the documentation and software included in the ELWIX and AITNET
        !            13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
        !            14: 
        !            15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
        !            16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
        !            17: 
        !            18: Redistribution and use in source and binary forms, with or without
        !            19: modification, are permitted provided that the following conditions
        !            20: are met:
        !            21: 1. Redistributions of source code must retain the above copyright
        !            22:    notice, this list of conditions and the following disclaimer.
        !            23: 2. Redistributions in binary form must reproduce the above copyright
        !            24:    notice, this list of conditions and the following disclaimer in the
        !            25:    documentation and/or other materials provided with the distribution.
        !            26: 3. All advertising materials mentioning features or use of this software
        !            27:    must display the following acknowledgement:
        !            28: This product includes software developed by Michael Pounov <misho@elwix.org>
        !            29: ELWIX - Embedded LightWeight unIX and its contributors.
        !            30: 4. Neither the name of AITNET nor the names of its contributors
        !            31:    may be used to endorse or promote products derived from this software
        !            32:    without specific prior written permission.
        !            33: 
        !            34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
        !            35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
        !            38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            44: SUCH DAMAGE.
        !            45: */
1.2       misho      46: #include "global.h"
                     47: #include "aitio.h"
                     48: 
                     49: 
                     50: /*
                     51:  * io_LTrimStr() Remove left whitespaces from text string
                     52:  * @psLine = Text string
                     53:  * return: 0 nothing to do; !=0 Removed bytes
                     54: */
1.3       misho      55: inline int io_LTrimStr(u_char * __restrict psLine)
1.2       misho      56: {
                     57:        int pos = 0;
                     58:        u_char *s;
                     59: 
                     60:        if (!psLine || !*psLine)
                     61:                return 0;
                     62: 
                     63:        for (s = psLine; isspace(*s); s++);
                     64:        pos = s - psLine;
                     65: 
                     66:        memmove(psLine, s, (strlen((char*) psLine) - pos) + 1);
                     67:        return pos;
                     68: }
                     69: 
                     70: /*
                     71:  * io_RTrimStr() Remove right whitespaces from text string
                     72:  * @psLine = Text string
                     73:  * return: 0 nothing to do; !=0 Removed bytes
                     74: */
1.3       misho      75: inline int io_RTrimStr(u_char * __restrict psLine)
1.2       misho      76: {
                     77:        u_char *t, *pos;
                     78: 
                     79:        if (!psLine || !*psLine)
                     80:                return 0;
                     81: 
                     82:        pos = psLine + strlen((char*) psLine);
                     83:        for (t = pos - 1; t > psLine && isspace(*t); t--);
                     84:        *++t = 0;
                     85: 
                     86:        return pos - t;
                     87: }
                     88: 
                     89: /*
                     90:  * io_TrimStr() Remove left and right whitespaces from text string
                     91:  * @psLine = Text string
                     92:  * return: 0 nothing to do; !=0 Removed bytes
                     93: */
1.3       misho      94: inline int io_TrimStr(u_char * __restrict psLine)
1.2       misho      95: {
                     96:        int ret = 0;
                     97: 
                     98:        ret = io_LTrimStr(psLine);
                     99:        ret += io_RTrimStr(psLine);
                    100: 
                    101:        return ret;
                    102: }
                    103: 
                    104: /*
                    105:  * io_UnquotStr() Remove quots from input text string 
                    106:  * @psLine = Text string
                    107:  * return: 0 nothing to do; 1 successful unquoted string
                    108: */
1.3       misho     109: inline int io_UnquotStr(u_char * __restrict psLine)
1.2       misho     110: {
                    111:        char *pos, *str = NULL;
                    112:        int flg;
                    113: 
                    114:        if (!psLine)
                    115:                return 0;
                    116: 
                    117:        switch (*psLine) {
                    118:                case '`':
                    119:                case '"':
                    120:                case '\'':
                    121:                        str = strdup((char*) psLine + 1);
                    122:                        for (pos = str, flg = 0; *pos; flg = ('\\' == *pos), pos++) {
                    123:                                if (!flg && *pos == *psLine) {
                    124:                                        *pos = 0;
                    125:                                        strlcpy((char*) psLine, str, strlen((char*) psLine) + 1);
                    126:                                        break;
                    127:                                }
                    128:                        }
                    129:                        free(str);
                    130:                        return 1;
                    131:        }
                    132: 
                    133:        return 0;
                    134: }
                    135: 
                    136: /*
1.3       misho     137:  * io_Ch2Hex() Convert from Char string to Hex string
1.2       misho     138:  * @psLine = Text string
                    139:  * @lineLen = Length of Text string
1.3       misho     140:  * return: NULL nothing to do or error; !=0 Allocated new converted data without term\0 (must be free)
1.2       misho     141: */
1.3       misho     142: inline u_char *io_Ch2Hex(u_char *psLine, int lineLen)
1.2       misho     143: {
                    144:        register int i;
1.3       misho     145:        char szWork[3];
                    146:        u_char *str;
1.2       misho     147: 
                    148:        if (!psLine || !*psLine || !lineLen)
                    149:                return NULL;
                    150: 
1.3       misho     151:        str = malloc(lineLen / 2);
1.2       misho     152:        if (!str) {
                    153:                LOGERR;
                    154:                return NULL;
                    155:        } else
1.3       misho     156:                memset(str, 0, lineLen / 2);
1.2       misho     157: 
1.3       misho     158:        for (i = 0; i < lineLen && psLine[i * 2]; i++) {
                    159:                strlcpy(szWork, (char*) &psLine[i * 2], 3);
                    160:                str[i] = (u_char) strtol(szWork, NULL, 16);
1.2       misho     161:        }
                    162: 
                    163:        return str;
                    164: }
                    165: 
                    166: 
                    167: /*
1.3       misho     168:  * io_Hex2Ch() Convert from Hex string to Char string
1.2       misho     169:  * @psLine = Text string
                    170:  * @lineLen = Length of Text string
                    171:  * return: NULL nothing to do or error; !=0 Allocated new converted string(must be free)
                    172: */
1.3       misho     173: inline char *io_Hex2Ch(u_char *psLine, int lineLen)
1.2       misho     174: {
                    175:        register int i;
                    176:        char szWork[3], *str;
                    177: 
                    178:        if (!psLine || !*psLine || !lineLen)
                    179:                return NULL;
                    180: 
1.3       misho     181:        str = malloc(lineLen * 2 + 1);
1.2       misho     182:        if (!str) {
                    183:                LOGERR;
                    184:                return NULL;
                    185:        } else
1.3       misho     186:                memset(str, 0, lineLen * 2 + 1);
1.2       misho     187: 
1.3       misho     188:        for (i = 0; i <= lineLen; i++) {
                    189:                memset(szWork, 0, 3);
                    190:                snprintf(szWork, 3, "%02X", (u_char) psLine[i]);
                    191:                strncat(str, szWork, 2);
1.2       misho     192:        }
                    193: 
                    194:        return str;
                    195: }

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