repo_writer.c (4663B)
1 #include "writer/repo_writer.h" 2 3 #include <err.h> 4 #include <stdlib.h> 5 6 #include "utils.h" 7 #include "writer/gopher/repo_writer.h" 8 #include "writer/html/repo_writer.h" 9 10 typedef void (*RepoWriterSetLogCachefile)(void* writer, const char* cachefile); 11 typedef void (*RepoWriterSetLogCommitLimit)(void* writer, size_t count); 12 typedef void (*RepoWriterSetBaseurl)(void* writer, const char* baseurl); 13 typedef void (*RepoWriterBegin)(void* writer); 14 typedef void (*RepoWriterAddCommit)(void* writer, const GitCommit* commit); 15 typedef void (*RepoWriterAddReference)(void* writer, const GitReference* ref); 16 typedef void (*RepoWriterAddFile)(void* writer, const GitFile* file); 17 typedef void (*RepoWriterEnd)(void* writer); 18 19 struct RepoWriter { 20 /* Writer implementation. */ 21 RepoWriterType type; 22 void* impl; 23 24 /* Writer configuration. */ 25 RepoWriterSetLogCachefile set_log_cachefile; 26 RepoWriterSetLogCommitLimit set_log_commit_limit; 27 RepoWriterSetBaseurl set_baseurl; 28 29 /* Writer operations. */ 30 RepoWriterBegin begin; 31 RepoWriterAddCommit add_commit; 32 RepoWriterAddReference add_reference; 33 RepoWriterAddFile add_file; 34 RepoWriterEnd end; 35 }; 36 37 static RepoWriter* htmlrepowriter_create(GitRepo* repo); 38 static void htmlrepowriter_free(RepoWriter* writer); 39 40 static RepoWriter* gopherrepowriter_create(GitRepo* repo); 41 static void gopherrepowriter_free(RepoWriter* writer); 42 43 RepoWriter* repowriter_create(RepoWriterType type, GitRepo* repo) { 44 switch (type) { 45 case kRepoWriterTypeHtml: 46 return htmlrepowriter_create(repo); 47 case kRepoWriterTypeGopher: 48 return gopherrepowriter_create(repo); 49 } 50 errx(1, "unknown RepoWriterType %d", type); 51 } 52 53 void repowriter_free(RepoWriter* writer) { 54 if (!writer) { 55 return; 56 } 57 switch (writer->type) { 58 case kRepoWriterTypeHtml: 59 htmlrepowriter_free(writer); 60 return; 61 case kRepoWriterTypeGopher: 62 gopherrepowriter_free(writer); 63 return; 64 } 65 errx(1, "unknown RepoWriterType %d", writer->type); 66 } 67 68 void repowriter_set_log_cachefile(RepoWriter* writer, const char* cachefile) { 69 writer->set_log_cachefile(writer->impl, cachefile); 70 } 71 72 void repowriter_set_log_commit_limit(RepoWriter* writer, size_t count) { 73 writer->set_log_commit_limit(writer->impl, count); 74 } 75 76 void repowriter_set_baseurl(RepoWriter* writer, const char* baseurl) { 77 writer->set_baseurl(writer->impl, baseurl); 78 } 79 80 void repowriter_begin(RepoWriter* writer) { 81 writer->begin(writer->impl); 82 } 83 84 void repowriter_add_commit(RepoWriter* writer, const GitCommit* commit) { 85 writer->add_commit(writer->impl, commit); 86 } 87 88 void repowriter_add_reference(RepoWriter* writer, const GitReference* ref) { 89 writer->add_reference(writer->impl, ref); 90 } 91 92 void repowriter_add_file(RepoWriter* writer, const GitFile* file) { 93 writer->add_file(writer->impl, file); 94 } 95 96 void repowriter_end(RepoWriter* writer) { 97 writer->end(writer->impl); 98 } 99 100 /* HtmlRepoWriter setup/teardown. */ 101 102 RepoWriter* htmlrepowriter_create(GitRepo* repo) { 103 RepoWriter* writer = ecalloc(1, sizeof(RepoWriter)); 104 HtmlRepoWriter* html_writer = html_repowriter_create(repo); 105 writer->type = kRepoWriterTypeHtml; 106 writer->impl = html_writer; 107 writer->set_log_cachefile = html_repowriter_set_log_cachefile; 108 writer->set_log_commit_limit = html_repowriter_set_log_commit_limit; 109 writer->set_baseurl = html_repowriter_set_baseurl; 110 writer->begin = html_repowriter_begin; 111 writer->add_commit = html_repowriter_add_commit; 112 writer->add_reference = html_repowriter_add_reference; 113 writer->add_file = html_repowriter_add_file; 114 writer->end = html_repowriter_end; 115 return writer; 116 } 117 118 void htmlrepowriter_free(RepoWriter* writer) { 119 if (!writer) { 120 return; 121 } 122 html_repowriter_free(writer->impl); 123 writer->impl = NULL; 124 free(writer); 125 } 126 127 /* GopherRepoWriter setup/teardown. */ 128 129 RepoWriter* gopherrepowriter_create(GitRepo* repo) { 130 RepoWriter* writer = ecalloc(1, sizeof(RepoWriter)); 131 GopherRepoWriter* gopher_writer = gopher_repowriter_create(repo); 132 writer->type = kRepoWriterTypeGopher; 133 writer->impl = gopher_writer; 134 writer->set_log_cachefile = gopher_repowriter_set_log_cachefile; 135 writer->set_log_commit_limit = gopher_repowriter_set_log_commit_limit; 136 writer->set_baseurl = gopher_repowriter_set_baseurl; 137 writer->begin = gopher_repowriter_begin; 138 writer->add_commit = gopher_repowriter_add_commit; 139 writer->add_reference = gopher_repowriter_add_reference; 140 writer->add_file = gopher_repowriter_add_file; 141 writer->end = gopher_repowriter_end; 142 return writer; 143 } 144 145 void gopherrepowriter_free(RepoWriter* writer) { 146 if (!writer) { 147 return; 148 } 149 gopher_repowriter_free(writer->impl); 150 writer->impl = NULL; 151 free(writer); 152 }