Browse Source

ischeme: inspace

Serj Kalichev 3 years ago
parent
commit
09c0a9d890
4 changed files with 152 additions and 0 deletions
  1. 11 0
      klish/Makefile.am
  2. 28 0
      klish/inspace.h
  3. 1 0
      klish/ischeme/Makefile.am
  4. 112 0
      klish/ischeme/inspace.c

+ 11 - 0
klish/Makefile.am

@@ -34,6 +34,17 @@ nobase_include_HEADERS += \
 	klish/knspace.h \
 	klish/kdb.h
 
+# iScheme
+nobase_include_HEADERS += \
+	klish/ischeme.h \
+	klish/iview.h \
+	klish/icommand.h \
+	klish/iparam.h \
+	klish/iplugin.h \
+	klish/iaction.h \
+	klish/iptype.h \
+	klish/inspace.h
+
 # Session
 nobase_include_HEADERS += \
 	klish/kudata.h \

+ 28 - 0
klish/inspace.h

@@ -0,0 +1,28 @@
+/** @file nspace.h
+ *
+ * @brief Klish scheme's "nspace" entry
+ */
+
+#ifndef _klish_inspace_h
+#define _klish_inspace_h
+
+#include <faux/error.h>
+#include <klish/icommand.h>
+#include <klish/knspace.h>
+
+typedef struct inspace_s {
+	char *ref;
+	char *prefix;
+} inspace_t;
+
+C_DECL_BEGIN
+
+bool_t inspace_parse(const inspace_t *info, knspace_t *nspace, faux_error_t *error);
+bool_t inspace_parse_nested(const inspace_t *inspace, knspace_t *knspace,
+	faux_error_t *error);
+knspace_t *inspace_load(const inspace_t *inspace, faux_error_t *error);
+char *inspace_deploy(const knspace_t *knspace, int level);
+
+C_DECL_END
+
+#endif // _klish_inspace_h

+ 1 - 0
klish/ischeme/Makefile.am

@@ -5,4 +5,5 @@ libklish_la_SOURCES += \
 	klish/ischeme/icommand.c \
 	klish/ischeme/iptype.c \
 	klish/ischeme/iview.c \
+	klish/ischeme/inspace.c \
 	klish/ischeme/ischeme.c

+ 112 - 0
klish/ischeme/inspace.c

@@ -0,0 +1,112 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include <faux/str.h>
+#include <faux/list.h>
+#include <faux/error.h>
+#include <klish/khelper.h>
+#include <klish/knspace.h>
+#include <klish/inspace.h>
+
+#define TAG "NSPACE"
+
+
+bool_t inspace_parse(const inspace_t *info, knspace_t *nspace, faux_error_t *error)
+{
+	bool_t retcode = BOOL_TRUE;
+
+	if (!info)
+		return BOOL_FALSE;
+	if (!nspace)
+		return BOOL_FALSE;
+
+	// Prefix
+	if (!faux_str_is_empty(info->prefix)) {
+		if (!knspace_set_prefix(nspace, info->prefix)) {
+			faux_error_add(error, TAG": Illegal 'prefix' attribute");
+			retcode = BOOL_FALSE;
+		}
+	}
+
+	return retcode;
+}
+
+
+bool_t inspace_parse_nested(const inspace_t *inspace, knspace_t *knspace,
+	faux_error_t *error)
+{
+	bool_t retval = BOOL_TRUE;
+
+	if (!knspace || !inspace) {
+		faux_error_add(error, TAG": Internal error");
+		return BOOL_FALSE;
+	}
+
+	// For now it's empty. NSPACE has no nested elements.
+
+	if (!retval)
+		faux_error_sprintf(error, TAG" \"%s\": Illegal nested elements",
+			knspace_view_ref(knspace));
+
+	return retval;
+}
+
+
+knspace_t *inspace_load(const inspace_t *inspace, faux_error_t *error)
+{
+	knspace_t *knspace = NULL;
+
+	if (!inspace)
+		return NULL;
+
+	// Ref [mandatory]
+	if (faux_str_is_empty(inspace->ref)) {
+		faux_error_add(error, TAG": Empty 'ref' attribute");
+		return NULL;
+	}
+
+	knspace = knspace_new(inspace->ref);
+	if (!knspace) {
+		faux_error_sprintf(error, TAG": \"%s\": Can't create object",
+			inspace->ref);
+		return NULL;
+	}
+
+	if (!inspace_parse(inspace, knspace, error)) {
+		knspace_free(knspace);
+		return NULL;
+	}
+
+	// Parse nested elements
+	if (!inspace_parse_nested(inspace, knspace, error)) {
+		knspace_free(knspace);
+		return NULL;
+	}
+
+	return knspace;
+}
+
+
+char *inspace_deploy(const knspace_t *knspace, int level)
+{
+	char *str = NULL;
+	char *tmp = NULL;
+
+	if (!knspace)
+		return NULL;
+
+	tmp = faux_str_sprintf("%*cnspace {\n", level, ' ');
+	faux_str_cat(&str, tmp);
+	faux_str_free(tmp);
+
+	attr2ctext(&str, "ref", knspace_view_ref(knspace), level + 1);
+	attr2ctext(&str, "prefix", knspace_prefix(knspace), level + 1);
+
+	tmp = faux_str_sprintf("%*c},\n\n", level, ' ');
+	faux_str_cat(&str, tmp);
+	faux_str_free(tmp);
+
+	return str;
+}