123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- #include <stddef.h>
- #include <string.h>
- #include <dirent.h>
- #include <sys/types.h>
- #include <errno.h>
-
- #include "cms_page.h"
- #include "cms_err.h"
- #include <stdlib.h>
-
- #define PREFIX_LENGTH 5
-
- cms_page* cms_page_create()
- {
- cms_page* page = malloc(sizeof(cms_page));
- page->_str = NULL;
- page->title = NULL;
- page->slug = NULL;
- page->numposts = 0;
- page->numsubs = 0;
- page->posts = NULL;
- page->subs = NULL;
- return page;
- }
-
- cms_err* cms_page_parse(cms_page* page, char* str, char* slugstr)
- {
- //Adding 1 because strlen() returns the length without \0
- size_t len = strlen(str) + 1;
-
- page->_str = malloc(len * sizeof(char));
- if (page->_str == NULL)
- return cms_err_create(CMS_ERR_ALLOC, NULL);
-
- memcpy(page->_str, str, len * sizeof(char));
-
- //The page's title will be the first line.
- page->title = page->_str;
-
- //Replace newlines with \0
- for (size_t i = 0; i < len; ++i)
- {
- char c = str[i];
-
- switch (c)
- {
- case '\n':
- case '\r':
- page->_str[i] = '\0';
- break;
- }
- }
-
- //Strip out the leading "xxxx-" from slugstr (the filename)
- //to get the real slug
- size_t slugstrlen = strlen(slugstr);
- page->slug = malloc((slugstrlen + 1 - PREFIX_LENGTH) * sizeof(char));
- if (page->slug == NULL)
- return cms_err_create(CMS_ERR_ALLOC, NULL);
-
- memcpy(page->slug, slugstr + PREFIX_LENGTH, (slugstrlen - PREFIX_LENGTH));
-
- //Add \0 to the end of the string
- page->slug[slugstrlen - PREFIX_LENGTH] = '\0';
-
- return cms_err_create(CMS_ERR_NONE, NULL);
- }
-
- cms_err* cms_page_add_post(cms_page* page, cms_post* post)
- {
- page->numposts += 1;
-
- page->posts = realloc(page->posts, page->numposts * sizeof(cms_post));
- if (page->posts == NULL)
- return cms_err_create(CMS_ERR_ALLOC, NULL);
-
- page->posts[page->numposts - 1] = *post;
-
- return cms_err_create(CMS_ERR_NONE, NULL);
- }
-
- cms_err* cms_page_add_sub(cms_page* page, cms_page* sub)
- {
- page->numsubs += 1;
- page->subs = realloc(page->subs, page->numsubs * sizeof(cms_page));
- if (page->subs == NULL)
- return cms_err_create(CMS_ERR_ALLOC, NULL);
-
- page->subs[page->numsubs - 1] = *sub;
-
- return cms_err_create(CMS_ERR_NONE, NULL);
- }
|