[rs-commit] r55 - in /redwax-tool/trunk: redwax_util.c redwax_util.h

rs-commit at redwax.eu rs-commit at redwax.eu
Mon Nov 22 21:28:34 CET 2021


Author: minfrin at redwax.eu
Date: Mon Nov 22 21:28:34 2021
New Revision: 55

Log:
Complete yaml array handling.

Modified:
    redwax-tool/trunk/redwax_util.c
    redwax-tool/trunk/redwax_util.h

Modified: redwax-tool/trunk/redwax_util.c
==============================================================================
--- redwax-tool/trunk/redwax_util.c	(original)
+++ redwax-tool/trunk/redwax_util.c	Mon Nov 22 21:28:34 2021
@@ -830,7 +830,7 @@
 {
     if (dummy) {
         redwax_metadata_t *m = dummy;
-        free(m->prefix);
+        free(m->object_prefix);
     }
 
     return APR_SUCCESS;
@@ -846,13 +846,16 @@
         m->prefix_len = 0;
     }
 
-    if (m->prefix) {
-        m->prefix = realloc(m->prefix, m->prefix_len);
-
-        if (m->prefix) {
+    if (m->object_prefix) {
+        m->object_prefix = realloc(m->object_prefix, m->prefix_len + 2);
+
+        if (m->object_prefix) {
             for (; i < m->prefix_len; i++) {
-                m->prefix[i] = ' ';
-            }
+                m->object_prefix[i] = ' ';
+            }
+            m->object_prefix[m->prefix_len] = '-';
+            m->object_prefix[m->prefix_len + 1] = ' ';
+            m->array_prefix = m->object_prefix + 2;
         }
     }
 }
@@ -871,7 +874,7 @@
     m->wv = wv;
     m->ctx = ctx;
     m->format = format;
-    m->prefix = malloc(0);
+    m->object_prefix = malloc(0);
 
     apr_pool_cleanup_register(pool, m, cleanup_metadata,
             apr_pool_cleanup_null);
