Ignore:
Timestamp:
02/27/13 14:34:31 (9 years ago)
Author:
gf
Message:

webif: Create and use private templates array.

This allows storing of additional information along with templates
data (which is const).

For now we store name hash but in the future the data that comes from
struct templates (in webif/pages.c) may need to be preprocessed
(uncompressed for example) and the abstraction in this commit makes
it easier to do such changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/webif/pages_gen.c

    r8427 r8434  
    4343        char file[128];
    4444        char deps[256];
     45        uint32_t data_len;
    4546        enum { TXT, BIN } type;
    4647    } data[MAX_TEMPLATES];
     
    204205}
    205206
    206 static void parse_deps(int tpl_idx) {
     207static void print_template(int tpl_idx) {
    207208    static bool ifdef_open = 0;
    208209    char *prev_deps = "";
     
    240241    }
    241242
    242     fprintf(output_file, "\t{ .tpl_name=\"%s\", .tpl_data=%s%s, .tpl_deps=\"%s\" },\n",
     243    fprintf(output_file, "\t{ .tpl_name=\"%s\", .tpl_data=%s%s, .tpl_deps=\"%s\", .tpl_data_len=%u },\n",
    243244        ident,
    244245        templates.data[tpl_idx].type == TXT ? "TPL" : "", ident,
    245         deps);
     246        deps,
     247        templates.data[tpl_idx].data_len
     248    );
    246249
    247250    if (ifdef_open && strcmp(deps, next_deps) != 0) {
     
    251254}
    252255
    253 static void dump_text(char *ident, uint8_t *buf, size_t buf_len) {
     256static uint32_t dump_text(char *ident, uint8_t *buf, size_t buf_len) {
    254257    int i;
    255258    fprintf(output_file, "#define TPL%s \\\n\"", ident);
     
    268271    }
    269272    fprintf(output_file, "\"\n\n");
     273    return buf_len;
    270274}
    271275
     
    282286}
    283287
    284 static void dump_base64(char *ident, char *mime, uint8_t *buf, size_t buf_len) {
     288static uint32_t dump_base64(char *ident, char *mime, uint8_t *buf, size_t buf_len) {
     289    char tpl_type[32];
    285290    size_t b64_buf_len = buf_len * 4 + 16;
    286291    uint8_t *b64_buf = malloc(b64_buf_len);
     
    288293        die("%s: can't alloc %zd bytes\n", __func__, b64_buf_len);
    289294    int i, b64_len = b64_encode(buf, buf_len, b64_buf, b64_buf_len);
    290     fprintf(output_file, "#define %s \"data:%s;base64,\\\n", ident, mime);
     295    snprintf(tpl_type, sizeof(tpl_type), "data:%s;base64,", mime);
     296    fprintf(output_file, "#define %s \"%s\\\n", ident, tpl_type);
    291297    for (i = 0; i < b64_len; i++) {
    292298        if (i && i % 76 == 0)
     
    296302    fprintf(output_file, "\"\n\n");
    297303    free(b64_buf);
     304    return strlen(tpl_type) + b64_len;
    298305}
    299306
     
    311318    fprintf(output_file, "  char *tpl_data;\n");
    312319    fprintf(output_file, "  char *tpl_deps;\n");
     320    fprintf(output_file, "  uint32_t tpl_data_len;\n");
    313321    fprintf(output_file, "};\n");
    314322    fprintf(output_file, "\n");
    315     fprintf(output_file, "int32_t tpl_count(void);\n");
     323    fprintf(output_file, "int32_t templates_count(void);\n");
    316324    fprintf(output_file, "\n");
    317325    fprintf(output_file, "#endif\n");
     
    328336    for (i = 0; i < templates.num; i++) {
    329337        uint8_t *buf;
    330         size_t buf_len;
     338        size_t buf_len, data_len = 0;
    331339        readfile(templates.data[i].file, &buf, &buf_len);
    332340        switch (templates.data[i].type) {
    333             case TXT: dump_text(templates.data[i].ident, buf, buf_len); break;
    334             case BIN: dump_base64(templates.data[i].ident, get_mime(templates.data[i].file), buf, buf_len); break;
     341            case TXT: data_len = dump_text(templates.data[i].ident, buf, buf_len); break;
     342            case BIN: data_len = dump_base64(templates.data[i].ident, get_mime(templates.data[i].file), buf, buf_len); break;
    335343        }
    336344        free(buf);
     345        templates.data[i].data_len = data_len;
    337346    }
    338347
    339348    fprintf(output_file, "const struct template templates[] = {\n");
    340349    for (i = 0; i < templates.num; i++) {
    341         parse_deps(i);
     350        print_template(i);
    342351    }
    343352    fprintf(output_file, "};\n");
    344353    fprintf(output_file, "\n");
    345     fprintf(output_file, "int32_t tpl_count(void) { return sizeof(templates) / sizeof(struct template); }\n");
     354    fprintf(output_file, "int32_t templates_count(void) { return sizeof(templates) / sizeof(struct template); }\n");
    346355    fprintf(output_file, "\n");
    347356    fprintf(output_file, "#endif\n");
Note: See TracChangeset for help on using the changeset viewer.