6. Source mapping

Worgle has the ability to produce Org data that outlines the structure of the named blocks.

<<mapping>>=
if(map_source_code && map_filename != NULL) {
    worgle_map_files(&worg, map_filename);
}

The idea behind this functionality is to provide a quick overview of the source code structure from the blocks perspective, rather than from the text perspective.

Every file has a top level block, which is recursively iterated through.

<<function_declarations>>=
void worgle_map(worgle_d *worg, worgle_block *b, int lvl, FILE *out);
<<functions>>=
void worgle_map(worgle_d *worg, worgle_block *b, int lvl, FILE *out)
{
    int i;
    worgle_segment *s;
    worgle_block *newblk;
    worgle_hashmap *h;

    h = &worg->dict;
    if(lvl != 0) {
        for(i = 0; i <= lvl; i++) {
            fputc('*', out);
        }
        fputc(' ', out);
        worgle_string_write(out, &b->name);
        fputc('\n', out);
    }

    s = b->head;

    newblk = NULL;

    for(i = 0; i < b->nsegs; i++) {
        if(s->type == SEGTYPE_TEXT) {
            if(s->str.size > 0) {
                worgle_string_write(out, s->filename);
                fprintf(out, ":%lu\n", s->linum);
                fprintf(out, "#+NAME: ");
                worgle_string_write(out, &b->name);
                fprintf(out, "_%d\n", i);
                fprintf(out, "#+BEGIN_SRC\n");
                worgle_string_write(out, &s->str);
                fprintf(out, "#+END_SRC");
            }
            fprintf(out, "\n");
        } else if(worgle_hashmap_find(h, &s->str, &newblk)) {
            worgle_map(worg, newblk, lvl + 1, out);
        }
        s = s->nxt;
    }
}
<<function_declarations>>=
void worgle_map_files(worgle_d *worg, char *filename);
void worgle_map_a_file(worgle_d *worg, worgle_file *file, FILE *out);
<<functions>>=
void worgle_map_files(worgle_d *worg, char *filename)
{
    int n;
    worgle_file *f;
    FILE *fp;

    fp = fopen(filename, "w");
    if(fp == NULL) return;

    f = worg->flist.head;

    fprintf(fp, "#+TITLE: Code Map\n");
    for(n = 0; n < worg->flist.nfiles; n++) {
        worgle_map_a_file(worg, f, fp);
        f = f->nxt;
    }

    fclose(fp);
}

void worgle_map_a_file(worgle_d *worg, worgle_file *file, FILE *out)
{
    fprintf(out, "* ");
    worgle_string_write(out, &file->filename);
    fprintf(out, "\n");
    worgle_map(worg, file->top, 0, out);
}



prev | home | next