diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5b895ec9..dd3c927f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -195,41 +195,7 @@ 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/String/String.c structures/src/List/tl.c structures/src/List/take.c structures/src/List/nth.c @@ -252,7 +218,6 @@ SET (C_DEPS_SRC structures/src/Constants/maxdouble.c structures/src/Constants/epidouble.c structures/src/Constants/randmax.c - structures/src/Char/ctype.c structures/src/Char/Char.c stdio/src/TermFile/stdstreams.c stdio/src/TermFile/printf.c @@ -439,6 +404,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 @@ -477,7 +443,7 @@ FOREACH (name ${SAC_SRC}) ADD_CUSTOM_COMMAND ( OUTPUT "${mod}" "${tree}" COMMAND - ${SAC2C} -v0 -maxoptcyc 2 -linksetsize ${LINKSETSIZE} ${NOTREE_FLAG} ${SAC2C_CC_FLAGS} -o ${DLL_BUILD_DIR} "${src}" + ${SAC2C} -v0 -maxoptcyc 2 -linksetsize ${LINKSETSIZE} ${NOTREE_FLAG} ${SAC2C_CC_FLAGS} -Xc -I"${src_dir}" -o ${DLL_BUILD_DIR} "${src}" WORKING_DIRECTORY "${dir}" MAIN_DEPENDENCY "${src}" diff --git a/src/structures/Char.sac b/src/structures/Char.sac index e222811e..8934dc63 100644 --- a/src/structures/Char.sac +++ b/src/structures/Char.sac @@ -14,57 +14,57 @@ export all; ******************************************************************************/ external bool isalpha(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisalpha" #pragma linksign [0,1] external bool isupper(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisupper" #pragma linksign [0,1] external bool islower(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACislower" #pragma linksign [0,1] external bool isdigit(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisdigit" #pragma linksign [0,1] external bool isxdigit(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisxdigit" #pragma linksign [0,1] external bool isspace(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisspace" #pragma linksign [0,1] external bool ispunct(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACispunct" #pragma linksign [0,1] external bool isalnum(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisalnum" #pragma linksign [0,1] external bool isprint(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisprint" #pragma linksign [0,1] external bool isgraph(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisgraph" #pragma linksign [0,1] external bool iscntrl(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACiscntrl" #pragma linksign [0,1] @@ -76,12 +76,12 @@ external bool iscntrl(char c); ******************************************************************************/ external bool isascii(int N); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACisascii" #pragma linksign [0,1] external char toascii(int N); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACtoascii" #pragma linksign [0,1] @@ -92,12 +92,12 @@ external char toascii(int N); ******************************************************************************/ external char tolower(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACtolower" #pragma linksign [0,1] external char toupper(char c); - #pragma linkobj "src/Char/ctype.o" + #pragma linkobj "src/Char/Char.o" #pragma linkname "SACtoupper" #pragma linksign [0,1] diff --git a/src/structures/String.sac b/src/structures/String.sac index 7167ebfe..88dcd37b 100644 --- a/src/structures/String.sac +++ b/src/structures/String.sac @@ -12,115 +12,130 @@ export all except { indent }; external typedef string; #pragma freefun "free_string" - #pragma linkobj "src/String/freestr.o" + #pragma linkobj "src/String/String.o" #pragma copyfun "copy_string" - #pragma linkobj "src/String/copystr.o" + #pragma linkobj "src/String/String.o" + #pragma ctype "char*" 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/String.o" + #pragma header "src/String/String.h" + /* Convert character array to string. */ + +external string to_auto_string(char[.] A); + #pragma linkname "SACautotostring" + #pragma sacarg [1] + #pragma linkobj "src/String/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.h" /* Generate a string depending on the format string and a variable number of arguments similar to the printf and fprintf functions. */ @@ -128,7 +143,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/String.o" + #pragma header "src/String/String.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 +154,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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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 +211,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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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 +249,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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.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/String.o" + #pragma header "src/String/String.h" /* Convert boolean into string. */ inline string indent(int indent, string str) diff --git a/src/structures/src/Char/ctype.c b/src/structures/src/Char/Char.c similarity index 100% rename from src/structures/src/Char/ctype.c rename to src/structures/src/Char/Char.c diff --git a/src/structures/src/String/String.c b/src/structures/src/String/String.c new file mode 100644 index 00000000..4968b3b9 --- /dev/null +++ b/src/structures/src/String/String.c @@ -0,0 +1,510 @@ +/* + * C implementation of standard module StringC + */ + + +#include +#include +#include +#include +#include +#include +#include +#include "String.h" + +string copy_string (string s) +{ + string s2 = malloc ((strlen(s) + 1) * sizeof (char)); + strcpy (s2, s); + return s2; +} + +void free_string (string s) +{ + free (s); +} + + + + +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'; + + 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; +} \ No newline at end of file diff --git a/src/structures/src/String/String.h b/src/structures/src/String/String.h new file mode 100644 index 00000000..ad33a94a --- /dev/null +++ b/src/structures/src/String/String.h @@ -0,0 +1,49 @@ +#ifndef STDLIB__STR__H +#define STDLIB__STR__H + +#include "sacinterface.h" +#include + +typedef char* string; + +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 // STDLIB__TOSTRING__H \ No newline at end of file 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; -} - -/*****************************************************************/