Browse Source

faux.vec: Add new dir for vec (variable length vector) subsystem

Serj Kalichev 3 years ago
parent
commit
ebf2002ea8
15 changed files with 253 additions and 12 deletions
  1. 3 0
      faux/Makefile.am
  2. 2 2
      faux/argv.h
  3. 1 1
      faux/conv.h
  4. 1 1
      faux/ctype.h
  5. 1 1
      faux/file.h
  6. 2 2
      faux/ini.h
  7. 1 1
      faux/list.h
  8. 1 1
      faux/log.h
  9. 1 1
      faux/str.h
  10. 1 1
      faux/sysdb.h
  11. 1 1
      faux/testc_helpers.h
  12. 24 0
      faux/vec.h
  13. 3 0
      faux/vec/Makefile.am
  14. 7 0
      faux/vec/private.h
  15. 204 0
      faux/vec/vec.c

+ 3 - 0
faux/Makefile.am

@@ -15,6 +15,7 @@ nobase_include_HEADERS += \
 	faux/conv.h \
 	faux/log.h \
 	faux/list.h \
+	faux/vec.h \
 	faux/ini.h \
 	faux/file.h \
 	faux/argv.h \
@@ -31,6 +32,7 @@ EXTRA_DIST += \
 	faux/conv/Makefile.am \
 	faux/log/Makefile.am \
 	faux/list/Makefile.am \
+	faux/vec/Makefile.am \
 	faux/ini/Makefile.am \
 	faux/file/Makefile.am \
 	faux/argv/Makefile.am \
@@ -46,6 +48,7 @@ include $(top_srcdir)/faux/sysdb/Makefile.am
 include $(top_srcdir)/faux/conv/Makefile.am
 include $(top_srcdir)/faux/log/Makefile.am
 include $(top_srcdir)/faux/list/Makefile.am
+include $(top_srcdir)/faux/vec/Makefile.am
 include $(top_srcdir)/faux/ini/Makefile.am
 include $(top_srcdir)/faux/file/Makefile.am
 include $(top_srcdir)/faux/argv/Makefile.am

+ 2 - 2
faux/argv.h

@@ -5,8 +5,8 @@
 #ifndef _faux_argv_h
 #define _faux_argv_h
 
-#include "faux/faux.h"
-#include "faux/list.h"
+#include <faux/faux.h>
+#include <faux/list.h>
 
 typedef struct faux_argv_s faux_argv_t;
 typedef faux_list_node_t faux_argv_node_t;

+ 1 - 1
faux/conv.h

@@ -5,7 +5,7 @@
 #ifndef _faux_conv_h
 #define _faux_conv_h
 
-#include "faux/faux.h"
+#include <faux/faux.h>
 
 C_DECL_BEGIN
 

+ 1 - 1
faux/ctype.h

@@ -7,7 +7,7 @@
 
 #include <ctype.h>
 
-#include "faux/faux.h"
+#include <faux/faux.h>
 
 C_DECL_BEGIN
 

+ 1 - 1
faux/file.h

@@ -10,7 +10,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 
-#include "faux/faux.h"
+#include <faux/faux.h>
 
 typedef struct faux_file_s faux_file_t;
 

+ 2 - 2
faux/ini.h

@@ -5,8 +5,8 @@
 #ifndef _faux_ini_h
 #define _faux_ini_h
 
-#include "faux/faux.h"
-#include "faux/list.h"
+#include <faux/faux.h>
+#include <faux/list.h>
 
 typedef struct faux_pair_s faux_pair_t;
 typedef struct faux_ini_s faux_ini_t;

+ 1 - 1
faux/list.h

@@ -7,7 +7,7 @@
 
 #include <stddef.h>
 
