29 #define DISABLE_DEBUGLOG 51 #include <gwenhywfar/gwenhywfarapi.h> 52 #include <gwenhywfar/debug.h> 53 #include <gwenhywfar/stringlist.h> 90 while (*src && (
unsigned char)(*src)<33)
107 while (*src && size<(maxsize-1)) {
122 if (!insideQuotes && strchr(delims, *src)!=0)
132 "Found a closing \" without an opening one " 133 "(consider using a backslash to escape)");
150 if (isspace((
int)((
unsigned char)*src)) && !lastWasEscape) {
174 if (strchr(delims, *src)==0) {
189 if (lastBlankPos!=-1)
190 buffer[lastBlankPos]=0;
205 const char *savedSrc=src;
213 while (*src && (
unsigned char)(*src)<33) {
214 if (strchr(delims, *src)) {
248 if (!insideQuotes && strchr(delims, *src)!=0)
258 "Found a closing \" without an opening one " 259 "(consider using a backslash to escape)");
276 if (!lastWasEscape && *((
unsigned char *)src)<33) {
297 if (strchr(delims, *src)==0) {
312 if (lastBlankPos!=-1)
324 unsigned int maxsize)
332 x=(
unsigned char)*src;
334 (x>=
'A' && x<=
'Z') ||
335 (x>=
'a' && x<=
'z') ||
336 (x>=
'0' && x<=
'9'))) {
339 if ((maxsize-1)<size+3) {
344 c=(((
unsigned char)(*src))>>4)&0xf;
349 c=((
unsigned char)(*src))&0xf;
356 if (size<(maxsize-1))
375 unsigned int maxsize)
383 x=(
unsigned char)*src;
385 (x>=
'A' && x<=
'Z') ||
386 (x>=
'a' && x<=
'z') ||
387 (x>=
'0' && x<=
'9') ||
397 if ((maxsize-1)<size+3) {
402 c=(((
unsigned char)(*src))>>4)&0xf;
407 c=((
unsigned char)(*src))&0xf;
414 if (size<(maxsize-1))
434 unsigned int maxsize)
440 while (*src && srclen>0) {
443 x=(
unsigned char)*src;
445 (x>=
'A' && x<=
'Z') ||
446 (x>=
'a' && x<=
'z') ||
447 (x>=
'0' && x<=
'9')) {
448 if (size<(maxsize-1))
457 unsigned char d1, d2;
466 if (!(*src) || !isxdigit((
int)*src)) {
471 d1=(
unsigned char)(toupper(*src));
475 if (!(*src) || !isxdigit((
int)*src)) {
479 d2=(
unsigned char)(toupper(*src));
490 if (size<(maxsize-1))
491 buffer[size++]=(
char)c;
500 "characters in escaped string (\"%s\")",
517 unsigned int maxsize)
530 unsigned int maxsize)
536 while (*src && srclen>0) {
539 x=(
unsigned char)*src;
541 (x>=
'A' && x<=
'Z') ||
542 (x>=
'a' && x<=
'z') ||
543 (x>=
'0' && x<=
'9') ||
551 if (size<(maxsize-1))
560 unsigned char d1, d2;
569 if (!(*src) || !isxdigit((
int)*src)) {
574 d1=(
unsigned char)(toupper(*src));
578 if (!(*src) || !isxdigit((
int)*src)) {
582 d2=(
unsigned char)(toupper(*src));
593 if (size<(maxsize-1))
594 buffer[size++]=(
char)c;
603 "characters in escaped string (\"%s\")",
620 unsigned int maxsize)
631 char *buffer,
unsigned int maxsize)
636 if ((l*2)+1 > maxsize) {
646 c=(((
unsigned char)(src[pos]))>>4)&0xf;
651 c=((
unsigned char)(src[pos]))&0xf;
668 unsigned int groupsize,
670 int skipLeadingZeroes)
686 c=(((
unsigned char)(src[pos]))>>4)&0xf;
687 if (skipLeadingZeroes) {
697 if (size+1>=maxsize) {
704 if (size+1>=maxsize) {
708 buffer[size++]=delimiter;
714 c=((
unsigned char)(src[pos]))&0xf;
715 if (skipLeadingZeroes) {
724 if (size+1>=maxsize) {
733 if (size+1>=maxsize) {
737 buffer[size++]=delimiter;
752 unsigned int groupsize,
754 int skipLeadingZeroes)
756 unsigned int pos = 0;
764 c=(((
unsigned char)(src[pos]))>>4)&0xf;
765 if (skipLeadingZeroes) {
780 if (groupsize && j==groupsize) {
790 c=((
unsigned char)(src[pos]))&0xf;
791 if (skipLeadingZeroes) {
806 if (groupsize && j==groupsize) {
825 unsigned int size = 0;
828 unsigned char d1, d2;
832 if (!isxdigit((
int)*src)) {
836 d1=(
unsigned char)(toupper(*src));
840 if (!(*src) || !isxdigit((
int)*src)) {
844 d2=(
unsigned char)(toupper(*src));
858 buffer[size++]=(
char)c;
873 unsigned char d1, d2;
877 if (isspace((
int)*src)) {
881 if (!isxdigit((
int)*src)) {
885 d1=(
unsigned char)(toupper(*src));
889 if (!(*src) || !isxdigit((
int)*src)) {
893 d2=(
unsigned char)(toupper(*src));
923 unsigned char d1, d2;
932 if (!isdigit((
int)*src)) {
936 d1=(
unsigned char)(*src);
940 if (!(*src) || !isxdigit((
int)*src)) {
944 d2=(
unsigned char)(*src);
963 unsigned int groupsize,
965 int skipLeadingZeroes)
979 c=(((
unsigned char)(src[pos]))>>4)&0xf;
980 if (skipLeadingZeroes) {
993 if (groupsize && j==groupsize) {
1003 c=((
unsigned char)(src[pos]))&0xf;
1004 if (skipLeadingZeroes) {
1005 if (c==0 && pos+1<l)
1008 skipLeadingZeroes=0;
1017 if (groupsize && j==groupsize) {
1049 return strcasecmp(s1, s2);
1051 return strcmp(s1, s2);
1059 while (*haystack && tolower(*haystack)!=tolower(*needle))
1070 while (*t && *s && (tolower(*t)==tolower(*s))) {
1092 const char *p,
unsigned int *ppos,
1094 unsigned int *matches)
1101 unsigned int _wpos = *wpos, _ppos = *ppos, _matches = *matches;
1108 while (_wpos<wlength && _ppos<plength) {
1114 *matches = _matches;
1124 if (a!=b && b!=
'?') {
1127 *matches = _matches;
1134 if (_wpos==wlength && _ppos==plength) {
1137 *matches = _matches;
1141 if (_wpos>=wlength && _ppos<plength)
1142 if (p[_ppos]==
'*') {
1145 *matches = _matches;
1151 *matches = _matches;
1158 const char *p,
unsigned int *ppos,
1160 unsigned int *matches)
1162 unsigned int lwpos, lppos, lmatches;
1169 while (lwpos<wlength) {
1185 unsigned int matches;
1186 unsigned int plength;
1188 ppos=wpos=matches=0;
1222 sprintf(lbuffer,
"%d", num);
1230 strcpy(buffer, lbuffer);
1232 buffer[i++]=fillchar;
1236 else if (fillchar<0) {
1244 strcat(buffer, lbuffer);
1249 strcpy(buffer, lbuffer);
1257 unsigned int insert)
1265 for (k=0; k<insert; k++)
1266 fprintf(stderr,
" ");
1267 fprintf(stderr,
"String size is %d:\n", l);
1269 for (k=0; k<insert; k++)
1270 fprintf(stderr,
" ");
1271 fprintf(stderr,
"%04x: ", pos);
1277 for (i=pos; i<j; i++) {
1278 fprintf(stderr,
"%02x ", (
unsigned char)s[i]);
1281 for (i=0; i<16-(j-pos); i++)
1282 fprintf(stderr,
" ");
1284 for (i=pos; i<j; i++) {
1286 fprintf(stderr,
".");
1288 fprintf(stderr,
"%c", s[i]);
1290 fprintf(stderr,
"\n");
1299 unsigned int insert)
1308 for (k=0; k<insert; k++)
1311 snprintf(numbuf,
sizeof(numbuf),
"%d", l);
1315 for (k=0; k<insert; k++)
1317 snprintf(numbuf,
sizeof(numbuf),
"%04x: ", pos);
1324 for (i=pos; i<j; i++) {
1325 snprintf(numbuf,
sizeof(numbuf),
"%02x ", (
unsigned char)s[i]);
1329 for (i=0; i<16-(j-pos); i++)
1332 for (i=pos; i<j; i++) {
1354 x=(
unsigned char)*src;
1356 (x>=
'A' && x<=
'Z') ||
1357 (x>=
'a' && x<=
'z') ||
1358 (x>=
'0' && x<=
'9'))) {
1362 c=(((
unsigned char)(*src))>>4)&0xf;
1367 c=((
unsigned char)(*src))&0xf;
1389 x=(
unsigned char)*src;
1391 (x>=
'A' && x<=
'Z') ||
1392 (x>=
'a' && x<=
'z') ||
1393 (x>=
'0' && x<=
'9')) {
1398 unsigned char d1, d2;
1403 if (!(*src) || !isxdigit((
int)*src)) {
1408 d1=(
unsigned char)(toupper(*src));
1412 if (!(*src) || !isxdigit((
int)*src)) {
1416 d2=(
unsigned char)(toupper(*src));
1431 "characters in escaped string (\"%s\")",
1449 x=(
unsigned char)*src;
1451 (x>=
'A' && x<=
'Z') ||
1452 (x>=
'a' && x<=
'z') ||
1453 (x>=
'0' && x<=
'9') ||
1466 c=(((
unsigned char)(*src))>>4)&0xf;
1471 c=((
unsigned char)(*src))&0xf;
1495 if (strlen(src)>2) {
1496 unsigned char d1, d2;
1499 if (isxdigit((
int)src[1]) && isxdigit((
int)src[2])) {
1503 d1=(
unsigned char)(toupper(*src));
1507 d2=(
unsigned char)(toupper(*src));
1546 (x>=
'A' && x<=
'Z') ||
1547 (x>=
'a' && x<=
'z') ||
1548 (x>=
'0' && x<=
'9') ||
1559 c=(((
unsigned char)x)>>4)&0xf;
1564 c=((
unsigned char)x)&0xf;
1580 const char *logDomain,
1608 for (i=0; i<size; i++) {
1610 if (isspace((
int)*p)) {
1611 if (!lastWasBlank) {
1627 if (lastBlankPos!=0)
1640 #ifdef HAVE_SETLOCALE 1641 const char *orig_locale = setlocale(LC_NUMERIC,
NULL);
1642 char *currentLocale = strdup(orig_locale ? orig_locale :
"C");
1643 setlocale(LC_NUMERIC,
"C");
1646 rv=snprintf(numbuf,
sizeof(numbuf),
"%f", num);
1648 #ifdef HAVE_SETLOCALE 1649 setlocale(LC_NUMERIC, currentLocale);
1650 free(currentLocale);
1653 if (rv<1 || rv>=
sizeof(numbuf))
1664 #ifdef HAVE_SETLOCALE 1665 const char *orig_locale = setlocale(LC_NUMERIC,
NULL);
1666 char *currentLocale = strdup(orig_locale ? orig_locale :
"C");
1667 setlocale(LC_NUMERIC,
"C");
1670 rv=sscanf(s,
"%lf", num);
1672 #ifdef HAVE_SETLOCALE 1673 setlocale(LC_NUMERIC, currentLocale);
1674 free(currentLocale);
1690 nboth=strlen(s1)+strlen(s2);
1693 while (*s1 && *s2) {
1701 if (toupper(*s1)==toupper(*t)) {
1705 if (isalnum((
int)*s1) && isalnum((
int)*t)) {
1721 while (*s1 && *s2) {
1733 if (toupper(*s1)==toupper(*t)) {
1737 if (isalnum((
int)*s1) && isalnum((
int)*t)) {
1753 pc=(nmatch*100)/nboth;
1781 while (handled<len) {
1785 c=(
unsigned char)*s;
1786 if ((c & 0xfe)==0xfc)
1788 else if ((c & 0xfc)==0xf8)
1790 else if ((c & 0xf8)==0xf0)
1792 else if ((c & 0xf0)==0xe0)
1794 else if ((c & 0xe0)==0xc0)
1796 else if (c & 0x80) {
1802 if (handled+i+1>len) {
1804 "Incomplete UTF8 sequence at pos %d", handled);
1811 for (j=0; j<i; j++) {
1812 if ((((
unsigned char)*s) & 0xc0)!=0xc0) {
1814 "Invalid UTF8 sequence at pos %d (rel %d of %d)",
1833 uint32_t bytesAdded;
1835 #define GWEN_TEXT__APPENDCHAR(chr) \ 1838 GWEN_Buffer_IncrementPos(buf, bytesAdded); \ 1839 GWEN_Buffer_AdjustUsedBytes(buf); \ 1841 GWEN_Buffer_AllocRoom(buf, 2); \ 1842 pdst=GWEN_Buffer_GetPosPointer(buf); \ 1843 roomLeft=GWEN_Buffer_GetMaxUnsegmentedWrite(buf); \ 1846 *(pdst++)=(unsigned char)chr; \ 1860 x=(
unsigned char)*src;
1863 unsigned char num=0;
1867 while (*src && isdigit((
int)*src)) {
1875 else if (strncmp(src+1,
"szlig;", 6)==0) {
1881 else if (strncmp(src+1,
"Auml;", 5)==0) {
1887 else if (strncmp(src+1,
"Ouml;", 5)==0) {
1893 else if (strncmp(src+1,
"Uuml;", 5)==0) {
1899 else if (strncmp(src+1,
"auml;", 5)==0) {
1905 else if (strncmp(src+1,
"ouml;", 5)==0) {
1911 else if (strncmp(src+1,
"uuml;", 5)==0) {
1924 if (strncasecmp(src, e->
replace, l)==0) {
1946 #undef GWEN_TEXT__APPENDCHAR 1959 x=(
unsigned char)*src;
1974 snprintf(numbuf,
sizeof(numbuf),
"&#%d;", x);
1989 const char *toCharset,
1990 const char *text,
int len,
1994 if (fromCharset && *fromCharset && toCharset && *toCharset &&
1995 strcasecmp(fromCharset, toCharset)!=0) {
1998 "iconv not available, can not convert from \"%s\" to \"%s\"",
1999 fromCharset, toCharset);
2003 ic=iconv_open(toCharset, fromCharset);
2004 if (ic==((iconv_t)-1)) {
2006 fromCharset, toCharset);
2022 pInbuf=(
char *)text;
2026 outbuf=(
char *)malloc(outLeft);
2030 pInbuf=(
char *)text;
2032 done=iconv(ic, &pInbuf, &inLeft, &pOutbuf, &outLeft);
2033 if (done==(
size_t)-1) {
2035 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
void 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)