diff options
Diffstat (limited to 'functions.c')
-rw-r--r-- | functions.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/functions.c b/functions.c index 97f8629..86a1f03 100644 --- a/functions.c +++ b/functions.c @@ -50,3 +50,80 @@ void appendcrlf(char *string) { string[startlen + 1] = '\n'; // Add LF string[startlen + 2] = '\0'; // Finish with null terminator } + +// Remove leading colon ':' which is the starting character of a prefix in an IRC message +void stripprefix(char *string) { + // Make a copy to work with + char string2[strlen(string)]; + + printf("stripprefix(): starting with '%s', strlen: %zd.\n", string, strlen(string)); + + // Don't bother if this isn't a prefix with a leading ':' + if (string[0] != ':') { + printf("stripprefix(): no leading ':', returning.\n"); + return; + } + + // Copy the old string into a new one, but... + for (size_t i = 1; i < strlen(string); i++) { + printf("i: %zd. strlen: %zd.\n", i, strlen(string)); + string2[i - 1] = string[i]; + } + + // Copy result back to original string + strncpy(string, string2, strlen(string) - 1); + + // Finish with null terminator + string[strlen(string) - 1] = '\0'; + + printf("stripprefix(): finishing with '%s', strlen: %zd.\n", string, strlen(string)); +} + +// Extract final parameter from IRC message, removing the leading colon ':' +// e.g. given this string: +// ":irc.tghost.co.uk 332 blabounce #test :This is a test topic!" +// We want to end up with: +// "This is a test topic!" +void extractfinalparameter(char *string) { + // Make a copy to work with + char string2[strlen(string)]; + + // Position of colon + int colonpos = -1; + + printf("extractfinalparameter(): starting with '%s', strlen: %zd.\n", string, strlen(string)); + + // Strip the colon at position 0 if there is one + stripprefix(string); + + // Find the colon + for (size_t i = 0; i < strlen(string); i++) { + printf("i: %zd. strlen: %zd.\n", i, strlen(string)); + if (string[i] == ':') { + printf("Found colon at position %zd!\n", i); + colonpos = i; + break; + } + } + + if (colonpos == -1) { + printf("no colon found, returning\n"); + return; + } + + // Build a new string starting from the next position after the colon + int counter = 0; + for (size_t i = colonpos + 1; i < strlen(string); i++) { + printf("i: %zd. strlen: %zd.\n", i, strlen(string)); + string2[counter] = string[i]; + counter++; + } + + // Copy result back to original string + strncpy(string, string2, counter); + + // Finish with null terminator + string[counter] = '\0'; + + printf("extractfinalparameter(): finishing with '%s', strlen: %zd.\n", string, strlen(string)); +} |