gout

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

files.c (1852B)


      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   gopher_page_free(files->page);
     40   free(files);
     41 }
     42 
     43 void gopher_files_begin(GopherFiles* files) {
     44   assert(files != NULL);
     45   FILE* out = files->out;
     46   gopher_page_begin(files->page);
     47   fprintf(out, "%-10.10s  ", "Mode");
     48   fprintf(out, "%-50.50s  ", "Name");
     49   fprintf(out, "%8.8s\n", "Size");
     50 }
     51 
     52 void gopher_files_add_file(GopherFiles* files, const GitFile* file) {
     53   assert(files != NULL);
     54   assert(file != NULL);
     55   FILE* out = files->out;
     56   fprintf(out, "[1|%s", file->mode);
     57   fprintf(out, "  ");
     58   print_gopher_link_padded(out, file->display_path, 50, ' ');
     59   fprintf(out, "  ");
     60 
     61   ssize_t size_lines = file->size_lines;
     62   ssize_t size_bytes = file->size_bytes;
     63   if (size_lines >= 0) {
     64     fprintf(out, "%7zdL", size_lines);
     65   } else if (size_bytes >= 0) {
     66     fprintf(out, "%7zdB", size_bytes);
     67   }
     68   fprintf(out, "|file/");
     69   print_gopher_link(out, file->repo_path);
     70   fprintf(out, ".gph|server|port]\n");
     71 }
     72 
     73 void gopher_files_end(GopherFiles* files) {
     74   assert(files != NULL);
     75   gopher_page_end(files->page);
     76 }