12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055 |
- /** @file str.c
- * @brief String related functions
- *
- * This file implements some often used string functions.
- * Some functions are more portable versions of standard
- * functions but others are original ones.
- */
- #include <stdlib.h>
- #include <string.h>
- #include <assert.h>
- #include <stdio.h>
- #include <stdarg.h>
- #include "faux/ctype.h"
- #include "faux/conv.h"
- #include "faux/str.h"
- /** @brief Free the memory allocated for the string.
- *
- * Safely free the memory allocated for the string. You can use NULL
- * pointer with this function. POSIX's free() checks for the NULL pointer
- * but not all systems do so.
- *
- * @param [in] str String to free
- */
- void faux_str_free(char *str)
- {
- faux_free(str);
- }
- /** @brief Duplicates the string.
- *
- * Duplicates the string. Same as standard strdup() function. Allocates
- * memory with malloc(). Checks for NULL pointer.
- *
- * @warning Resulting string must be freed by faux_str_free().
- *
- * @param [in] str String to duplicate.
- * @return Pointer to allocated string or NULL.
- */
- char *faux_str_dup(const char *str)
- {
- if (!str)
- return NULL;
- return strdup(str);
- }
- /** @brief Duplicates the first n bytes of the string.
- *
- * Duplicates at most n bytes of the string. Allocates
- * memory with malloc(). Checks for NULL pointer. Function will allocate
- * n + 1 bytes to store string and terminating null byte.
- *
- * @warning Resulting string must be freed by faux_str_free().
- *
- * @param [in] str String to duplicate.
- * @param [in] n Number of bytes to copy.
- * @return Pointer to allocated string or NULL.
- */
- char *faux_str_dupn(const char *str, size_t n)
- {
- char *res = NULL;
- size_t len = 0;
- if (!str)
- return NULL;
- // Search for terminating '\0' among first n bytes
- // Don't use strlen() because it can be not null-terminated.
- for (len = 0; len < n; len++)
- if ('\0' == str[len])
- break;
- len = (len < n) ? len : n;
- res = faux_zmalloc(len + 1);
- if (!res)
- return NULL;
- strncpy(res, str, len);
- res[len] = '\0';
- return res;
- }
- /** @brief Generates lowercase copy of input string.
- *
- * Allocates the copy of input string and convert that copy to lowercase.
- *
- * @warning Resulting string must be freed by faux_str_free().
- *
- * @param [in] str String to convert.
- * @return Pointer to lowercase string copy or NULL.
- */
- char *faux_str_tolower(const char *str)
- {
- char *res = faux_str_dup(str);
- char *p = res;
- if (!res)
- return NULL;
- while (*p) {
- *p = faux_ctype_tolower(*p);
- p++;
- }
- return res;
- }
- /** @brief Generates uppercase copy of input string.
- *
- * Allocates the copy of input string and convert that copy to uppercase.
- *
- * @warning Resulting string must be freed by faux_str_free().
- *
- * @param [in] str String to convert.
- * @return Pointer to lowercase string copy or NULL.
- */
- char *faux_str_toupper(const char *str)
- {
- char *res = faux_str_dup(str);
- char *p = res;
- if (!res)
- return NULL;
- while (*p) {
- *p = faux_ctype_toupper(*p);
- p++;
- }
- return res;
- }
- /** @brief Add n bytes of text to existent string.
- *
- * Concatenate two strings. Add n bytes of second string to the end of the
- * first one. The first argument is address of string pointer. The pointer
- * can be changed due to realloc() features. The first pointer can be NULL.
- * In this case the memory will be malloc()-ed and stored to the first pointer.
- *
- * @param [in,out] str Address of first string pointer.
- * @param [in] text Text to add to the first string.
- * @param [in] n Number of bytes to add.
- * @return Pointer to resulting string or NULL.
- */
- char *faux_str_catn(char **str, const char *text, size_t n)
- {
- size_t str_len = 0;
- size_t text_len = 0;
- char *res = NULL;
- char *p = NULL;
- if (!text)
- return *str;
- str_len = (*str) ? strlen(*str) : 0;
- text_len = strlen(text);
- text_len = (text_len < n) ? text_len : n;
- res = realloc(*str, str_len + text_len + 1);
- if (!res)
- return NULL;
- p = res + str_len;
- strncpy(p, text, text_len);
- p[text_len] = '\0';
- *str = res;
- return res;
- }
- /** @brief Add some text to existent string.
- *
- * Concatenate two strings. Add second string to the end of the first one.
- * The first argument is address of string pointer. The pointer can be
- * changed due to realloc() features. The first pointer can be NULL. In this
- * case the memory will be malloc()-ed and stored to the first pointer.
- *
- * @param [in,out] str Address of first string pointer.
- * @param [in] text Text to add to the first string.
- * @return Pointer to resulting string or NULL.
- */
- char *faux_str_cat(char **str, const char *text)
- {
- size_t len = 0;
- if (!text)
- return *str;
- len = strlen(text);
- return faux_str_catn(str, text, len);
- }
- /** @brief Add multiply text strings to existent string.
- *
- * Concatenate multiply strings. Add next string to the end of the previous one.
- * The first argument is address of string pointer. The pointer can be
- * changed due to realloc() features. The first pointer can be NULL. In this
- * case the memory will be malloc()-ed and stored to the first pointer.
- * The last argument must be 'NULL'. It marks the last argument within
- * variable arguments list.
- *
- * @warning If last argument is not 'NULL' then behaviour is undefined.
- *
- * @param [in,out] str Address of first string pointer.
- * @param [in] text Text to add to the first string.
- * @return Pointer to resulting string or NULL.
- */
- char *faux_str_mcat(char **str, ...)
- {
- va_list ap;
- const char *arg = NULL;
- char *retval = *str;
- va_start(ap, str);
- while ((arg = va_arg(ap, const char *))) {
- retval = faux_str_cat(str, arg);
- }
- va_end(ap);
- return retval;
- }
- /** @brief Allocates memory and vsprintf() to it.
- *
- * Function tries to find out necessary amount of memory for specified format
- * string and arguments. Format is same as for vsprintf() function. Then
- * function allocates memory for resulting string and vsprintf() to it. So
- * user doesn't need to allocate buffer himself. Function returns allocated
- * string that need to be freed by faux_str_free() function later.
- *
- * @warning The returned pointer must be free by faux_str_free().
- *
- * @param [in] fmt Format string like the sprintf()'s fmt.
- * @param [in] ap The va_list argument.
- * @return Allocated resulting string or NULL on error.
- */
- char *faux_str_vsprintf(const char *fmt, va_list ap)
- {
- int size = 1;
- char calc_buf[1] = "";
- char *line = NULL;
- va_list ap2;
- // Calculate buffer size
- va_copy(ap2, ap);
- size = vsnprintf(calc_buf, size, fmt, ap2);
- va_end(ap2);
- // The snprintf() prior to 2.0.6 glibc version returns -1 if string
- // was truncated. The later glibc returns required buffer size.
- // The calc_buf can be NULL and size can be 0 for recent glibc but
- // probably some exotic implementations can break on it. So use
- // minimal buffer with length = 1.
- if (size < 0)
- return NULL;
- size++; // Additional byte for '\0'
- line = faux_zmalloc(size);
- if (!line) // Memory problems
- return NULL;
- // Format real string
- size = vsnprintf(line, size, fmt, ap);
- if (size < 0) { // Some problems
- faux_str_free(line);
- return NULL;
- }
- return line;
- }
- /** @brief Allocates memory and sprintf() to it.
- *
- * Function tries to find out necessary amount of memory for specified format
- * string and arguments. Format is same as for sprintf() function. Then
- * function allocates memory for resulting string and sprintf() to it. So
- * user doesn't need to allocate buffer himself. Function returns allocated
- * string that need to be freed by faux_str_free() function later.
- *
- * @warning The returned pointer must be free by faux_str_free().
- *
- * @param [in] fmt Format string like the sprintf()'s fmt.
- * @param [in] arg Number of arguments.
- * @return Allocated resulting string or NULL on error.
- */
- char *faux_str_sprintf(const char *fmt, ...)
- {
- char *line = NULL;
- va_list ap;
- va_start(ap, fmt);
- line = faux_str_vsprintf(fmt, ap);
- va_end(ap);
- return line;
- }
- /** @brief Service function to compare two chars in right way.
- *
- * The problem is char type can be signed or unsigned on different
- * platforms. So stright comparision can return different results.
- *
- * @param [in] char1 First char
- * @param [in] char2 Second char
- * @return
- * < 0 if char1 < char2
- * = 0 if char1 = char2
- * > 0 if char1 > char2
- */
- static int faux_str_cmp_chars(char char1, char char2)
- {
- unsigned char ch1 = (unsigned char)char1;
- unsigned char ch2 = (unsigned char)char2;
- return (int)ch1 - (int)ch2;
- }
- /** @brief Compare n first characters of two strings.
- *
- * @param [in] str1 First string to compare.
- * @param [in] str2 Second string to compare.
- * @param [in] n Number of characters to compare.
- * @return < 0, 0, > 0, see the strcasecmp().
- */
- int faux_str_cmpn(const char *str1, const char *str2, size_t n)
- {
- if (!str1 && !str2) // Empty strings are equal
- return 0;
- if (!str1) // Consider NULL string to be less then empty string
- return -1;
- if (!str2) // Consider NULL string to be less then empty string
- return 1;
- return strncmp(str1, str2, n);
- }
- /** @brief Compare two strings.
- *
- * @param [in] str1 First string to compare.
- * @param [in] str2 Second string to compare.
- * @return < 0, 0, > 0, see the strcmp().
- */
- int faux_str_cmp(const char *str1, const char *str2)
- {
- if (!str1 && !str2) // Empty strings are equal
- return 0;
- if (!str1) // Consider NULL string to be less then empty string
- return -1;
- if (!str2) // Consider NULL string to be less then empty string
- return 1;
- return strcmp(str1, str2);
- }
- /** @brief Compare n first characters of two strings ignoring case.
- *
- * The difference beetween this function an standard strncasecmp() is
- * faux function uses faux ctype functions. It can be important for
- * portability.
- *
- * @param [in] str1 First string to compare.
- * @param [in] str2 Second string to compare.
- * @param [in] n Number of characters to compare.
- * @return < 0, 0, > 0, see the strcasecmp().
- */
- int faux_str_casecmpn(const char *str1, const char *str2, size_t n)
- {
- const char *p1 = str1;
- const char *p2 = str2;
- size_t num = n;
- while (*p1 != '\0' && *p2 != '\0' && num != 0) {
- int res = faux_str_cmp_chars(
- faux_ctype_tolower(*p1), faux_ctype_tolower(*p2));
- if (res != 0)
- return res;
- p1++;
- p2++;
- num--;
- }
- if (0 == n) // It means n first characters are equal.
- return 0;
- return faux_str_cmp_chars(
- faux_ctype_tolower(*p1), faux_ctype_tolower(*p2));
- }
- /** @brief Compare two strings ignoring case.
- *
- * The difference beetween this function an standard strcasecmp() is
- * faux function uses faux ctype functions. It can be important for
- * portability.
- *
- * @param [in] str1 First string to compare.
- * @param [in] str2 Second string to compare.
- * @return < 0, 0, > 0, see the strcasecmp().
- */
- int faux_str_casecmp(const char *str1, const char *str2)
- {
- const char *p1 = str1;
- const char *p2 = str2;
- if (!p1 && !p2) // Empty strings are equal
- return 0;
- if (!p1) // Consider NULL string to be less then empty string
- return -1;
- if (!p2) // Consider NULL string to be less then empty string
- return 1;
- while (*p1 != '\0' && *p2 != '\0') {
- int res = faux_str_cmp_chars(
- faux_ctype_tolower(*p1), faux_ctype_tolower(*p2));
- if (res != 0)
- return res;
- p1++;
- p2++;
- }
- return faux_str_cmp_chars(
- faux_ctype_tolower(*p1), faux_ctype_tolower(*p2));
- }
- /** @brief Compare two strings considering numbers.
- *
- * "a2" < "a10"
- *
- * @param [in] str1 First string to compare.
- * @param [in] str2 Second string to compare.
- * @return < 0, 0, > 0, see the strcasecmp().
- */
- int faux_str_numcmp(const char *str1, const char *str2)
- {
- const char *p1 = str1;
- const char *p2 = str2;
- if (!p1 && !p2) // Empty strings are equal
- return 0;
- if (!p1) // Consider NULL string to be less then empty string
- return -1;
- if (!p2) // Consider NULL string to be less then empty string
- return 1;
- while (*p1 != '\0' && *p2 != '\0') {
- if (faux_ctype_isdigit(*p1) && faux_ctype_isdigit(*p2)) {
- unsigned long long int v1 = 0;
- unsigned long long int v2 = 0;
- if (!faux_conv_atoull(p1, &v1, 10) ||
- !faux_conv_atoull(p2, &v2, 10)) // Overflow?
- return faux_str_cmp(str1, str2); // Standard comparison
- if (v1 > v2)
- return 1;
- if (v1 < v2)
- return -1;
- // Skip all digits if equal
- while (faux_ctype_isdigit(*p1))
- p1++;
- while (faux_ctype_isdigit(*p2))
- p2++;
- } else {
- int res = faux_str_cmp_chars(*p1, *p2);
- if (res != 0)
- return res;
- p1++;
- p2++;
- }
- }
- return faux_str_cmp_chars(*p1, *p2);
- }
- /** @brief Finds the first occurrence of the substring in the string
- *
- * Function is a faux version of strcasestr() function.
- *
- * @param [in] haystack String to find substring in it.
- * @param [in] needle Substring to find.
- * @return
- * Pointer to first occurence of substring in the string.
- * NULL on error
- */
- char *faux_str_casestr(const char *haystack, const char *needle)
- {
- const char *ptr = haystack;
- size_t ptr_len = 0;
- size_t needle_len = 0;
- if (!haystack || !needle)
- return NULL;
- ptr_len = strlen(haystack);
- needle_len = strlen(needle);
- while ((*ptr != '\0') && (ptr_len >= needle_len)) {
- int res = faux_str_casecmpn(ptr, needle, needle_len);
- if (0 == res)
- return (char *)ptr;
- ptr++;
- ptr_len--;
- }
- return NULL; // Not found
- }
- /** Prepare string for embedding to C-code (make escaping).
- *
- * @warning The returned pointer must be freed by faux_str_free().
- * @param [in] src String for escaping.
- * @return Escaped string or NULL on error.
- */
- char *faux_str_c_esc(const char *src)
- {
- const char *src_ptr = src;
- char *dst = NULL;
- char *dst_ptr = NULL;
- char *escaped = NULL;
- size_t src_len = 0;
- size_t dst_len = 0;
- if (!src)
- return NULL;
- src_len = strlen(src);
- // Calculate max destination string size.
- // The worst case is when each src character will be replaced by
- // something like '\xff'. So it's 4 dst chars for 1 src one.
- dst_len = (src_len * 4) + 1; // one byte for '\0'
- dst = faux_zmalloc(dst_len);
- assert(dst);
- if (!dst)
- return NULL;
- dst_ptr = dst;
- while (*src_ptr != '\0') {
- char *esc = NULL; // escaped replacement
- char buf[5]; // longest 'char' (4 bytes) + '\0'
- size_t len = 0;
- switch (*src_ptr) {
- case '\n':
- esc = "\\n";
- break;
- case '\"':
- esc = "\\\"";
- break;
- case '\\':
- esc = "\\\\";
- break;
- case '\'':
- esc = "\\\'";
- break;
- case '\r':
- esc = "\\r";
- break;
- case '\t':
- esc = "\\t";
- break;
- default:
- // Check is the symbol control character. Control
- // characters has codes from 0x00 to 0x1f.
- if (((unsigned char)*src_ptr & 0xe0) == 0) { // control
- snprintf(buf, sizeof(buf), "\\x%02x",
- (unsigned char)*src_ptr);
- buf[4] = '\0'; // for safety
- } else {
- buf[0] = *src_ptr; // Common character
- buf[1] = '\0';
- }
- esc = buf;
- break;
- }
- len = strlen(esc);
- memcpy(dst_ptr, esc, len); // zmalloc() nullify the rest
- dst_ptr += len;
- src_ptr++;
- }
- escaped = faux_str_dup(dst); // Free some memory
- faux_str_free(dst); // 'dst' size >= 'escaped' size
- return escaped;
- }
- /** Escape string and add quotes if necessary.
- *
- * Quotes will be added if string contains spaces.
- *
- * @warning The returned pointer must be freed by faux_str_free().
- * @param [in] src Input string.
- * @return Processed string or NULL on error.
- */
- char *faux_str_c_esc_quote(const char *src)
- {
- char *space = NULL;
- char *escaped = NULL;
- char *result = NULL;
- if (!src)
- return NULL;
- escaped = faux_str_c_esc(src);
- // String with space must have quotes
- space = strchr(escaped, ' ');
- if (space) {
- result = faux_str_sprintf("\"%s\"", escaped);
- faux_str_free(escaped);
- } else {
- result = escaped;
- }
- return result;
- }
- #define BYTE_CONV_LEN 4 // Length of one byte converted to string
- /** Prepare binary block for embedding to C-code.
- *
- * @warning The returned pointer must be freed by faux_str_free().
- * @param [in] src Binary block for conversion.
- * @return C-string or NULL on error.
- */
- char *faux_str_c_bin(const char *src, size_t n)
- {
- const char *src_ptr = src;
- char *dst = NULL;
- char *dst_ptr = NULL;
- size_t dst_len = 0;
- if (!src)
- return NULL;
- // Calculate destination string size.
- // Each src character will be replaced by
- // something like '\xff'. So it's 4 dst chars for 1 src char.
- dst_len = (n * BYTE_CONV_LEN) + 1; // one byte for '\0'
- dst = faux_zmalloc(dst_len);
- assert(dst);
- if (!dst)
- return NULL;
- dst_ptr = dst;
- while (src_ptr < (src + n)) {
- char buf[BYTE_CONV_LEN + 1]; // longest 'char' (4 bytes) + '\0'
- snprintf(buf, sizeof(buf), "\\x%02x", (unsigned char)*src_ptr);
- memcpy(dst_ptr, buf, BYTE_CONV_LEN); // zmalloc() nullify the rest
- dst_ptr += BYTE_CONV_LEN;
- src_ptr++;
- }
- return dst;
- }
- /** @brief Search the n-th chars of string for one of the specified chars.
- *
- * The function search for any of specified characters within string.
- * The search is limited to first n characters of the string. If
- * terminating '\0' is before n-th character then search will stop on
- * it. Can be used with raw memory block.
- *
- * @param [in] str String (or memory block) to search in.
- * @param [in] chars_to_string Chars enumeration to search for.
- * @param [in] n Maximum number of bytes to search within.
- * @return Pointer to the first occurence of one of specified chars.
- * NULL on error.
- */
- char *faux_str_charsn(const char *str, const char *chars_to_search, size_t n)
- {
- const char *current_char = str;
- size_t len = n;
- if (!str || !chars_to_search)
- return NULL;
- while ((len > 0) && (*current_char != '\0')) {
- if (strchr(chars_to_search, *current_char))
- return (char *)current_char;
- current_char++;
- len--;
- }
- return NULL;
- }
- /** @brief Search string for one of the specified chars.
- *
- * The function search for any of specified characters within string.
- *
- * @param [in] str String to search in.
- * @param [in] chars_to_string Chars enumeration to search for.
- * @return Pointer to the first occurence of one of specified chars.
- * NULL on error.
- */
- char *faux_str_chars(const char *str, const char *chars_to_search)
- {
- if (!str)
- return NULL;
- return faux_str_charsn(str, chars_to_search, strlen(str));
- }
- /** @brief Remove escaping. Convert string to internal view.
- *
- * Find backslashes (before escaped symbols) and remove it. Escaped symbol
- * will not be analyzed so `\\` will lead to `\`.
- *
- * @param [in] string Escaped string.
- * @param [in] len Length of string to de-escape.
- * @return Allocated de-escaped string
- * @warning Returned value must be freed by faux_str_free() later.
- */
- static char *faux_str_deesc(const char *string, size_t len)
- {
- const char *s = string;
- char *res = NULL;
- char *p = NULL;
- bool_t escaped = BOOL_FALSE;
- if (!string)
- return NULL;
- if (0 == len)
- return NULL;
- res = faux_zmalloc(len + 1);
- assert(res);
- if (!res)
- return NULL;
- p = res;
- while ((*s != '\0') && (s < (string +len))) {
- if (('\\' == *s) && !escaped) {
- escaped = BOOL_TRUE;
- s++;
- continue;
- }
- escaped = BOOL_FALSE;
- *p = *s;
- s++;
- p++;
- }
- *p = '\0';
- return res;
- }
- /*--------------------------------------------------------- */
- /** @brief Find next word or quoted substring within string
- *
- * The quotation can be of several different kinds.
- *
- * The first kind is standard double quoting. In this case the internal (within
- * quotation) `"` and `\` symbols must be escaped. But symbols will be deescaped
- * before writing to internal buffers.
- *
- * The second kind of quotation is alternative quotation. Any symbol can become
- * quote sign. For example "`" and "'" can be considered as a quotes. To use
- * some symbols as a quote them must be specified by `alt_quotes` function
- * parameter. The single symbol can be considered as a start of quotation or
- * a sequence of the same symbols can be considered as a start of quotation. In
- * this case the end of quotation is a sequence of the same symbols. The same
- * symbol can appear inside quotation but number of symbols (sequence) must be
- * less than opening quote sequence. The example of alternatively quoted string
- * is ```some text``and anothe`r```. The backslash has no special meaning inside
- * quoted string.
- *
- * The substring can be unquoted string without spaces. The space, backslash and
- * quote can be escaped by backslash.
- *
- * Parts of text with different quotes can be glued together to get single
- * substring like this: aaa"inside dbl quote"bbb``alt quote"`here``ccc.
- *
- * @param [in] str String to parse.
- * @param [out] saveptr Pointer to first symbol after found substring.
- * @param [in] alt_quotes Possible alternative quotes.
- * @param [out] qclosed Flag is quote closed.
- * @return Allocated buffer with found substring (without quotes).
- * @warning Returned alocated buffer must be freed later by faux_str_free()
- */
- char *faux_str_nextword(const char *str, const char **saveptr,
- const char *alt_quotes, bool_t *qclosed)
- {
- const char *string = str;
- const char *word = NULL;
- size_t len = 0;
- const char dbl_quote = '"';
- bool_t dbl_quoted = BOOL_FALSE;
- char alt_quote = '\0';
- unsigned int alt_quote_num = 0; // Number of opening alt quotes
- bool_t alt_quoted = BOOL_FALSE;
- char *result = NULL;
- // Find the start of a word (not including an opening quote)
- while (*string && isspace(*string))
- string++;
- word = string; // Suppose not quoted string
- while (*string != '\0') {
- // Standard double quotation
- if (dbl_quoted) {
- // End of word
- if (*string == dbl_quote) {
- if (len > 0) {
- char *s = faux_str_deesc(word, len);
- faux_str_cat(&result, s);
- faux_str_free(s);
- }
- dbl_quoted = BOOL_FALSE;
- string++;
- word = string;
- len = 0;
- // Escaping
- } else if (*string == '\\') {
- // Skip escaping
- string++;
- len++;
- // Skip escaped symbol
- if (*string) {
- string++;
- len++;
- }
- } else {
- string++;
- len++;
- }
- // Alternative multi quotation
- } else if (alt_quoted) {
- unsigned int qnum = alt_quote_num;
- while (string && (*string == alt_quote) && qnum) {
- string++;
- len++;
- qnum--;
- }
- if (0 == qnum) { // End of word was found
- // Quotes themselfs are not a part of a word
- len -= alt_quote_num;
- if (len > 0)
- faux_str_catn(&result, word, len);
- alt_quoted = BOOL_FALSE;
- word = string;
- len = 0;
- } else if (qnum == alt_quote_num) { // No quote syms
- string++;
- len++;
- }
- // Not quoted
- } else {
- // Start of a double quoted string
- if (*string == dbl_quote) {
- if (len > 0) {
- char *s = faux_str_deesc(word, len);
- faux_str_cat(&result, s);
- faux_str_free(s);
- }
- dbl_quoted = BOOL_TRUE;
- string++;
- word = string;
- len = 0;
- // Start of alt quoted string
- } else if (alt_quotes && strchr(alt_quotes, *string)) {
- if (len > 0) {
- char *s = faux_str_deesc(word, len);
- faux_str_cat(&result, s);
- faux_str_free(s);
- }
- alt_quoted = BOOL_TRUE;
- alt_quote = *string;
- alt_quote_num = 0;
- while (string && (*string == alt_quote)) {
- string++;
- alt_quote_num++; // Count starting quotes
- }
- word = string;
- len = 0;
- // End of word
- } else if (isspace(*string)) {
- if (len > 0) {
- char *s = faux_str_deesc(word, len);
- faux_str_cat(&result, s);
- faux_str_free(s);
- }
- word = string;
- len = 0;
- break;
- // Escaping
- } else if (*string == '\\') {
- // Skip escaping
- string++;
- len++;
- // Skip escaped symbol
- if (*string) {
- string++;
- len++;
- }
- } else {
- string++;
- len++;
- }
- }
- }
- if (len > 0) {
- if (alt_quoted) {
- faux_str_catn(&result, word, len);
- } else {
- char *s = faux_str_deesc(word, len);
- faux_str_cat(&result, s);
- faux_str_free(s);
- }
- }
- if (saveptr)
- *saveptr = string;
- if (qclosed)
- *qclosed = ! (dbl_quoted || alt_quoted);
- return result;
- }
- /** @brief Indicates is string is empty.
- *
- * @param [in] str String to analyze.
- * @return BOOL_TRUE if pointer is NULL or empty, BOOL_FALSE if not empty.
- */
- bool_t faux_str_is_empty(const char *str)
- {
- if (!str)
- return BOOL_TRUE;
- if ('\0' == *str)
- return BOOL_TRUE;
- return BOOL_FALSE;
- }
- /** @brief Gets line from multiline string.
- *
- * @param [in] str String to analyze.
- * @param [out] saveptr Pointer to the position after found EOL.
- * @return Allocated line or NULL if string is empty.
- */
- char *faux_str_getline(const char *str, const char **saveptr)
- {
- const char *find_pos = NULL;
- const char *eol = "\n\r";
- if (!str)
- return NULL;
- if ('\0' == *str) {
- if (saveptr)
- *saveptr = str;
- return NULL;
- }
- find_pos = faux_str_chars(str, eol);
- if (find_pos) {
- size_t len = find_pos - str;
- char *res = NULL;
- res = faux_zmalloc(len + 1);
- if (len > 0)
- memcpy(res, str, len);
- if (saveptr)
- *saveptr = find_pos + 1;
- return res;
- }
- // Line without EOL
- if (saveptr)
- *saveptr = str + strlen(str);
- return faux_str_dup(str);
- }
- /** @brief Indicates if string has unclosed quotes.
- *
- * @param [in] str String to analyze.
- * @return BOOL_TRUE if string has unclosed quotes, BOOL_FALSE if doesn't.
- */
- bool_t faux_str_unclosed_quotes(const char *str, const char *alt_quotes)
- {
- const char *saveptr = str;
- char *word = NULL;
- if (faux_str_is_empty(str))
- return BOOL_FALSE;
- do {
- bool_t closed_quotes = BOOL_TRUE;
- word = faux_str_nextword(saveptr, &saveptr, alt_quotes, &closed_quotes);
- faux_str_free(word);
- if (!closed_quotes)
- return BOOL_TRUE;
- } while (word);
- return BOOL_FALSE;
- }
- /** @brief Indicates is string has content.
- *
- * Empty string has no content. String contains only spaces is considered
- * doesn't have content too.
- *
- * @param [in] str String to analyze.
- * @return BOOL_TRUE if string has content, BOOL_FALSE if doesn't.
- */
- bool_t faux_str_has_content(const char *str)
- {
- const char *l = str;
- if (faux_str_is_empty(l))
- return BOOL_FALSE;
- while (*l) {
- if (!isspace(*l))
- return BOOL_TRUE;
- l++;
- }
- return BOOL_FALSE;
- }
|