-#include "faux.h"
+#include <faux/faux.h>
 
 typedef enum {
 	FAUX_LIST_SORTED = BOOL_TRUE,

+ 1 - 1
faux/log.h

@@ -7,7 +7,7 @@
 
 #include <syslog.h>
 
-#include "faux/faux.h"
+#include <faux/faux.h>
 
 C_DECL_BEGIN
 

+ 1 - 1
faux/str.h

@@ -7,7 +7,7 @@
 
 #include <stddef.h>
 
-#include "faux/faux.h"
+#include <faux/faux.h>
 
 #define UTF8_MASK 0xC0
 #define UTF8_7BIT_MASK 0x80 // One byte or multibyte

+ 1 - 1
faux/sysdb.h

@@ -10,7 +10,7 @@
 #include <pwd.h>
 #include <grp.h>
 
-#include "faux/faux.h"
+#include <faux/faux.h>
 
 C_DECL_BEGIN
 

+ 1 - 1
faux/testc_helpers.h

@@ -7,7 +7,7 @@
 
 #include <stddef.h>
 
-#include "faux/faux.h"
+#include <faux/faux.h>
 
 #define FAUX_TESTC_TMPDIR_ENV "TESTC_TMPDIR"
 

+ 24 - 0
faux/vec.h

@@ -0,0 +1,24 @@
+/** @file vec.h
+ * @brief Public interface for a variable length vector.
+ */
+
+#ifndef _faux_vec_h
+#define _faux_vec_h
+
+#include <stddef.h>
+
+#include <faux/faux.h>
+
+typedef struct faux_vec_s faux_vec_t;
+
+//typedef int (*faux_list_cmp_fn)(const void *new_item, const void *list_item);
+//typedef int (*faux_list_kcmp_fn)(const void *key, const void *list_item);
+//typedef void (*faux_list_free_fn)(void *list_item);
+
+C_DECL_BEGIN
+
+
+C_DECL_END
+
+#endif				/* _faux_vec_h */
+

+ 3 - 0
faux/vec/Makefile.am

@@ -0,0 +1,3 @@
+libfaux_la_SOURCES += \
+	faux/vec/vec.c \
+	faux/vec/vec.h

+ 7 - 0
faux/vec/private.h

@@ -0,0 +1,7 @@
+#include "faux/vec.h"
+
+struct faux_vec_s {
+	void *data;
+	size_t len;
+	size_t item_size;
+};

+ 204 - 0
faux/vec/vec.c

@@ -0,0 +1,204 @@
+/** @file vec.c
+ *
+ */
+
+
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <ctype.h>
+#include <stdio.h>
+
+#include "private.h"
+#include "faux/str.h"
+
+#if 0
+/*--------------------------------------------------------- */
+static void lub_argv_init(lub_argv_t * this, const char *line, size_t off)
+{
+	size_t len = 0;
+	const char *word = NULL;
+	lub_arg_t *arg = NULL;
+	bool_t quoted = BOOL_FALSE;
+	bool_t alt_quoted = BOOL_FALSE;
+	const char *str = line + off; // Start on specified offset
+	const char *offset = NULL;
+
+	this->argv = NULL;
+	this->argc = 0;
+	if (!line)
+		return;
+	/* first of all count the words in the line */
+	this->argc = lub_string_wordcount(line);
+	if (0 == this->argc)
+		return;
+	/* allocate space to hold the vector */
+	arg = this->argv = malloc(sizeof(lub_arg_t) * this->argc);
+	assert(arg);
+
+	/* then fill out the array with the words */
+	for (word = lub_string_nextword(str, &len, &offset, &quoted, NULL, &alt_quoted);
+		word && (*word != '\0');
+		word = lub_string_nextword(str, &len, &offset, &quoted, NULL, &alt_quoted)) {
+		if (alt_quoted)
+			(*arg).arg = lub_string_dupn(word, len);
+		else
+			(*arg).arg = lub_string_ndecode(word, len);
+		(*arg).offset = offset - line;
+		(*arg).quoted = quoted;
+		str = offset;
+		arg++;
+	}
+}
+
+/*--------------------------------------------------------- */
+lub_argv_t *lub_argv_new(const char *line, size_t offset)
+{
+	lub_argv_t *this;
+
+	this = malloc(sizeof(lub_argv_t));
+	if (this)
+		lub_argv_init(this, line, offset);
+
+	return this;
+}
+
+/*--------------------------------------------------------- */
+void lub_argv_add(lub_argv_t * this, const char *text)
+{
+	lub_arg_t * arg;
+
+	if (!text)
+		return;
+
+	/* allocate space to hold the vector */
+	arg = realloc(this->argv, sizeof(lub_arg_t) * (this->argc + 1));
+	assert(arg);
+	this->argv = arg;
+	(this->argv[this->argc++]).arg = strdup(text);
+}
+
+/*--------------------------------------------------------- */
+static void lub_argv_fini(lub_argv_t * this)
+{
+	unsigned i;
+
+	for (i = 0; i < this->argc; i++)
+		free(this->argv[i].arg);
+	free(this->argv);
+	this->argv = NULL;
+}
+
+/*--------------------------------------------------------- */
+void lub_argv_delete(lub_argv_t * this)
+{
+	lub_argv_fini(this);
+	free(this);
+}
+
+/*--------------------------------------------------------- */
+char *lub_argv__get_line(const lub_argv_t * this)
+{
+	int space = 0;
+	const char *p;
+	unsigned i;
+	char *line = NULL;
+
+	for (i = 0; i < this->argc; i++) {
+		if (i != 0)
+			lub_string_cat(&line, " ");
+		space = 0;
+		/* Search for spaces */
+		for (p = this->argv[i].arg; *p; p++) {
+			if (isspace(*p)) {
+				space = 1;
+				break;
+			}
+		}
+		if (space)
+			lub_string_cat(&line, "\"");
+		lub_string_cat(&line, this->argv[i].arg);
+		if (space)
+			lub_string_cat(&line, "\"");
+	}
+
+	return line;
+}
+
+/*--------------------------------------------------------- */
+char **lub_argv__get_argv(const lub_argv_t * this, const char *argv0)
+{
+	char **result = NULL;
+	unsigned i;
+	unsigned a = 0;
+
+	if (argv0)
+		a = 1;
+
+	result = malloc(sizeof(char *) * (this->argc + 1 + a));
+
+	if (argv0)
+		result[0] = strdup(argv0);
+	for (i = 0; i < this->argc; i++)
+		result[i + a] = strdup(this->argv[i].arg);
+	result[i + a] = NULL;
+
+	return result;
+}
+
+/*--------------------------------------------------------- */
+void lub_argv__free_argv(char **argv)
+{
+	unsigned i;
+
+	if (!argv)
+		return;
+
+	for (i = 0; argv[i]; i++)
+		free(argv[i]);
+	free(argv);
+}
+
+/*--------------------------------------------------------- */
+const char *lub_argv__get_arg(const lub_argv_t *this, unsigned int index)
+{
+	const char *result = NULL;
+
+	if (!this)
+		return NULL;
+	if (this->argc > index)
+		result = this->argv[index].arg;
+
+	return result;
+}
+
+/*--------------------------------------------------------- */
+unsigned lub_argv__get_count(const lub_argv_t * this)
+{
+	return this->argc;
+}
+
+/*--------------------------------------------------------- */
+size_t lub_argv__get_offset(const lub_argv_t * this, unsigned index)
+{
+	size_t result = 0;
+
+	if (this->argc > index)
+		result = this->argv[index].offset;
+
+	return result;
+}
+
+/*--------------------------------------------------------- */
+bool_t lub_argv__get_quoted(const lub_argv_t * this, unsigned index)
+{
+	bool_t result = BOOL_FALSE;
+
+	if (this->argc > index)
+		result = this->argv[index].quoted;
+
+	return result;
+}
+
+/*--------------------------------------------------------- */
+#endif