gout

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

files.c (1895B)


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