diff options
author | LAN-TW <lantw44@gmail.com> | 2013-12-07 19:44:12 +0800 |
---|---|---|
committer | LAN-TW <lantw44@gmail.com> | 2013-12-07 19:44:12 +0800 |
commit | 5fac283e3f4fae867b8429174d26078a090d2e0d (patch) | |
tree | d234f04f0939fd1ea3187a31e39d3c62bb9e7318 | |
parent | dc258e02720308981a4f8a2e4848c582c1b71123 (diff) | |
download | l4basic-5fac283e3f4fae867b8429174d26078a090d2e0d.tar.gz l4basic-5fac283e3f4fae867b8429174d26078a090d2e0d.tar.zst l4basic-5fac283e3f4fae867b8429174d26078a090d2e0d.zip |
Change all tabs to spaces and add vim modeline
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | l4array.c | 359 | ||||
-rw-r--r-- | l4array.h | 2 | ||||
-rw-r--r-- | l4common.h | 1 | ||||
-rw-r--r-- | l4file.c | 39 | ||||
-rw-r--r-- | l4file.h | 7 | ||||
-rw-r--r-- | test-array.c | 1 | ||||
-rw-r--r-- | test-file.c | 1 |
8 files changed, 209 insertions, 203 deletions
@@ -28,7 +28,7 @@ INCLUDEDIR= $(DESTDIR)$(PREFIX)/include # Tasks definition lib_LIBRARIES= libl4basic.a libl4basic_a_OBJECTS= l4array.o l4array2.o l4file.o l4list.o l4arg.o -libl4basic_a_HEADERS= $(libl4basic_a_OBJECTS:.o=.h) +libl4basic_a_HEADERS= $(libl4basic_a_OBJECTS:.o=.h) l4common.h check_PROGRAMS= test-array test-array2 test-file test-list test-arg check_OBJECTS= $(check_PROGRAMS:=.o) @@ -1,232 +1,233 @@ +/* vim: set sw=4 ts=4 sts=4 et: */ #include "l4array.h" #include <stdlib.h> #include <string.h> LbsArray* lbs_array_new_with_max (size_t size, size_t max) { - if (size <= 0) { - return NULL; - } - - LbsArray* array = malloc (sizeof (LbsArray)); - if (array == NULL) { - return NULL; - } - - if (max > 0) { - void* data = malloc (size * max); - if (data == NULL) { - free (array); - return NULL; - } - array->data = data; - } else { - array->data = NULL; - } - - array->free_func = NULL; - array->size = size; - array->len = max; - array->max = max; - array->ref_count = 1; - array->is_alloc = true; - - return array; + if (size <= 0) { + return NULL; + } + + LbsArray* array = malloc (sizeof (LbsArray)); + if (array == NULL) { + return NULL; + } + + if (max > 0) { + void* data = malloc (size * max); + if (data == NULL) { + free (array); + return NULL; + } + array->data = data; + } else { + array->data = NULL; + } + + array->free_func = NULL; + array->size = size; + array->len = max; + array->max = max; + array->ref_count = 1; + array->is_alloc = true; + + return array; } int lbs_array_init_with_max (LbsArray* array, size_t size, size_t max) { - if (size <= 0) { - return -1; - } - - if (max > 0) { - void* data = malloc (size * max); - if (data == NULL) { - return -1; - } - array->data = data; - } else { - array->data = NULL; - } - - array->free_func = NULL; - array->size = size; - array->len = max; - array->max = max; - array->ref_count = 1; - array->is_alloc = false; - - return 0; + if (size <= 0) { + return -1; + } + + if (max > 0) { + void* data = malloc (size * max); + if (data == NULL) { + return -1; + } + array->data = data; + } else { + array->data = NULL; + } + + array->free_func = NULL; + array->size = size; + array->len = max; + array->max = max; + array->ref_count = 1; + array->is_alloc = false; + + return 0; } LbsArray* lbs_array_copy (LbsArray* dest, const LbsArray* src) { - if (dest == NULL) { - dest = lbs_array_new_with_max (src->size, src->max); - if (dest == NULL) { - return NULL; - } - } else { - if (lbs_array_init_with_max (dest, src->size, src->max) < 0) { - return NULL; - } - } - - dest->len = src->len; - dest->free_func = src->free_func; - memcpy (dest->data, src->data, src->size * src->len); - return dest; + if (dest == NULL) { + dest = lbs_array_new_with_max (src->size, src->max); + if (dest == NULL) { + return NULL; + } + } else { + if (lbs_array_init_with_max (dest, src->size, src->max) < 0) { + return NULL; + } + } + + dest->len = src->len; + dest->free_func = src->free_func; + memcpy (dest->data, src->data, src->size * src->len); + return dest; } LbsArray* lbs_array_cat (LbsArray* dest, const LbsArray* more) { - if (dest == NULL) { - return lbs_array_copy (dest, more); - } + if (dest == NULL) { + return lbs_array_copy (dest, more); + } - if (dest->size != more->size) { - return NULL; - } + if (dest->size != more->size) { + return NULL; + } - int oldlen = dest->len; - if (lbs_array_set_len (dest, dest->len + more->len) < 0) { - return NULL; - } + int oldlen = dest->len; + if (lbs_array_set_len (dest, dest->len + more->len) < 0) { + return NULL; + } - memcpy (lbs_array_vp (dest, oldlen), more->data, more->size * more->len); - return dest; + memcpy (lbs_array_vp (dest, oldlen), more->data, more->size * more->len); + return dest; } void* lbs_array_ref_generic (void* array_generic) { - LbsArray* array = LBS_ARRAY (array_generic); - int oldref = array->ref_count; - int newref = oldref + 1; - if (newref <= oldref) { - return NULL; - } - - array->ref_count = newref; - return array; + LbsArray* array = LBS_ARRAY (array_generic); + int oldref = array->ref_count; + int newref = oldref + 1; + if (newref <= oldref) { + return NULL; + } + + array->ref_count = newref; + return array; } void lbs_array_unref_generic (void* array_generic) { - LbsArray* array = LBS_ARRAY (array_generic); - array->ref_count--; - if (array->ref_count <= 0) { - lbs_array_free (array); - } + LbsArray* array = LBS_ARRAY (array_generic); + array->ref_count--; + if (array->ref_count <= 0) { + lbs_array_free (array); + } } void lbs_array_free_generic (void* array_generic) { - LbsArray* array = LBS_ARRAY (array_generic); - if (array->free_func != NULL) { - int i = 0; - char* d = array->data; - for (; i < array->len; i++, d += array->size) { - (*(array->free_func)) (*((void**)d)); - } - } - free (array->data); - if (array->is_alloc) { - free (array); - } + LbsArray* array = LBS_ARRAY (array_generic); + if (array->free_func != NULL) { + int i = 0; + char* d = array->data; + for (; i < array->len; i++, d += array->size) { + (*(array->free_func)) (*((void**)d)); + } + } + free (array->data); + if (array->is_alloc) { + free (array); + } } void* lbs_array_drop_struct (LbsArray* array) { - if (!array->is_alloc) { - return array->data; - } + if (!array->is_alloc) { + return array->data; + } - void* data = array->data; - free (array); - return data; + void* data = array->data; + free (array); + return data; } LbsArray* lbs_array_make_struct (LbsArray* array, - size_t size, size_t len, size_t max, void* data) { - - if (array == NULL) { - array = lbs_array_new (size); - if (array == NULL) { - return NULL; - } - } else { - if (lbs_array_init (array, size) < 0) { - return NULL; - } - } - - array->len = len; - array->max = max; - array->data = data; - return array; + size_t size, size_t len, size_t max, void* data) { + + if (array == NULL) { + array = lbs_array_new (size); + if (array == NULL) { + return NULL; + } + } else { + if (lbs_array_init (array, size) < 0) { + return NULL; + } + } + + array->len = len; + array->max = max; + array->data = data; + return array; } int lbs_array_set_len (LbsArray* array, size_t len) { - if (len > (array->max)){ - if (lbs_array_set_max (array, len) < 0) { - return -1; - } else { - array->len = len; - } - } else { - array->len = len; - return 0; - } - return 0; + if (len > (array->max)){ + if (lbs_array_set_max (array, len) < 0) { + return -1; + } else { + array->len = len; + } + } else { + array->len = len; + return 0; + } + return 0; } int lbs_array_set_max (LbsArray* array, size_t max) { - void* ptr = realloc (array->data, array->size * max); - if (ptr == NULL) { - return -1; - } - - array->max = max; - array->data = ptr; - return 0; + void* ptr = realloc (array->data, array->size * max); + if (ptr == NULL) { + return -1; + } + + array->max = max; + array->data = ptr; + return 0; } int lbs_array_append_ptr (LbsArray* array, const void* ptr) { - return lbs_array_append_data (array, &ptr); + return lbs_array_append_data (array, &ptr); } int lbs_array_append_data (LbsArray* array, const void* data) { - if (array->max < array->len + 1) { - if (array->max > 0){ - if (lbs_array_set_max (array, array->max * 2) < 0) { - return -1; - } - } else { - if (lbs_array_set_max (array, 1) < 0){ - return -1; - } - } - } - - memcpy (lbs_array_vp (array, array->len), data, array->size); - array->len++; - return 0; + if (array->max < array->len + 1) { + if (array->max > 0){ + if (lbs_array_set_max (array, array->max * 2) < 0) { + return -1; + } + } else { + if (lbs_array_set_max (array, 1) < 0){ + return -1; + } + } + } + + memcpy (lbs_array_vp (array, array->len), data, array->size); + array->len++; + return 0; } int lbs_array_remove (LbsArray* array) { - if (array->len <= 0) { - return -1; - } - - array->len--; - if (array->len < array->max * 2) { - lbs_array_minimize (array); - } - return 0; + if (array->len <= 0) { + return -1; + } + + array->len--; + if (array->len < array->max * 2) { + lbs_array_minimize (array); + } + return 0; } int lbs_array_minimize (LbsArray* array) { - if (array->max > array->len) { - void* ptr = realloc (array->data, array->size * array->len); - if (ptr == NULL) { - return -1; - } - array->max = array->len; - array->data = ptr; - } - return 0; + if (array->max > array->len) { + void* ptr = realloc (array->data, array->size * array->len); + if (ptr == NULL) { + return -1; + } + array->max = array->len; + array->data = ptr; + } + return 0; } @@ -7,12 +7,12 @@ typedef struct LbsArrayStruct { /*< public >*/ void* data; /* data */ - size_t len; /* current length */ void (*free_func) (void* data); /* function to free the element */ /*< private >*/ size_t size; /* element size */ + size_t len; /* current length */ size_t max; /* maximal length */ unsigned ref_count; /* reference count */ bool is_alloc; /* is allocated using malloc */ @@ -1,3 +1,4 @@ +/* vim: set sw=4 ts=4 sts=4 et: */ #ifndef LBS_COMMON_H #define LBS_COMMON_H @@ -1,26 +1,27 @@ +/* vim: set sw=4 ts=4 sts=4 et: */ #include "l4file.h" #include <stdio.h> LbsArray* lbs_file_read_file_line_delim (FILE* infile, int delim) { - LbsArray* str = lbs_array_new (sizeof (char)); - if (str == NULL){ - return NULL; - } + LbsArray* str = lbs_array_new (sizeof (char)); + if (str == NULL){ + return NULL; + } - int c; - char towrite; - while ((c = getc (infile)) != delim && !feof (infile)) { - towrite = c; - if (lbs_array_append_var (str, towrite) < 0){ - lbs_array_unref (str); - return NULL; - } - } - towrite = '\0'; - if (lbs_array_append_var (str, towrite) < 0) { - lbs_array_unref (str); - return NULL; - } - return str; + int c; + char towrite; + while ((c = getc (infile)) != delim && !feof (infile)) { + towrite = c; + if (lbs_array_append_var (str, towrite) < 0){ + lbs_array_unref (str); + return NULL; + } + } + towrite = '\0'; + if (lbs_array_append_var (str, towrite) < 0) { + lbs_array_unref (str); + return NULL; + } + return str; } @@ -1,3 +1,4 @@ +/* vim: set sw=4 ts=4 sts=4 et: */ #ifndef LBS_FILE_H #define LBS_FILE_H @@ -5,11 +6,11 @@ #include <stdio.h> #define lbs_file_read_stdin_line() \ - (lbs_file_read_file_line_delim (stdin, '\n')) + (lbs_file_read_file_line_delim (stdin, '\n')) #define lbs_file_read_stdin_line_delim(delim) \ - (lbs_file_read_file_line_delim (stdin, (delim))) + (lbs_file_read_file_line_delim (stdin, (delim))) #define lbs_file_read_file_line(infile) \ - (lbs_file_read_file_line_delim ((infile), '\n')) + (lbs_file_read_file_line_delim ((infile), '\n')) LbsArray* lbs_file_read_file_line_delim (FILE* infile, int delim); #endif /* LBS_FILE_H */ diff --git a/test-array.c b/test-array.c index d2a4d20..7955b29 100644 --- a/test-array.c +++ b/test-array.c @@ -1,3 +1,4 @@ +/* vim: set sw=4 ts=4 sts=4 et: */ #undef NDEBUG #define _POSIX_C_SOURCE 200809L #include <l4array.h> diff --git a/test-file.c b/test-file.c index 5db6ec6..42f5ef8 100644 --- a/test-file.c +++ b/test-file.c @@ -1,3 +1,4 @@ +/* vim: set sw=4 ts=4 sts=4 et: */ #undef NDEBUG #define _POSIX_C_SOURCE 200809L #include <l4array.h> |