Browse Source

session: Initial versions of klevel_t and kpath_t classes

Serj Kalichev 2 years ago
parent
commit
155a86cdf4
5 changed files with 196 additions and 2 deletions
  1. 2 1
      klish/Makefile.am
  2. 40 0
      klish/kpath.h
  3. 3 1
      klish/ksession/Makefile.am
  4. 47 0
      klish/ksession/klevel.c
  5. 104 0
      klish/ksession/kpath.c

+ 2 - 1
klish/Makefile.am

@@ -37,7 +37,8 @@ nobase_include_HEADERS += \
 nobase_include_HEADERS += \
 	klish/kudata.h \
 	klish/kustore.h \
-	klish/kcontext.h
+	klish/kcontext.h \
+	klish/kpath.h
 
 # XML-helper
 nobase_include_HEADERS += \

+ 40 - 0
klish/kpath.h

@@ -0,0 +1,40 @@
+/** @file kpath.h
+ *
+ * @brief Klish path stack
+ */
+
+#ifndef _klish_kpath_h
+#define _klish_kpath_h
+
+#include <faux/list.h>
+#include <klish/kview.h>
+
+typedef struct kpath_s kpath_t;
+typedef struct klevel_s klevel_t;
+
+typedef faux_list_node_t kpath_levels_node_t;
+
+
+C_DECL_BEGIN
+
+// Level
+
+klevel_t *klevel_new(kview_t *view);
+void klevel_free(klevel_t *level);
+
+kview_t *klevel_view(const klevel_t *level);
+
+// Path
+
+kpath_t *kpath_new(void);
+void kpath_free(kpath_t *path);
+
+size_t kpath_len(const kpath_t *path);
+size_t kpath_is_empty(const kpath_t *path);
+bool_t kpath_push(kpath_t *path, klevel_t *level);
+bool_t kpath_pop(kpath_t *path);
+klevel_t *kpath_current(const kpath_t *path);
+
+C_DECL_END
+
+#endif // _klish_kpath_h

+ 3 - 1
klish/ksession/Makefile.am

@@ -1,4 +1,6 @@
 libklish_la_SOURCES += \
 	klish/ksession/kudata.c \
 	klish/ksession/kustore.c \
-	klish/ksession/kcontext.c
+	klish/ksession/kcontext.c \
+	klish/ksession/klevel.c \
+	klish/ksession/kpath.c

+ 47 - 0
klish/ksession/klevel.c

@@ -0,0 +1,47 @@
+/** @file klevel.c
+ */
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <faux/str.h>
+#include <klish/khelper.h>
+#include <klish/kpath.h>
+#include <klish/kview.h>
+
+struct klevel_s {
+	kview_t *view;
+};
+
+
+// View
+KGET(level, kview_t *, view);
+
+
+klevel_t *klevel_new(kview_t *view)
+{
+	klevel_t *level = NULL;
+
+	if (!view)
+		return NULL;
+
+	level = faux_zmalloc(sizeof(*level));
+	assert(level);
+	if (!level)
+		return NULL;
+
+	// Initialize
+	level->view = view;
+
+	return level;
+}
+
+
+void klevel_free(klevel_t *level)
+{
+	if (!level)
+		return;
+
+	faux_free(level);
+}

+ 104 - 0
klish/ksession/kpath.c

@@ -0,0 +1,104 @@
+/** @file kpath.c
+ */
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <faux/list.h>
+#include <klish/khelper.h>
+#include <klish/kudata.h>
+#include <klish/kpath.h>
+
+struct kpath_s {
+	faux_list_t *levels;
+};
+
+
+kpath_t *kpath_new()
+{
+	kpath_t *path = NULL;
+
+	path = faux_zmalloc(sizeof(*path));
+	assert(path);
+	if (!path)
+		return NULL;
+
+	// User data blobs list
+	path->levels = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
+		NULL, NULL, (void (*)(void *))klevel_free);
+	assert(path->levels);
+
+	return path;
+}
+
+
+void kpath_free(kpath_t *path)
+{
+	if (!path)
+		return;
+
+	faux_list_free(path->levels);
+
+	free(path);
+}
+
+
+size_t kpath_len(const kpath_t *path)
+{
+	assert(path);
+	if (!path)
+		return 0;
+
+	return faux_list_len(path->levels);
+}
+
+
+size_t kpath_is_empty(const kpath_t *path)
+{
+	assert(path);
+	if (!path)
+		return 0;
+
+	return faux_list_is_empty(path->levels);
+}
+
+
+bool_t kpath_push(kpath_t *path, klevel_t *level)
+{
+	assert(path);
+	assert(level);
+	if (!path)
+		return BOOL_FALSE;
+	if (!level)
+		return BOOL_FALSE;
+
+	if (!faux_list_add(path->levels, level))
+		return BOOL_FALSE;
+
+	return BOOL_TRUE;
+}
+
+
+bool_t kpath_pop(kpath_t *path)
+{
+	assert(path);
+	if (!path)
+		return BOOL_FALSE;
+	if (kpath_is_empty(path))
+		return BOOL_FALSE;
+
+	return faux_list_del(path->levels, faux_list_tail(path->levels));
+}
+
+
+klevel_t *kpath_current(const kpath_t *path)
+{
+	assert(path);
+	if (!path)
+		return NULL;
+	if (kpath_is_empty(path))
+		return NULL;
+
+	return (klevel_t *)faux_list_data(faux_list_tail(path->levels));
+}