A static site generator, written in C
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <stdlib.h>
  2. #include <stddef.h>
  3. #include <string.h>
  4. #include "cms_post.h"
  5. #include "cms_err.h"
  6. #define PREFIX_LENGTH 5
  7. cms_post* cms_post_create()
  8. {
  9. cms_post* post = malloc(sizeof(cms_post));
  10. return post;
  11. }
  12. cms_err* cms_post_parse(cms_post* post, char* str, char* slugstr)
  13. {
  14. //Adding 1 because strlen() returns the length without \0
  15. size_t len = strlen(str) + 1;
  16. post->_str = malloc(len * sizeof(char));
  17. if (post->_str == NULL)
  18. return cms_err_create(CMS_ERR_ALLOC, NULL);
  19. memcpy(post->_str, str, len * sizeof(char));
  20. //The post's title will be the first line.
  21. post->title = post->_str;
  22. size_t line = 0;
  23. for (size_t i = 0; i < len; ++i)
  24. {
  25. char c = str[i];
  26. switch (c)
  27. {
  28. case '\n':
  29. line += 1;
  30. if (line == 2)
  31. {
  32. post->html = (post->_str + i + 1);
  33. }
  34. //falls through
  35. case '\r':
  36. post->_str[i] = '\0';
  37. break;
  38. }
  39. if (line == 2)
  40. break;
  41. }
  42. //Strip out the leading "xxxx-" from slugstr (the filename)
  43. //to get the real slug
  44. size_t slugstrlen = strlen(slugstr);
  45. post->slug = malloc((slugstrlen + 1 - PREFIX_LENGTH) * sizeof(char));
  46. if (post->slug == NULL)
  47. return cms_err_create(CMS_ERR_ALLOC, NULL);
  48. memcpy(post->slug, slugstr + PREFIX_LENGTH, (slugstrlen - PREFIX_LENGTH));
  49. //Add \0 to the end of the string
  50. post->slug[slugstrlen - PREFIX_LENGTH] = '\0';
  51. if (line >= 2)
  52. return cms_err_create(CMS_ERR_NONE, NULL);
  53. else
  54. return cms_err_create(CMS_ERR_PARSE, slugstr);
  55. }