Skip to content

PWA Manifest Generator — C source

Assemble a valid Web App Manifest — name, colors, display mode, icon sizes, maskable purpose — with a stable key order and instant JSON you can copy or download.

This is the C implementation — the same logic the interactive tool runs, in a shareable, citable form.

/* pwa-manifest — C port: fixed-key-order manifest.json builder for the
   pwa-manifest-generator tool. C11, stdlib only. Port of
   src/lib/pwa-manifest.ts — same logic as this dir's typescript.ts and the
   live Go twin at cli/pwa-manifest-generator/pwa-manifest-generator.go.
   Fixed key order (name, short_name, description, start_url, display,
   orientation, background_color, theme_color, icons); blank optional fields
   are omitted; icon sizes default to [192, 512]; hard errors suppress
   emission; the short_name >12 warning never blocks. The caller frees every
   result (see pwa_manifest_result_free). */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Mirrors the ManifestInput interface in src/lib/pwa-manifest.ts. A NULL
   icon_sizes means "not provided" and falls back to the TS default
   [192, 512], while a non-NULL empty array emits an empty icons array —
   the same distinction the TS lib draws with `??`. */
typedef struct {
    const char *name;
    const char *short_name;
    const char *description;
    const char *start_url;
    const char *display;         /* "" means absent */
    const char *orientation;     /* "" means absent */
    const char *background_color;
    const char *theme_color;
    const int *icon_sizes;
    int icon_sizes_len;
    int maskable;
} PwaManifestInput;

/* Mirrors the ManifestResult interface in src/lib/pwa-manifest.ts. */
typedef struct {
    int ok;
    char **errors;
    int error_count;
    char **warnings;
    int warning_count;
    char *json; /* malloc'd; NULL when !ok */
} PwaManifestResult;

/* Growable byte buffer — the string builder every emitter below shares. */
typedef struct {
    char *data;
    size_t len;
    size_t cap;
} Buf;

static void buf_init(Buf *b) {
    b->cap = 64;
    b->len = 0;
    b->data = malloc(b->cap);
    b->data[0] = '\0';
}

static void buf_append_n(Buf *b, const char *s, size_t n) {
    if (b->len + n + 1 > b->cap) {
        while (b->cap < b->len + n + 1) b->cap *= 2;
        b->data = realloc(b->data, b->cap);
    }
    memcpy(b->data + b->len, s, n);
    b->len += n;
    b->data[b->len] = '\0';
}

static void buf_append(Buf *b, const char *s) { buf_append_n(b, s, strlen(s)); }

/* printf-formatted append (vsnprintf twice: measure, then write). */
static void buf_appendf(Buf *b, const char *fmt, ...) {
    va_list ap;
    va_start(ap, fmt);
    int n = vsnprintf(NULL, 0, fmt, ap);
    va_end(ap);
    if (n < 0) return;
    char *tmp = malloc((size_t) n + 1);
    va_start(ap, fmt);
    vsnprintf(tmp, (size_t) n + 1, fmt, ap);
    va_end(ap);
    buf_append_n(b, tmp, (size_t) n);
    free(tmp);
}

static char *buf_release(Buf *b) {
    char *out = b->data;
    b->data = NULL;
    return out;
}

static void push_str(char ***list, int *count, const char *s) {
    *list = realloc(*list, (size_t) (*count + 1) * sizeof(char *));
    (*list)[(*count)++] = strdup(s);
}

/* Trim leading/trailing ASCII whitespace (JS .trim() for the inputs we see). */
static char *trim_dup(const char *s) {
    if (!s) return strdup("");
    const char *start = s;
    while (*start == ' ' || *start == '\t' || *start == '\n' || *start == '\r' || *start == '\f' || *start == '\v') start++;
    size_t end = strlen(start);
    while (end > 0) {
        char c = start[end - 1];
        if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v') end--;
        else break;
    }
    char *out = malloc(end + 1);
    memcpy(out, start, end);
    out[end] = '\0';
    return out;
}

/* Same contract as TS HEX = /^#[0-9a-fA-F]{6}$/. */
static int is_hex_color(const char *s) {
    size_t n = strlen(s);
    if (n != 7 || s[0] != '#') return 0;
    for (size_t i = 1; i < n; i++) {
        char c = s[i];
        if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) return 0;
    }
    return 1;
}

/* Count UTF-8 code points — the rune count the Go twin uses for the
   short_name length warning. */
static size_t rune_count(const char *s) {
    size_t n = 0;
    while (*s) {
        if ((*s & 0xC0) != 0x80) n++;
        s++;
    }
    return n;
}

/* Double-quote s exactly like JavaScript's JSON.stringify: escape ", \, and
   the control characters (short escapes for \b \f \n \r \t, \u00xx for the
   rest), leave everything else — including non-ASCII bytes — untouched. */
static void buf_append_json_string(Buf *b, const char *s) {
    buf_append(b, "\"");
    for (const unsigned char *p = (const unsigned char *) s; *p; p++) {
        switch (*p) {
        case '"': buf_append(b, "\\\""); break;
        case '\\': buf_append(b, "\\\\"); break;
        case '\b': buf_append(b, "\\b"); break;
        case '\f': buf_append(b, "\\f"); break;
        case '\n': buf_append(b, "\\n"); break;
        case '\r': buf_append(b, "\\r"); break;
        case '\t': buf_append(b, "\\t"); break;
        default:
            if (*p < 0x20) buf_appendf(b, "\\u%04x", *p);
            else buf_append_n(b, (const char *) p, 1);
        }
    }
    buf_append(b, "\"");
}

/* Emit one "  " + JSON key + ": " + JSON value line into the lines list —
   the fixed-key-order document body, one entry per line. */
