A static site generator, written in C
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cms_page.c 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "cms_page.h"
  2. #include "cms_err.h"
  3. #include <stdlib.h>
  4. #include <stddef.h>
  5. #include <string.h>
  6. #include <dirent.h>
  7. #include <sys/types.h>
  8. #include <errno.h>
  9. cms_page* cms_page_create()
  10. {
  11. cms_page* page = malloc(sizeof(cms_page));
  12. if (page == NULL)
  13. cms_err_panic(CMS_ERR_ALLOC, NULL);
  14. return page;
  15. }
  16. cms_err cms_page_parse(cms_page* page, char* str)
  17. {
  18. size_t len = strlen(str) + 1;
  19. page->_str = malloc(len * sizeof(char));
  20. if (page->_str == 0)
  21. return CMS_ERR_ALLOC;
  22. memcpy(page->_str, str, len * sizeof(char));
  23. page->title = page->_str;
  24. size_t line = 0;
  25. for (size_t i = 0; i < len; ++i)
  26. {
  27. char c = str[i];
  28. switch (c)
  29. {
  30. case '\n':
  31. line += 1;
  32. if (line == 1)
  33. page->slug = (page->_str + i + 1);
  34. case '\r':
  35. page->_str[i] = '\0';
  36. break;
  37. }
  38. if (line == 2)
  39. break;
  40. }
  41. if (line == 2)
  42. return CMS_ERR_NONE;
  43. else
  44. return CMS_ERR_PARSE;
  45. }
  46. cms_err cms_page_add_post(cms_page* page, cms_post* post)
  47. {
  48. page->numposts += 1;
  49. page->posts = realloc(page->posts, page->numposts * sizeof(cms_post));
  50. if (page->posts == NULL)
  51. return CMS_ERR_ALLOC;
  52. page->posts[page->numposts - 1] = *post;
  53. return CMS_ERR_NONE;
  54. }
  55. cms_err cms_page_create_tree(cms_page* root, const char* path)
  56. {
  57. DIR* dp = opendir(path);
  58. if (dp == NULL)
  59. return cms_err_from_std_err(errno);
  60. closedir(dp);
  61. return CMS_ERR_NONE;
  62. }