]> git.mdlowis.com Git - projs/libcds.git/commitdiff
first crack at basic string module functions
authorMichael D. Lowis <mike.lowis@gentex.com>
Mon, 11 Aug 2014 18:00:41 +0000 (14:00 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Mon, 11 Aug 2014 18:00:41 +0000 (14:00 -0400)
Rakefile
source/string/str.c [new file with mode: 0644]
source/string/str.h [new file with mode: 0644]

index d6888f5ec0401cce62993c509526b74b15480ed3..8510b591dfefb9674967a0efe4527bec4289f411 100644 (file)
--- a/Rakefile
+++ b/Rakefile
@@ -20,7 +20,7 @@ task(:posix){ is_windows = false }
 # Define the compiler environment
 Env = Rscons::Environment.new do |env|
   env.build_dir('source/','build/obj/source')
-  env["CFLAGS"] += ['-Wall', '-Wextra', '-Werror']
+  env["CFLAGS"] += ['--std=c99', '-Wall', '-Wextra', '-Werror']
   env['CPPPATH'] += Dir['source/**/']
 end
 
diff --git a/source/string/str.c b/source/string/str.c
new file mode 100644 (file)
index 0000000..eeed415
--- /dev/null
@@ -0,0 +1,115 @@
+/**
+  @file str.c
+  @brief See header for details
+  $Revision$
+  $HeadURL$
+*/
+#include "str.h"
+#include <assert.h>
+
+/* Forward declare our struct */
+struct str_t {
+    size_t size;
+    char data[];
+};
+
+str_t* str_new(const char* p_cstr)
+{
+    str_t* p_str = NULL;
+    size_t len   = 0;
+    assert(NULL != p_cstr);
+    len   = strlen(p_cstr);
+    p_str = (str_t*)mem_allocate(sizeof(str_t) + len + 1, NULL);
+    p_str->size = len;
+    memcpy(p_str->data, p_cstr, len+1);
+    return p_str;
+}
+
+size_t str_size(str_t* p_str)
+{
+    assert(NULL != p_str);
+    return p_str->size;
+}
+
+const char* str_cstr(str_t* p_str)
+{
+    assert(NULL != p_str);
+    return p_str->data;
+}
+
+str_t* str_copy(str_t* p_str)
+{
+    str_t* p_newstr;
+    assert(NULL != p_str);
+    p_newstr = (str_t*)mem_allocate(sizeof(str_t) + p_str->size + 1, NULL);
+    memcpy(p_newstr->data, p_str->data, p_str->size+1);
+    p_newstr->size = p_str->size;
+    return p_newstr;
+}
+
+char str_at(str_t* p_str, size_t index)
+{
+    assert(NULL != p_str);
+    return (index >= p_str->size) ? '\0' : p_str->data[index];
+}
+
+str_t* str_set(str_t* p_str, size_t index, char val)
+{
+    (void)p_str;
+    (void)index;
+    (void)val;
+    return NULL;
+}
+
+str_t* str_concat(str_t* p_str1, str_t* p_str2)
+{
+    (void)p_str1;
+    (void)p_str2;
+    return NULL;
+}
+
+str_t* str_insert(str_t* p_str1, size_t index, str_t* p_str2)
+{
+    (void)p_str1;
+    (void)index;
+    (void)p_str2;
+    return NULL;
+}
+
+str_t* str_erase(str_t* p_str, size_t start, size_t end)
+{
+    (void)p_str;
+    (void)start;
+    (void)end;
+    return NULL;
+}
+
+str_t* str_substr(str_t* p_str, size_t start, size_t end)
+{
+    (void)p_str;
+    (void)start;
+    (void)end;
+    return NULL;
+}
+
+int str_compare(str_t* p_str1, str_t* p_str2)
+{
+    (void)p_str1;
+    (void)p_str2;
+    return 0;
+}
+
+size_t str_find(str_t* p_str1, str_t* p_str2)
+{
+    (void)p_str1;
+    (void)p_str2;
+    return 0;
+}
+
+size_t str_rfind(str_t* p_str1, str_t* p_str2)
+{
+    (void)p_str1;
+    (void)p_str2;
+    return 0;
+}
+
diff --git a/source/string/str.h b/source/string/str.h
new file mode 100644 (file)
index 0000000..e748bf2
--- /dev/null
@@ -0,0 +1,42 @@
+/**
+  @file str.h
+  @brief An updated string type for C.
+  $Revision$
+  $HeadURL$
+*/
+#ifndef STR_H
+#define STR_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <string.h>
+#include <stdbool.h>
+#include "mem.h"
+
+/* Forward declare our struct */
+struct str_t;
+
+/** A safe string data structure */
+typedef struct str_t str_t;
+
+str_t* str_new(const char* p_cstr);
+size_t str_size(str_t* p_str);
+const char* str_cstr(str_t* p_str);
+str_t* str_copy(str_t* p_str);
+char str_at(str_t* p_str, size_t index);
+str_t* str_set(str_t* p_str, size_t index, char val);
+str_t* str_concat(str_t* p_str1, str_t* p_str2);
+str_t* str_insert(str_t* p_str1, size_t index, str_t* p_str2);
+str_t* str_erase(str_t* p_str, size_t start, size_t end);
+str_t* str_substr(str_t* p_str, size_t start, size_t end);
+int str_compare(str_t* p_str1, str_t* p_str2);
+size_t str_find(str_t* p_str1, str_t* p_str2);
+size_t str_rfind(str_t* p_str1, str_t* p_str2);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* STR_H */