Browse Source

scheme: Add kptype.c

Serj Kalichev 3 years ago
parent
commit
9adf65e53e
2 changed files with 81 additions and 3 deletions
  1. 4 3
      klish/kscheme/Makefile.am
  2. 77 0
      klish/kscheme/kptype.c

+ 4 - 3
klish/kscheme/Makefile.am

@@ -1,5 +1,6 @@
 libklish_la_SOURCES += \
-	klish/kscheme/kscheme.c \
-	klish/kscheme/kview.c \
+	klish/kscheme/kptype.c \
+	klish/kscheme/kparam.c \
 	klish/kscheme/kcommand.c \
-	klish/kscheme/kparam.c
+	klish/kscheme/kview.c \
+	klish/kscheme/kscheme.c

+ 77 - 0
klish/kscheme/kptype.c

@@ -0,0 +1,77 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include <faux/str.h>
+#include <faux/list.h>
+#include <klish/kptype.h>
+
+
+struct kptype_s {
+	bool_t is_static;
+	kptype_info_t info;
+};
+
+
+static kptype_t *kptype_new_internal(kptype_info_t info, bool_t is_static)
+{
+	kptype_t *ptype = NULL;
+
+	ptype = faux_zmalloc(sizeof(*ptype));
+	assert(ptype);
+	if (!ptype)
+		return NULL;
+
+	// Initialize
+	ptype->is_static = is_static;
+	ptype->info = info;
+
+	return ptype;
+}
+
+
+kptype_t *kptype_new(kptype_info_t info)
+{
+	return kptype_new_internal(info, BOOL_FALSE);
+}
+
+
+kptype_t *kptype_new_static(kptype_info_t info)
+{
+	return kptype_new_internal(info, BOOL_TRUE);
+}
+
+
+void kptype_free(kptype_t *ptype)
+{
+	if (!ptype)
+		return;
+
+	if (!ptype->is_static) {
+		faux_str_free(ptype->info.name);
+		faux_str_free(ptype->info.help);
+	}
+
+	faux_free(ptype);
+}
+
+
+const char *kptype_name(const kptype_t *ptype)
+{
+	assert(ptype);
+	if (!ptype)
+		return NULL;
+
+	return ptype->info.name;
+}
+
+
+const char *kptype_help(const kptype_t *ptype)
+{
+	assert(ptype);
+	if (!ptype)
+		return NULL;
+
+	return ptype->info.help;
+}