compare attributes case-sensitively, remove duplicate comparisons - jfconvert - JSON Feed (subset) to sfeed or Atom converter
 (HTM) git clone git://git.codemadness.org/jfconvert
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 59db718080ecd81eeaff6ac50298d488044c3001
 (DIR) parent f7cde52eef12a6e77c28199a678de8665836e9e6
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Mon,  3 Apr 2023 18:30:22 +0200
       
       compare attributes case-sensitively, remove duplicate comparisons
       
       Diffstat:
         M jf2atom.c                           |      97 +++++++++++++++----------------
       
       1 file changed, 47 insertions(+), 50 deletions(-)
       ---
 (DIR) diff --git a/jf2atom.c b/jf2atom.c
       @@ -16,6 +16,9 @@
        /* control-character in the ASCII range 0-127: compatible with UTF-8 */
        #define ISCNTRL(c) ((c) < ' ' || (c) == 0x7f)
        
       +/* compare attributes case-sensitively */
       +#define attrcmp strcmp
       +
        static int itemisopen = 0, enclosureisopen = 0;
        
        /* Escape characters below as HTML 2.0 / XML 1.0. */
       @@ -43,15 +46,15 @@ processnode(struct json_node *nodes, size_t depth, const char *value)
                if (depth == 2) {
                        if (nodes[0].type == JSON_TYPE_OBJECT) {
                                if (nodes[1].type == JSON_TYPE_STRING) {
       -                                if (!strcasecmp(nodes[1].name, "title")) {
       +                                if (!attrcmp(nodes[1].name, "title")) {
                                                fputs("<title type=\"text\">", stdout);
                                                xmlencode(value, stdout);
                                                fputs("</title>\n", stdout);
       -                                } else if (!strcasecmp(nodes[1].name, "home_page_url")) {
       +                                } else if (!attrcmp(nodes[1].name, "home_page_url")) {
                                                fputs("<link rel=\"alternate\" type=\"text/html\" href=\"", stdout);
                                                xmlencode(value, stdout);
                                                fputs("\" />\n", stdout);
       -                                } else if (!strcasecmp(nodes[1].name, "description")) {
       +                                } else if (!attrcmp(nodes[1].name, "description")) {
                                                fputs("<subtitle>", stdout);
                                                xmlencode(value, stdout);
                                                fputs("</subtitle>\n", stdout);
       @@ -65,7 +68,7 @@ processnode(struct json_node *nodes, size_t depth, const char *value)
                        if (nodes[0].type == JSON_TYPE_OBJECT &&
                            nodes[1].type == JSON_TYPE_ARRAY &&
                            nodes[2].type == JSON_TYPE_OBJECT &&
       -                    !strcasecmp(nodes[1].name, "items")) {
       +                    !attrcmp(nodes[1].name, "items")) {
                                if (enclosureisopen) {
                                        fputs(" />\n", stdout);
                                        enclosureisopen = 0;
       @@ -82,28 +85,28 @@ processnode(struct json_node *nodes, size_t depth, const char *value)
                        if (nodes[0].type == JSON_TYPE_OBJECT &&
                            nodes[1].type == JSON_TYPE_ARRAY &&
                            nodes[2].type == JSON_TYPE_OBJECT &&
       -                    !strcasecmp(nodes[1].name, "items")) {
       +                    !attrcmp(nodes[1].name, "items")) {
                                outtag = NULL;
                                outtype = NULL;
                                outhref = NULL;
        
       -                        if (!strcasecmp(nodes[3].name, "content_html")) {
       +                        if (!attrcmp(nodes[3].name, "content_html")) {
                                        outtag = "content";
                                        outtype = "html";
       -                        } else if (!strcasecmp(nodes[3].name, "content_text")) {
       +                        } else if (!attrcmp(nodes[3].name, "content_text")) {
                                        outtag = "content";
                                        outtype = "text";
       -                        } else if (!strcasecmp(nodes[3].name, "date_published")) {
       +                        } else if (!attrcmp(nodes[3].name, "date_published")) {
                                        outtag = "published";
       -                        } else if (!strcasecmp(nodes[3].name, "date_modified")) {
       +                        } else if (!attrcmp(nodes[3].name, "date_modified")) {
                                        outtag = "updated";
       -                        } else if (!strcasecmp(nodes[3].name, "id")) {
       +                        } else if (!attrcmp(nodes[3].name, "id")) {
                                        outtag = "id";
       -                        } else if (!strcasecmp(nodes[3].name, "summary")) {
       +                        } else if (!attrcmp(nodes[3].name, "summary")) {
                                        outtag = "summary";
       -                        } else if (!strcasecmp(nodes[3].name, "title")) {
       +                        } else if (!attrcmp(nodes[3].name, "title")) {
                                        outtag = "title";
       -                        } else if (!strcasecmp(nodes[3].name, "url")) {
       +                        } else if (!attrcmp(nodes[3].name, "url")) {
                                        outtag = "link";
                                        outhref = value;
                                        value = NULL;
       @@ -132,63 +135,42 @@ processnode(struct json_node *nodes, size_t depth, const char *value)
                        }
                }
        
       -        /* 1.0 author name */
                if (depth == 5) {
       +                /* 1.0 author name */
                        if (nodes[0].type == JSON_TYPE_OBJECT &&
                            nodes[1].type == JSON_TYPE_ARRAY &&
                            nodes[2].type == JSON_TYPE_OBJECT &&
                            nodes[3].type == JSON_TYPE_OBJECT &&
                            nodes[4].type == JSON_TYPE_STRING &&
       -                    !strcasecmp(nodes[1].name, "items") &&
       -                    !strcasecmp(nodes[3].name, "author") &&
       -                    !strcasecmp(nodes[4].name, "name")) {
       +                    !attrcmp(nodes[1].name, "items") &&
       +                    !attrcmp(nodes[3].name, "author") &&
       +                    !attrcmp(nodes[4].name, "name")) {
                                fputs("\t<author><name>", stdout);
                                xmlencode(value, stdout);
                                fputs("</name></author>\n", stdout);
                        }
       -        }
        
       -        /* 1.1 author name */
       -        if (depth == 6) {
       -                if (nodes[0].type == JSON_TYPE_OBJECT &&
       -                    nodes[1].type == JSON_TYPE_ARRAY &&
       -                    nodes[2].type == JSON_TYPE_OBJECT &&
       -                    nodes[3].type == JSON_TYPE_ARRAY &&
       -                    nodes[4].type == JSON_TYPE_OBJECT &&
       -                    nodes[5].type == JSON_TYPE_STRING &&
       -                    !strcasecmp(nodes[1].name, "items") &&
       -                    !strcasecmp(nodes[3].name, "authors") &&
       -                    !strcasecmp(nodes[5].name, "name")) {
       -                        fputs("\t<author><name>", stdout);
       -                        xmlencode(value, stdout);
       -                        fputs("</name></author>\n", stdout);
       -                }
       -        }
       -
       -        /* tags / categories */
       -        if (depth == 5) {
       +                /* tags / categories */
                        if (nodes[0].type == JSON_TYPE_OBJECT &&
                            nodes[1].type == JSON_TYPE_ARRAY &&
                            nodes[2].type == JSON_TYPE_OBJECT &&
                            nodes[3].type == JSON_TYPE_ARRAY &&
                            nodes[4].type == JSON_TYPE_STRING &&
       -                    !strcasecmp(nodes[1].name, "items") &&
       -                    !strcasecmp(nodes[3].name, "tags")) {
       +                    !attrcmp(nodes[1].name, "items") &&
       +                    !attrcmp(nodes[3].name, "tags")) {
                                fputs("\t<category term=\"", stdout);
                                xmlencode(value, stdout);
                                fputs("\" />\n", stdout);
                        }
       -        }
        
       -        /* enclosure */
       -        if (depth == 5) {
       +                /* enclosure */
                        if (nodes[0].type == JSON_TYPE_OBJECT &&
                            nodes[1].type == JSON_TYPE_ARRAY &&
                            nodes[2].type == JSON_TYPE_OBJECT &&
                            nodes[3].type == JSON_TYPE_ARRAY &&
                            nodes[4].type == JSON_TYPE_OBJECT &&
       -                    !strcasecmp(nodes[1].name, "items") &&
       -                    !strcasecmp(nodes[3].name, "attachments")) {
       +                    !attrcmp(nodes[1].name, "items") &&
       +                    !attrcmp(nodes[3].name, "attachments")) {
                                if (enclosureisopen)
                                        fputs(" />\n", stdout);
                                fputs("\t<link rel=\"enclosure\"", stdout);
       @@ -196,25 +178,40 @@ processnode(struct json_node *nodes, size_t depth, const char *value)
                        }
                }
        
       -        /* enclosure attributes */
                if (depth == 6) {
       +                /* 1.1 author name */
       +                if (nodes[0].type == JSON_TYPE_OBJECT &&
       +                    nodes[1].type == JSON_TYPE_ARRAY &&
       +                    nodes[2].type == JSON_TYPE_OBJECT &&
       +                    nodes[3].type == JSON_TYPE_ARRAY &&
       +                    nodes[4].type == JSON_TYPE_OBJECT &&
       +                    nodes[5].type == JSON_TYPE_STRING &&
       +                    !attrcmp(nodes[1].name, "items") &&
       +                    !attrcmp(nodes[3].name, "authors") &&
       +                    !attrcmp(nodes[5].name, "name")) {
       +                        fputs("\t<author><name>", stdout);
       +                        xmlencode(value, stdout);
       +                        fputs("</name></author>\n", stdout);
       +                }
       +
       +                /* enclosure attributes */
                        if (nodes[0].type == JSON_TYPE_OBJECT &&
                            nodes[1].type == JSON_TYPE_ARRAY &&
                            nodes[2].type == JSON_TYPE_OBJECT &&
                            nodes[3].type == JSON_TYPE_ARRAY &&
                            nodes[4].type == JSON_TYPE_OBJECT &&
                            (nodes[5].type == JSON_TYPE_STRING || nodes[5].type == JSON_TYPE_NUMBER) &&
       -                    !strcasecmp(nodes[1].name, "items") &&
       -                    !strcasecmp(nodes[3].name, "attachments")) {
       -                        if (!strcasecmp(nodes[5].name, "url")) {
       +                    !attrcmp(nodes[1].name, "items") &&
       +                    !attrcmp(nodes[3].name, "attachments")) {
       +                        if (!attrcmp(nodes[5].name, "url")) {
                                        fputs(" href=\"", stdout);
                                        xmlencode(value, stdout);
                                        fputs("\"", stdout);
       -                        } else if (!strcasecmp(nodes[5].name, "mime_type")) {
       +                        } else if (!attrcmp(nodes[5].name, "mime_type")) {
                                        fputs(" type=\"", stdout);
                                        xmlencode(value, stdout);
                                        fputs("\"", stdout);
       -                        } else if (!strcasecmp(nodes[5].name, "size_in_bytes")) {
       +                        } else if (!attrcmp(nodes[5].name, "size_in_bytes")) {
                                        fputs(" length=\"", stdout);
                                        xmlencode(value, stdout);
                                        fputs("\"", stdout);