From 07606d60544c25129ee8968f49f13711f1119a54 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Thu, 8 May 2014 14:12:18 +0000 Subject: sfeed: make code a bit more clear Signed-off-by: Hiltjo Posthuma --- TODO | 4 +++- sfeed.c | 17 +++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/TODO b/TODO index dc238ac..7621c49 100644 --- a/TODO +++ b/TODO @@ -7,4 +7,6 @@ [ ] dogfood sfeed_frames, html content. [ ] in links browsers: items are centered in the table for some reason. [ ] feeds file: empty tab (encoding can't be detected?) -[ ] optimize afgets() +[ ] optimize + [ ] afgets() + [ ] namedentitytostr, xml parser, etc. diff --git a/sfeed.c b/sfeed.c index 0db0a4d..2b4c9b7 100644 --- a/sfeed.c +++ b/sfeed.c @@ -145,9 +145,8 @@ codepointtoutf8(unsigned long cp, unsigned long *utf) { return *utf ? 1 : 0; /* 1 byte */ } -static int +static size_t namedentitytostr(const char *e, char *buffer, size_t bufsiz) { - /* TODO: optimize lookup? */ char *entities[6][2] = { { "<", "<" }, { ">", ">" }, @@ -171,12 +170,14 @@ namedentitytostr(const char *e, char *buffer, size_t bufsiz) { return 0; } -static int +/* convert named- or numeric entity string to buffer string + * returns byte-length of string. */ +static size_t entitytostr(const char *e, char *buffer, size_t bufsiz) { unsigned long l = 0, cp = 0, b; size_t len; - if(*e != '&' || bufsiz < 5) /* doesnt start with & */ + if(*e != '&' || bufsiz < 5) /* doesn't start with & */ return 0; if(e[1] == '#') { e += 2; /* skip &# */ @@ -197,17 +198,17 @@ entitytostr(const char *e, char *buffer, size_t bufsiz) { buffer[0] = '\\'; buffer[1] = 'n'; buffer[2] = '\0'; - return 2; + return 2; /* len */ } else if(buffer[0] == '\\') { /* escape \ */ buffer[0] = '\\'; buffer[1] = '\\'; buffer[2] = '\0'; - return 2; + return 2; /* len */ } else if(buffer[0] == '\t') { /* escape tab */ buffer[0] = '\\'; buffer[1] = 't'; buffer[2] = '\0'; - return 2; + return 2; /* len */ } } return len; @@ -601,7 +602,7 @@ xml_handler_data_entity(XMLParser *p, const char *data, size_t datalen) { size_t len; /* try to translate entity, else just pass as data */ - if((len = entitytostr(data, buffer, sizeof(buffer)))) + if((len = entitytostr(data, buffer, sizeof(buffer))) > 0) xml_handler_data(p, buffer, len); else xml_handler_data(p, data, datalen); -- cgit v1.2.3