aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLAN-TW <lantw44@gmail.com>2013-12-07 19:44:12 +0800
committerLAN-TW <lantw44@gmail.com>2013-12-07 19:44:12 +0800
commit5fac283e3f4fae867b8429174d26078a090d2e0d (patch)
treed234f04f0939fd1ea3187a31e39d3c62bb9e7318
parentdc258e02720308981a4f8a2e4848c582c1b71123 (diff)
downloadl4basic-5fac283e3f4fae867b8429174d26078a090d2e0d.tar
l4basic-5fac283e3f4fae867b8429174d26078a090d2e0d.tar.gz
l4basic-5fac283e3f4fae867b8429174d26078a090d2e0d.tar.bz2
l4basic-5fac283e3f4fae867b8429174d26078a090d2e0d.tar.lz
l4basic-5fac283e3f4fae867b8429174d26078a090d2e0d.tar.xz
l4basic-5fac283e3f4fae867b8429174d26078a090d2e0d.tar.zst
l4basic-5fac283e3f4fae867b8429174d26078a090d2e0d.zip
Change all tabs to spaces and add vim modeline
-rw-r--r--Makefile2
-rw-r--r--l4array.c359
-rw-r--r--l4array.h2
-rw-r--r--l4common.h1
-rw-r--r--l4file.c39
-rw-r--r--l4file.h7
-rw-r--r--test-array.c1
-rw-r--r--test-file.c1
8 files changed, 209 insertions, 203 deletions
diff --git a/Makefile b/Makefile
index 9804974..d666836 100644
--- a/Makefile
+++ b/Makefile
@@ -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)
diff --git a/l4array.c b/l4array.c
index fed2876..5e1a69a 100644
--- a/l4array.c
+++ b/l4array.c
@@ -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;
}
diff --git a/l4array.h b/l4array.h
index d3e74ee..73fc884 100644
--- a/l4array.h
+++ b/l4array.h
@@ -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 */
diff --git a/l4common.h b/l4common.h
index d054b34..4c0860c 100644
--- a/l4common.h
+++ b/l4common.h
@@ -1,3 +1,4 @@
+/* vim: set sw=4 ts=4 sts=4 et: */
#ifndef LBS_COMMON_H
#define LBS_COMMON_H
diff --git a/l4file.c b/l4file.c
index b4f8a57..de965fd 100644
--- a/l4file.c
+++ b/l4file.c
@@ -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;
}
diff --git a/l4file.h b/l4file.h
index 3e8e0f7..eb1cc54 100644
--- a/l4file.h
+++ b/l4file.h
@@ -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>