housecat | housecat | ||||
testdir |
#include <sys/stat.h> | #include <sys/stat.h> | ||||
#include <sys/types.h> | #include <sys/types.h> | ||||
#include <fcntl.h> | |||||
#include <unistd.h> | |||||
#include <dirent.h> | #include <dirent.h> | ||||
#include <errno.h> | #include <errno.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include "cms_page.h" | #include "cms_page.h" | ||||
#include "cms_post.h" | #include "cms_post.h" | ||||
#include <stdio.h> | |||||
cms_err* cms_build_write_files(cms_page* root, char* path) | |||||
{ | |||||
char* dirpath = cms_util_path_join(path, root->slug); | |||||
if (dirpath == NULL) | |||||
return cms_err_create(CMS_ERR_ALLOC, NULL); | |||||
if (mkdir(dirpath, 077) == -1 && errno != EEXIST) | |||||
return cms_err_from_std_err(errno); | |||||
//Go through each post in the page, writing it to disk | |||||
size_t i; | |||||
for (i = 0; i < root->numposts; ++i) | |||||
{ | |||||
cms_post post = root->posts[i]; | |||||
char* postdirpath = cms_util_path_join(dirpath, post.slug); | |||||
if (postdirpath == NULL) | |||||
return cms_err_create(CMS_ERR_ALLOC, NULL); | |||||
if (mkdir(postdirpath, 0777) == -1 && errno != EEXIST) | |||||
return cms_err_from_std_err(errno); | |||||
char* filepath = cms_util_path_join(postdirpath, CMS_FILE_INDEX); | |||||
if (postdirpath == NULL) | |||||
return cms_err_create(CMS_ERR_ALLOC, NULL); | |||||
int file = open(filepath, O_WRONLY | O_CREAT, 0777); | |||||
if (file == -1) | |||||
return cms_err_from_std_err(errno); | |||||
write(file, post.html, strlen(post.html)); | |||||
close(file); | |||||
free(filepath); | |||||
free(postdirpath); | |||||
} | |||||
//Go through each sub in the page recursively | |||||
for (i = 0; i < root->numsubs; ++i) | |||||
{ | |||||
cms_page sub = root->subs[i]; | |||||
char* subdirpath = cms_util_path_join(dirpath, sub.slug); | |||||
if (subdirpath == NULL) | |||||
return cms_err_create(CMS_ERR_ALLOC, NULL); | |||||
cms_build_write_files(&sub, subdirpath); | |||||
free(subdirpath); | |||||
} | |||||
free(dirpath); | |||||
} | |||||
cms_err* cms_build_make_tree(cms_page* root, char* path, char* dirname) | cms_err* cms_build_make_tree(cms_page* root, char* path, char* dirname) | ||||
{ | { | ||||
struct dirent** namelist; | struct dirent** namelist; |
#include "cms_err.h" | #include "cms_err.h" | ||||
#include "cms_page.h" | #include "cms_page.h" | ||||
cms_err* cms_build_write_files(cms_page* root, char* path); | |||||
cms_err* cms_build_make_tree(cms_page* root, char* path, char* dirname); | cms_err* cms_build_make_tree(cms_page* root, char* path, char* dirname); | ||||
#endif | #endif |
//Directory containing the user's resources. | //Directory containing the user's resources. | ||||
#define CMS_FILE_ROOT "pages" | #define CMS_FILE_ROOT "pages" | ||||
//Directory to write the output to. | |||||
#define CMS_FILE_OUT "public" | |||||
//Name for html files | |||||
#define CMS_FILE_INDEX "index.html" | |||||
#endif | #endif |
#include "cms_template.h" | |||||
cms_template_args* cms_template_args_create() | |||||
{ | |||||
cms_template_args* args = malloc(sizeof(cms_template_args)); | |||||
if (!args) | |||||
return NULL; | |||||
args->argument = NULL; | |||||
args->argnum = 0; | |||||
args->allocd = 0; | |||||
return args; | |||||
} | |||||
cms_err* cms_template_args_append(cms_template_args* args, char* key, char* val) | |||||
{ | |||||
args->argnum += 1; | |||||
if (args->argnum > args->allocd) | |||||
{ | |||||
args->allocd *= 2; | |||||
args->arguments = realloc(args->arguments, sizeof(cms_template_arg) * args->allocd); | |||||
if (!args->arguments) | |||||
return cms_err_create(CMS_ERR_ALLOC, NULL); | |||||
} | |||||
args->argumens[args->argnum - 1] = {key, val}; | |||||
return cms_err_create(CMS_ERR_NONE, NULL); | |||||
} | |||||
static cms_err* str_insert(char* str1, char* str2, size_t start, size_t end, size_t len) | |||||
{ | |||||
size_t len2 = strlen(str2); | |||||
} | |||||
static cms_err* templatify_arg(char* str, cms_template_arg arg, len) | |||||
{ | |||||
#define OFFSET i - match_begin | |||||
size_t match_begin; | |||||
size_t i = 0; | |||||
char c; | |||||
while ((c = str[i]) != NULL) | |||||
{ | |||||
if (match_begin == NULL && arg.key[0] == c) | |||||
{ | |||||
match_begin = i; | |||||
} | |||||
if (match_begin != NULL && arg.key[OFFSET] == NULL) | |||||
{ | |||||
match_end = i; | |||||
} | |||||
i += 1; | |||||
} | |||||
#undef OFFSET | |||||
} | |||||
cms_err* cms_templateify(char* str, cms_template_args* args) | |||||
{ | |||||
size_t len = strlen(str); | |||||
size_t i; | |||||
for (i = 0; i < args->argnum; ++i) | |||||
{ | |||||
cms_err* err = templatify_arg(str, args->arguments[i], len); | |||||
if (err) | |||||
return err; | |||||
} | |||||
return cms_err_create(CMS_ERR_NONE, NULL); | |||||
} |
#ifndef CMS_TEMPLATE_H | |||||
#define CMS_TEMPLATE_H | |||||
#include "cms_err.h" | |||||
typedef struct cms_template_arg | |||||
{ | |||||
char* key; | |||||
char* val; | |||||
} cms_template_arg; | |||||
typedef struct cms_template_args | |||||
{ | |||||
cms_template_arg* arguments; | |||||
size_t argnum; | |||||
size_t allocd; | |||||
} | |||||
cms_template_args* cms_template_args_create(); | |||||
cms_err* cms_template_args_append(cms_template_args* args, char* key, char* val); | |||||
cms_err* cms_templateify(char* name, cms_template_args* args); | |||||
#endif |
#include "cms_util.h" | #include "cms_util.h" | ||||
#include "cms_err.h" | #include "cms_err.h" | ||||
#include <stdio.h> | |||||
int cms_util_file_exists(char* fname) | int cms_util_file_exists(char* fname) | ||||
{ | { | ||||
int f = open(fname, O_RDONLY); | int f = open(fname, O_RDONLY); | ||||
char* cms_util_path_join(char* str1, char* str2) | char* cms_util_path_join(char* str1, char* str2) | ||||
{ | { | ||||
if (str1 == NULL || str2 == NULL) | |||||
return NULL; | |||||
size_t len1 = strlen(str1); | size_t len1 = strlen(str1); | ||||
size_t len2 = strlen(str2); | size_t len2 = strlen(str2); | ||||
if (err) | if (err) | ||||
cms_err_panic(err); | cms_err_panic(err); | ||||
char* outpath = cms_util_path_join(dirname, CMS_FILE_OUT); | |||||
if (outpath == NULL) | |||||
cms_err_panic(cms_err_create(CMS_ERR_ALLOC, NULL)); | |||||
cms_build_write_files(root, outpath); | |||||
print_page_tree(root); | print_page_tree(root); | ||||
} | } | ||||