diff options
author | lantw44 <lantw44@gmail.com> | 2012-08-29 00:33:30 +0800 |
---|---|---|
committer | lantw44 <lantw44@gmail.com> | 2012-08-29 00:33:30 +0800 |
commit | 6e703378037a1fe28e5e5e124851d40b392f3817 (patch) | |
tree | 879a4fd8ba3986965bc0aaf02230b8fcfd45455d | |
download | l4basic-testauto-6e703378037a1fe28e5e5e124851d40b392f3817.tar.gz l4basic-testauto-6e703378037a1fe28e5e5e124851d40b392f3817.tar.zst l4basic-testauto-6e703378037a1fe28e5e5e124851d40b392f3817.zip |
First Commit - Copy project files and using GNU autotool
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | README | 8 | ||||
-rw-r--r-- | basic/Makefile.am | 3 | ||||
-rw-r--r-- | basic/config.h | 80 | ||||
-rw-r--r-- | basic/d1array.c | 110 | ||||
-rw-r--r-- | basic/d1arrstr.c | 43 | ||||
-rw-r--r-- | basic/d2array.c | 9 | ||||
-rw-r--r-- | basic/l4basic.h | 146 | ||||
-rw-r--r-- | configure.ac | 27 |
9 files changed, 427 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..8e91541 --- /dev/null +++ b/Makefile.am @@ -0,0 +1 @@ +SUBDIRS = basic @@ -0,0 +1,8 @@ +名稱:l4basic +功能:基本資料結構、命令列解析、其他常用函式 +作者:藍挺瑋 +版本:測試中,尚未完成 +日期:2010-06-25 +===================================================================== + + diff --git a/basic/Makefile.am b/basic/Makefile.am new file mode 100644 index 0000000..9e2a96b --- /dev/null +++ b/basic/Makefile.am @@ -0,0 +1,3 @@ +lib_LTLIBRARIES = libl4basic.la +libl4basic_la_LDFLAGS = -version-info 9:1:9 +libl4basic_la_SOURCES = l4basic.h d1array.c d1arrstr.c diff --git a/basic/config.h b/basic/config.h new file mode 100644 index 0000000..a8f7313 --- /dev/null +++ b/basic/config.h @@ -0,0 +1,80 @@ +/* basic/config.h. Generated from config.h.in by configure. */ +/* basic/config.h.in. Generated from configure.ac by autoheader. */ + +/* Define to 1 if you have the <dlfcn.h> header file. */ +#define HAVE_DLFCN_H 1 + +/* Define to 1 if you have the <inttypes.h> header file. */ +#define HAVE_INTTYPES_H 1 + +/* Define to 1 if your system has a GNU libc compatible `malloc' function, and + to 0 otherwise. */ +#define HAVE_MALLOC 1 + +/* Define to 1 if you have the <memory.h> header file. */ +#define HAVE_MEMORY_H 1 + +/* Define to 1 if your system has a GNU libc compatible `realloc' function, + and to 0 otherwise. */ +#define HAVE_REALLOC 1 + +/* Define to 1 if you have the <stdint.h> header file. */ +#define HAVE_STDINT_H 1 + +/* Define to 1 if you have the <stdlib.h> header file. */ +#define HAVE_STDLIB_H 1 + +/* Define to 1 if you have the <strings.h> header file. */ +#define HAVE_STRINGS_H 1 + +/* Define to 1 if you have the <string.h> header file. */ +#define HAVE_STRING_H 1 + +/* Define to 1 if you have the <sys/stat.h> header file. */ +#define HAVE_SYS_STAT_H 1 + +/* Define to 1 if you have the <sys/types.h> header file. */ +#define HAVE_SYS_TYPES_H 1 + +/* Define to 1 if you have the <unistd.h> header file. */ +#define HAVE_UNISTD_H 1 + +/* Define to the sub-directory in which libtool stores uninstalled libraries. + */ +#define LT_OBJDIR ".libs/" + +/* Define to 1 if your C compiler doesn't accept -c and -o together. */ +/* #undef NO_MINUS_C_MINUS_O */ + +/* Name of package */ +#define PACKAGE "l4darr" + +/* Define to the address where bug reports for this package should be sent. */ +#define PACKAGE_BUGREPORT "lantw44@gmail.com" + +/* Define to the full name of this package. */ +#define PACKAGE_NAME "l4darr" + +/* Define to the full name and version of this package. */ +#define PACKAGE_STRING "l4darr 0.9" + +/* Define to the one symbol short name of this package. */ +#define PACKAGE_TARNAME "l4darr" + +/* Define to the home page for this package. */ +#define PACKAGE_URL "" + +/* Define to the version of this package. */ +#define PACKAGE_VERSION "0.9" + +/* Define to 1 if you have the ANSI C header files. */ +#define STDC_HEADERS 1 + +/* Version number of package */ +#define VERSION "0.9" + +/* Define to rpl_malloc if the replacement function should be used. */ +/* #undef malloc */ + +/* Define to rpl_realloc if the replacement function should be used. */ +/* #undef realloc */ diff --git a/basic/d1array.c b/basic/d1array.c new file mode 100644 index 0000000..460fb12 --- /dev/null +++ b/basic/d1array.c @@ -0,0 +1,110 @@ +#include <stdlib.h> +#include <string.h> +#include "l4basic.h" + +L4DA* l4da_create_setmax(int itemsize, int len, int maxlen){ + if(itemsize <= 0 || len < 0 || maxlen < len){ + return NULL; + } + L4DA* arr = (L4DA*)malloc(sizeof(L4DA)); + if(arr == NULL){ + return NULL; + } + arr->arr_itemsize = itemsize; + arr->arr_curlen = len; + arr->arr_maxlen = maxlen; + if(maxlen != 0){ + arr->arr_data = malloc(itemsize * maxlen); + if(arr->arr_data == NULL){ + free(arr); + return NULL; + } + }else{ + arr->arr_data = NULL; + } + return arr; +} + +L4DA* l4da_create(int itemsize, int len){ + return l4da_create_setmax(itemsize, len, len); +} + +void l4da_free(L4DA* arr){ + if(arr->arr_data != NULL){ + free(arr->arr_data); + } + free(arr); +} + +int l4da_pushback(L4DA* arr, void* data){ + if((arr->arr_maxlen) < (arr->arr_curlen + 1)){ + if(arr->arr_maxlen != 0){ + if(l4da_setmax(arr, arr->arr_maxlen*2) < 0){ + return -1; + } + }else{ + if(l4da_setmax(arr, 1) < 0){ + return -1; + } + } + } + memcpy(l4da_vp(arr, arr->arr_curlen), data, arr->arr_itemsize); + arr->arr_curlen++; + return 0; +} + +int l4da_setlen(L4DA* arr, int len){ + if(len > (arr->arr_maxlen)){ + if(l4da_setmax(arr, len) < 0){ + return -1; + }else{ + arr->arr_curlen = len; + } + }else{ + arr->arr_curlen = len; + return 0; + } + return 0; +} + +int l4da_setmax(L4DA* arr, int max){ + void* newptr; + if(arr->arr_data == NULL){ + newptr = malloc((arr->arr_itemsize)*max); + if(newptr == NULL){ + return -1; + }else{ + arr->arr_maxlen = max; + arr->arr_data = newptr; + return 0; + } + } + newptr = realloc(arr->arr_data, (arr->arr_itemsize)*max); + if(newptr == NULL){ + return -1; + }else{ + arr->arr_maxlen = max; + arr->arr_data = newptr; + } + return 0; +} + +int l4da_strip(L4DA* arr){ + if(arr->arr_data == NULL){ + return 0; + } + L4DA* newptr; + /* 其實縮小空間營該一定會成功才對...... + * 不過還是保險一點,加個判斷式,別說 memory leak 是我害的 + * 當然也是避免編譯器一直跳 warning + */ + if((arr->arr_maxlen) > (arr->arr_curlen)){ + arr->arr_maxlen = arr->arr_curlen; + newptr = realloc(arr->arr_data, (arr->arr_curlen)*(arr->arr_itemsize)); + if(newptr == NULL){ + return -1; + } + arr->arr_data = newptr; + } + return 0; +} diff --git a/basic/d1arrstr.c b/basic/d1arrstr.c new file mode 100644 index 0000000..98329f1 --- /dev/null +++ b/basic/d1arrstr.c @@ -0,0 +1,43 @@ +#include <string.h> +#include "l4basic.h" + +L4DA* l4da_dup(const L4DA* arr){ + L4DA* newarr = l4da_create_setmax( + l4da_itemsize(arr), l4da_getlen(arr), l4da_getmax(arr)); + if(newarr == NULL){ + return NULL; + } + memcpy(newarr->arr_data, arr->arr_data, + l4da_getlen(arr) * l4da_itemsize(arr)); + return newarr; +} + +int l4da_combine(L4DA* arr, const L4DA* att){ + if(l4da_itemsize(arr) != l4da_itemsize(att)){ + return -2; + } + if(l4da_setlen(arr, l4da_getlen(arr) + l4da_getlen(att)) < 0){ + return -1; + } + memcpy(l4da_vp(arr, l4da_getlen(arr)), att->arr_data, + l4da_getlen(att) * l4da_itemsize(att)); + return 0; +} + +L4DA* l4da_filereadline_delim(FILE* infile, int chr){ + L4DA* newarr = l4da_create(1, 0); + if(newarr == NULL){ + return NULL; + } + int c; + char towrite; + do{ + c = getc(infile); + towrite = c; + if(l4da_pushback(newarr, (void*)&towrite) < 0){ + l4da_free(newarr); + return NULL; + } + }while(c != chr); + return newarr; +} diff --git a/basic/d2array.c b/basic/d2array.c new file mode 100644 index 0000000..6c472a3 --- /dev/null +++ b/basic/d2array.c @@ -0,0 +1,9 @@ +#include <stdlib.h> +#include "l4basic.h" + +L4DA2* l4da2_create_setmax(int itemsize, L4XY len, L4XY maxlen){ + if(len.x < 0 || len.y < 0 || maxlen.x < 0 || maxlen.y < 0) + return NULL; + } + L4DA2* arr = (L4DA2*)malloc(); +} diff --git a/basic/l4basic.h b/basic/l4basic.h new file mode 100644 index 0000000..9c9cd3c --- /dev/null +++ b/basic/l4basic.h @@ -0,0 +1,146 @@ + +#ifndef L4LIB_DYNAMIC_ARRAY +#define L4LIB_DYNAMIC_ARRAY + +#include <stdio.h> /* 取得 FILE */ + +#if 0 +/* 這兩個常數僅供 create 類函式作為參數使用*/ +#define L4DARR_NONE 0 /* 起始大小為零 */ +#define L4DARR_AUTO -1 /* 自動決定起始大小 */ +#endif + +/*********** 一維陣列 ***********/ + +typedef struct l4lib_dyn_arr{ + int arr_itemsize; /* 每個項目的大小 */ + int arr_curlen; /* 陣列總長度 */ + int arr_maxlen; /* 陣列最大長度 */ + void* arr_data; /* 資料區 */ +} L4DA ; + +L4DA* l4da_create_setmax(int, int, int); +/* #define l4da_create(itemsize, len) \ + (l4da_create_setmax((itemsize), (len), (len))) */ +L4DA* l4da_create(int, int); +void l4da_free(L4DA*); +int l4da_pushback(L4DA*, void*); +#define l4da_popback(arr) (((arr)->arr_curlen)--) +#define l4da_getlen(arr) ((arr)->arr_curlen) +int l4da_setlen(L4DA*, int); +#define l4da_getmax(arr) ((arr)->arr_maxlen) +int l4da_setmax(L4DA*, int); +int l4da_strip(L4DA*); +#define l4da_itemsize(arr) ((arr)->arr_itemsize) +#define l4da_v(arr, type, num) \ + (*(((type*)((arr)->arr_data))+(num))) +#define l4da_vp(arr, num) \ + ((void*)(((char*)((arr)->arr_data))+(((arr)->arr_itemsize)*(num)))) + +#define l4da_readline (l4da_filereadline_delim(stdin, '\n')) +#define l4da_readline_delim(delim) (l4da_filereadline_delim(stdin, (delim))) +#define l4da_filereadline(infile) (l4da_filereadline_delim((infile), '\n')) +L4DA* l4da_filereadline_delim(FILE*, int); + +L4DA* l4da_dup(const L4DA*); +int l4da_combine(L4DA*, const L4DA*); + +/*********** 二維陣列 (其實是用一維陣列來模擬,功能有限) ***********/ + +typedef struct l4lib_dyn_2darr{ + int arr_itemsize; /* 每個項目的大小 */ + int arr_lenx; /* 陣列 x 方向長度 */ + int arr_leny; /* 陣列 y 方向長度 */ + void* arr_data; /* 資料區 */ +} L4DA2 ; + +L4DA2* l4da2_create(int, int, int); +void l4da2_free(L4DA2*); +#define l4da2_getlenx(arr) ((arr)->arr_lenx) +#define l4da2_getleny(arr) ((arr)->arr_leny) +#define l4da2_itemsize(arr) ((arr)->arr_itemsize) +#define l4da2_v(arr, type, numx, numy) \ + (*(((type*)((arr)->arr_data))+((numx)*(l4da2_getlenx(arr)))+(numy))) +#define l4da2_vp(arr, numx, numy) \ + ((void*)(((char*)((arr)->arr_data))+ \ + ((arr)->arr_itemsize)*((numx)*(l4da2_getlenx(arr))+(numy)))) + +#if 0 +typedef struct l4lib_xycoord{ /* 為了方便一次傳兩個數字回來 */ + int x; + int y; +} L4XY; + +typedef struct l4lib_dyn_2darr{ + int arr_itemsize; /* 每個項目的大小 */ + L4XY arr_curlen; /* 陣列總長度 */ + L4XY arr_maxlen; /* 陣列最大長度 */ + void** arr_data; /* 資料區 */ +} L4DA2 ; + +L4DA2* l4da2_create_setmax(int, L4XY, L4XY); +/* #define l4da2_create(itemsize, len) \ + (l4da2_create_setmax((itemsize), (len), (len))) */ +L4DA2* l4da2_create(int, L4XY); +void l4da2_free(L4DA2*); +L4XY l4da2_getlen(L4DA2*); +int l4da2_setlen(L4DA2*, L4XY); +L4XY l4da2_getmax(L4DA2*); +int l4da2_setmax(L4DA2*, L4XY); +int l4da2_strip(L4DA2*); +#define l4da2_itemsize(arr) ((arr)->arr_itemsize) +#define l4da2_v(arr, type, numx, numy) \ + (*((*(((type**)((arr)->arr_data))+(numx)))+(numy))) +#define l4da2_vp(arr, type, numx, numy) \ + ((*(((type**)((arr)->arr_data))+(numx)))+(numy)) +#define l4da2_v_coord(arr, type, num) \ + (l4da2_v((arr), (type), ((num).x)), ((num).y)) +#define l4da2_vp_coord(arr, num) \ + (l4da2_vp((arr), (type), ((num).x)), ((num).y)) +#endif + + +/*********** List ***********/ + +struct l4lib_list_node{ /* list 中每一個項目用的資料結構,會有很多個 */ + struct l4lib_list_node* node_prev; + struct l4lib_list_node* node_next; + void* node_data; +}; + +typedef struct l4lib_list{ /* 管理 list 用的,每個 list 只有一個 */ + struct l4lib_list_node* list_first; + struct l4lib_list_node* list_last; + struct l4lib_list_node* list_current; + int list_prevcount; + int list_nextcount; + int list_len; +} L4LL; + +/* 這兩個常數僅供 l4ll_remove 使用 */ +#define L4LL_PREV 1 /* 刪除後移至上一項 */ +#define L4LL_NEXT 2 /* 刪除後移至下一項 */ + +/* 這兩個常數僅供 l4ll_goto 使用 */ +#define L4LL_FRONT 3 +#define L4LL_BACK 4 + +L4LL* l4ll_create(void); +void l4ll_free(L4LL*); +void* l4ll_getcur(L4LL*); +void* l4ll_getback(L4LL*); +void* l4ll_getfront(L4LL*); +int l4ll_pushback(L4LL*, void*); +int l4ll_pushfront(L4LL*, void*); +int l4ll_popback(L4LL*); +int l4ll_popfront(L4LL*); +int l4ll_remove(L4LL*, int); +int l4ll_insprev(L4LL*); +int l4ll_insnext(L4LL*); +int l4ll_goto(L4LL*, int, int); +#define l4ll_getlen(list) +#define l4ll_getc(list) +#define l4ll_getpc(list) +#define l4ll_getnc(list) + +#endif diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..5ad2533 --- /dev/null +++ b/configure.ac @@ -0,0 +1,27 @@ +# -*- Autoconf -*- +# Process this file with autoconf to produce a configure script. + +AC_INIT([l4darr], [0.9], [lantw44@gmail.com]) +AM_INIT_AUTOMAKE([foreign -Wall]) +AM_SILENT_RULES([yes]) +AC_CONFIG_SRCDIR([basic]) +AC_CONFIG_HEADERS([basic/config.h]) + +# Checks for programs. +AC_PROG_CC +AM_PROG_CC_C_O +AC_PROG_LIBTOOL + +# Checks for libraries. + +# Checks for header files. +AC_CHECK_HEADERS([stdlib.h string.h]) + +# Checks for typedefs, structures, and compiler characteristics. + +# Checks for library functions. +AC_FUNC_MALLOC +AC_FUNC_REALLOC + +AC_CONFIG_FILES([Makefile basic/Makefile]) +AC_OUTPUT |