~tagglink/node

e55c96533c28a662ddd2f4570469a8f729055c5a — Tomas Granlund 26 days ago 1a3e3c3 master
Footer can be any length

And some other refactorings
1 files changed, 44 insertions(+), 30 deletions(-)

M src/main.c
M src/main.c => src/main.c +44 -30
@@ 41,8 41,38 @@ typedef struct nav_params {
#include "debug.c"
#endif

size_t
parse_metadata_count(FILE *tree_f)
{
  size_t page_count = 0;
  int c = 0;
  int linepos = 0;
  int non_space_chars = 0;
  while (!feof(tree_f))
  {
    c = fgetc(tree_f);
    if (non_space_chars == 0 && c == '#') {
      do {
        c = fgetc(tree_f);
      } while (c != EOF && c != '\n' && !ferror(tree_f));
    }
    if (c == '\n' || c == EOF) {
      if (non_space_chars > 0) {
        page_count++;
      }
      non_space_chars = 0;
    }
    else if (!isspace(c))
    { 
      non_space_chars++;
    }
  }
  return page_count;
}

int
parse_metadata(FILE *tree_f, meta_page *pages, size_t page_count) {
parse_metadata(FILE *tree_f, meta_page *pages, size_t page_count)
{
  int curr = 0;
  int tcount = 0;
  int pcount = 0;


@@ 230,7 260,7 @@ print_nav_callback(meta_page *page, int depth, void *data) {

// new <tree> [-h <head>] [-f <foot>] [-o <output-dir>] [-c <content-dir>]
int
main(int argc, char **argv) {
main(int argc, const char **argv) {
  int exitcode = 0;
  if (argc < 2) {
    fputs("not enough args!\n", stderr);


@@ 238,33 268,15 @@ main(int argc, char **argv) {
  }

  int acount = 1;
  char *tree_file_path = argv[acount++];
  const char *tree_file_path = argv[acount++];
  FILE *tree_f = fopen(tree_file_path, "r");
  if (!tree_f) {
    fprintf(stderr, "could not open file %s\n", argv[2]);
    fprintf(stderr, "could not open file %s\n", tree_file_path);
    return ERR_FILE;
  }

  size_t page_count = 0;
  int c = 0;
  int non_space_chars = 0;
  char line[STR_SIZE];
  while (!feof(tree_f))
  {
    c = fgetc(tree_f);
    if (c == '\n' || c == EOF) {
      if (non_space_chars > 0) {
        page_count++;
      }
      non_space_chars = 0;
    }
    else if (!isspace(c))
    {
      non_space_chars++;
    }
  }
  size_t page_count = parse_metadata_count(tree_f);
  rewind(tree_f);

  meta_page *pages = malloc(sizeof(meta_page) * page_count);
  memset(pages, 0, sizeof(meta_page) * page_count);
#ifndef NDEBUG


@@ 279,11 291,11 @@ main(int argc, char **argv) {
  // print_tree(pages, 0);
  // visit_page_family(&pages[7], NULL, print_indented_page);

  char *arg;
  char *head_file_path = "head.html";
  char *foot_file_path = "foot.html";
  char *arg_output_dir = NULL;
  char *content_dir = ".";
  const char *arg;
  const char *head_file_path = "head.html";
  const char *foot_file_path = "foot.html";
  const char *arg_output_dir = NULL;
  const char *content_dir = ".";
  while (acount + 1 < argc) {
    arg = argv[acount++];
    if (strcmp(arg, "-h") == 0) {


@@ 389,8 401,10 @@ main(int argc, char **argv) {

    if (foot_f) {
      char block[STR_SIZE];
      size_t len = fread(block, sizeof(char), STR_SIZE, foot_f);
      fwrite(block, sizeof(char), len, out_f);
      while (!feof(foot_f) && !ferror(foot_f)) {
        len = fread(block, sizeof(char), STR_SIZE, foot_f);
        fwrite(block, sizeof(char), len, out_f);
      }
      rewind(foot_f);
    }
    fclose(out_f);