gitout

A static git page generator
git clone https://git.bracken.jp/gitout.git
Log | Files | Refs | Submodules | README | LICENSE

files.c (1614B)


      1 #include "writer/gopher/files.h"
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <sys/types.h>
      6 
      7 #include "format.h"
      8 #include "utils.h"
      9 #include "writer/gopher/page.h"
     10 
     11 struct GopherFiles {
     12   const GitRepo* repo;
     13   FILE* out;
     14   GopherPage* page;
     15 };
     16 
     17 GopherFiles* gopher_files_create(const GitRepo* repo) {
     18   GopherFiles* files = ecalloc(1, sizeof(GopherFiles));
     19   files->repo = repo;
     20   files->out = efopen("files.gph", "w");
     21   files->page = gopher_page_create(files->out, repo, "Files", "");
     22   return files;
     23 }
     24 
     25 void gopher_files_free(GopherFiles* files) {
     26   if (!files) {
     27     return;
     28   }
     29   fclose(files->out);
     30   files->out = NULL;
     31   gopher_page_free(files->page);
     32   files->page = NULL;
     33   free(files);
     34 }
     35 
     36 void gopher_files_begin(GopherFiles* files) {
     37   FILE* out = files->out;
     38   gopher_page_begin(files->page);
     39   fprintf(out, "%-10.10s  ", "Mode");
     40   fprintf(out, "%-50.50s  ", "Name");
     41   fprintf(out, "%8.8s\n", "Size");
     42 }
     43 
     44 void gopher_files_add_file(GopherFiles* files, const GitFile* file) {
     45   FILE* out = files->out;
     46   fprintf(out, "[1|%s", gitfile_mode(file));
     47   fprintf(out, "  ");
     48   print_gopher_link_padded(out, gitfile_display_path(file), 50, ' ');
     49   fprintf(out, "  ");
     50 
     51   ssize_t size_lines = gitfile_size_lines(file);
     52   ssize_t size_bytes = gitfile_size_lines(file);
     53   if (size_lines >= 0) {
     54     fprintf(out, "%7zdL", size_lines);
     55   } else if (size_bytes >= 0) {
     56     fprintf(out, "%7zdB", size_bytes);
     57   }
     58   fprintf(out, "|file/");
     59   print_gopher_link(out, gitfile_repo_path(file));
     60   fprintf(out, ".gph|server|port]\n");
     61 }
     62 
     63 void gopher_files_end(GopherFiles* files) {
     64   gopher_page_end(files->page);
     65 }