inspace.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <faux/str.h>
  6. #include <faux/list.h>
  7. #include <faux/error.h>
  8. #include <klish/khelper.h>
  9. #include <klish/knspace.h>
  10. #include <klish/inspace.h>
  11. #define TAG "NSPACE"
  12. bool_t inspace_parse(const inspace_t *info, knspace_t *nspace, faux_error_t *error)
  13. {
  14. bool_t retcode = BOOL_TRUE;
  15. if (!info)
  16. return BOOL_FALSE;
  17. if (!nspace)
  18. return BOOL_FALSE;
  19. // Prefix
  20. if (!faux_str_is_empty(info->prefix)) {
  21. if (!knspace_set_prefix(nspace, info->prefix)) {
  22. faux_error_add(error, TAG": Illegal 'prefix' attribute");
  23. retcode = BOOL_FALSE;
  24. }
  25. }
  26. return retcode;
  27. }
  28. bool_t inspace_parse_nested(const inspace_t *inspace, knspace_t *knspace,
  29. faux_error_t *error)
  30. {
  31. bool_t retval = BOOL_TRUE;
  32. if (!knspace || !inspace) {
  33. faux_error_add(error, TAG": Internal error");
  34. return BOOL_FALSE;
  35. }
  36. // For now it's empty. NSPACE has no nested elements.
  37. if (!retval)
  38. faux_error_sprintf(error, TAG" \"%s\": Illegal nested elements",
  39. knspace_view_ref(knspace));
  40. return retval;
  41. }
  42. knspace_t *inspace_load(const inspace_t *inspace, faux_error_t *error)
  43. {
  44. knspace_t *knspace = NULL;
  45. if (!inspace)
  46. return NULL;
  47. // Ref [mandatory]
  48. if (faux_str_is_empty(inspace->ref)) {
  49. faux_error_add(error, TAG": Empty 'ref' attribute");
  50. return NULL;
  51. }
  52. knspace = knspace_new(inspace->ref);
  53. if (!knspace) {
  54. faux_error_sprintf(error, TAG": \"%s\": Can't create object",
  55. inspace->ref);
  56. return NULL;
  57. }
  58. if (!inspace_parse(inspace, knspace, error)) {
  59. knspace_free(knspace);
  60. return NULL;
  61. }
  62. // Parse nested elements
  63. if (!inspace_parse_nested(inspace, knspace, error)) {
  64. knspace_free(knspace);
  65. return NULL;
  66. }
  67. return knspace;
  68. }
  69. char *inspace_deploy(const knspace_t *knspace, int level)
  70. {
  71. char *str = NULL;
  72. char *tmp = NULL;
  73. if (!knspace)
  74. return NULL;
  75. tmp = faux_str_sprintf("%*cnspace {\n", level, ' ');
  76. faux_str_cat(&str, tmp);
  77. faux_str_free(tmp);
  78. attr2ctext(&str, "ref", knspace_view_ref(knspace), level + 1);
  79. attr2ctext(&str, "prefix", knspace_prefix(knspace), level + 1);
  80. tmp = faux_str_sprintf("%*c},\n\n", level, ' ');
  81. faux_str_cat(&str, tmp);
  82. faux_str_free(tmp);
  83. return str;
  84. }