File:  [ELWIX - Embedded LightWeight unIX -] / libaitcfg / src / Attic / tools.c
Revision 1.2.4.4: download - view: text, annotated - select for diffs - revision graph
Mon Mar 22 15:05:14 2010 UTC (14 years, 3 months ago) by misho
Branches: cfg3_2
Diff to: branchpoint 1.2: preferred, unified
correct len

    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.2.4.4 2010/03/22 15:05:14 misho Exp $
    7: *
    8: *************************************************************************/
    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: 
   26: 	memmove(psLine, psLine + pos, (strlen((char*) psLine) - pos) + 1);
   27: 	return pos;
   28: }
   29: 
   30: // rtrim() Right trim whitespaces
   31: inline int rtrim(u_char *psLine)
   32: {
   33: 	register int i;
   34: 	int pos = 0;
   35: 
   36: 	if (!psLine)
   37: 		return 0;
   38: 	for (i = strlen((char*) psLine) - 1; i; i--) {
   39: 		switch (psLine[i]) {
   40: 			case ' ':
   41: 			case '\t':
   42: 			case '\r':
   43: 			case '\n':
   44: 				psLine[i] = 0;
   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: }
   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>