@@ -938,7 +941,7 @@
 
         const struct iovec vec[] = {
                 {"\n", 1},
-                {m->prefix, m->prefix_len},
+                {m->object_prefix, m->prefix_len},
                 {"</", 2},
                 {ml->k, ml->klen},
                 {">\n", 2},
@@ -950,7 +953,7 @@
 
         const struct iovec vec[] = {
                 {"\n", 1},
-                {m->prefix, m->prefix_len},
+                {m->object_prefix, m->prefix_len},
                 {"}\n", 2}
         };
 
@@ -1000,7 +1003,7 @@
 
             const struct iovec vec[] = {
                     {"\n", 1},
-                    {m->prefix, m->prefix_len},
+                    {m->object_prefix, m->prefix_len},
                     {"<", 1},
                     {ml->k, ml->klen},
                     {" />\n", 4},
@@ -1012,7 +1015,7 @@
 
             const struct iovec vec[] = {
                     {"\n", 1},
-                    {m->prefix, m->prefix_len},
+                    {m->object_prefix, m->prefix_len},
                     {"<", 1},
                     {ml->k, ml->klen},
                     {">", 1},
@@ -1030,7 +1033,7 @@
             const struct iovec vec[] = {
                     {next ? "," : "", next ? 1 : 0},
                     {"\n", 1},
-                    {m->prefix, m->prefix_len},
+                    {m->object_prefix, m->prefix_len},
                     {"\"", 1},
                     {ml->k, ml->klen},
                     {"\": [", 4}
@@ -1043,7 +1046,7 @@
             const struct iovec vec[] = {
                     {next ? "," : "", next ? 1 : 0},
                     {"\n", 1},
-                    {m->prefix, m->prefix_len},
+                    {m->object_prefix, m->prefix_len},
                     {"[", 1}
             };
 
@@ -1058,7 +1061,7 @@
 
             const struct iovec vec[] = {
                     {"\n", 1},
-                    {m->prefix, m->prefix_len},
+                    {m->object_prefix, m->prefix_len},
                     {ml->k, ml->klen},
                     {": ", 2}
             };
@@ -1069,11 +1072,12 @@
 
             const struct iovec vec[] = {
                     {"\n", 1},
+                    {m->array_prefix, m->prefix_len},
                     {ml->k, ml->klen},
                     {": ", 2}
             };
 
-            return m->wv(m->ctx, vec, 3);
+            return m->wv(m->ctx, vec, 4);
         }
 
         break;
@@ -1089,7 +1093,7 @@
 {
     redwax_metadata_level_t *ml = apr_array_pop(m->levels);
 
-    char *prefix = m->prefix;
+    char *prefix = m->object_prefix;
     int prefix_len = m->prefix_len;
 
     int empty = m->level->empty;
@@ -1191,7 +1195,7 @@
 
                 const struct iovec vec[] = {
                         {"\n", 1},
-                        {m->prefix, m->prefix_len},
+                        {m->object_prefix, m->prefix_len},
                         {"<", 1},
                         {ml->k, ml->klen},
                         {" />\n", 4},
@@ -1203,7 +1207,7 @@
 
                 const struct iovec vec[] = {
                         {"\n", 1},
-                        {m->prefix, m->prefix_len},
+                        {m->object_prefix, m->prefix_len},
                         {"<", 1},
                         {ml->k, ml->klen},
                         {">", 1},
@@ -1222,7 +1226,7 @@
             const struct iovec vec[] = {
                     {next ? "," : "", next ? 1 : 0},
                     {"\n", 1},
-                    {m->prefix, m->prefix_len},
+                    {m->object_prefix, m->prefix_len},
                     {"\"", 1},
                     {ml->k, ml->klen},
                     {"\": {", 4}
@@ -1235,7 +1239,7 @@
             const struct iovec vec[] = {
                     {next ? "," : "", next ? 1 : 0},
                     {"\n", 1},
-                    {m->prefix, m->prefix_len},
+                    {m->object_prefix, m->prefix_len},
                     {"{", 1}
             };
 
@@ -1250,7 +1254,7 @@
 
             const struct iovec vec[] = {
                     {"\n", 1},
-                    {m->prefix, m->prefix_len},
+                    {m->object_prefix, m->prefix_len},
                     {ml->k, ml->klen},
                     {": ", 2}
             };
@@ -1261,11 +1265,12 @@
 
             const struct iovec vec[] = {
                     {"\n", 1},
-                    {m->prefix, m->prefix_len},
-                    {"- ", 2}
-            };
-
-            return m->wv(m->ctx, vec, 3);
+                    {m->array_prefix, m->prefix_len},
+                    {ml->k, ml->klen},
+                    {": ", 2}
+            };
+
+            return m->wv(m->ctx, vec, 4);
         }
 
         break;
@@ -1281,7 +1286,7 @@
 {
     redwax_metadata_level_t *ml = apr_array_pop(m->levels);
 
-    char *prefix = m->prefix;
+    char *prefix = m->object_prefix;
     int prefix_len = m->prefix_len;
 
     int empty = m->level->empty;
@@ -1380,7 +1385,7 @@
 
             const struct iovec vec[] = {
                     {"\n", 1},
-                    {m->prefix, m->prefix_len},
+                    {m->object_prefix, m->prefix_len},
                     {"<", 1},
                     {k, klen},
                     {" />", 3},
@@ -1393,7 +1398,7 @@
 
             const struct iovec vec[] = {
                     {"\n", 1},
-                    {m->prefix, m->prefix_len},
+                    {m->object_prefix, m->prefix_len},
                     {"<", 1},
                     {k, klen},
                     {">", 1},
@@ -1420,7 +1425,7 @@
                 const struct iovec vec[] = {
                         {next ? "," : "", next ? 1 : 0},
                         {"\n", 1},
-                        {m->prefix, m->prefix_len},
+                        {m->object_prefix, m->prefix_len},
                         {"\"", 1},
                         {k, klen},
                         {"\": null", 7}
@@ -1433,7 +1438,7 @@
                 const struct iovec vec[] = {
                         {next ? "," : "", next ? 1 : 0},
                         {"\n", 1},
-                        {m->prefix, m->prefix_len},
+                        {m->object_prefix, m->prefix_len},
                         {"\"", 1},
                         {k, klen},
                         {"\": \"", 4},
@@ -1452,7 +1457,7 @@
                 const struct iovec vec[] = {
                         {next ? "," : "", next ? 1 : 0},
                         {"\n", 1},
-                        {m->prefix, m->prefix_len},
+                        {m->object_prefix, m->prefix_len},
                         {"null", 4}
                 };
 
@@ -1463,7 +1468,7 @@
                 const struct iovec vec[] = {
                         {next ? "," : "", next ? 1 : 0},
                         {"\n", 1},
-                        {m->prefix, m->prefix_len},
+                        {m->object_prefix, m->prefix_len},
                         {"\"", 1},
                         {v, vlen},
                         {"\"", 1}
@@ -1486,9 +1491,9 @@
 
                 const struct iovec vec[] = {
                         {"\n", 1},
-                        {m->prefix, m->prefix_len},
+                        {m->object_prefix, m->prefix_len},
                         {k, klen},
-                        {": ", 2},
+                        {": ~", 3},
                 };
 
                 status = m->wv(m->ctx, vec, 4);
@@ -1497,7 +1502,7 @@
 
                 const struct iovec vec[] = {
                         {"\n", 1},
-                        {m->prefix, m->prefix_len},
+                        {m->object_prefix, m->prefix_len},
                         {k, klen},
                         {": ", 2}
                 };
@@ -1519,7 +1524,7 @@
                         while (APR_SUCCESS == status && l) {
 
                             const struct iovec vec[] = {
-                                    {m->prefix, m->prefix_len},
+                                    {m->object_prefix, m->prefix_len},
                                     {"  ", 2},
                                     {v, l - v + 1},
                             };
@@ -1532,7 +1537,7 @@
                         if (APR_SUCCESS == status) {
 
                             const struct iovec vec[] = {
-                                    {m->prefix, m->prefix_len},
+                                    {m->object_prefix, m->prefix_len},
                                     {"  ", 2},
                                     {v, strlen(v)},
                             };
@@ -1556,13 +1561,13 @@
 
         }
         else if (array) {
-// FIXME: yaml indentation still not 100%
+
             if (!v) {
 
                 const struct iovec vec[] = {
                         {"\n", 1},
-                        {k, klen},
-                        {":\n", 2}
+                        {m->array_prefix, m->prefix_len},
+                        {"~", 1},
                 };
 
                 status = m->wv(m->ctx, vec, 3);
@@ -1571,11 +1576,59 @@
 
                 const struct iovec vec[] = {
                         {"\n", 1},
-                        {k, klen},
-                        {":\n", 2}
+                        {m->array_prefix, m->prefix_len}
                 };
 
-                status = m->wv(m->ctx, vec, 3);
+                status = m->wv(m->ctx, vec, 2);
+
+                if (APR_SUCCESS == status) {
+
+                    void *l = strchr(v, '\n');
+
+                    if (l) {
+
+                        const struct iovec vec[] = {
+                                {"|\n", 2}
+                        };
+
+                        status = m->wv(m->ctx, vec, 1);
+
+                        while (APR_SUCCESS == status && l) {
+
+                            const struct iovec vec[] = {
+                                    {m->object_prefix, m->prefix_len},
+                                    {"  ", 2},
+                                    {v, l - v + 1},
+                            };
+
+                            status = m->wv(m->ctx, vec, 3);
+
+                            l = strchr((v = l + 1), '\n');
+                        };
+
+                        if (APR_SUCCESS == status) {
+
+                            const struct iovec vec[] = {
+                                    {m->object_prefix, m->prefix_len},
+                                    {"  ", 2},
+                                    {v, strlen(v)},
+                            };
+
+                            status = m->wv(m->ctx, vec, 3);
+                        }
+
+                    }
+
+                    else if (APR_SUCCESS == status) {
+
+                        const struct iovec vec[] = {
+                                {v, strlen(v)},
+                        };
+
+                        status = m->wv(m->ctx, vec, 1);
+                    }
+
+                }
             }
 
         }

Modified: redwax-tool/trunk/redwax_util.h
==============================================================================
--- redwax-tool/trunk/redwax_util.h	(original)
+++ redwax-tool/trunk/redwax_util.h	Mon Nov 22 21:28:34 2021
@@ -104,7 +104,8 @@
                     apr_size_t nvec);
     void *ctx;
     redwax_format_e format;
-    char *prefix;
+    char *object_prefix;
+    char *array_prefix;
     int prefix_len;
 } redwax_metadata_t;
 



More information about the rs-commit mailing list