1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- #include "cms_page.h"
- #include "cms_err.h"
- #include <stdlib.h>
- #include <stddef.h>
- #include <string.h>
- #include <dirent.h>
- #include <sys/types.h>
- #include <errno.h>
-
- cms_page* cms_page_create()
- {
- cms_page* page = malloc(sizeof(cms_page));
- if (page == NULL)
- cms_err_panic(CMS_ERR_ALLOC, NULL);
- return page;
- }
-
- cms_err cms_page_parse(cms_page* page, char* str)
- {
- size_t len = strlen(str) + 1;
-
- page->_str = malloc(len * sizeof(char));
-
- if (page->_str == 0)
- return CMS_ERR_ALLOC;
-
- memcpy(page->_str, str, len * sizeof(char));
-
- page->title = page->_str;
-
- size_t line = 0;
- for (size_t i = 0; i < len; ++i)
- {
- char c = str[i];
-
- switch (c)
- {
- case '\n':
- line += 1;
- if (line == 1)
- page->slug = (page->_str + i + 1);
-
- case '\r':
- page->_str[i] = '\0';
- break;
- }
-
- if (line == 2)
- break;
- }
-
- if (line == 2)
- return CMS_ERR_NONE;
- else
- return CMS_ERR_PARSE;
- }
-
- 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_ALLOC;
-
- page->posts[page->numposts - 1] = *post;
-
- return CMS_ERR_NONE;
- }
-
- cms_err cms_page_create_tree(cms_page* root, const char* path)
- {
- DIR* dp = opendir(path);
- if (dp == NULL)
- return cms_err_from_std_err(errno);
-
- closedir(dp);
-
- return CMS_ERR_NONE;
- }
|