diff options
-rw-r--r-- | src/main.c | 21 |
1 files changed, 10 insertions, 11 deletions
@@ -68,10 +68,9 @@ void logmsg(char* msg) { /** * unwrap: - * Poorly written function to grab the text between two delimiters (usually - * quotes). Will traverse the provided string until the first occurence of the - * delimiter is found, and will continue until the closing delimiter is found - * (or end of line). + * Grabs the text between a delimiter (usually a quote). Will traverse the + * provided string until the first occurence of the delimiter is found, and + * will continue until the closing delimiter is found (or end of line). * * @wrapchar Delimiter character wrapping the string * @line Line to search for the wrapped string @@ -80,14 +79,14 @@ void logmsg(char* msg) { * @return Pointer to the buffer */ char* unwrap(char wrapchar, char* line, char* buf) { - int i = 0; - while(line[0] != wrapchar && line[0] != '\0') - line++; - strcpy(buf, line + 1); + char *start = NULL, *end = NULL; - while(buf[i] != wrapchar && buf[i] != '\0') - i++; - buf[i] = '\0'; + start = strchr(line, wrapchar); // Locate first occurrence + start++; // Advance one past it + end = strchr(start, wrapchar); // Locate last occurrence + + strncpy(buf, start, end - start); // Copy to the buffer + buf[end - start] = '\0'; // Null terminate return buf; } |