Browse Source

various changes

master
mort 8 years ago
parent
commit
79058ebf21
8 changed files with 180 additions and 0 deletions
  1. 1
    0
      .gitignore
  2. 58
    0
      src/cms_build.c
  3. 2
    0
      src/cms_build.h
  4. 6
    0
      src/cms_files.h
  5. 77
    0
      src/cms_template.c
  6. 25
    0
      src/cms_template.h
  7. 5
    0
      src/cms_util.c
  8. 6
    0
      src/main.c

+ 1
- 0
.gitignore View File

@@ -1 +1,2 @@
housecat
testdir

+ 58
- 0
src/cms_build.c View File

@@ -1,5 +1,7 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
@@ -12,6 +14,62 @@
#include "cms_page.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)
{
struct dirent** namelist;

+ 2
- 0
src/cms_build.h View File

@@ -4,6 +4,8 @@
#include "cms_err.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);

#endif

+ 6
- 0
src/cms_files.h View File

@@ -14,4 +14,10 @@
//Directory containing the user's resources.
#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

+ 77
- 0
src/cms_template.c View File

@@ -0,0 +1,77 @@
#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);
}

+ 25
- 0
src/cms_template.h View File

@@ -0,0 +1,25 @@
#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

+ 5
- 0
src/cms_util.c View File

@@ -12,6 +12,8 @@
#include "cms_util.h"
#include "cms_err.h"

#include <stdio.h>

int cms_util_file_exists(char* fname)
{
int f = open(fname, O_RDONLY);
@@ -147,6 +149,9 @@ cms_err* cms_util_dir_copy_recursive(char* dir1, char* dir2)

char* cms_util_path_join(char* str1, char* str2)
{
if (str1 == NULL || str2 == NULL)
return NULL;

size_t len1 = strlen(str1);
size_t len2 = strlen(str2);


+ 6
- 0
src/main.c View File

@@ -106,6 +106,12 @@ int main(int argc, char** argv)
if (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);
}


Loading…
Cancel
Save