💾 Archived View for gmi.noulin.net › gitRepositories › ervy › file › utils.c.gmi captured on 2024-07-09 at 00:17:17. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-01-29)

-=-=-=-=-=-=-

ervy

Log

Files

Refs

README

LICENSE

utils.c (1167B)

     1 
     2 #include "utils.h"
     3 #include "math.h"
     4 
     5 char *padMid(char *str, size_t width) {
     6   var mid = round((double)(width - lenG(str)) / 2);
     7 
     8   if (lenG(str) > width) {
     9     ret dupG(str);
    10   }
    11   else {
    12     var pmid  = repeatS(PAD, mid);
    13     var pmid2 = repeatS(PAD, mid + ((mid * 2 + lenG(str)) > width ? -1 : 0));
    14     var r     = catS(pmid, str, pmid2);
    15     freeManyS(pmid, pmid2);
    16     ret r;
    17   }
    18 }
    19 
    20 smallJsont* verifyData(char *data) {
    21 
    22   createAllocateSmallJson(r);
    23 
    24   parseG(r, data);
    25 
    26   var top = getTopTypeG(r);
    27 
    28   if (!eqG(top, "array") or !lenG(r)) goto fail;
    29 
    30   bool allHaveKeyValue = true;
    31 
    32   range(i, lenG(r)) {
    33     var d = getG(r, rtSmallDictt, i);
    34     if (!d) {
    35       allHaveKeyValue = false;
    36       break;
    37     }
    38     if (!hasG(d, "key") or !hasG(d, "value")) {
    39       finishG(d);
    40       allHaveKeyValue = false;
    41       break;
    42     }
    43     finishG(d);
    44   }
    45 
    46   if (!allHaveKeyValue) goto fail;
    47 
    48   ret r;
    49 
    50   fail:
    51   logC("Invalid data: %s", data);
    52   terminateG(r);
    53   ret NULL;
    54 }
    55 
    56 size_t maxKeyLen(smallJsont* data) {
    57   size_t r = 0;
    58 
    59   iter(data, V) {
    60     cast(smallDictt*,v,V);
    61     r = maxV(r, lenG(getG(v, rtChar, "key")));
    62   }
    63 
    64   ret r;
    65 }
    66 
    67 // vim: set expandtab ts=2 sw=2: