index_writer.c (3720B)
1 #include "writer/index_writer.h" 2 3 #include <assert.h> 4 #include <err.h> 5 #include <stdlib.h> 6 7 #include "utils.h" 8 #include "writer/gemini/index_writer.h" 9 #include "writer/gopher/index_writer.h" 10 #include "writer/html/index_writer.h" 11 12 typedef struct IndexWriterOps { 13 void (*set_me_url)(void* impl, const char* url); 14 void (*begin)(void* impl); 15 void (*add_repo)(void* impl, const GitRepo* repo); 16 void (*end)(void* impl); 17 void (*free)(void* impl); 18 } IndexWriterOps; 19 20 struct IndexWriter { 21 const IndexWriterOps* ops; 22 void* impl; 23 }; 24 25 static void html_idx_set_me_url(void* impl, const char* url) { 26 html_indexwriter_set_me_url(impl, url); 27 } 28 29 static void html_idx_begin(void* impl) { 30 html_indexwriter_begin(impl); 31 } 32 33 static void html_idx_add_repo(void* impl, const GitRepo* repo) { 34 html_indexwriter_add_repo(impl, repo); 35 } 36 37 static void html_idx_end(void* impl) { 38 html_indexwriter_end(impl); 39 } 40 41 static void html_idx_free(void* impl) { 42 html_indexwriter_free(impl); 43 } 44 45 static const IndexWriterOps kHtmlIndexWriterOps = { 46 .set_me_url = html_idx_set_me_url, 47 .begin = html_idx_begin, 48 .add_repo = html_idx_add_repo, 49 .end = html_idx_end, 50 .free = html_idx_free, 51 }; 52 53 static void gopher_idx_begin(void* impl) { 54 gopher_indexwriter_begin(impl); 55 } 56 57 static void gopher_idx_add_repo(void* impl, const GitRepo* repo) { 58 gopher_indexwriter_add_repo(impl, repo); 59 } 60 61 static void gopher_idx_end(void* impl) { 62 gopher_indexwriter_end(impl); 63 } 64 65 static void gopher_idx_free(void* impl) { 66 gopher_indexwriter_free(impl); 67 } 68 69 static const IndexWriterOps kGopherIndexWriterOps = { 70 .set_me_url = NULL, 71 .begin = gopher_idx_begin, 72 .add_repo = gopher_idx_add_repo, 73 .end = gopher_idx_end, 74 .free = gopher_idx_free, 75 }; 76 77 static void gemini_idx_begin(void* impl) { 78 gemini_indexwriter_begin(impl); 79 } 80 81 static void gemini_idx_add_repo(void* impl, const GitRepo* repo) { 82 gemini_indexwriter_add_repo(impl, repo); 83 } 84 85 static void gemini_idx_end(void* impl) { 86 gemini_indexwriter_end(impl); 87 } 88 89 static void gemini_idx_free(void* impl) { 90 gemini_indexwriter_free(impl); 91 } 92 93 static const IndexWriterOps kGeminiIndexWriterOps = { 94 .set_me_url = NULL, 95 .begin = gemini_idx_begin, 96 .add_repo = gemini_idx_add_repo, 97 .end = gemini_idx_end, 98 .free = gemini_idx_free, 99 }; 100 101 IndexWriter* indexwriter_create(IndexWriterType type, FILE* out) { 102 assert(out != NULL); 103 IndexWriter* writer = ecalloc(1, sizeof(IndexWriter)); 104 switch (type) { 105 case kIndexWriterTypeHtml: 106 writer->ops = &kHtmlIndexWriterOps; 107 writer->impl = html_indexwriter_create(out); 108 return writer; 109 case kIndexWriterTypeGopher: 110 writer->ops = &kGopherIndexWriterOps; 111 writer->impl = gopher_indexwriter_create(out); 112 return writer; 113 case kIndexWriterTypeGemini: 114 writer->ops = &kGeminiIndexWriterOps; 115 writer->impl = gemini_indexwriter_create(out); 116 return writer; 117 } 118 free(writer); 119 errx(1, "unknown IndexWriterType %d", type); 120 } 121 122 void indexwriter_free(IndexWriter* writer) { 123 if (!writer) { 124 return; 125 } 126 writer->ops->free(writer->impl); 127 writer->impl = NULL; 128 free(writer); 129 } 130 131 void indexwriter_set_me_url(IndexWriter* writer, const char* url) { 132 assert(writer != NULL); 133 assert(url != NULL); 134 if (writer->ops->set_me_url) { 135 writer->ops->set_me_url(writer->impl, url); 136 } 137 } 138 139 void indexwriter_begin(IndexWriter* writer) { 140 assert(writer != NULL); 141 writer->ops->begin(writer->impl); 142 } 143 144 void indexwriter_add_repo(IndexWriter* writer, const GitRepo* repo) { 145 assert(writer != NULL); 146 assert(repo != NULL); 147 writer->ops->add_repo(writer->impl, repo); 148 } 149 150 void indexwriter_end(IndexWriter* writer) { 151 assert(writer != NULL); 152 writer->ops->end(writer->impl); 153 }