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. C++17, standard library 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.
#include <cstdint>
#include <sstream>
#include <string>
#include <vector>
// Mirrors the ManifestInput interface in src/lib/pwa-manifest.ts. A nullopt
// icon_sizes means "not provided" and falls back to the TS default
// [192, 512], while an explicit empty vector emits an empty icons array —
// the same distinction the TS lib draws with `??`.
struct ManifestInput {
std::string name;
std::string short_name;
std::string description;
std::string start_url;
std::string display; // "" means absent
std::string orientation; // "" means absent
std::string background_color;
std::string theme_color;
std::vector<long long> icon_sizes; // empty AND has_sizes==false → default
bool has_sizes = false;
bool maskable = false;
};
// Mirrors the ManifestResult interface in src/lib/pwa-manifest.ts.
struct ManifestResult {
bool ok = false;
std::vector<std::string> errors;
std::vector<std::string> warnings;
std::string json;
};
// Same contract as TS HEX = /^#[0-9a-fA-F]{6}$/.
static bool is_hex_color(const std::string &s) {
if (s.size() != 7 || s[0] != '#') return false;
for (size_t i = 1; i < s.size(); i++) {
char c = s[i];
bool hex = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
if (!hex) return false;
}
return true;
}
// Count UTF-8 code points — the rune count the Go twin uses for the
// short_name length warning.
static size_t rune_count(const std::string &s) {
size_t n = 0;
for (unsigned char c : s)
if ((c & 0xC0) != 0x80) n++;
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 std::string json_string(const std::string &s) {
std::ostringstream out;
out << '"';
for (unsigned char c : s) {
switch (c) {
case '"': out << "\\\""; break;
case '\\': out << "\\\\"; break;
case '\b': out << "\\b"; break;
case '\f': out << "\\f"; break;
case '\n': out << "\\n"; break;
case '\r': out << "\\r"; break;
case '\t': out << "\\t"; break;
default:
if (c < 0x20) {
char buf[8];
snprintf(buf, sizeof(buf), "\\u%04x", c);
out << buf;
} else {
out << static_cast<char>(c);
}
}
}
out << '"';
return out.str();
}
// The C++ twin of buildManifest() in src/lib/pwa-manifest.ts.
ManifestResult build_manifest(const ManifestInput &input) {
using std::string;
using std::vector;
ManifestResult result;
vector<string> &errors = result.errors;
auto trim = [](const string &s) {
size_t b = 0, e = s.size();
while (b < e && (s[b] == ' ' || s[b] == '\t' || s[b] == '\n' || s[b] == '\r' || s[b] == '\f' || s[b] == '\v')) b++;
while (e > b && (s[e - 1] == ' ' || s[e - 1] == '\t' || s[e - 1] == '\n' || s[e - 1] == '\r' || s[e - 1] == '\f' || s[e - 1] == '\v')) e--;
return s.substr(b, e - b);
};
const string name = trim(input.name);
const string short_name = trim(input.short_name);
const string description = trim(input.description);
const string start_url = trim(input.start_url);
const string background_color = trim(input.background_color);
const string theme_color = trim(input.theme_color);
// `has_sizes` mirrors the TS `??` distinction: absent → [192, 512],
// explicit empty vector → empty icons array.
const vector<long long> default_sizes = {192, 512};
const vector<long long> &sizes = input.has_sizes ? input.icon_sizes : default_sizes;
if (name.empty()) errors.push_back("name is required.");
if (!background_color.empty() && !is_hex_color(background_color))
errors.push_back("background_color must be a #rrggbb hex value.");
if (!theme_color.empty() && !is_hex_color(theme_color))
errors.push_back("theme_color must be a #rrggbb hex value.");
bool bad_size = false;
for (long long s : sizes)
if (s <= 0) { bad_size = true; break; }
if (bad_size) errors.push_back("icon sizes must be positive integers.");
for (size_t i = 0; i < sizes.size(); i++) {
for (size_t j = 0; j < i; j++) {
if (sizes[j] == sizes[i]) {
errors.push_back("icon size " + std::to_string(sizes[i]) + " is listed twice.");
break;
}
}
}
if (!errors.empty()) {
result.ok = false;
result.warnings.clear();
result.json.clear();
return result;
}
const size_t sn_len = rune_count(short_name);
if (sn_len > 12) {
result.warnings.push_back("short_name is " + std::to_string(sn_len) +
" characters \xe2\x80\x94 home-screen labels truncate around 12.");
}
vector<string> lines;
auto add = [&lines](const string &key, const string &value) {
lines.push_back(" " + json_string(key) + ": " + json_string(value));
};
add("name", name);
if (!short_name.empty()) add("short_name", short_name);
if (!description.empty()) add("description", description);
if (!start_url.empty()) add("start_url", start_url);
if (!input.display.empty()) add("display", input.display);
if (!input.orientation.empty()) add("orientation", input.orientation);
if (!background_color.empty()) add("background_color", background_color);
if (!theme_color.empty()) add("theme_color", theme_color);
const string purpose = input.maskable ? "maskable" : "any";
vector<string> icon_entries;
for (long long s : sizes) {
std::ostringstream entry;
entry << " {\n"
<< " \"src\": " << json_string("icons/icon-" + std::to_string(s) + "x" + std::to_string(s) + ".png") << ",\n"
<< " \"sizes\": " << json_string(std::to_string(s) + "x" + std::to_string(s)) << ",\n"
<< " \"type\": \"image/png\",\n"
<< " \"purpose\": " << json_string(purpose) << "\n"
<< " }";
icon_entries.push_back(entry.str());
}
std::ostringstream doc;
doc << "{\n";
for (const string &line : lines) doc << line << ",\n";
if (icon_entries.empty()) {
doc << " \"icons\": []"; // JSON.stringify renders an empty array inline
} else {
doc << " \"icons\": [\n";
for (size_t i = 0; i < icon_entries.size(); i++) {
if (i > 0) doc << ",\n";
doc << icon_entries[i];
}
doc << "\n ]";
}
doc << "\n}\n";
result.ok = true;
result.json = doc.str();
return result;
}
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 →