Add some more preprocessor stuff and consider CRLF in tokenizing/assembly

This commit is contained in:
Alex D. 2021-02-23 22:43:25 +00:00
parent bb6be1d8ec
commit 4ccfc377cd
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
3 changed files with 32 additions and 9 deletions

View File

@ -23,15 +23,19 @@
#ifndef UIRC_GUARD_PUBLIC_MEMORY
#define UIRC_GUARD_PUBLIC_MEMORY
#ifdef UIRC_FEATURE_IRCV3
IRC_Tag* uirc_malloc_tag(const char* key, const char* value);
IRC_Capability* uirc_malloc_capability(const char* name);
IRC_User* uirc_malloc_user(const char* nick, const char* user, const char* real, const char* host);
IRC_Message* uirc_malloc_message(const char* command, ...);
IRC_Buffer* uirc_malloc_buffer(const char* name, const char* topic, const char* key);
IRC_Network* uirc_malloc_network(const char* addr, const char* svc, const char* pass, const char* quitmsg);
#endif /* UIRC_FEATURE_IRCV3 */
IRC_User* uirc_malloc_user(const char* nick, const char* user, const char* real, const char* host);
IRC_Message* uirc_malloc_message(const char* command, ...);
IRC_Buffer* uirc_malloc_buffer(const char* name, const char* topic, const char* key);
IRC_Network* uirc_malloc_network(const char* addr, const char* svc, const char* pass, const char* quitmsg);
#ifdef UIRC_FEATURE_IRCV3
void uirc_free_tag(IRC_Tag* t);
void uirc_free_capability(IRC_Capability* c);
#endif /* UIRC_FEATURE_IRCV3 */
void uirc_free_user(IRC_User* u);
void uirc_free_message(IRC_Message* m);
void uirc_free_buffer(IRC_Buffer* b);

View File

@ -79,14 +79,21 @@ uirc_assembler_message(char* buf, const IRC_Message* m, size_t len)
for (unsigned int i = 0; i < IRC_MAXARGS && m->args[i] != NULL; i++) {
if (len < 2) return -1;
int ret;
if ((ret = snprintf(buf, len, (i + 1 == IRC_MAXARGS || (m->args[i + 1] == NULL && m->trailing)) ? ":%s" : "%s ", m->args[i])) >= 0) {
if ((ret = snprintf(buf, len, (i < IRC_MAXARGS - 1 && m->args[i + 1] == NULL && m->trailing) ? ":%s" : "%s ", m->args[i])) >= 0) {
buf += (size_t) ret;
len -= (size_t) ret;
} else
return -1;
}
assert(sv <= buf);
printf("%li\n", buf - sv);
int ret;
if ((ret = snprintf(buf, len, "\r\n")) >= 0) {
buf += (size_t) ret;
len -= (size_t) ret;
} else
return -1;
assert(sv <= buf);
return (ssize_t)(buf - sv);
}

View File

@ -35,9 +35,20 @@ uirc_tokenizer_message(const char* str)
{
assert(str != NULL);
IRC_Message* m = malloc(sizeof(IRC_Message));
char* const ws = malloc_string(str, strlen(str));
char* p = ws;
IRC_Message* m = malloc(sizeof(IRC_Message));
size_t len = strlen(str);
// Ignore CRLF at the end, irrelevant for parsing
// NOTE: len can be used directly as it's the lenght of the string without terminating null byte
// this is also the same as the last element in a array (off by one)
if (str[len] == '\n') {
len--;
if (str[len] == '\r') len--;
}
char* const ws = malloc_string(str, len); // NOTE: Some compilers might warn you about modifying contents of p because it points to ws which
// is "immutable". This is safe to ignore and it's purpose is to not use ws for tokenizing
char* p = ws;
if (ws == NULL || m == NULL) {
free(ws);
@ -101,6 +112,7 @@ uirc_tokenizer_message(const char* str)
}
m->args[i] = NULL;
}
free(ws);
return m;
}