diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 71787321..36e9e284 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -195,64 +195,10 @@ SET (C_DEPS_SRC structures/src/StringArray/dim.c structures/src/StringArray/copy.c structures/src/StringArray/alloc.c - structures/src/String/trim.c - structures/src/String/tostring.c - structures/src/String/strtok.c - structures/src/String/strtoi.c - structures/src/String/strtof.c - structures/src/String/strtod.c - structures/src/String/strtake.c - structures/src/String/strstr.c - structures/src/String/strsel.c - structures/src/String/strrchr.c - structures/src/String/strovwt.c - structures/src/String/strncat.c - structures/src/String/strmod.c - structures/src/String/strlen.c - structures/src/String/strins.c - structures/src/String/strext.c - structures/src/String/strdrop.c - structures/src/String/strcmp.c - structures/src/String/strncmp.c - structures/src/String/strcasecmp.c - structures/src/String/strncasecmp.c - structures/src/String/strchr.c - structures/src/String/strcat.c - structures/src/String/sscanf.c - structures/src/String/sscanfstr.c - structures/src/String/strcspn.c - structures/src/String/strspn.c - structures/src/String/sprintf.c - structures/src/String/itos.c - structures/src/String/ftos.c - structures/src/String/dtos.c - structures/src/String/ctos.c - structures/src/String/copystr.c - structures/src/String/freestr.c - structures/src/String/btos.c - structures/src/List/tl.c - structures/src/List/take.c - structures/src/List/nth.c - structures/src/List/nil.c - structures/src/List/length.c - structures/src/List/hd.c - structures/src/List/free.c - structures/src/List/empty.c - structures/src/List/drop.c - structures/src/List/cons.c - structures/src/List/append.c + structures/src/String/Str.c + structures/src/List/List.c structures/src/Format/wordsize.c - structures/src/Constants/tinydouble.c - structures/src/Constants/minmax.c - structures/src/Constants/minint.c - structures/src/Constants/minfloat.c - structures/src/Constants/mindouble.c - structures/src/Constants/maxint.c - structures/src/Constants/maxfloat.c - structures/src/Constants/maxdouble.c - structures/src/Constants/epidouble.c - structures/src/Constants/randmax.c - structures/src/Char/ctype.c + structures/src/Constants/Constants.c structures/src/Char/Char.c stdio/src/TermFile/stdstreams.c stdio/src/TermFile/printf.c @@ -435,6 +381,7 @@ FOREACH (name ${SAC_SRC}) # of the call to sac2c. GET_FILENAME_COMPONENT (dir "${CMAKE_CURRENT_BINARY_DIR}/${name}" DIRECTORY) GET_FILENAME_COMPONENT (dst ${name} NAME_WE) + # GET_FILENAME_COMPONENT (src_dir "${src}" DIRECTORY) SET (mod "${DLL_BUILD_DIR}/${TARGET_ENV}/${SBI}/lib${dst}Mod${VARIANT}${MODEXT}") SET (tree @@ -473,6 +420,7 @@ FOREACH (name ${SAC_SRC}) ADD_CUSTOM_COMMAND ( OUTPUT "${mod}" "${tree}" COMMAND + # ${SAC2C} -v0 -maxoptcyc 2 -linksetsize ${LINKSETSIZE} ${NOTREE_FLAG} ${SAC2C_CC_FLAGS} -Xc -I"${src_dir}" -o ${DLL_BUILD_DIR} "${src}" ${SAC2C} -v0 -maxoptcyc 2 -linksetsize ${LINKSETSIZE} ${NOTREE_FLAG} ${SAC2C_CC_FLAGS} -o ${DLL_BUILD_DIR} "${src}" WORKING_DIRECTORY "${dir}" diff --git a/src/structures/Char.sac b/src/structures/Char.sac index e222811e..dda232e0 100644 --- a/src/structures/Char.sac +++ b/src/structures/Char.sac @@ -14,59 +14,59 @@ export all; ******************************************************************************/ external bool isalpha(char c); - #pragma linkobj "src/Char/ctype.o" #pragma linkname "SACisalpha" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + // #pragma header "src/Char/Char.h" external bool isupper(char c); - #pragma linkobj "src/Char/ctype.o" #pragma linkname "SACisupper" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + // #pragma header "src/Char/Char.h" external bool islower(char c); - #pragma linkobj "src/Char/ctype.o" #pragma linkname "SACislower" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + // #pragma header "src/Char/Char.h" external bool isdigit(char c); - #pragma linkobj "src/Char/ctype.o" #pragma linkname "SACisdigit" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + // #pragma header "src/Char/Char.h" external bool isxdigit(char c); - #pragma linkobj "src/Char/ctype.o" #pragma linkname "SACisxdigit" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + // #pragma header "src/Char/Char.h" external bool isspace(char c); - #pragma linkobj "src/Char/ctype.o" #pragma linkname "SACisspace" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + // #pragma header "src/Char/Char.h" external bool ispunct(char c); - #pragma linkobj "src/Char/ctype.o" #pragma linkname "SACispunct" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + // #pragma header "src/Char/Char.h" external bool isalnum(char c); - #pragma linkobj "src/Char/ctype.o" #pragma linkname "SACisalnum" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + // #pragma header "src/Char/Char.h" external bool isprint(char c); - #pragma linkobj "src/Char/ctype.o" #pragma linkname "SACisprint" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + // #pragma header "src/Char/Char.h" external bool isgraph(char c); - #pragma linkobj "src/Char/ctype.o" #pragma linkname "SACisgraph" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + // #pragma header "src/Char/Char.h" external bool iscntrl(char c); - #pragma linkobj "src/Char/ctype.o" #pragma linkname "SACiscntrl" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + // #pragma header "src/Char/Char.h" /****************************************************************************** * @@ -76,14 +76,14 @@ external bool iscntrl(char c); ******************************************************************************/ external bool isascii(int N); - #pragma linkobj "src/Char/ctype.o" #pragma linkname "SACisascii" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + // #pragma header "src/Char/Char.h" external char toascii(int N); - #pragma linkobj "src/Char/ctype.o" #pragma linkname "SACtoascii" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + // #pragma header "src/Char/Char.h" /****************************************************************************** * @@ -92,14 +92,14 @@ external char toascii(int N); ******************************************************************************/ external char tolower(char c); - #pragma linkobj "src/Char/ctype.o" #pragma linkname "SACtolower" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + // #pragma header "src/Char/Char.h" external char toupper(char c); - #pragma linkobj "src/Char/ctype.o" #pragma linkname "SACtoupper" - #pragma linksign [0,1] + #pragma linkobj "src/Char/Char.o" + // #pragma header "src/Char/Char.h" /****************************************************************************** * diff --git a/src/structures/Constants.sac b/src/structures/Constants.sac index 062f4263..35f30b45 100644 --- a/src/structures/Constants.sac +++ b/src/structures/Constants.sac @@ -7,107 +7,111 @@ export all; #ifdef FULLTYPES external byte minbyte(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external byte maxbyte(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external short minshort(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external short maxshort(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external longlong minlonglong(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external longlong maxlonglong(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external ubyte minubyte(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external ubyte maxubyte(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external ushort minushort(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external ushort maxushort(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external ulonglong minulonglong(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external ulonglong maxulonglong(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" #endif /* FULLTYPES */ external int minint(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minint.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external int maxint(); - #pragma linksign[0] - #pragma linkobj "src/Constants/maxint.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external long minlong(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external long maxlong(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external uint minuint(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external uint maxuint(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external ulong minulong(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external ulong maxulong(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minmax.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external float minfloat(); - #pragma linksign[0] - #pragma linkobj "src/Constants/minfloat.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external float maxfloat(); - #pragma linksign[0] - #pragma linkobj "src/Constants/maxfloat.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external double mindouble(); - #pragma linksign[0] - #pragma linkobj "src/Constants/mindouble.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external double tinydouble(); - #pragma linksign[0] - #pragma linkobj "src/Constants/tinydouble.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external double maxdouble(); - #pragma linksign[0] - #pragma linkobj "src/Constants/maxdouble.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" external double epidouble(); - #pragma linksign[0] - #pragma linkobj "src/Constants/epidouble.o" + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" + +external int randmax(); + #pragma linkobj "src/Constants/Constants.o" + // #pragma header "src/Constants/Constants.h" \ No newline at end of file diff --git a/src/structures/List.sac b/src/structures/List.sac index edf0e95b..75d6b6dd 100644 --- a/src/structures/List.sac +++ b/src/structures/List.sac @@ -5,61 +5,65 @@ module List deprecated "The List module contains a space leak."; export all; /* - * The SAC standard module List provides lists of integers as an - * additional data type offering access functions as usual. + * The SAC standard module List provides lists of integers as an + * additional data type offering access functions as usual. */ external typedef list; - #pragma linkobj "src/List/free.o" - #pragma freefun "SAC_List_free_list" + #pragma copyfun "SAClistcopy" + #pragma freefun "SAClistfree" + #pragma linkobj "src/List/List.o" + // #pragma ctype "list*" external list nil(); - #pragma linkname "SAC_List_nil" - #pragma linksign [1] - #pragma refcounting [0] - #pragma linkobj "src/List/nil.o" -external list cons( int ELEM, list LIST); - #pragma linkname "SAC_List_cons" - #pragma linksign [1,2,3] - #pragma refcounting [0,2] - #pragma linkobj "src/List/cons.o" -external int hd( list LIST); - #pragma linkname "SAC_List_hd" - #pragma linksign [0,1] - #pragma refcounting [1] - #pragma linkobj "src/List/hd.o" -external list tl( list LIST); - #pragma linkname "SAC_List_tl" - #pragma linksign [1,2] - #pragma refcounting [0,1] - #pragma linkobj "src/List/tl.o" -external bool empty( list LIST); + #pragma linkname "SAClistnil" + #pragma linkobj "src/List/List.o" + // #pragma header "src/List/List.h" + +external list cons(int ELEM, list LIST); + #pragma linkname "SAClistcons" + #pragma linkobj "src/List/List.o" + // #pragma header "src/List/List.h" + +external int hd(list LIST); + #pragma linkname "SAClisthead" + #pragma linkobj "src/List/List.o" + // #pragma header "src/List/List.h" + +external list tl(list LIST); + #pragma linkname "SAClisttail" + #pragma sacarg [0] + #pragma linkobj "src/List/List.o" + // #pragma header "src/List/List.h" + +external bool empty(list LIST); #pragma linkname "SAC_List_empty" - #pragma linksign [0,1] - #pragma refcounting [1] - #pragma linkobj "src/List/empty.o" -external list append( list LIST1, list LIST2); - #pragma linkname "SAC_List_append" - #pragma linksign [1,2,3] - #pragma refcounting [0,1,2] - #pragma linkobj "src/List/append.o" -external int nth( int N, list LIST); - #pragma linkname "SAC_List_nth" - #pragma linksign [0,1,2] - #pragma refcounting [2] - #pragma linkobj "src/List/nth.o" -external int length( list LIST); - #pragma linkname "SAC_List_length" - #pragma linksign [0,1] - #pragma refcounting [1] - #pragma linkobj "src/List/length.o" -external list drop( int N, list LIST); - #pragma linkname "SAC_List_drop" - #pragma linksign [1,2,3] - #pragma refcounting [0,2] - #pragma linkobj "src/List/drop.o" -external list take( int N, list LIST); - #pragma linkname "SAC_List_take" - #pragma linksign [1,2,3] - #pragma refcounting [0,2] - #pragma linkobj "src/List/take.o" + #pragma linkobj "src/List/List.o" + // #pragma header "src/List/List.h" + +external list append(list LIST1, list LIST2); + #pragma linkname "SAClistappend" + #pragma sacarg [0,1,2] + #pragma linkobj "src/List/List.o" + // #pragma header "src/List/List.h" + +external int nth(int N, list LIST); + #pragma linkname "SAClistnth" + #pragma linkobj "src/List/List.o" + // #pragma header "src/List/List.h" + +external int length(list LIST); + #pragma linkname "SAClistlength" + #pragma linkobj "src/List/List.o" + // #pragma header "src/List/List.h" + +external list drop(int N, list LIST); + #pragma linkname "SAClistdrop" + #pragma sacarg [0,2] + #pragma linkobj "src/List/List.o" + // #pragma header "src/List/List.h" + +external list take(int N, list LIST); + #pragma linkname "SAClisttake" + #pragma linkobj "src/List/List.o" + // #pragma header "src/List/List.h" diff --git a/src/structures/String.sac b/src/structures/String.sac index 7167ebfe..1f84cf4c 100644 --- a/src/structures/String.sac +++ b/src/structures/String.sac @@ -11,116 +11,130 @@ module String; export all except { indent }; external typedef string; - #pragma freefun "free_string" - #pragma linkobj "src/String/freestr.o" + #pragma ctype "char*" #pragma copyfun "copy_string" - #pragma linkobj "src/String/copystr.o" + #pragma freefun "free_string" + #pragma linkobj "src/String/Str.o" external string to_string(char[.] A, int LENGTH); - #pragma linksign [1,2,3] - #pragma refcounting [0,1] - #pragma linkobj "src/String/tostring.o" + #pragma linkname "SACtostring" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" + /* Convert character array to string. */ + +external string to_auto_string(char[.] A); + #pragma linkname "SACautotostring" + #pragma sacarg [1] + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert character array to string. */ external string strmod(string str, int P, char C); - #pragma linksign [1,2,3,4] - #pragma refcounting [0,1] - #pragma linkobj "src/String/strmod.o" + #pragma linkname "SACstrmod" + #pragma linksign [1,1,2,3] + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Change position P of string str to character C. */ external string strins(string S1, int P, string S2); - #pragma linkobj "src/String/strins.o" - #pragma linksign [0,1,2,3] + #pragma linkname "SACstrins" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Insert string S2 into string S1 starting at position P. */ external string strovwt(string S1, int P, string S2); - #pragma linksign [1,2,3,4] - #pragma refcounting [0,1] - #pragma linkobj "src/String/strovwt.o" + #pragma linkname "SACstrovwt" + #pragma linksign [1,1,2,3] + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Overwrite string S2 with string S1 starting at position P. */ external char strsel(string str, int P); - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strsel.o" + #pragma linkname "SACstrsel" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Select character P of string str. */ external string strcat(string S1, string S2); #pragma linkname "SACstrcat" - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strcat.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Concatenate strings S1 and S2. */ external string +(string S1, string S2); #pragma linkname "SACstrcat" - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strcat.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Infix version of strcat. */ external string strncat(string S1, string S2, int N); #pragma linkname "SACstrncat" - #pragma linksign [0,1,2,3] - #pragma linkobj "src/String/strncat.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Concatenate string S1 and the first N characters of string S2. */ external int strcmp(string S1, string S2); #pragma linkname "SACstrcmp" - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strcmp.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Compare strings S1 and S2. Result is negative if S1 < S2, positive if S1 > S2 and zero if S1 == S2. */ external int strncmp(string S1, string S2, int N); #pragma linkname "SACstrncmp" - #pragma linksign [0,1,2,3] - #pragma linkobj "src/String/strncmp.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Compare the first N characters of strings S1 and S2. Result is negative if S1 < S2, positive if S1 > S2 and zero if S1 == S2. */ external int strcasecmp(string S1, string S2); #pragma linkname "SACstrcasecmp" - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strcasecmp.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Compare strings S1 and S2 while ignoring upper/lower case. Result is negative if S1 < S2 alphabetically, positive if S1 > S2 and zero if S1 == S2, all modulo case. */ external int strncasecmp(string S1, string S2, int N); #pragma linkname "SACstrncasecmp" - #pragma linksign [0,1,2,3] - #pragma linkobj "src/String/strncasecmp.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Compare the first N characters of strings S1 and S2 ignoring case. Result is negative if S1 < S2 alphabetically, positive if S1 > S2 and zero if S1 == S2, all modulo case. */ external int strlen(string str); - #pragma linksign [0,1] #pragma linkname "SACstrlen" - #pragma linkobj "src/String/strlen.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Return the length of string str. */ external string strtake(string str, int N); - #pragma linksign [1,2,3] - #pragma refcounting [0,1] - #pragma linkobj "src/String/strtake.o" + #pragma linkname "SACstrtake" + #pragma linksign [1, 1, 2] + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Return the first N characters of string str. */ external string strdrop(string str, int N); - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strdrop.o" + #pragma linkname "SACstrdrop" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Return all but the first N characters of string str. */ external string strext(string str, int FIRST, int LEN); - #pragma linksign [0,1,2,3] - #pragma linkobj "src/String/strext.o" + #pragma linkname "SACstrext" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Extract the substring beginning at character FIRST with length LEN from string str. */ external string sprintf(string FORMAT, ...); #pragma linkname "SACsprintf" - #pragma linksign [0,1] - #pragma linkobj "src/String/sprintf.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Generate a string depending on the format string and a variable number of arguments similar to the printf and fprintf functions. */ @@ -128,7 +142,8 @@ external string sprintf(string FORMAT, ...); external int, ... sscanf(string str, string FORMAT); #pragma linkname "SACsscanf" #pragma linksign [0,1,2] - #pragma linkobj "src/String/sscanf.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Scan the string str concerning the format string FORMAT. The format string is the same as for fscanf. The int result gives the maximum number of successfully performed @@ -138,54 +153,56 @@ external int, ... sscanf(string str, string FORMAT); errors. Use sscanf_str instead. */ external string sscanf_str(string str, string FORMAT); - #pragma linksign [0,1,2] - #pragma linkobj "src/String/sscanfstr.o" + #pragma linkname "SACsscanf_str" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Scan the string str concerning the format string FORMAT. FORMAT must have exactly one string conversion specifier. The scanned substring is returned. */ external int strchr(string str, char C); - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strchr.o" #pragma linkname "SACstrchr" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Return the position of the first occurrence of character C in string str. The result is -1 if character C does not occurr at all. */ external int strrchr(string str, char C); #pragma linkname "SACstrrchr" - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strrchr.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Return the position of the last occurrence of character C in string str. The result is -1 if character C does not occurr at all. */ external int strcspn(string str, string REJECT); #pragma linkname "SACstrcspn" - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strcspn.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Return the length of the initial segment of str which consists entirely of characters not in REJECT. */ external int strspn(string str, string ACCEPT); #pragma linkname "SACstrspn" - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strspn.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Return the length of the initial segment of str which consists entirely of characters in ACCEPT. */ external int strstr(string HAYSTACK, string NEEDLE); #pragma linkname "SACstrstr" - #pragma linksign [0,1,2] - #pragma linkobj "src/String/strstr.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Find the first occurrence of the string NEEDLE in the string HAYSTACK. Return the index to this occurrence. If NEEDLE could not be found then return -1. */ external string, string strtok(string str, string SEP); #pragma linkname "SACstrtok" - #pragma linksign [0,1,2,3] - #pragma linkobj "src/String/strtok.o" + #pragma linksign [1, 2, 3, 4] + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Split string str into a token and the remainder string using the characters from SEP as delimiters. If str starts with characters from SEP then these are skipped until @@ -193,32 +210,35 @@ external string, string strtok(string str, string SEP); external string chomp(string str); #pragma linkname "SACchomp" - #pragma linksign [0,1] - #pragma linkobj "src/String/trim.o" + #pragma linksign [1,1] + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Remove trailing line terminators from str. */ external string rtrim(string str); #pragma linkname "SACrtrim" - #pragma linksign [0,1] - #pragma linkobj "src/String/trim.o" + #pragma linksign [1,1] + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Remove trailing whitespace from str. */ external string ltrim(string str); #pragma linkname "SACltrim" - #pragma linksign [0,1] - #pragma linkobj "src/String/trim.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Remove leading whitespace from str. */ external string trim(string str); #pragma linkname "SACtrim" - #pragma linksign [0,1] - #pragma linkobj "src/String/trim.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Remove leading and trailing whitespace from str. */ external int, string strtoi(string str, int BASE); #pragma linkname "SACstrtoi" #pragma linksign [0,1,2,3] - #pragma linkobj "src/String/strtoi.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert the string str into an integer value. BASE specifies the format and must be in the range from 2 to 32. This feature allows for scanning hexadecimal or octal numbers as well as decimal @@ -228,58 +248,60 @@ external int, string strtoi(string str, int BASE); external float, string strtof(string str); #pragma linkname "SACstrtof" #pragma linksign [0,1,2] - #pragma linkobj "src/String/strtof.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert the string str into a float value. The string result contains the remainder of the input string. */ external double, string strtod(string str); #pragma linkname "SACstrtod" #pragma linksign [0,1,2] - #pragma linkobj "src/String/strtod.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert the string str into a double value. The string result contains the remainder of the input string. */ external int toi(string str); #pragma linkname "SACtoi" - #pragma linksign [0,1] - #pragma linkobj "src/String/strtoi.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert the string str into an integer value. In contrast to strtoi the number must always be in decimal representation. */ external float tof(string str); #pragma linkname "SACtof" - #pragma linksign [0,1] - #pragma linkobj "src/String/strtof.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert the string str into a float value. */ external double tod(string str); #pragma linkname "SACtod" - #pragma linksign [0,1] - #pragma linkobj "src/String/strtod.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert the string str into a double value. */ external string tos(int N); #pragma linkname "SACitos" - #pragma linksign [0,1] - #pragma linkobj "src/String/itos.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert integer into string. */ external string tos(float N); #pragma linkname "SACftos" - #pragma linksign [0,1] - #pragma linkobj "src/String/ftos.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert float into string. */ external string tos(double N); #pragma linkname "SACdtos" - #pragma linksign [0,1] - #pragma linkobj "src/String/dtos.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert double into string. */ external string tos(bool B); #pragma linkname "SACbtos" - #pragma linksign [0,1] - #pragma linkobj "src/String/btos.o" + #pragma linkobj "src/String/Str.o" + // #pragma header "src/String/Str.h" /* Convert boolean into string. */ inline string indent(int indent, string str) diff --git a/src/structures/src/Char/Char.c b/src/structures/src/Char/Char.c new file mode 100644 index 00000000..503308ef --- /dev/null +++ b/src/structures/src/Char/Char.c @@ -0,0 +1,78 @@ +#include + +#include "Char.h" + +bool SACisalpha(uchar c) +{ + return isalpha(c) != 0; +} + +bool SACisupper(uchar c) +{ + return isupper(c) != 0; +} + +bool SACislower(uchar c) +{ + return islower(c) != 0; +} + +bool SACisdigit(uchar c) +{ + return isdigit(c) != 0; +} + +bool SACisxdigit(uchar c) +{ + return isxdigit(c) != 0; +} + +bool SACisspace(uchar c) +{ + return isspace(c) != 0; +} + +bool SACispunct(uchar c) +{ + return ispunct(c) != 0; +} + +bool SACisalnum(uchar c) +{ + return isalnum(c) != 0; +} + +bool SACisprint(uchar c) +{ + return isprint(c) != 0; +} + +bool SACisgraph(uchar c) +{ + return isgraph(c) != 0; +} + +bool SACiscntrl(uchar c) +{ + return iscntrl(c) != 0; +} + +bool SACisascii(sac_int c) +{ + return c >= 0 && c < 256 && isascii(c) != 0; +} + +uchar SACtoascii(sac_int c) +{ + return toascii(c & 0xFF); +} + +uchar SACtolower(uchar c) +{ + return tolower(c); +} + +uchar SACtoupper(uchar c) +{ + return toupper(c); +} diff --git a/src/structures/src/Char/Char.h b/src/structures/src/Char/Char.h new file mode 100644 index 00000000..41241ec2 --- /dev/null +++ b/src/structures/src/Char/Char.h @@ -0,0 +1,36 @@ +#ifndef _SAC_CHAR_H_ +#define _SAC_CHAR_H_ + +/* + * Implementation of standard module Char ctype functions. It is sort of + * unfortunate that we have to use these functions as they incur a per-character + * overhead. However, without them incorrect values were produced. + * There were two problems: + * - The isxxx(.) functions work on unsigned chars and not on chars. + * They may segfault when not in the range [-1,255]. + * - They may return any non-zero value when true, but a SaC bool is true if 1. + */ + +#include + +#include "sacinterface.h" + +typedef unsigned char uchar; + +bool SACisalpha(uchar c); +bool SACisupper(uchar c); +bool SACislower(uchar c); +bool SACisdigit(uchar c); +bool SACisxdigit(uchar c); +bool SACisspace(uchar c); +bool SACispunct(uchar c); +bool SACisalnum(uchar c); +bool SACisprint(uchar c); +bool SACisgraph(uchar c); +bool SACiscntrl(uchar c); +bool SACisascii(sac_int c); +uchar SACtoascii(sac_int c); +uchar SACtolower(uchar c); +uchar SACtoupper(uchar c); + +#endif /* _SAC_CHAR_H_ */ diff --git a/src/structures/src/Char/ctype.c b/src/structures/src/Char/ctype.c deleted file mode 100644 index 301e5ae3..00000000 --- a/src/structures/src/Char/ctype.c +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Implementation of standard module Char ctype functions. - * It is sort of unfortunate that we have to use these - * functions as they incur a per-character overhead. - * However, without them incorrect values were produced. - * There were two problems: - * The isxxx(.) functions work on unsigned chars and not on chars. - * They may segfault when not in the range [-1,255]. - * They may return any non-zero value when true, but a SAC bool is true if 1. - */ - -#include -#include - -typedef unsigned char uchar; - -int SACisalpha(uchar c) -{ - return isalpha(c) != 0; -} - -int SACisupper(uchar c) -{ - return isupper(c) != 0; -} - -int SACislower(uchar c) -{ - return islower(c) != 0; -} - -int SACisdigit(uchar c) -{ - return isdigit(c) != 0; -} - -int SACisxdigit(uchar c) -{ - return isxdigit(c) != 0; -} - -int SACisspace(uchar c) -{ - return isspace(c) != 0; -} - -int SACispunct(uchar c) -{ - return ispunct(c) != 0; -} - -int SACisalnum(uchar c) -{ - return isalnum(c) != 0; -} - -int SACisprint(uchar c) -{ - return isprint(c) != 0; -} - -int SACisgraph(uchar c) -{ - return isgraph(c) != 0; -} - -int SACiscntrl(uchar c) -{ - return iscntrl(c) != 0; -} - -int SACisascii(sac_int c) -{ - return c >= 0 && c < 256 && isascii(c) != 0; -} - -int SACtoascii(sac_int c) -{ - return toascii(c & 0xFF); -} - -int SACtolower(uchar c) -{ - return tolower(c); -} - -int SACtoupper(uchar c) -{ - return toupper(c); -} - diff --git a/src/structures/src/Constants/Constants.c b/src/structures/src/Constants/Constants.c new file mode 100644 index 00000000..e30a8877 --- /dev/null +++ b/src/structures/src/Constants/Constants.c @@ -0,0 +1,79 @@ +#include +#include +#include +#include "Constants.h" + +#define UCHAR_MIN 0 +#define USHRT_MIN 0 +#define UINT_MIN 0 +#define ULONG_MIN 0 +#define ULLONG_MIN 0 + +//---------- +#define minmax(rtype, SACtype, typeucase) \ +rtype max##SACtype( void ) \ +{ \ + return( typeucase##_MAX); \ +} \ + \ +rtype min##SACtype( void ) \ +{ \ + return( typeucase##_MIN); \ +} + +#include "Constants.mac" +#undef minmax +//---------- + +sac_int maxint( void) +{ + sac_int num_bits = 8 * sizeof(sac_int); + /** + * To avoid overflow: + * 2^(num_bits - 1) - 1 = 2 * 2^(num_bits - 2) - 1 = + * 2^(num_bits - 2) + (2^(num_bits - 2) - 1) + **/ + sac_int half = (sac_int)1 << (num_bits - 2); + return half + (half - 1); +} + +sac_int minint( void) +{ + sac_int num_bits = 8 * sizeof(sac_int); + return -((sac_int)1 << (num_bits - 1)); +} + +float minfloat( void) +{ + return( -FLT_MAX); /* do not use FLT_MIN here!!! */ +} + +float maxfloat( void) +{ + return( FLT_MAX); +} + +double mindouble( void) +{ + return( -DBL_MAX); +} + +double maxdouble( void) +{ + return( DBL_MAX); +} + +double tinydouble( void) +{ + return( DBL_MIN); +} + +double epidouble( void) +{ + return( DBL_EPSILON); +} + +sac_int randmax( void) +{ + return (sac_int)RAND_MAX; +} diff --git a/src/structures/src/Constants/Constants.h b/src/structures/src/Constants/Constants.h new file mode 100644 index 00000000..23bb4c75 --- /dev/null +++ b/src/structures/src/Constants/Constants.h @@ -0,0 +1,25 @@ +#ifndef STDLIB__CONSTANTS__H +#define STDLIB__CONSTANTS__H + +#include "sacinterface.h" + +//---------- +#define minmax(rtype, SACtype, typeucase) \ +rtype max##SACtype( void ); \ +rtype min##SACtype( void ); + +#include "Constants.mac" +#undef minmax +//---------- + +sac_int maxint( void); +sac_int minint( void); +float minfloat( void); +float maxfloat( void); +double mindouble( void); +double maxdouble( void); +double tinydouble( void); +double epidouble( void); +sac_int randmax( void); + +#endif // STDLIB__CONSTANTS__H diff --git a/src/structures/src/Constants/Constants.mac b/src/structures/src/Constants/Constants.mac new file mode 100644 index 00000000..678fb0da --- /dev/null +++ b/src/structures/src/Constants/Constants.mac @@ -0,0 +1,9 @@ +minmax(char, byte, CHAR) +minmax(short, short, SHRT) +minmax(long, long, LONG) +minmax(long long, longlong, LLONG) +minmax(unsigned char, ubyte, UCHAR) +minmax(unsigned short, ushort, USHRT) +minmax(unsigned int, uint, UINT) +minmax(unsigned long, ulong, ULONG) +minmax(unsigned long long, ulonglong, ULLONG) diff --git a/src/structures/src/Constants/epidouble.c b/src/structures/src/Constants/epidouble.c deleted file mode 100644 index b32593a3..00000000 --- a/src/structures/src/Constants/epidouble.c +++ /dev/null @@ -1,6 +0,0 @@ -#include - -double epidouble( void) -{ - return( DBL_EPSILON); -} diff --git a/src/structures/src/Constants/maxdouble.c b/src/structures/src/Constants/maxdouble.c deleted file mode 100644 index f70d7542..00000000 --- a/src/structures/src/Constants/maxdouble.c +++ /dev/null @@ -1,7 +0,0 @@ -#include - - -double maxdouble( void) -{ - return( DBL_MAX); -} diff --git a/src/structures/src/Constants/maxfloat.c b/src/structures/src/Constants/maxfloat.c deleted file mode 100644 index b7689521..00000000 --- a/src/structures/src/Constants/maxfloat.c +++ /dev/null @@ -1,7 +0,0 @@ -#include - - -float maxfloat( void) -{ - return( FLT_MAX); -} diff --git a/src/structures/src/Constants/maxint.c b/src/structures/src/Constants/maxint.c deleted file mode 100644 index f5080f5a..00000000 --- a/src/structures/src/Constants/maxint.c +++ /dev/null @@ -1,15 +0,0 @@ -#include - -#include - -sac_int maxint( void) -{ - sac_int num_bits = 8 * sizeof(sac_int); - /** - * To avoid overflow: - * 2^(num_bits - 1) - 1 = 2 * 2^(num_bits - 2) - 1 = - * 2^(num_bits - 2) + (2^(num_bits - 2) - 1) - **/ - sac_int half = (sac_int)1 << (num_bits - 2); - return half + (half - 1); -} diff --git a/src/structures/src/Constants/mindouble.c b/src/structures/src/Constants/mindouble.c deleted file mode 100644 index 1027eeaf..00000000 --- a/src/structures/src/Constants/mindouble.c +++ /dev/null @@ -1,18 +0,0 @@ -#include - -/* !! This function does not return DBL_MIN !! - * The reason for this is that DBL_MIN is the smallest - * normalised value that can represented. For consistency - * with maxint() and minint(), this function returns the - * lowest negative number that can be represented, which - * is the exact opposite of DBL_MAX: -DBL_MAX. - * - * To get DBL_MIN, look at tinydouble() - * - * More information can be found at: - * http://forums.codeguru.com/showthread.php?260921-DBL_MIN-and-DBL_MAX&p=799431#post799431 - */ -double mindouble( void) -{ - return( -DBL_MAX); -} diff --git a/src/structures/src/Constants/minfloat.c b/src/structures/src/Constants/minfloat.c deleted file mode 100644 index 2c587e2b..00000000 --- a/src/structures/src/Constants/minfloat.c +++ /dev/null @@ -1,7 +0,0 @@ -#include - - -float minfloat( void) -{ - return( -FLT_MAX); /* do not use FLT_MIN here!!! */ -} diff --git a/src/structures/src/Constants/minint.c b/src/structures/src/Constants/minint.c deleted file mode 100644 index b5127486..00000000 --- a/src/structures/src/Constants/minint.c +++ /dev/null @@ -1,8 +0,0 @@ -#include -#include - -sac_int minint( void) -{ - sac_int num_bits = 8 * sizeof(sac_int); - return -((sac_int)1 << (num_bits - 1)); -} diff --git a/src/structures/src/Constants/minmax.c b/src/structures/src/Constants/minmax.c deleted file mode 100644 index 94876ebb..00000000 --- a/src/structures/src/Constants/minmax.c +++ /dev/null @@ -1,28 +0,0 @@ -#include - -#define UCHAR_MIN 0 -#define USHRT_MIN 0 -#define UINT_MIN 0 -#define ULONG_MIN 0 -#define ULLONG_MIN 0 - -#define minmax(rtype, SACtype, typeucase) \ -rtype max##SACtype( void ) \ -{ \ - return( typeucase##_MAX); \ -} \ - \ -rtype min##SACtype( void ) \ -{ \ - return( typeucase##_MIN); \ -} - -minmax(char, byte, CHAR) -minmax(short, short, SHRT) -minmax(long, long, LONG) -minmax(long long, longlong, LLONG) -minmax(unsigned char, ubyte, UCHAR) -minmax(unsigned short, ushort, USHRT) -minmax(unsigned int, uint, UINT) -minmax(unsigned long, ulong, ULONG) -minmax(unsigned long long, ulonglong, ULLONG) diff --git a/src/structures/src/Constants/randmax.c b/src/structures/src/Constants/randmax.c deleted file mode 100644 index 7325aacf..00000000 --- a/src/structures/src/Constants/randmax.c +++ /dev/null @@ -1,6 +0,0 @@ -#include - -int randmax( void) -{ - return RAND_MAX; -} diff --git a/src/structures/src/Constants/tinydouble.c b/src/structures/src/Constants/tinydouble.c deleted file mode 100644 index 458acf02..00000000 --- a/src/structures/src/Constants/tinydouble.c +++ /dev/null @@ -1,9 +0,0 @@ -#include - -/* DBL_MIN represents the smallest normilised - * value that can be represented. - */ -double tinydouble( void) -{ - return( DBL_MIN); -} diff --git a/src/structures/src/List/List.c b/src/structures/src/List/List.c new file mode 100644 index 00000000..38a9940a --- /dev/null +++ b/src/structures/src/List/List.c @@ -0,0 +1,188 @@ +#include +#include + +#include "List.h" + +list *SAClistcopy(list *lst) +{ + if (lst == NULL) { + return NULL; + } + + list *cpy = malloc(sizeof(list)); + cpy->elem = lst->elem; + cpy->cons = SACARGduplicateSaCArray(lst->cons); + return cpy; +} + +list *SAClistfree(list *lst) +{ + if (lst == NULL) { + return NULL; + } + + // Check whether this triggers a refcount decrease + SACARGdeleteSacArray(&(lst->cons)); + free (lst); + return NULL; +} + +// n = 0 for unlimited +// Last element will not have cons initialized! +void deepcopy(list **out_root, list **out_last, const list *lst, sac_int n) +{ + *out_root = malloc(sizeof(list)); + *out_last = *out_root; + (*out_last)->elem = lst->elem; + n -= 1; + + while (n != 0) { + lst = SACARGgetSharedData(SACTYPE__LIST__list, lst->cons); + if (lst == NULL && n > 0) { + SAC_RuntimeError("Nth element outside of list"); + } + + if (lst == NULL) { + break; + } + + list *next = malloc(sizeof(list)); + (*out_last)->cons = SACARGcreateFromPointer(SACTYPE__LIST__list, next, 0, NULL); + *out_last = next; + (*out_last)->elem = lst->elem; + n -= 1; + } +} + +list *SAClistnil(void) +{ + return NULL; +} + +list *SAClistcons(sac_int elem, list *cons) +{ + list *res = malloc(sizeof(list)); + res->elem = elem; + res->cons = SACARGduplicateSaCArray(cons->cons); + return res; +} + +sac_int SAClisthead(list *lst) +{ + if (lst == NULL) { + SAC_RuntimeError ("Tried to get head of empty list"); + } + + return lst->elem; +} + +SACarg *SAClisttail(list *lst) +{ + if (lst == NULL) { + SAC_RuntimeError ("Tried to get tail of empty list"); + } + + return SACARGduplicateSaCArray(lst->cons); +} + +bool SAClistempty(list *lst) +{ + return lst != NULL; +} + +SACarg *SAClistappend(SACarg *sa, SACarg *sb) +{ + const list *a = SACARGgetSharedData(SACTYPE__LIST__list, sa); + if (a == NULL) { + SACARGdeleteSacArray(&sa); + return sb; + } + + list *res = NULL; + list *taila = NULL; + deepcopy(&res, &taila, a, 0); + taila->cons = sb; + + SACarg *sres = SACARGcreateFromPointer(SACTYPE__LIST__list, res, 0, NULL); + + return sres; +} + +sac_int SAClistnth(sac_int n, list *lstnc) +{ + const list *lst = lstnc; + if (n < 0) { + SAC_RuntimeError("Cannot get negative nth element of list"); + } + + while (lst != NULL) { + if (n == 0) { + return lst->elem; + } + + n -= 1; + lst = SACARGgetSharedData(SACTYPE__LIST__list, lst->cons); + } + + SAC_RuntimeError("Nth element outside of list"); + return 0; +} + +sac_int SAClistlength(list *lstnc) +{ + const list *lst = lstnc; + sac_int length = 0; + + while (lst != NULL) { + length += 1; + lst = SACARGgetSharedData(SACTYPE__LIST__list, lst->cons); + } + + return length; +} + + +SACarg *SAClistdrop(sac_int n, SACarg *slst_old) +{ + if (n < 0) { + SAC_RuntimeError("Cannot get negative nth element of list"); + } + + SACarg *slst = SACARGduplicateSaCArray(slst_old); + + while (true) { + if (n == 0) { + break; + } + + slst = ((list*)SACARGgetSharedData(SACTYPE__LIST__list, slst))->cons; + n -= 1; + } + + // This seems wrong? + // The above loop only breaks when n == 0, so this should never be possible. + if (n > 0) { + SAC_RuntimeError("Nth element outside of list"); + } + + SACARGdeleteSacArray(&slst_old); + return slst; +} + +list *SAClisttake(sac_int n, list *lst) +{ + if (n == 0) { + return NULL; + } + + if (n < 0) { + SAC_RuntimeError("Cannot get a negative amount of elements from a list"); + } + + list *res = NULL; + list *taila = NULL; + deepcopy(&res, &taila, lst, n); + taila->cons = SACARGcreateFromPointer(SACTYPE__LIST__list, NULL, 0, NULL); + + return res; +} diff --git a/src/structures/src/List/List.h b/src/structures/src/List/List.h index 218b70ac..2f13cfbb 100644 --- a/src/structures/src/List/List.h +++ b/src/structures/src/List/List.h @@ -1,22 +1,36 @@ +#ifndef _SAC_LIST_H_ +#define _SAC_LIST_H_ + /* - * Implementation of SAC standard module List + * Implementation of SAC standard module List. + * Do not try to touch this, it will fuck with your mind. + * If you decide to go ahead anyway, good luck. + * Nil = NULL inside the SACarg :) */ +#include -#include -#include - -#include "sac.h" - - -#define TRACE 0 +#include "sacinterface.h" +extern SACtypes* SACTYPE__LIST__list; typedef struct LIST { - int elem; - struct LIST *rest; - SAC_array_descriptor_t desc; + sac_int elem; + SACarg* cons; } list; - -extern void SAC_List_free_list( list *elems); +list *SAClistcopy(list *lst); +list *SAClistfree(list *lst); + +list *SAClistnil(void); +list *SAClistcons(sac_int elem, list *cons); +sac_int SAClisthead(list *lst); +SACarg* SAClisttail(list *lst); +bool SAClistempty(list *lst); +SACarg* SAClistappend(SACarg* sa, SACarg* sb); +sac_int SAClistnth(sac_int n, list *lst); +sac_int SAClistlength(list *lst); +SACarg* SAClistdrop(sac_int n, SACarg* slst_old); +list *SAClisttake(sac_int n, list *lst); + +#endif /* _SAC_LIST_H_ */ diff --git a/src/structures/src/List/append.c b/src/structures/src/List/append.c deleted file mode 100644 index 45a40d48..00000000 --- a/src/structures/src/List/append.c +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -#define res_nt (res, T_OLD((SCL, (HID, (NUQ,))))) -#define elemsA_nt (elemsA, T_OLD((SCL, (HID, (NUQ,))))) -#define elemsB_nt (elemsB, T_OLD((SCL, (HID, (NUQ,))))) -#define new_nt (new, T_OLD((SCL, (HID, (NUQ,))))) - -void SAC_List_append( SAC_ND_PARAM_out( res_nt, list *), - SAC_ND_PARAM_in( elemsA_nt, list *), - SAC_ND_PARAM_in( elemsB_nt, list *)) -{ - SAC_ND_DECL__DESC( new_nt, ) - SAC_ND_DECL__DATA( new_nt, list *, ) - - if (elemsA->rest == NULL) { /* elemsA == NIL! */ - SAC_ND_RET_out( res_nt, elemsB_nt) - if (--(DESC_RC( elemsA->desc)) == 0) { - SAC_List_free_list( elemsA); - } - } - else { - - if (DESC_RC( elemsA->desc) == 1) { /* re-use all elems while (rc == 1)! */ - SAC_ND_RET_out( res_nt, elemsA_nt) - - do { - new = elemsA; - elemsA = elemsA->rest; - } - while ((elemsA->rest != NULL) && (DESC_RC( elemsA->desc) == 1)); - /* - * Now, we decrement the "rest" of elemsA. - * Although this may lead to a 0 rc in case of NIL, - * we do NOT free that node, since we have to make - * sure that it survives the copying while-loop. - * After that loop we check, whether the rc is 0. - * If that is the case, we know that there were - * no copies to be done and we can free the NIL! - */ - (DESC_RC( elemsA->desc))--; -#if TRACE - fprintf( stderr, "changing CONS at (%p)\n", new); -#endif - } - - else { /* copy first elem & decrement rc of 'elemsA'! */ - new = (list *) SAC_MALLOC( sizeof( list)); - new->elem = elemsA->elem; - SAC_ND_ALLOC__DESC( new_nt, 0) - SAC_ND_SET__RC( new_nt, 1) - new->desc = SAC_ND_A_DESC( new_nt); -#if TRACE - fprintf( stderr, "creating CONS at (%p)\n", new); -#endif - SAC_ND_RET_out( res_nt, new_nt) - (DESC_RC( elemsA->desc))--; - - elemsA = elemsA->rest; /* 'elemsA' has to be one in advance of 'new'! */ - } - - /* - * 'new' points to the last elem reused/copied - * 'elemsA' is one in front - */ - while (elemsA->rest != NULL) { - new->rest = (list *) SAC_MALLOC( sizeof( list)); -#if TRACE - fprintf( stderr, " [ %d . (%p)]\n", new->elem, new->rest); - fprintf( stderr, "creating CONS at (%p)\n", new->rest); -#endif - new = new->rest; - new->elem = elemsA->elem; - SAC_ND_ALLOC__DESC( new_nt, 0) - SAC_ND_SET__RC( new_nt, 1) - new->desc = SAC_ND_A_DESC( new_nt); - elemsA = elemsA->rest; - } - new->rest = elemsB; -#if TRACE - fprintf( stderr, " [ %d . (%p)]\n", new->elem, new->rest); -#endif - /* Finally, we have to do some housekeeping! (see comment above!) */ - if (DESC_RC( elemsA->desc) == 0) { - SAC_List_free_list( elemsA); - } - } -} - -#undef res_nt -#undef elemsA_nt -#undef elemsB_nt -#undef new_nt diff --git a/src/structures/src/List/cons.c b/src/structures/src/List/cons.c deleted file mode 100644 index 33ae4fc5..00000000 --- a/src/structures/src/List/cons.c +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -#define res_nt (res, T_OLD((SCL, (HID, (NUQ,))))) -#define elems_nt (elems, T_OLD((SCL, (HID, (NUQ,))))) - -void SAC_List_cons( SAC_ND_PARAM_out( res_nt, list *), - int elem, - SAC_ND_PARAM_in( elems_nt, list *)) -{ - SAC_ND_DECL__DESC( res_nt, ) - SAC_ND_DECL__DATA( res_nt, list *, ) - - res = (list *) SAC_MALLOC( sizeof( list)); - res->elem = elem; - res->rest = elems; - SAC_ND_ALLOC__DESC( res_nt, 0) - SAC_ND_SET__RC( res_nt, 1) - res->desc = SAC_ND_A_DESC( res_nt); - -#if TRACE - fprintf( stderr, "creating CONS at (%p)\n", res); - fprintf( stderr, " [ %d . (%p)]\n", elem, elems); -#endif - - SAC_ND_RET_out( res_nt, res_nt) -} - -#undef res_nt -#undef elemsA_nt -#undef elemsB_nt -#undef new_nt diff --git a/src/structures/src/List/drop.c b/src/structures/src/List/drop.c deleted file mode 100644 index 55909d65..00000000 --- a/src/structures/src/List/drop.c +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -#define res_nt (res, T_OLD((SCL, (HID, (NUQ,))))) -#define elems_nt (elems, T_OLD((SCL, (HID, (NUQ,))))) - -void SAC_List_drop( SAC_ND_PARAM_out( res_nt, list *), - int n, - SAC_ND_PARAM_in( elems_nt, list *)) -{ - SAC_ND_DECL__DESC( res_nt, ) - SAC_ND_DECL__DATA( res_nt, list *, ) - - if (n < 0) { - SAC_RuntimeError( "negative first arg of drop\n"); - } - - SAC_ND_A_DESC( res_nt) = SAC_ND_A_DESC( elems_nt); - SAC_ND_A_FIELD( res_nt) = SAC_ND_A_FIELD( elems_nt); - - while (n > 0) { - if (res->rest == NULL) { - SAC_RuntimeError( "first arg of drop %d larger than length of list\n", n); - } - res=res->rest; - n--; - } - (DESC_RC( res->desc))++; - - if (--(DESC_RC( elems->desc)) == 0) { - SAC_List_free_list( elems); - } - - SAC_ND_A_DESC( res_nt) = res->desc; - SAC_ND_RET_out( res_nt, res_nt) -} - -#undef res_nt -#undef elems_nt diff --git a/src/structures/src/List/empty.c b/src/structures/src/List/empty.c deleted file mode 100644 index f109b50a..00000000 --- a/src/structures/src/List/empty.c +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -#define elems_nt (elems, T_OLD((SCL, (HID, (NUQ,))))) - -int SAC_List_empty( SAC_ND_PARAM_in( elems_nt, list *)) -{ - int res; - - res = (elems->rest == NULL); - - if (--(DESC_RC( elems->desc)) == 0) { - SAC_List_free_list( elems); - } - - return( res); -} - -#undef elems_nt diff --git a/src/structures/src/List/free.c b/src/structures/src/List/free.c deleted file mode 100644 index ea300f9f..00000000 --- a/src/structures/src/List/free.c +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -void SAC_List_free_list( list *elems) -{ - list *next; - - do { - next = elems->rest; - -#if TRACE - fprintf( stderr, "freeing (%p)\n", elems); -#endif - - SAC_FREE( elems); - elems = next; - } - while ((elems != NULL) && (--(DESC_RC( elems->desc)) == 0)); -} diff --git a/src/structures/src/List/hd.c b/src/structures/src/List/hd.c deleted file mode 100644 index 4d557a27..00000000 --- a/src/structures/src/List/hd.c +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -#define elems_nt (elems, T_OLD((SCL, (HID, (NUQ,))))) - -int SAC_List_hd( SAC_ND_PARAM_in( elems_nt, list *)) -{ - int res; - - if (elems->rest == NULL) { - SAC_RuntimeError( "hd applied to NIL\n"); - } - res = elems->elem; - - if (--(DESC_RC( elems->desc)) == 0) { - SAC_List_free_list( elems); - } - - return( res); -} - -#undef elems_nt diff --git a/src/structures/src/List/length.c b/src/structures/src/List/length.c deleted file mode 100644 index d2b5f18e..00000000 --- a/src/structures/src/List/length.c +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -#define elems_nt (elems, T_OLD((SCL, (HID, (NUQ,))))) - -int SAC_List_length( SAC_ND_PARAM_in( elems_nt, list *)) -{ - list *ptr = elems; - int res = 0; - - while (ptr->rest != NULL) { - ptr = ptr->rest; - res++; - } - if (--(DESC_RC( elems->desc)) == 0) { - SAC_List_free_list( elems); - } - - return( res); -} - -#undef elems_nt diff --git a/src/structures/src/List/nil.c b/src/structures/src/List/nil.c deleted file mode 100644 index 9ed0b168..00000000 --- a/src/structures/src/List/nil.c +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -#define res_nt (res, T_OLD((SCL, (HID, (NUQ,))))) - -void SAC_List_nil( SAC_ND_PARAM_out( res_nt, list *)) -{ - SAC_ND_DECL__DESC( res_nt, ) - SAC_ND_DECL__DATA( res_nt, list *, ) - - res = (list *) SAC_MALLOC( sizeof( list)); - res->rest = NULL; - SAC_ND_ALLOC__DESC( res_nt, 0) - SAC_ND_SET__RC( res_nt, 1) - res->desc = SAC_ND_A_DESC( res_nt); - -#if TRACE - fprintf( stderr, "creating NIL at (%p)\n", res); -#endif - - SAC_ND_RET_out( res_nt, res_nt) -} - -#undef res_nt diff --git a/src/structures/src/List/nth.c b/src/structures/src/List/nth.c deleted file mode 100644 index b4fac9bf..00000000 --- a/src/structures/src/List/nth.c +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -#define elems_nt (elems, T_OLD((SCL, (HID, (NUQ,))))) - -int SAC_List_nth( int n, SAC_ND_PARAM_in( elems_nt, list *)) -{ - list *ptr; - int res; - - if (n < 0) { - SAC_RuntimeError( "negative first arg of nth\n"); - } - - ptr = elems; - while (n > 0) { - ptr = ptr->rest; - if (ptr->rest == NULL) { - SAC_RuntimeError( "first arg of nth %d larger than length of list\n", n); - } - n--; - } - res = ptr->elem; - - if (--(DESC_RC( elems->desc)) == 0) { - SAC_List_free_list( elems); - } - - return( res); -} - -#undef elems_nt diff --git a/src/structures/src/List/take.c b/src/structures/src/List/take.c deleted file mode 100644 index 5b6d0ac0..00000000 --- a/src/structures/src/List/take.c +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -#define res_nt (res, T_OLD((SCL, (HID, (NUQ,))))) -#define elems_nt (elems, T_OLD((SCL, (HID, (NUQ,))))) -#define new_nt (new, T_OLD((SCL, (HID, (NUQ,))))) -#define last_nt (last, T_OLD((SCL, (HID, (NUQ,))))) - -void SAC_List_take( SAC_ND_PARAM_out( res_nt, list *), - int n, - SAC_ND_PARAM_in( elems_nt, list *)) -{ - SAC_ND_DECL__DESC( new_nt, ) - SAC_ND_DECL__DATA( new_nt, list *, ) - SAC_ND_DECL__DESC( last_nt, ) - SAC_ND_DECL__DATA( last_nt, list *, ) - - if (n < 0) { - SAC_RuntimeError( "negative first arg of take\n"); - } - - last = (list *) SAC_MALLOC( sizeof( list)); - last->rest = NULL; - SAC_ND_ALLOC__DESC( last_nt, 0) - SAC_ND_SET__RC( last_nt, 1) - last->desc = SAC_ND_A_DESC( last_nt); -#if TRACE - fprintf( stderr, "creating NIL at (%p)\n", last); -#endif - - if (n == 0) { - SAC_ND_RET_out( res_nt, last_nt) - - if (--(DESC_RC( elems->desc)) == 0) { - SAC_List_free_list( elems); - } - } - else { - /* - * Now, we do know that we have to take at least one element! - */ - - if (DESC_RC( elems->desc) == 1) { - /* - * re-use all elems while ((rc == 1) && (n > 0)) ! - */ - SAC_ND_RET_out( res_nt, elems_nt) - - do { - if (elems->rest == NULL) - SAC_RuntimeError( "first arg of take %d larger than length of list\n", - n); - new = elems; - elems = elems->rest; - } - while ((--n > 0) && (DESC_RC( elems->desc) == 1)); - /* - * Now, we decrement the "rest" of elems. - */ - if (--(DESC_RC( elems->desc)) == 0) { - SAC_List_free_list( elems); - } -#if TRACE - fprintf( stderr, "changing CONS at (%p)\n", new); -#endif - } - - else { - if (elems->rest == NULL) { - SAC_RuntimeError( "first arg of take %d larger than length of list\n", - n); - } - /* - * Now, we do know that we have at least one element to copy. - * After doing so, we have to decrement the rc of elems! - */ - new = (list *) SAC_MALLOC( sizeof( list)); - new->elem = elems->elem; - SAC_ND_ALLOC__DESC( new_nt, 0) - SAC_ND_SET__RC( new_nt, 1) - new->desc = SAC_ND_A_DESC( new_nt); -#if TRACE - fprintf( stderr, "creating CONS at (%p)\n", new); -#endif - SAC_ND_RET_out( res_nt, new_nt); - - (DESC_RC( elems->desc))--; - elems = elems->rest; /* elems has to be one in advance of new! */ - n--; - } - - /* - * 'new' points to the last elem reused/copied - * 'elems' is one in front! - */ - while (n > 0) { - if (elems->rest == NULL) - SAC_RuntimeError( "first arg of take %d larger than length of list\n", - n); - new->rest = (list *) SAC_MALLOC( sizeof( list)); -#if TRACE - fprintf( stderr, " [ %d . (%p)]\n", new->elem, new->rest); - fprintf( stderr, "creating CONS at (%p)\n", new->rest); -#endif - new = new->rest; - new->elem = elems->elem; - SAC_ND_ALLOC__DESC( new_nt, 0) - SAC_ND_SET__RC( new_nt, 1) - new->desc = SAC_ND_A_DESC( new_nt); - - elems = elems->rest; - n--; - } - new->rest = last; - -#if TRACE - fprintf( stderr, " [ %d . (%p)]\n", new->elem, new->rest); -#endif - } -} - -#undef res_nt -#undef elems_nt -#undef new_nt -#undef last_nt diff --git a/src/structures/src/List/tl.c b/src/structures/src/List/tl.c deleted file mode 100644 index e3fe5677..00000000 --- a/src/structures/src/List/tl.c +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Implementation of SAC standard module List - */ - - -#include "List.h" - - -#define res_nt (res, T_OLD((SCL, (HID, (NUQ,))))) -#define elems_nt (elems, T_OLD((SCL, (HID, (NUQ,))))) - -void SAC_List_tl( SAC_ND_PARAM_out( res_nt, list *), - SAC_ND_PARAM_in( elems_nt, list *)) -{ - SAC_ND_DECL__DESC( res_nt, ) - SAC_ND_DECL__DATA( res_nt, list *, ) - - if (elems->rest == NULL) { - SAC_RuntimeError( "tl applied to NIL\n"); - } - res = elems->rest; - - (DESC_RC( res->desc))++; - - if (--(DESC_RC( elems->desc)) == 0) { - SAC_List_free_list( elems); - } - - SAC_ND_A_DESC( res_nt) = res->desc; - SAC_ND_RET_out( res_nt, res_nt) -} - -#undef res_nt -#undef elems_nt diff --git a/src/structures/src/String/Str.c b/src/structures/src/String/Str.c new file mode 100644 index 00000000..1ec9dd79 --- /dev/null +++ b/src/structures/src/String/Str.c @@ -0,0 +1,523 @@ +#include +#include +#include +#include +#include +#include + +#include "Str.h" + +string copy_string(string s) +{ + string s2 = malloc((strlen(s) + 1) * sizeof(char)); + strcpy(s2, s); + return s2; +} + +string free_string(string s) +{ + free(s); + return NULL; +} + +string SACtostring(unsigned char *arr, sac_int length) +{ + string res = malloc((size_t) length + 1); + + memcpy(res, arr,(size_t)length); + res[length] = '\0'; + + return res; +} + +string SACautotostring(SACarg *sarr) +{ + const string *parr = SACARGgetSharedData(SACTYPE__MAIN__char, sarr); + string arr = parr[0]; + sac_int length = SACARGgetShape(sarr, 0); + + string res = malloc((size_t) length + 1); + + strncpy(res, arr,(size_t)length); + res[length] = '\0'; + + SACARGdeleteSacArray(&sarr); + return res; +} + +void SACstrmod(string str, sac_int pos, unsigned char c) +{ + size_t strlength = strlen(str); + + if (strlength <=(size_t)pos) + SAC_RuntimeError("strmod: pos("PRIisac") outside of string(length %zu)", + pos, strlength); + + str[pos] = c; +} + +string SACstrins(string outer, sac_int pos, string inner) +{ + size_t outer_length = strlen(outer); + size_t inner_length = strlen(inner); + + string res = malloc(outer_length + inner_length + 1); + + strncpy(res, outer,(size_t)pos); + strcpy(res+(size_t)pos, inner); + strcpy(res+(size_t)pos+inner_length, outer+(size_t)pos); + + return res; +} + +void SACstrovwt(string outer, sac_int pos, string inner) +{ + size_t outer_length = strlen(outer); + size_t inner_length = strlen(inner); + + if ((size_t)pos+inner_length > outer_length) { + SAC_RuntimeError("strovwt: Overwriting string ends at position "PRIisac + " while string is of length %zu", + pos + inner_length, outer_length); + } + + memcpy(outer+(size_t)pos, inner, inner_length * sizeof(char)); +} + +unsigned char SACstrsel(string str, sac_int pos) +{ + size_t strlength = strlen(str); + if ((size_t)pos >= strlength) + SAC_RuntimeError("strsel: pos("PRIisac") outside of string(length %zu)", + pos, strlength); + + return str[pos]; +} + +string SACstrcat(string fst, string snd) +{ + size_t fstlength = strlen(fst); + size_t sndlength = strlen(snd); + + string res = malloc(fstlength + sndlength + 1); + + strcpy(res, fst); + strcpy(res + fstlength, snd); + + return res; +} + +string SACstrncat(string fst, string snd, sac_int n) +{ + size_t fstlength = strlen(fst); + + string res = malloc(fstlength +(size_t)n + 1); + + strcpy(res, fst); + strncpy(res+fstlength, snd, (size_t)n); + res[fstlength+(size_t)n] = '\n'; + + return res; +} + +sac_int SACstrcmp(string fst, string snd) +{ + return (sac_int)strcmp(fst, snd); +} + +sac_int SACstrncmp(string fst, string snd, sac_int n) +{ + return (sac_int)strncmp(fst, snd,(size_t) n); +} + +sac_int SACstrcasecmp(string fst, string snd) +{ + return (sac_int)strcasecmp(fst, snd); +} + +sac_int SACstrncasecmp(string fst, string snd, sac_int n) +{ + return (sac_int)strncasecmp(fst, snd, (size_t)n); +} + +sac_int SACstrlen(string fst) +{ + return (sac_int)strlen(fst); +} + +void SACstrtake(string str, sac_int pos) +{ + size_t strlength = strlen(str); + + if ((size_t)pos >= strlength) { + SAC_RuntimeError("strtake: pos("PRIisac") outside of string(length %zu)", + pos, strlength); + } + + str[pos] = '\0'; +} + +string SACstrdrop(string str, sac_int pos) +{ + size_t strlength = strlen(str); + + string res = malloc(strlength -(size_t)pos + 1); + + strcpy(res, str + (size_t)pos); + + return res; +} + +string SACstrext(string str, sac_int pos, sac_int len) +{ + size_t strlength = strlen(str); + + string res = malloc(strlength -(size_t)len + 1); + + if ((size_t)(pos + len) >= strlength) { + SAC_RuntimeError("strext: Selecting substring ends at position "PRIisac + " while string is of length %zu", + pos + len, strlength); + } + + strncpy(res, str + (size_t)pos, (size_t)len); + + return res; +} + +bool chr_in_delims(char c, string delimiters) +{ + while (true) { + if (delimiters[0] == '\0') { + return false; + } + + if (delimiters[0] == c) { + return true; + } + + delimiters++; + } +} + +string next_in_delims(string str, string delimiters, bool is_delimiter) +{ + while (true) { + if (str[0] == '\0') { + return NULL; + } + + if (chr_in_delims(str[0], delimiters) == is_delimiter) { + return str; + } + + str++; + } +} + +static char fmtstart = '%'; +static char fmtspecifiers[] = "di"; +static char fmtmodifiers[] = "+- #.*0123456789"; + +string find_fmtstr_spec_loc(string format) +{ + do { + // Find % + format = strchr(format, fmtstart); + // Skip modifiers + format = next_in_delims(format, fmtmodifiers, false); + } + // Check if at end, or delimiter has been found + while (format != NULL && !chr_in_delims(*format, fmtspecifiers)); + + return format; +} + +string fix_fmtstr_sac_int(string format) +{ + // Count the number of replaces that need to occur, to comput the format string new size + size_t replaces = 0; + string fmt = find_fmtstr_spec_loc(format); + while (fmt != NULL) { + replaces++; + fmt = find_fmtstr_spec_loc(fmt); + } + + // Compute the size of the new string, and allocat enough memory + // For each replace, we remove 1 character and add the length of PRIisac + size_t priisaclength = strlen(PRIisac); + size_t formatlength = strlen(format); + string res = malloc((formatlength + replaces *(formatlength-1) + 1) * sizeof(char)); + + // Copy parts of strings into the result + string start = format; + string respos = res; + while (format != NULL) { + // Find the next occurence of a format string specifier to be found + format = find_fmtstr_spec_loc(start); + + if (format == NULL) { + // If at end, simply copy over the rest of the string + strcpy(respos, start); + } else { + // If found, copy the part of the string until the specifier, including the % and modifiers + strncpy(respos, start, format-start); + respos += format-start; + // Set new start to just after the i or d character + start = format+1; + // Add the format string specifier for our SaC ints + strcpy(respos, PRIisac); + respos += priisaclength; + } + } + + return res; +} + +string SACsprintf(string format_raw, ...) +{ + string format = fix_fmtstr_sac_int(format_raw); + + va_list args; + + // Compute the length of the string + va_start(args, format_raw); + int lengthwo0 = vsnprintf(NULL, 0, format, args); + va_end(args); + + if (lengthwo0 < 0) + SAC_RuntimeError("printf error %d", lengthwo0); + + size_t length = lengthwo0 + 1; + + // Allocate result string + string res = malloc(length); + + // Printf string + va_start(args, format_raw); + vsnprintf(res, length, format, args); + va_end(args); + + return res; +} + +sac_int SACsscanf(string s, string format_raw, ...) +{ + string format = fix_fmtstr_sac_int(format_raw); + + va_list arg_p; + + va_start(arg_p, format_raw); + sac_int res =(sac_int)vsscanf(s, format, arg_p); + va_end(arg_p); + + return res; +} + +string SACsscanf_str(string source, string format_raw) +{ + string format = fix_fmtstr_sac_int(format_raw); + + string res = malloc(strlen(source) + 1); + res[0] = '\0'; + + sscanf(source, format, res); + + return res; +} + +sac_int SACstrchr(string str, unsigned char c) +{ + string occurrence = strchr(str, c); + + if (occurrence == NULL) { + return -1; + } + + return(sac_int)(occurrence - str); +} + +sac_int SACstrrchr(string str, unsigned char c) +{ + string occurrence = strrchr(str, c); + + if (occurrence == NULL) { + return -1; + } + + return(sac_int)(occurrence - str); +} + +sac_int SACstrcspn(string str, string reject) +{ + return (sac_int)strcspn(str, reject); +} + +sac_int SACstrspn(string str, string accept) +{ + return (sac_int)strspn(str, accept); +} + +sac_int SACstrstr(string haystack, string needle) +{ + return (sac_int)strstr(haystack, needle); +} + +void SACstrtok(string* out_token, string* out_rest, string str, string delimiters) +{ + string start = next_in_delims(str, delimiters, false); + string end = next_in_delims(start, delimiters, true); + + size_t tokenlength = end - start; + *out_token = malloc((tokenlength + 1) * sizeof(char)); + strncpy(*out_token, start, tokenlength); + *out_token[tokenlength] = '\0'; + + *out_rest = malloc((strlen(end) + 1) * sizeof(char)); + strcpy(*out_rest, end); +} + +void SACchomp(string str) +{ + string end = str + strlen(str); + + while (end > str) { + end--; + + if (*end == '\n' || *end == '\r') { + *end = '\0'; + } else { + break; + } + } +} + +void SACrtrim(string str) +{ + string end = str + strlen(str); + + while (end > str) { + end --; + + if (isspace(*end)) { + *end = '\0'; + } else { + break; + } + } +} + +string SACltrim(string str) +{ + string end = str + strlen(str); + + while (str < end) { + if (!isspace(*end)) { + *end = '\0'; + } else { + break; + } + + str++; + } + + string res = malloc((strlen(str) + 1) * sizeof(char)); + strcpy(res, str); + + return res; +} + +string SACtrim(string str) +{ + string res = SACltrim(str); + SACrtrim(res); + return res; +} + +sac_int SACstrtoi(string* remain, string input, sac_int base) +{ + string rem; + sac_int res =(sac_int) strtol(input, &rem,(size_t)base); + + *remain = malloc((strlen(rem) + 1) * sizeof(char)); + strcpy(*remain, rem); + + return res; +} + +float SACstrtof(string* remain, string input) +{ + string rem; + float res =(float) strtod(input, &rem); + + *remain = malloc((strlen(rem) + 1) * sizeof(char)); + strcpy(*remain, rem); + + return res; +} + +double SACstrtod(string* remain, string input) +{ + string rem; + double res = strtod(input, &rem); + + *remain = malloc((strlen(rem) + 1) * sizeof(char)); + strcpy(*remain, rem); + + return res; +} + +sac_int SACtoi(string input) +{ + return (sac_int)strtol(input, NULL, 0); +} + +float SACtof(string input) +{ + return(float) strtod(input, NULL); +} + +double SACtod(string input) +{ + return strtod(input, NULL); +} + +string SACitos(sac_int n) +{ + string res = malloc(40); + + sprintf(res, "%"PRIisac, n); + + return res; +} + +string SACftos(float n) +{ + string res = malloc(40); + + sprintf(res, "%g", n); + + return res; +} + +string SACdtos(double n) +{ + string res = malloc(40); + + sprintf(res, "%g", n); + + return res; +} + +string SACbtos(bool n) +{ + string res = malloc(6 * sizeof(char)); + + if (n) { + strcpy(res, "true"); + } else { + strcpy(res, "false"); + } + + return res; +} diff --git a/src/structures/src/String/Str.h b/src/structures/src/String/Str.h new file mode 100644 index 00000000..648a546a --- /dev/null +++ b/src/structures/src/String/Str.h @@ -0,0 +1,57 @@ +#ifndef _SAC_STR_H_ +#define _SAC_STR_H_ + +/* + * C implementation of standard module String. + */ + +#include + +#include "sacinterface.h" + +typedef char *string; + +string copy_string(string s); +string free_string(string s); + +string SACtostring(unsigned char *arr, sac_int length); +string SACautotostring(SACarg *sarr); +void SACstrmod(string str, sac_int pos, unsigned char c); +string SACstrins(string outer, sac_int pos, string inner); +void SACstrovwt(string outer, sac_int pos, string inner); +unsigned char SACstrsel(string str, sac_int pos); +string SACstrcat(string fst, string snd); +string SACstrncat(string fst, string snd, sac_int n); +sac_int SACstrcmp(string fst, string snd); +sac_int SACstrncmp(string fst, string snd, sac_int n); +sac_int SACstrcasecmp(string fst, string snd); +sac_int SACstrncasecmp(string fst, string snd, sac_int n); +sac_int SACstrlen(string fst); +void SACstrtake(string str, sac_int pos); +string SACstrdrop(string str, sac_int pos); +string SACstrext(string str, sac_int pos, sac_int len); +string SACsprintf(string format, ...); +sac_int SACsscanf(string s, string format, ...); +string SACsscanf_str(string s, string format); +sac_int SACstrchr(string str, unsigned char c); +sac_int SACstrrchr(string str, unsigned char c); +sac_int SACstrcspn(string str, string reject); +sac_int SACstrspn(string str, string accept); +sac_int SACstrstr(string haystack, string needle); +void SACstrtok(string *out_token, string *out_rest, string str, string delimiters); +void SACchomp(string str); +void SACrtrim(string str); +string SACltrim(string str); +string SACtrim(string str); +sac_int SACstrtoi(string *remain, string input, sac_int base); +float SACstrtof(string *remain, string input); +double SACstrtod(string *remain, string input); +sac_int SACtoi(string input); +float SACtof(string input); +double SACtod(string input); +string SACitos(sac_int n); +string SACftos(float n); +string SACdtos(double n); +string SACbtos(bool n); + +#endif /* _SAC_STR_H_ */ diff --git a/src/structures/src/String/StringC.h b/src/structures/src/String/StringC.h deleted file mode 100644 index 0cb23b93..00000000 --- a/src/structures/src/String/StringC.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Implementation of standard module StringC - */ - - -#include -#include -#include -#include - -#include "sac.h" - - -#ifdef CHECK - -#define RANGECHECK(check, lower, upper, str) \ - if ((checkupper)) \ - SAC_RuntimeError("Range violation upon string access:\n" \ - "tried to access character %d of string\n" \ - "\"%s\"", check, str); - - -#else /* CHECK */ - -#define RANGECHECK(check, lower, upper, str) - -#endif /* CHECK */ - - -#define STRDUP(new, old) new=(string)SAC_MALLOC(strlen(old)+1); \ - strcpy(new, old); - -#define STRFREE(str) SAC_FREE(str); - -typedef char* string; diff --git a/src/structures/src/String/btos.c b/src/structures/src/String/btos.c deleted file mode 100644 index 6902c6a5..00000000 --- a/src/structures/src/String/btos.c +++ /dev/null @@ -1,27 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string SACbtos( bool n) -{ - char *res; - - res = (char *) SAC_MALLOC( 6); - - if (n) { - strcpy( res, "true"); - } - else { - strcpy( res, "false"); - } - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/copystr.c b/src/structures/src/String/copystr.c deleted file mode 100644 index 35171ce2..00000000 --- a/src/structures/src/String/copystr.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string copy_string( string s) -{ - string new; - - STRDUP( new, s); - - return( new); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/ctos.c b/src/structures/src/String/ctos.c deleted file mode 100644 index bf8a2199..00000000 --- a/src/structures/src/String/ctos.c +++ /dev/null @@ -1,22 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string SACctos( double *n) -{ - char *res; - - res = (char *) SAC_MALLOC( 120); - - sprintf( res, "(%g,%g)", n[0], n[1]); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/dtos.c b/src/structures/src/String/dtos.c deleted file mode 100644 index 14de6ab4..00000000 --- a/src/structures/src/String/dtos.c +++ /dev/null @@ -1,22 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string SACdtos( double n) -{ - char *res; - - res = (char *) SAC_MALLOC( 60); - - sprintf( res, "%g", n); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/freestr.c b/src/structures/src/String/freestr.c deleted file mode 100644 index 40bb4931..00000000 --- a/src/structures/src/String/freestr.c +++ /dev/null @@ -1,17 +0,0 @@ - -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -void free_string( string s) -{ - STRFREE( s); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/ftos.c b/src/structures/src/String/ftos.c deleted file mode 100644 index 115ff796..00000000 --- a/src/structures/src/String/ftos.c +++ /dev/null @@ -1,22 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string SACftos( float n) -{ - char *res; - - res = (char *) SAC_MALLOC( 60); - - sprintf( res, "%g", n); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/itos.c b/src/structures/src/String/itos.c deleted file mode 100644 index 35b9c410..00000000 --- a/src/structures/src/String/itos.c +++ /dev/null @@ -1,22 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string SACitos( int n) -{ - char *res; - - res = (char *) SAC_MALLOC( 40); - - sprintf( res, "%d", n); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/sprintf.c b/src/structures/src/String/sprintf.c deleted file mode 100644 index 8aeb46e2..00000000 --- a/src/structures/src/String/sprintf.c +++ /dev/null @@ -1,48 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" -#include - -/* Allow for long path names and URLs with long parameters. */ -#if defined(PATH_MAX) && PATH_MAX >= 2048 -#define BUFFER_SIZE PATH_MAX -#else -#define BUFFER_SIZE 2048 -#endif - - -/*****************************************************************/ - -string SACsprintf( string format, ...) -{ - va_list arg_p; - char buffer[BUFFER_SIZE]; - int n; - string new; - - buffer[0] = '\0'; - va_start( arg_p, format); - n = vsnprintf( buffer, sizeof buffer, format, arg_p); - va_end( arg_p); - if ((size_t)n >= sizeof buffer) { - new = (string) SAC_MALLOC( n + 1); - va_start( arg_p, format); - n = vsnprintf( new, n + 1, format, arg_p); - va_end( arg_p); - } - else if (n >= 0) { - new = (string) SAC_MALLOC( strlen( buffer) + 1); - strcpy( new, buffer); - } - else { - new = (string) SAC_MALLOC( 1); - new[0] = '\0'; - } - - return( new); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/sscanf.c b/src/structures/src/String/sscanf.c deleted file mode 100644 index b5703003..00000000 --- a/src/structures/src/String/sscanf.c +++ /dev/null @@ -1,23 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACsscanf( string s, string format, ...) -{ - int res; - va_list arg_p; - - va_start( arg_p, format); - res = vsscanf( s, format, arg_p); - va_end( arg_p); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/sscanfstr.c b/src/structures/src/String/sscanfstr.c deleted file mode 100644 index 2dd446fc..00000000 --- a/src/structures/src/String/sscanfstr.c +++ /dev/null @@ -1,24 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string sscanf_str( string s, string format) -{ - string new; - - new = (string) SAC_MALLOC( strlen( s) + 1); - - new[0] = 0; - - sscanf( s, format, new); - - return( new); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strcasecmp.c b/src/structures/src/String/strcasecmp.c deleted file mode 100644 index bf7a01aa..00000000 --- a/src/structures/src/String/strcasecmp.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrcasecmp( string first, string second) -{ - int res; - - res = strcasecmp( first, second); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strcat.c b/src/structures/src/String/strcat.c deleted file mode 100644 index 05d89f4d..00000000 --- a/src/structures/src/String/strcat.c +++ /dev/null @@ -1,27 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string SACstrcat( string first, string second) -{ - size_t len1; - size_t len2; - string new; - - len1 = strlen( first); - len2 = strlen( second); - new = (string) SAC_MALLOC( len1 + len2 + 1); - - strcpy( new, first); - strcpy( new + len1, second); - - return( new); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strchr.c b/src/structures/src/String/strchr.c deleted file mode 100644 index 32de4f93..00000000 --- a/src/structures/src/String/strchr.c +++ /dev/null @@ -1,25 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrchr( string str, char c) -{ - char *occur; - - occur = strchr( str, c); - - if (occur == NULL) { - return( -1); - } - else { - return( occur - str); - } -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strcmp.c b/src/structures/src/String/strcmp.c deleted file mode 100644 index 9598e989..00000000 --- a/src/structures/src/String/strcmp.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrcmp( string first, string second) -{ - int res; - - res = strcmp( first, second); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strcspn.c b/src/structures/src/String/strcspn.c deleted file mode 100644 index bcc7ab07..00000000 --- a/src/structures/src/String/strcspn.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrcspn( string first, string second) -{ - int res; - - res = strcspn( first, second); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strdrop.c b/src/structures/src/String/strdrop.c deleted file mode 100644 index aed250ed..00000000 --- a/src/structures/src/String/strdrop.c +++ /dev/null @@ -1,23 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string strdrop( string old, int n) -{ - string new; - - RANGECHECK( (size_t) n, 0, strlen( old), old); - - new = (string) SAC_MALLOC( strlen( old) - n + 1); - strcpy( new, old + n); - - return( new); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strext.c b/src/structures/src/String/strext.c deleted file mode 100644 index 92681ad1..00000000 --- a/src/structures/src/String/strext.c +++ /dev/null @@ -1,36 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string strext( string old, int first, int len) -{ - string new; - - RANGECHECK( (size_t) first, 0, strlen( old) - 1, old); - - if (len <= 0) { - new = (string) SAC_MALLOC( 1); - new[ 0] = 0; - } - else { - new = (string) SAC_MALLOC( len + 1); - strncpy( new, old + first, len); - - if ((size_t) (first + len) <= strlen( old)) { - new[ len] = 0; - } - else { - new[ strlen( old) - first] = 0; - } - } - - return(new); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strins.c b/src/structures/src/String/strins.c deleted file mode 100644 index 7070afca..00000000 --- a/src/structures/src/String/strins.c +++ /dev/null @@ -1,28 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string strins( string old, int pos, string insert) -{ - string new; - - RANGECHECK( (size_t) pos, 0, strlen( old), old); - - new = (string) SAC_MALLOC( strlen( old) + strlen( insert) + 1); - - strncpy( new, old, pos); - new[pos] = 0; - - strcat( new, insert); - strcat( new, old + pos); - - return( new); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strlen.c b/src/structures/src/String/strlen.c deleted file mode 100644 index bc24f338..00000000 --- a/src/structures/src/String/strlen.c +++ /dev/null @@ -1,16 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrlen(string s) -{ - return( strlen( s)); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strmod.c b/src/structures/src/String/strmod.c deleted file mode 100644 index 8b9f6c8d..00000000 --- a/src/structures/src/String/strmod.c +++ /dev/null @@ -1,43 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -#define new_nt (new, T_OLD((SCL, (HID, (NUQ,))))) -#define old_nt (old, T_OLD((SCL, (HID, (NUQ,))))) - -void strmod( SAC_ND_PARAM_out( new_nt, string), - SAC_ND_PARAM_in( old_nt, string), - int pos, char c) -{ - SAC_ND_DECL__DATA( new_nt, string, ) - SAC_ND_DECL__DESC( new_nt, ) - - RANGECHECK( (size_t)pos, 0, strlen( SAC_ND_A_FIELD( old_nt)) - 1, - SAC_ND_A_FIELD( old_nt)); - - if (SAC_ND_A_RC( old_nt) == 1) { - SAC_ND_A_DESC( new_nt) = SAC_ND_A_DESC( old_nt); - SAC_ND_A_FIELD( new_nt) = SAC_ND_A_FIELD( old_nt); - } - else { - SAC_ND_ALLOC__DESC( new_nt, 0) - SAC_ND_SET__RC( new_nt, 1) - STRDUP( SAC_ND_A_FIELD( new_nt), SAC_ND_A_FIELD( old_nt)); - - SAC_ND_DEC_RC_FREE( old_nt, 1, SAC_FREE) - } - SAC_ND_A_FIELD( new_nt)[pos] = c; - - SAC_ND_RET_out( new_nt, new_nt) -} - -#undef new_nt -#undef old_nt - -/*****************************************************************/ diff --git a/src/structures/src/String/strncasecmp.c b/src/structures/src/String/strncasecmp.c deleted file mode 100644 index 38db2151..00000000 --- a/src/structures/src/String/strncasecmp.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrncasecmp( string first, string second, int n) -{ - int res; - - res = strncasecmp( first, second, n); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strncat.c b/src/structures/src/String/strncat.c deleted file mode 100644 index a5b6c31e..00000000 --- a/src/structures/src/String/strncat.c +++ /dev/null @@ -1,23 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string SACstrncat( string first, string second, int n) -{ - string new; - - new = (string) SAC_MALLOC( strlen( first) + n + 1); - - strcpy( new, first); - strncat( new, second, n); - - return( new); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strncmp.c b/src/structures/src/String/strncmp.c deleted file mode 100644 index 58783d2b..00000000 --- a/src/structures/src/String/strncmp.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrncmp( string first, string second, int n) -{ - int res; - - res = strncmp( first, second, n); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strovwt.c b/src/structures/src/String/strovwt.c deleted file mode 100644 index b86bae2b..00000000 --- a/src/structures/src/String/strovwt.c +++ /dev/null @@ -1,64 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -#define new_nt (new, T_OLD((SCL, (HID, (NUQ,))))) -#define old_nt (old, T_OLD((SCL, (HID, (NUQ,))))) - -void strovwt( SAC_ND_PARAM_out( new_nt, string), - SAC_ND_PARAM_in( old_nt, string), - int pos, string insert) -{ - SAC_ND_DECL__DESC( new_nt, ) - SAC_ND_DECL__DATA( new_nt, string, ) - int len_old = strlen( SAC_ND_A_FIELD( old_nt)); - int len_insert = strlen( insert); - int len_insert_pos = len_insert+pos; - char store; - - RANGECHECK( (size_t)pos, 0, strlen( SAC_ND_A_FIELD( old_nt)), - SAC_ND_A_FIELD( old_nt)); - - if (len_insert_pos <= len_old) { - store = SAC_ND_A_FIELD( old_nt)[len_insert_pos]; - - if (SAC_ND_A_RC( old_nt) == 1) { - SAC_ND_A_DESC( new_nt) = SAC_ND_A_DESC( old_nt); - SAC_ND_A_FIELD( new_nt) = SAC_ND_A_FIELD( old_nt); - strcpy( SAC_ND_A_FIELD( new_nt) + pos, insert); - } - else { - SAC_ND_ALLOC__DESC( new_nt, 0) - SAC_ND_SET__RC( new_nt, 1) - STRDUP( SAC_ND_A_FIELD( new_nt), SAC_ND_A_FIELD( old_nt)); - strcpy( SAC_ND_A_FIELD( new_nt) + pos, insert); - - SAC_ND_DEC_RC_FREE( old_nt, 1, SAC_FREE) - } - SAC_ND_A_FIELD( new_nt)[len_insert_pos] = store; - } - else { - SAC_ND_ALLOC__DESC( new_nt, 0) - SAC_ND_SET__RC( new_nt, 1) - SAC_ND_A_FIELD( new_nt) = (string) SAC_MALLOC( len_insert_pos + 1); - strncpy( SAC_ND_A_FIELD( new_nt), SAC_ND_A_FIELD( old_nt), pos); - SAC_ND_A_FIELD( new_nt)[pos] = '\0'; - - strcat( SAC_ND_A_FIELD( new_nt), insert); - - SAC_ND_DEC_RC_FREE( old_nt, 1, SAC_FREE) - } - - SAC_ND_RET_out( new_nt, new_nt) -} - -#undef new_nt -#undef old_nt - -/*****************************************************************/ diff --git a/src/structures/src/String/strrchr.c b/src/structures/src/String/strrchr.c deleted file mode 100644 index 903607e3..00000000 --- a/src/structures/src/String/strrchr.c +++ /dev/null @@ -1,25 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrrchr( string str, char c) -{ - char *occur; - - occur = strrchr( str, c); - - if (occur == NULL) { - return( -1); - } - else { - return( occur-str); - } -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strsel.c b/src/structures/src/String/strsel.c deleted file mode 100644 index 82233e9e..00000000 --- a/src/structures/src/String/strsel.c +++ /dev/null @@ -1,18 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -char strsel( string s, int pos) -{ - RANGECHECK( (size_t) pos, 0, strlen( s) - 1, s); - - return( s[pos]); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strspn.c b/src/structures/src/String/strspn.c deleted file mode 100644 index 95dbf4fd..00000000 --- a/src/structures/src/String/strspn.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrspn( string first, string second) -{ - int res; - - res = strspn( first, second); - - return( res); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strstr.c b/src/structures/src/String/strstr.c deleted file mode 100644 index 59ef530b..00000000 --- a/src/structures/src/String/strstr.c +++ /dev/null @@ -1,17 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrstr( string haystack, string needle) -{ - string found = strstr( haystack, needle); - return (found) ? (found - haystack) : -1; -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strtake.c b/src/structures/src/String/strtake.c deleted file mode 100644 index 297564d0..00000000 --- a/src/structures/src/String/strtake.c +++ /dev/null @@ -1,43 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -#define new_nt (new, T_OLD((SCL, (HID, (NUQ,))))) -#define old_nt (old, T_OLD((SCL, (HID, (NUQ,))))) - -void strtake( SAC_ND_PARAM_out( new_nt, string), - SAC_ND_PARAM_in( old_nt, string), - int n) -{ - SAC_ND_DECL__DESC( new_nt, ) - SAC_ND_DECL__DATA( new_nt, string, ) - - RANGECHECK( (size_t)n, 0, strlen( SAC_ND_A_FIELD( old_nt)), SAC_ND_A_FIELD( old_nt)); - - if (SAC_ND_A_RC( old_nt) == 1) { - SAC_ND_A_DESC( new_nt) = SAC_ND_A_DESC( old_nt); - SAC_ND_A_FIELD( new_nt) = SAC_ND_A_FIELD( old_nt); - } - else { - SAC_ND_ALLOC__DESC( new_nt, 0) - SAC_ND_SET__RC( new_nt, 1) - SAC_ND_A_FIELD( new_nt) = (string) SAC_MALLOC( n + 1); - strncpy( SAC_ND_A_FIELD( new_nt), SAC_ND_A_FIELD( old_nt), n); - - SAC_ND_DEC_RC_FREE( old_nt, 1, SAC_FREE) - } - SAC_ND_A_FIELD( new_nt)[n] = '\0'; - - SAC_ND_RET_out( new_nt, new_nt) -} - -#undef new_nt -#undef old_nt - -/*****************************************************************/ diff --git a/src/structures/src/String/strtod.c b/src/structures/src/String/strtod.c deleted file mode 100644 index 140302e1..00000000 --- a/src/structures/src/String/strtod.c +++ /dev/null @@ -1,31 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -double SACstrtod( string *remain, string input) -{ - double res; - char *rem; - - res = strtod( input, &rem); - - *remain = (string) SAC_MALLOC( strlen( rem) + 1); - strcpy( *remain, rem); - - return( res); -} - -/*****************************************************************/ - -double SACtod( string input) -{ - return( strtod( input, NULL)); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strtof.c b/src/structures/src/String/strtof.c deleted file mode 100644 index 723fad7f..00000000 --- a/src/structures/src/String/strtof.c +++ /dev/null @@ -1,31 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -float SACstrtof( string *remain, string input) -{ - float res; - char *rem; - - res = (float) strtod( input, &rem); - - *remain = (string) SAC_MALLOC( strlen( rem) + 1); - strcpy( *remain, rem); - - return( res); -} - -/*****************************************************************/ - -float SACtof( string input) -{ - return( (float) strtod( input, NULL)); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strtoi.c b/src/structures/src/String/strtoi.c deleted file mode 100644 index 51239568..00000000 --- a/src/structures/src/String/strtoi.c +++ /dev/null @@ -1,31 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -int SACstrtoi( string *remain, string input, int base) -{ - int res; - char *rem; - - res = (int) strtol( input, &rem, base); - - *remain = (string) SAC_MALLOC( strlen( rem) + 1); - strcpy( *remain, rem); - - return( res); -} - -/*****************************************************************/ - -int SACtoi( string input) -{ - return( (int) strtol( input, NULL, 0)); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/strtok.c b/src/structures/src/String/strtok.c deleted file mode 100644 index dd9fbbd8..00000000 --- a/src/structures/src/String/strtok.c +++ /dev/null @@ -1,51 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - - -/*****************************************************************/ - -string SACstrtok( string *remain, string input, string sep) -{ - int i, j, k; - string token; - - /* increment 'i' as long as input[i] is in "sep". */ - i = k = 0; - while (input[i] && sep[k]) { - k = 0; - while (sep[k] && input[i] != sep[k]) { - ++k; - } - if (sep[k]) { - ++i; - } - } - - /* increment 'j' as long as input[j] is not in "sep". */ - j = i; - k = 0; - while (input[j] && sep[k] != input[j]) { - k = 0; - while (sep[k] && input[j] != sep[k]) { - ++k; - } - if (!sep[k]) { - ++j; - } - } - - token = (string) SAC_MALLOC( j - i + 1); - strncpy(token, input + i, j - i); - token[j - i] = '\0'; - - *remain = (string) SAC_MALLOC( strlen( input + j) + 1); - strcpy( *remain, input + j); - - return( token); -} - -/*****************************************************************/ diff --git a/src/structures/src/String/tostring.c b/src/structures/src/String/tostring.c deleted file mode 100644 index bacf8fd1..00000000 --- a/src/structures/src/String/tostring.c +++ /dev/null @@ -1,32 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include "StringC.h" - -/*****************************************************************/ - -#define str_nt (str, T_OLD((SCL, (HID, (NUQ,))))) -#define ar_nt (ar, T_OLD((AUD, (NHD, (NUQ,))))) - -void to_string( SAC_ND_PARAM_out( str_nt, string), - SAC_ND_PARAM_in( ar_nt, char), - int length) -{ - SAC_ND_DECL__DESC( str_nt, ) - SAC_ND_DECL__DATA( str_nt, string, ) - - SAC_ND_ALLOC__DESC( str_nt, 0) - SAC_ND_SET__RC( str_nt, 1) - SAC_ND_A_FIELD( str_nt) = (string) SAC_MALLOC( length + 1); - strncpy( SAC_ND_A_FIELD( str_nt), SAC_ND_A_FIELD( ar_nt), length); - SAC_ND_A_FIELD( str_nt)[length] = '\0'; - - SAC_ND_RET_out( str_nt, str_nt) -} - -#undef str_nt -#undef ar_nt - -/*****************************************************************/ diff --git a/src/structures/src/String/trim.c b/src/structures/src/String/trim.c deleted file mode 100644 index bca1aa1c..00000000 --- a/src/structures/src/String/trim.c +++ /dev/null @@ -1,105 +0,0 @@ -/* - * implementation of standard module StringC - */ - - -#include -#include "StringC.h" - - -/*****************************************************************/ - -string SACchomp(string in) -{ - size_t len = strlen(in); - size_t i; - string out; - - for (i = len; i > 0; ) { - --i; - if (in[i] != '\r' && in[i] != '\n') { - ++i; - break; - } - } - - out = (string) SAC_MALLOC( i + 1); - strncpy(out, in, i); - out[i] = '\0'; - - return out; -} - -/*****************************************************************/ - -string SACrtrim(string in) -{ - size_t len = strlen(in); - size_t i; - string out; - - for (i = len; i > 0; ) { - --i; - if (!isspace((unsigned char) in[i])) { - ++i; - break; - } - } - - out = (string) SAC_MALLOC( i + 1); - strncpy(out, in, i); - out[i] = '\0'; - - return out; -} - -/*****************************************************************/ - -string SACltrim(string in) -{ - size_t len = strlen(in); - size_t i; - string out; - - for (i = 0; i < len; ++i) { - if (!isspace((unsigned char) in[i])) { - break; - } - } - - out = (string) SAC_MALLOC( len - i + 1); - strncpy(out, in + i, len - i); - out[len - i] = '\0'; - - return out; -} - -/*****************************************************************/ - -string SACtrim(string in) -{ - size_t len = strlen(in); - size_t i, j; - string out; - - for (i = len; i > 0; ) { - --i; - if (!isspace((unsigned char) in[i])) { - ++i; - break; - } - } - for (j = 0; j < i; ++j) { - if (!isspace((unsigned char) in[j])) { - break; - } - } - - out = (string) SAC_MALLOC( i - j + 1); - strncpy(out, in + j, i - j); - out[i - j] = '\0'; - - return out; -} - -/*****************************************************************/