file.c (1399B)
1 #include "git/file.h" 2 3 #include <assert.h> 4 #include <stdlib.h> 5 #include <string.h> 6 7 #include "git/internal.h" 8 9 GitFile* gitfile_create(FileType type, 10 const char* mode, 11 const char* display_path, 12 const char* repo_path, 13 const char* commit_oid, 14 ssize_t size_bytes, 15 ssize_t size_lines, 16 const char* content) { 17 assert(display_path != NULL); 18 assert(repo_path != NULL); 19 20 GitFile* file = ecalloc(1, sizeof(GitFile)); 21 file->type = type; 22 if (mode) { 23 estrlcpy(file->mode, mode, sizeof(file->mode)); 24 } else { 25 file->mode[0] = '\0'; 26 } 27 file->display_path = display_path ? estrdup(display_path) : NULL; 28 file->repo_path = repo_path ? estrdup(repo_path) : NULL; 29 if (commit_oid) { 30 estrlcpy(file->commit_oid, commit_oid, sizeof(file->commit_oid)); 31 } else { 32 file->commit_oid[0] = '\0'; 33 } 34 file->size_bytes = size_bytes; 35 file->size_lines = size_lines; 36 if (content && size_bytes >= 0) { 37 file->content = ecalloc(size_bytes + 1, sizeof(char)); 38 memcpy(file->content, content, size_bytes); 39 } else { 40 file->content = NULL; 41 } 42 return file; 43 } 44 45 void gitfile_free(GitFile* file) { 46 if (!file) { 47 return; 48 } 49 free(file->display_path); 50 free(file->repo_path); 51 free(file->content); 52 free(file); 53 }