File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / src / Attic / tools.c
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Fri Sep 10 12:39:41 2010 UTC (13 years, 9 months ago) by misho
Branches: MAIN
CVS tags: io1_7, io1_6, io1_5, io1_4, IO1_6, IO1_5, IO1_4, IO1_3, HEAD
new version 1.3

    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.3 2010/09/10 12:39:41 misho Exp $
    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 * __restrict 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 * __restrict 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 * __restrict 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 * __restrict 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: }
   98: 
   99: /*
  100:  * io_Ch2Hex() 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 data without term\0 (must be free)
  104: */
  105: inline u_char *io_Ch2Hex(u_char *psLine, int lineLen)
  106: {
  107: 	register int i;
  108: 	char szWork[3];
  109: 	u_char *str;
  110: 
  111: 	if (!psLine || !*psLine || !lineLen)
  112: 		return NULL;
  113: 
  114: 	str = malloc(lineLen / 2);
  115: 	if (!str) {
  116: 		LOGERR;
  117: 		return NULL;
  118: 	} else
  119: 		memset(str, 0, lineLen / 2);
  120: 
  121: 	for (i = 0; i < lineLen && psLine[i * 2]; i++) {
  122: 		strlcpy(szWork, (char*) &psLine[i * 2], 3);
  123: 		str[i] = (u_char) strtol(szWork, NULL, 16);
  124: 	}
  125: 
  126: 	return str;
  127: }
  128: 
  129: 
  130: /*
  131:  * io_Hex2Ch() Convert from Hex string to Char string
  132:  * @psLine = Text string
  133:  * @lineLen = Length of Text string
  134:  * return: NULL nothing to do or error; !=0 Allocated new converted string(must be free)
  135: */
  136: inline char *io_Hex2Ch(u_char *psLine, int lineLen)
  137: {
  138: 	register int i;
  139: 	char szWork[3], *str;
  140: 
  141: 	if (!psLine || !*psLine || !lineLen)
  142: 		return NULL;
  143: 
  144: 	str = malloc(lineLen * 2 + 1);
  145: 	if (!str) {
  146: 		LOGERR;
  147: 		return NULL;
  148: 	} else
  149: 		memset(str, 0, lineLen * 2 + 1);
  150: 
  151: 	for (i = 0; i <= lineLen; i++) {
  152: 		memset(szWork, 0, 3);
  153: 		snprintf(szWork, 3, "%02X", (u_char) psLine[i]);
  154: 		strncat(str, szWork, 2);
  155: 	}
  156: 
  157: 	return str;
  158: }

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