static void push_line(char ***lines, int *count, const char *key, const char *value) {
    Buf b;
    buf_init(&b);
    buf_append(&b, "  ");
    buf_append_json_string(&b, key);
    buf_append(&b, ": ");
    buf_append_json_string(&b, value);
    push_str(lines, count, b.data);
    free(b.data);
}

/* The C twin of buildManifest() in src/lib/pwa-manifest.ts. The caller
   frees every result via pwa_manifest_result_free. */
PwaManifestResult build_manifest(const PwaManifestInput *input) {
    PwaManifestResult result;
    memset(&result, 0, sizeof(result));

    char **errors = NULL;
    int error_count = 0;
    char **warnings = NULL;
    int warning_count = 0;

    char *name = trim_dup(input->name);
    char *short_name = trim_dup(input->short_name);
    char *description = trim_dup(input->description);
    char *start_url = trim_dup(input->start_url);
    char *background_color = trim_dup(input->background_color);
    char *theme_color = trim_dup(input->theme_color);

    const int default_sizes[2] = {192, 512};
    const int *sizes = input->icon_sizes ? input->icon_sizes : default_sizes;
    int sizes_len = input->icon_sizes ? input->icon_sizes_len : 2;

    if (name[0] == '\0') push_str(&errors, &error_count, "name is required.");
    if (background_color[0] != '\0' && !is_hex_color(background_color))
        push_str(&errors, &error_count, "background_color must be a #rrggbb hex value.");
    if (theme_color[0] != '\0' && !is_hex_color(theme_color))
        push_str(&errors, &error_count, "theme_color must be a #rrggbb hex value.");
    int bad_size = 0;
    for (int i = 0; i < sizes_len; i++)
        if (sizes[i] <= 0) { bad_size = 1; break; }
    if (bad_size) push_str(&errors, &error_count, "icon sizes must be positive integers.");
    for (int i = 0; i < sizes_len; i++) {
        for (int j = 0; j < i; j++) {
            if (sizes[j] == sizes[i]) {
                Buf msg;
                buf_init(&msg);
                buf_appendf(&msg, "icon size %d is listed twice.", sizes[i]);
                push_str(&errors, &error_count, msg.data);
                free(buf_release(&msg));
                break;
            }
        }
    }

    if (error_count > 0) {
        result.ok = 0;
        result.errors = errors;
        result.error_count = error_count;
    } else {
        size_t sn_len = rune_count(short_name);
        if (sn_len > 12) {
            Buf msg;
            buf_init(&msg);
            /* \xe2\x80\x94 is the em dash — kept as bytes so C sources stay ASCII-safe. */
            buf_appendf(&msg, "short_name is %zu characters \xe2\x80\x94 home-screen labels truncate around 12.",
                        sn_len);
            push_str(&warnings, &warning_count, msg.data);
            free(buf_release(&msg));
        }

        char **lines = NULL;
        int line_count = 0;
        push_line(&lines, &line_count, "name", name);
        if (short_name[0] != '\0') push_line(&lines, &line_count, "short_name", short_name);
        if (description[0] != '\0') push_line(&lines, &line_count, "description", description);
        if (start_url[0] != '\0') push_line(&lines, &line_count, "start_url", start_url);
        if (input->display && input->display[0] != '\0') push_line(&lines, &line_count, "display", input->display);
        if (input->orientation && input->orientation[0] != '\0') push_line(&lines, &line_count, "orientation", input->orientation);
        if (background_color[0] != '\0') push_line(&lines, &line_count, "background_color", background_color);
        if (theme_color[0] != '\0') push_line(&lines, &line_count, "theme_color", theme_color);

        const char *purpose = input->maskable ? "maskable" : "any";
        Buf icons;
        buf_init(&icons);
        for (int i = 0; i < sizes_len; i++) {
            if (i > 0) buf_append(&icons, ",\n");
            buf_append(&icons, "    {\n      \"src\": ");
            buf_appendf(&icons, "\"icons/icon-%dx%d.png\"", sizes[i], sizes[i]);
            buf_append(&icons, ",\n      \"sizes\": ");
            buf_appendf(&icons, "\"%dx%d\"", sizes[i], sizes[i]);
            buf_append(&icons, ",\n      \"type\": \"image/png\",\n      \"purpose\": ");
            buf_append_json_string(&icons, purpose);
            buf_append(&icons, "\n    }");
        }

        Buf json;
        buf_init(&json);
        buf_append(&json, "{\n");
        for (int i = 0; i < line_count; i++) {
            buf_append(&json, lines[i]);
            buf_append(&json, ",\n");
        }
        if (sizes_len == 0) {
            buf_append(&json, "  \"icons\": []"); /* JSON.stringify renders an empty array inline */
        } else {
            buf_append(&json, "  \"icons\": [\n");
            buf_append(&json, icons.data);
            buf_append(&json, "\n  ]");
        }
        buf_append(&json, "\n}\n");

        for (int i = 0; i < line_count; i++) free(lines[i]);
        free(lines);
        free(buf_release(&icons));

        result.ok = 1;
        result.json = buf_release(&json);
    }

    free(name);
    free(short_name);
    free(description);
    free(start_url);
    free(background_color);
    free(theme_color);

    result.errors = errors;
    result.error_count = error_count;
    result.warnings = warnings;
    result.warning_count = warning_count;
    return result;
}

/* Free everything build_manifest handed back. */
void pwa_manifest_result_free(PwaManifestResult *r) {
    for (int i = 0; i < r->error_count; i++) free(r->errors[i]);
    free(r->errors);
    for (int i = 0; i < r->warning_count; i++) free(r->warnings[i]);
    free(r->warnings);
    free(r->json);
    r->errors = NULL;
    r->warnings = NULL;
    r->json = NULL;
    r->error_count = 0;
    r->warning_count = 0;
}

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →