Rlist *RlistFromSplitString(const char *string, char sep) /* Splits a string containing a separator like "," into a linked list of separate items, supports escaping separators, e.g. \, */ { if (string == NULL) { return NULL; } Rlist *liststart = NULL; char node[CF_MAXVARSIZE]; int maxlen = strlen(string); CfDebug("SplitStringAsRList(%s)\n", string); for (const char *sp = string; *sp != '\0'; sp++) { if (*sp == '\0' || sp > string + maxlen) { break; } memset(node, 0, CF_MAXVARSIZE); sp += SubStrnCopyChr(node, sp, CF_MAXVARSIZE, sep); RlistAppendScalar(&liststart, node); } return liststart; }
Rlist *RlistFromSplitString(const char *string, char sep) /* Splits a string on a separator - e.g. "," - into a linked list of * separate items. Supports escaping separators - e.g. "\," isn't a * separator, it contributes a simple "," in a list entry. */ { if (string == NULL || string[0] == '\0') { return NULL; } Rlist *liststart = NULL; for (const char *sp = string; *sp != '\0';) { sp += SubStrnCopyChr(&liststart, sp, sep); assert(sp - string <= strlen(string)); if (*sp) { assert(*sp == sep && (sp == string || sp[-1] != '\\')); sp++; } } RlistReverse(&liststart); return liststart; }
/** * Splits the given string into lines. On Windows, both \n and \r\n newlines are * detected. Escaped newlines are respected/ignored too. * * @param detect_crlf whether to try to detect and respect "\r\n" line endings * @return: an #Rlist where items are the individual lines **without** the * trailing newline character(s) * @note: Free the result with RlistDestroy() * @warning: This function doesn't work properly if @string uses "\r\n" newlines * and contains '\r' characters that are not part of any "\r\n" * sequence because it first splits @string on '\r'. */ Rlist *RlistFromStringSplitLines(const char *string, bool detect_crlf) { if (string == NULL || string[0] == '\0') { return NULL; } if (!detect_crlf || (strstr(string, "\r\n") == NULL)) { return RlistFromSplitString(string, '\n'); } /* else we split on '\r' just like RlistFromSplitString() above, but * strip leading '\n' in every chunk, thus effectively split on \r\n. See * the warning in the function's documentation.*/ Rlist *liststart = NULL; for (const char *sp = string; *sp != '\0';) { sp += SubStrnCopyChr(&liststart, sp, '\r', '\n'); assert(sp - string <= strlen(string)); if (*sp) { assert(*sp == '\r' && (sp == string || sp[-1] != '\\')); sp++; } } RlistReverse(&liststart); return liststart; }