29 #define DISABLE_DEBUGLOG
51 #include <gwenhywfar/gwenhywfarapi.h>
52 #include <gwenhywfar/debug.h>
53 #include <gwenhywfar/stringlist.h>
89 while(*src && (
unsigned char)(*src)<33)
106 while(*src && size<(maxsize-1)) {
121 if (!insideQuotes && strchr(delims, *src)!=0)
131 "Found a closing \" without an opening one "
132 "(consider using a backslash to escape)");
149 if (isspace((
int)((
unsigned char)*src)) && !lastWasEscape) {
173 if (strchr(delims, *src)==0) {
188 if (lastBlankPos!=-1)
189 buffer[lastBlankPos]=0;
203 const char *savedSrc=src;
211 while(*src && (
unsigned char)(*src)<33) {
212 if (strchr(delims, *src)) {
246 if (!insideQuotes && strchr(delims, *src)!=0)
256 "Found a closing \" without an opening one "
257 "(consider using a backslash to escape)");
274 if (!lastWasEscape && *((
unsigned char*)src)<33) {
295 if (strchr(delims, *src)==0) {
310 if (lastBlankPos!=-1)
322 unsigned int maxsize) {
329 x=(
unsigned char)*src;
331 (x>=
'A' && x<=
'Z') ||
332 (x>=
'a' && x<=
'z') ||
333 (x>=
'0' && x<=
'9'))) {
336 if ((maxsize-1)<size+3) {
341 c=(((
unsigned char)(*src))>>4)&0xf;
346 c=((
unsigned char)(*src))&0xf;
353 if (size<(maxsize-1))
372 unsigned int maxsize) {
379 x=(
unsigned char)*src;
381 (x>=
'A' && x<=
'Z') ||
382 (x>=
'a' && x<=
'z') ||
383 (x>=
'0' && x<=
'9') ||
393 if ((maxsize-1)<size+3) {
398 c=(((
unsigned char)(*src))>>4)&0xf;
403 c=((
unsigned char)(*src))&0xf;
410 if (size<(maxsize-1))
430 unsigned int maxsize) {
435 while(*src && srclen>0) {
438 x=(
unsigned char)*src;
440 (x>=
'A' && x<=
'Z') ||
441 (x>=
'a' && x<=
'z') ||
442 (x>=
'0' && x<=
'9')) {
443 if (size<(maxsize-1))
452 unsigned char d1, d2;
461 if (!(*src) || !isxdigit((
int)*src)) {
466 d1=(
unsigned char)(toupper(*src));
470 if (!(*src) || !isxdigit((
int)*src)) {
474 d2=(
unsigned char)(toupper(*src));
485 if (size<(maxsize-1))
486 buffer[size++]=(
char)c;
495 "characters in escaped string (\"%s\")",
512 unsigned int maxsize) {
524 unsigned int maxsize) {
529 while(*src && srclen>0) {
532 x=(
unsigned char)*src;
534 (x>=
'A' && x<=
'Z') ||
535 (x>=
'a' && x<=
'z') ||
536 (x>=
'0' && x<=
'9') ||
544 if (size<(maxsize-1))
553 unsigned char d1, d2;
562 if (!(*src) || !isxdigit((
int)*src)) {
567 d1=(
unsigned char)(toupper(*src));
571 if (!(*src) || !isxdigit((
int)*src)) {
575 d2=(
unsigned char)(toupper(*src));
586 if (size<(maxsize-1))
587 buffer[size++]=(
char)c;
596 "characters in escaped string (\"%s\")",
613 unsigned int maxsize) {
623 char *buffer,
unsigned int maxsize) {
627 if ((l*2)+1 > maxsize) {
637 c=(((
unsigned char)(src[pos]))>>4)&0xf;
642 c=((
unsigned char)(src[pos]))&0xf;
659 unsigned int groupsize,
661 int skipLeadingZeroes) {
676 c=(((
unsigned char)(src[pos]))>>4)&0xf;
677 if (skipLeadingZeroes) {
687 if (size+1>=maxsize) {
694 if (size+1>=maxsize) {
698 buffer[size++]=delimiter;
704 c=((
unsigned char)(src[pos]))&0xf;
705 if (skipLeadingZeroes) {
714 if (size+1>=maxsize) {
723 if (size+1>=maxsize) {
727 buffer[size++]=delimiter;
742 unsigned int groupsize,
744 int skipLeadingZeroes) {
745 unsigned int pos = 0;
753 c=(((
unsigned char)(src[pos]))>>4)&0xf;
754 if (skipLeadingZeroes) {
769 if (groupsize && j==groupsize) {
779 c=((
unsigned char)(src[pos]))&0xf;
780 if (skipLeadingZeroes) {
795 if (groupsize && j==groupsize) {
813 unsigned int size = 0;
816 unsigned char d1, d2;
820 if (!isxdigit((
int)*src)) {
824 d1=(
unsigned char)(toupper(*src));
828 if (!(*src) || !isxdigit((
int)*src)) {
832 d2=(
unsigned char)(toupper(*src));
846 buffer[size++]=(
char)c;
860 unsigned char d1, d2;
864 if (isspace((
int)*src)) {
868 if (!isxdigit((
int)*src)) {
872 d1=(
unsigned char)(toupper(*src));
876 if (!(*src) || !isxdigit((
int)*src)) {
880 d2=(
unsigned char)(toupper(*src));
909 unsigned char d1, d2;
918 if (!isdigit((
int)*src)) {
922 d1=(
unsigned char)(*src);
926 if (!(*src) || !isxdigit((
int)*src)) {
930 d2=(
unsigned char)(*src);
949 unsigned int groupsize,
951 int skipLeadingZeroes) {
964 c=(((
unsigned char)(src[pos]))>>4)&0xf;
965 if (skipLeadingZeroes) {
978 if (groupsize && j==groupsize) {
988 c=((
unsigned char)(src[pos]))&0xf;
989 if (skipLeadingZeroes) {
1002 if (groupsize && j==groupsize) {
1033 return strcasecmp(s1, s2);
1035 return strcmp(s1, s2);
1042 while(*haystack && tolower(*haystack)!=tolower(*needle))
1053 while(*t && *s && (tolower(*t)==tolower(*s))) {
1075 const char *p,
unsigned int *ppos,
1077 unsigned int *matches) {
1083 unsigned int _wpos = *wpos, _ppos = *ppos, _matches = *matches;
1090 while (_wpos<wlength && _ppos<plength) {
1096 *matches = _matches;
1106 if (a!=b && b!=
'?') {
1109 *matches = _matches;
1116 if (_wpos==wlength && _ppos==plength) {
1119 *matches = _matches;
1123 if (_wpos>=wlength && _ppos<plength)
1124 if (p[_ppos]==
'*') {
1127 *matches = _matches;
1133 *matches = _matches;
1140 const char *p,
unsigned int *ppos,
1142 unsigned int *matches) {
1143 unsigned int lwpos, lppos, lmatches;
1150 while(lwpos<wlength) {
1165 unsigned int matches;
1166 unsigned int plength;
1168 ppos=wpos=matches=0;
1201 sprintf(lbuffer,
"%d", num);
1209 strcpy(buffer, lbuffer);
1211 buffer[i++]=fillchar;
1215 else if (fillchar<0) {
1223 strcat(buffer, lbuffer);
1228 strcpy(buffer, lbuffer);
1236 unsigned int insert) {
1243 for (k=0; k<insert; k++)
1244 fprintf(stderr,
" ");
1245 fprintf(stderr,
"String size is %d:\n",l);
1247 for (k=0; k<insert; k++)
1248 fprintf(stderr,
" ");
1249 fprintf(stderr,
"%04x: ",pos);
1255 for (i=pos; i<j; i++) {
1256 fprintf(stderr,
"%02x ",(
unsigned char)s[i]);
1259 for (i=0; i<16-(j-pos); i++)
1260 fprintf(stderr,
" ");
1262 for (i=pos; i<j; i++) {
1264 fprintf(stderr,
".");
1266 fprintf(stderr,
"%c",s[i]);
1268 fprintf(stderr,
"\n");
1277 unsigned int insert) {
1285 for (k=0; k<insert; k++)
1288 snprintf(numbuf,
sizeof(numbuf),
"%d", l);
1292 for (k=0; k<insert; k++)
1294 snprintf(numbuf,
sizeof(numbuf),
"%04x: ",pos);
1301 for (i=pos; i<j; i++) {
1302 snprintf(numbuf,
sizeof(numbuf),
"%02x ", (
unsigned char)s[i]);
1306 for (i=0; i<16-(j-pos); i++)
1309 for (i=pos; i<j; i++) {
1330 x=(
unsigned char)*src;
1332 (x>=
'A' && x<=
'Z') ||
1333 (x>=
'a' && x<=
'z') ||
1334 (x>=
'0' && x<=
'9'))) {
1338 c=(((
unsigned char)(*src))>>4)&0xf;
1343 c=((
unsigned char)(*src))&0xf;
1364 x=(
unsigned char)*src;
1366 (x>=
'A' && x<=
'Z') ||
1367 (x>=
'a' && x<=
'z') ||
1368 (x>=
'0' && x<=
'9')) {
1373 unsigned char d1, d2;
1378 if (!(*src) || !isxdigit((
int)*src)) {
1383 d1=(
unsigned char)(toupper(*src));
1387 if (!(*src) || !isxdigit((
int)*src)) {
1391 d2=(
unsigned char)(toupper(*src));
1406 "characters in escaped string (\"%s\")",
1423 x=(
unsigned char)*src;
1425 (x>=
'A' && x<=
'Z') ||
1426 (x>=
'a' && x<=
'z') ||
1427 (x>=
'0' && x<=
'9') ||
1440 c=(((
unsigned char)(*src))>>4)&0xf;
1445 c=((
unsigned char)(*src))&0xf;
1468 if (strlen(src)>2) {
1469 unsigned char d1, d2;
1472 if (isxdigit((
int)src[1]) && isxdigit((
int)src[2])) {
1476 d1=(
unsigned char)(toupper(*src));
1480 d2=(
unsigned char)(toupper(*src));
1518 (x>=
'A' && x<=
'Z') ||
1519 (x>=
'a' && x<=
'z') ||
1520 (x>=
'0' && x<=
'9') ||
1531 c=(((
unsigned char)x)>>4)&0xf;
1536 c=((
unsigned char)x)&0xf;
1552 const char *logDomain,
1578 for (i=0; i<size; i++) {
1580 if (isspace((
int)*p)) {
1581 if (!lastWasBlank) {
1597 if (lastBlankPos!=0)
1609 #ifdef HAVE_SETLOCALE
1610 const char *orig_locale = setlocale(LC_NUMERIC,
NULL);
1611 char *currentLocale = strdup(orig_locale ? orig_locale :
"C");
1612 setlocale(LC_NUMERIC,
"C");
1615 rv=snprintf(numbuf,
sizeof(numbuf),
"%f", num);
1617 #ifdef HAVE_SETLOCALE
1618 setlocale(LC_NUMERIC, currentLocale);
1619 free(currentLocale);
1622 if (rv<1 || rv>=
sizeof(numbuf))
1632 #ifdef HAVE_SETLOCALE
1633 const char *orig_locale = setlocale(LC_NUMERIC,
NULL);
1634 char *currentLocale = strdup(orig_locale ? orig_locale :
"C");
1635 setlocale(LC_NUMERIC,
"C");
1638 rv=sscanf(s,
"%lf", num);
1640 #ifdef HAVE_SETLOCALE
1641 setlocale(LC_NUMERIC, currentLocale);
1642 free(currentLocale);
1657 nboth=strlen(s1)+strlen(s2);
1668 if (toupper(*s1)==toupper(*t)) {
1672 if (isalnum((
int)*s1) && isalnum((
int)*t)) {
1700 if (toupper(*s1)==toupper(*t)) {
1704 if (isalnum((
int)*s1) && isalnum((
int)*t)) {
1720 pc=(nmatch*100)/nboth;
1746 while(handled<len) {
1750 c=(
unsigned char)*s;
1751 if ((c & 0xfe)==0xfc)
1753 else if ((c & 0xfc)==0xf8)
1755 else if ((c & 0xf8)==0xf0)
1757 else if ((c & 0xf0)==0xe0)
1759 else if ((c & 0xe0)==0xc0)
1761 else if (c & 0x80) {
1767 if (handled+i+1>len) {
1769 "Incomplete UTF8 sequence at pos %d", handled);
1776 for (j=0; j<i; j++) {
1777 if ((((
unsigned char)*s) & 0xc0)!=0xc0) {
1779 "Invalid UTF8 sequence at pos %d (rel %d of %d)",
1797 uint32_t bytesAdded;
1799 #define GWEN_TEXT__APPENDCHAR(chr) \
1802 GWEN_Buffer_IncrementPos(buf, bytesAdded); \
1803 GWEN_Buffer_AdjustUsedBytes(buf); \
1805 GWEN_Buffer_AllocRoom(buf, 2); \
1806 pdst=GWEN_Buffer_GetPosPointer(buf); \
1807 roomLeft=GWEN_Buffer_GetMaxUnsegmentedWrite(buf); \
1810 *(pdst++)=(unsigned char)chr; \
1824 x=(
unsigned char)*src;
1827 unsigned char num=0;
1831 while(*src && isdigit((
int)*src)) {
1839 else if (strncmp(src+1,
"szlig;", 6)==0) {
1845 else if (strncmp(src+1,
"Auml;", 5)==0) {
1851 else if (strncmp(src+1,
"Ouml;", 5)==0) {
1857 else if (strncmp(src+1,
"Uuml;", 5)==0) {
1863 else if (strncmp(src+1,
"auml;", 5)==0) {
1869 else if (strncmp(src+1,
"ouml;", 5)==0) {
1875 else if (strncmp(src+1,
"uuml;", 5)==0) {
1888 if (strncasecmp(src, e->
replace, l)==0) {
1910 #undef GWEN_TEXT__APPENDCHAR
1922 x=(
unsigned char)*src;
1937 snprintf(numbuf,
sizeof(numbuf),
"&#%d;", x);
1952 const char *toCharset,
1953 const char *text,
int len,
1956 if (fromCharset && *fromCharset && toCharset && *toCharset &&
1957 strcasecmp(fromCharset, toCharset)!=0) {
1960 "iconv not available, can not convert from \"%s\" to \"%s\"",
1961 fromCharset, toCharset);
1965 ic=iconv_open(toCharset, fromCharset);
1966 if (ic==((iconv_t)-1)) {
1968 fromCharset, toCharset);
1988 outbuf=(
char*)malloc(outLeft);
1994 done=iconv(ic, &pInbuf, &inLeft, &pOutbuf, &outLeft);
1995 if (done==(
size_t)-1) {
1997 strerror(errno), errno);
int GWEN_Text__cmpSegment(const char *w, unsigned int *wpos, const char *p, unsigned int *ppos, int sensecase, unsigned int *matches)
uint32_t GWEN_Buffer_GetBytesLeft(GWEN_BUFFER *bf)
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
void GWEN_Text_CondenseBuffer(GWEN_BUFFER *buf)
#define GWEN_TEXT_FLAGS_DEL_TRAILING_BLANKS
uint32_t GWEN_Buffer_GetMaxUnsegmentedWrite(GWEN_BUFFER *bf)
uint32_t GWEN_Buffer_GetUsedBytes(const GWEN_BUFFER *bf)
void GWEN_Text_DumpString(const char *s, unsigned int l, unsigned int insert)
int GWEN_Text_EscapeToBufferTolerant(const char *src, GWEN_BUFFER *buf)
int GWEN_Text_FromHex(const char *src, char *buffer, unsigned maxsize)
int GWEN_Text_ConvertCharset(const char *fromCharset, const char *toCharset, const char *text, int len, GWEN_BUFFER *tbuf)
int GWEN_Buffer_AdjustUsedBytes(GWEN_BUFFER *bf)
uint32_t GWEN_Buffer_GetPos(const GWEN_BUFFER *bf)
static const GWEN_TEXT_ESCAPE_ENTRY gwen_text__xml_escape_chars[]
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
int GWEN_Text_UnescapeToBufferTolerant(const char *src, GWEN_BUFFER *buf)
char * GWEN_Buffer_GetPosPointer(const GWEN_BUFFER *bf)
char * GWEN_Text_GetWord(const char *src, const char *delims, char *buffer, unsigned int maxsize, uint32_t flags, const char **next)
int GWEN_Buffer_IncrementPos(GWEN_BUFFER *bf, uint32_t i)
double GWEN_Text_CheckSimilarity(const char *s1, const char *s2, int ign)
int GWEN_Text_CountUtf8Chars(const char *s, int len)
void GWEN_Text_DumpString2Buffer(const char *s, unsigned int l, GWEN_BUFFER *mbuf, unsigned int insert)
#define GWEN_TEXT_FLAGS_NULL_IS_DELIMITER
int GWEN_Logger_Log(const char *logDomain, GWEN_LOGGER_LEVEL priority, const char *s)
#define DBG_DEBUG(dbg_logger, format, args...)
#define GWEN_TEXT_FLAGS_NEED_DELIMITER
int GWEN_Text_ToBcdBuffer(const char *src, unsigned l, GWEN_BUFFER *buf, unsigned int groupsize, char delimiter, int skipLeadingZeroes)
int GWEN_Text_EscapeToBuffer(const char *src, GWEN_BUFFER *buf)
const char * GWEN_Text_StrCaseStr(const char *haystack, const char *needle)
char * GWEN_Text_Unescape(const char *src, char *buffer, unsigned int maxsize)
#define GWEN_ERROR_GENERIC
#define GWEN_TEXT_FLAGS_DEL_MULTIPLE_BLANKS
void GWEN_Text_LogString(const char *s, unsigned int l, const char *logDomain, GWEN_LOGGER_LEVEL lv)
char * GWEN_Text_UnescapeTolerantN(const char *src, unsigned int srclen, char *buffer, unsigned int maxsize)
int GWEN_Buffer_AppendByte(GWEN_BUFFER *bf, char c)
int GWEN_Text_FromBcdBuffer(const char *src, GWEN_BUFFER *buf)
int GWEN_Text_UnescapeXmlToBuffer(const char *src, GWEN_BUFFER *buf)
double GWEN_Text__CheckSimilarity(const char *s1, const char *s2, int ign)
int GWEN_Text_EscapeXmlToBuffer(const char *src, GWEN_BUFFER *buf)
void GWEN_Buffer_free(GWEN_BUFFER *bf)
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
int GWEN_Text_UnescapeToBuffer(const char *src, GWEN_BUFFER *buf)
int GWEN_Text_Compare(const char *s1, const char *s2, int ign)
int GWEN_Text__findSegment(const char *w, unsigned int *wpos, const char *p, unsigned int *ppos, int sensecase, unsigned int *matches)
int GWEN_Buffer_Crop(GWEN_BUFFER *bf, uint32_t pos, uint32_t l)
int GWEN_Text_NumToString(int num, char *buffer, unsigned int bufsize, int fillchar)
#define DBG_ERROR(dbg_logger, format, args...)
char * GWEN_Text_ToHex(const char *src, unsigned l, char *buffer, unsigned int maxsize)
int GWEN_Text_ComparePattern(const char *w, const char *p, int sensecase)
char * GWEN_Text_ToHexGrouped(const char *src, unsigned l, char *buffer, unsigned maxsize, unsigned int groupsize, char delimiter, int skipLeadingZeroes)
int GWEN_Text_StringToDouble(const char *s, double *num)
#define DBG_INFO(dbg_logger, format, args...)
#define GWEN_TEXT_FLAGS_DEL_QUOTES
char * GWEN_Text_UnescapeTolerant(const char *src, char *buffer, unsigned int maxsize)
int GWEN_Text_GetWordToBuffer(const char *src, const char *delims, GWEN_BUFFER *buf, uint32_t flags, const char **next)
char * GWEN_Text_EscapeTolerant(const char *src, char *buffer, unsigned int maxsize)
int GWEN_Buffer_ReadByte(GWEN_BUFFER *bf)
char * GWEN_Text_Escape(const char *src, char *buffer, unsigned int maxsize)
#define GWEN_TEXT__APPENDCHAR(chr)
#define GWEN_TEXT_FLAGS_DEL_LEADING_BLANKS
int GWEN_Buffer_AppendBytes(GWEN_BUFFER *bf, const char *buffer, uint32_t size)
#define GWEN_TEXT_FLAGS_CHECK_BACKSLASH
int GWEN_Text_FromHexBuffer(const char *src, GWEN_BUFFER *buf)
int GWEN_Text_EscapeToBufferTolerant2(GWEN_BUFFER *src, GWEN_BUFFER *buf)
int GWEN_Text_DoubleToBuffer(double num, GWEN_BUFFER *buf)
int GWEN_Text_ToHexBuffer(const char *src, unsigned l, GWEN_BUFFER *buf, unsigned int groupsize, char delimiter, int skipLeadingZeroes)
int GWEN_Buffer_AppendString(GWEN_BUFFER *bf, const char *buffer)
char * GWEN_Text_UnescapeN(const char *src, unsigned int srclen, char *buffer, unsigned int maxsize)