/*************************************************************************
* (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
* by Michael Pounov <misho@openbsd-bg.org>
*
* $Author: misho $
* $Id: tools.c,v 1.2.4.2 2009/11/11 14:46:36 misho Exp $
*
*************************************************************************/
#include "global.h"
#include "aitcfg.h"
#include "tools.h"
#pragma GCC visibility push(hidden)
// ltrim() Left trim whitespaces
inline int ltrim(u_char *psLine)
{
int pos = 0;
if (!psLine)
return 0;
pos = strspn((char*) psLine, " \t\r\n");
if (!pos)
return 0;
memmove(psLine, psLine + pos, strlen((char*) psLine));
return pos;
}
// rtrim() Right trim whitespaces
inline int rtrim(u_char *psLine)
{
register int i;
int pos = 0;
if (!psLine)
return 0;
for (i = strlen((char*) psLine) - 1; i; i--) {
switch (psLine[i]) {
case ' ':
case '\t':
case '\r':
case '\n':
psLine[i] = 0;
pos++;
continue;
}
break;
}
return pos;
}
#pragma GCC visibility pop
// trim() Triming all whitespaces both left/right
inline int trim(u_char *psLine)
{
int ret = 0;
ret = ltrim(psLine);
ret += rtrim(psLine);
return ret;
}
// unquot() Unquoted string
inline int unquot(u_char *psLine)
{
char *pos, *str = NULL;
int flg;
if (!psLine)
return 0;
switch (*psLine) {
case '`':
case '"':
case '\'':
str = strdup((char*) psLine + 1);
for (pos = str, flg = 0; *pos; flg = ('\\' == *pos), pos++) {
if (!flg && *pos == *psLine) {
*pos = 0;
strlcpy((char*) psLine, str, strlen((char*) psLine) + 1);
break;
}
}
free(str);
return 1;
}
return 0;
}
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>