index_writer.c (3049B)
1 #include "writer/index_writer.h" 2 3 #include <err.h> 4 #include <stdlib.h> 5 6 #include "utils.h" 7 #include "writer/gopher/index_writer.h" 8 #include "writer/html/index_writer.h" 9 10 typedef void (*IndexWriterVoidFunc)(void* impl); 11 typedef void (*IndexWriterStringFunc)(void* impl, const char* url); 12 typedef void (*IndexWriterRepoInfoFunc)(void* impl, GitRepo* repo); 13 14 struct IndexWriter { 15 /* Writer implementation. */ 16 IndexWriterType type; 17 void* impl; 18 19 /* Writer operations. */ 20 IndexWriterStringFunc set_me_url; 21 IndexWriterVoidFunc begin; 22 IndexWriterRepoInfoFunc add_repo; 23 IndexWriterVoidFunc end; 24 }; 25 26 static IndexWriter* htmlindexwriter_create(); 27 static void htmlindexwriter_free(IndexWriter* writer); 28 29 static IndexWriter* gopherindexwriter_create(); 30 static void gopherindexwriter_free(IndexWriter* writer); 31 32 IndexWriter* indexwriter_create(IndexWriterType type) { 33 switch (type) { 34 case kIndexWriterTypeHtml: 35 return htmlindexwriter_create(); 36 case kIndexWriterTypeGopher: 37 return gopherindexwriter_create(); 38 } 39 errx(1, "unknown IndexWriterType %d", type); 40 } 41 42 void indexwriter_free(IndexWriter* writer) { 43 if (!writer) { 44 return; 45 } 46 switch (writer->type) { 47 case kIndexWriterTypeHtml: 48 htmlindexwriter_free(writer); 49 return; 50 case kIndexWriterTypeGopher: 51 gopherindexwriter_free(writer); 52 return; 53 } 54 errx(1, "unknown IndexWriterType %d", writer->type); 55 } 56 57 void indexwriter_set_me_url(IndexWriter* writer, const char* url) { 58 writer->set_me_url(writer->impl, url); 59 } 60 61 void indexwriter_begin(IndexWriter* writer) { 62 writer->begin(writer->impl); 63 } 64 65 void indexwriter_add_repo(IndexWriter* writer, GitRepo* repo) { 66 writer->add_repo(writer->impl, repo); 67 } 68 69 void indexwriter_end(IndexWriter* writer) { 70 writer->end(writer->impl); 71 } 72 73 /* HtmlIndexWriter setup/teardown. */ 74 75 IndexWriter* htmlindexwriter_create() { 76 IndexWriter* writer = ecalloc(1, sizeof(IndexWriter)); 77 HtmlIndexWriter* html_writer = html_indexwriter_create(); 78 writer->type = kIndexWriterTypeHtml; 79 writer->impl = html_writer; 80 writer->set_me_url = html_indexwriter_set_me_url; 81 writer->begin = html_indexwriter_begin; 82 writer->add_repo = html_indexwriter_add_repo; 83 writer->end = html_indexwriter_end; 84 return writer; 85 } 86 87 void htmlindexwriter_free(IndexWriter* writer) { 88 if (!writer) { 89 return; 90 } 91 html_indexwriter_free(writer->impl); 92 writer->impl = NULL; 93 free(writer); 94 } 95 96 /* GopherIndexWriter setup/teardown. */ 97 98 IndexWriter* gopherindexwriter_create() { 99 IndexWriter* writer = ecalloc(1, sizeof(IndexWriter)); 100 GopherIndexWriter* gopher_writer = gopher_indexwriter_create(); 101 writer->type = kIndexWriterTypeGopher; 102 writer->impl = gopher_writer; 103 writer->set_me_url = NULL; 104 writer->begin = gopher_indexwriter_begin; 105 writer->add_repo = gopher_indexwriter_add_repo; 106 writer->end = gopher_indexwriter_end; 107 return writer; 108 } 109 110 void gopherindexwriter_free(IndexWriter* writer) { 111 if (!writer) { 112 return; 113 } 114 gopher_indexwriter_free(writer->impl); 115 writer->impl = NULL; 116 free(writer); 117 }