From dfb6ee80a6a5c7f3c1a8977229b029b406c38654 Mon Sep 17 00:00:00 2001 From: Egor Date: Sun, 22 Oct 2023 21:11:56 +0300 Subject: [PATCH] rename trie_{get,set,unset,free} to map_* --- src/common.h | 4 +- src/irc.c | 116 +++++++++++++++++++++++------------------------ src/main.c | 8 ++-- src/map_dynarr.c | 6 +-- src/map_dynarr.h | 8 ++-- src/net.c | 8 ++-- src/trie.c | 6 +-- src/trie.h | 8 ++-- 8 files changed, 82 insertions(+), 82 deletions(-) diff --git a/src/common.h b/src/common.h index 12da8c8..5f89599 100644 --- a/src/common.h +++ b/src/common.h @@ -95,8 +95,8 @@ struct channel { #define DMODE_NOEXTERNAL 8 #define DMODE_SECRET 16 #define DMODE_TOPICPROT 32 -#define is_chanop(chan, client) (!!trie_get(&(chan)->o, (client)->key)) -#define is_chanvoice(chan, client) (!!trie_get(&(chan)->v, (client)->key)) +#define is_chanop(chan, client) (!!map_get(&(chan)->o, (client)->key)) +#define is_chanvoice(chan, client) (!!map_get(&(chan)->v, (client)->key)) extern map_t client_map; extern map_t channel_map; extern map_t autojoin; diff --git a/src/irc.c b/src/irc.c index 678b67e..6d6f64b 100644 --- a/src/irc.c +++ b/src/irc.c @@ -404,9 +404,9 @@ int processPacket(struct client_data *client, char *source, char *command, int a struct channel *chan = findChannel(args[0]); if (!chan) return err_nosuchchannel(client, args[0]), sendbuf(client), 0; - if (!trie_get(&chan->members, client->key)) + if (!map_get(&chan->members, client->key)) return err_notonchannel(client, chan), sendbuf(client), 0; - if (trie_set(&chan->o, client->key, client, NULL) == -1) + if (map_set(&chan->o, client->key, client, NULL) == -1) abort(); copy_strf(&databuf, ":cia!cia@cia.gov MODE %s +o %s\r\n", chan->name, client->nick); sendbuf_chan(chan); @@ -464,7 +464,7 @@ int processPacket(struct client_data *client, char *source, char *command, int a channel_sendtopic(client, chan, 0); } else { // TODO: membership check, maybe? - if (chan->dmode & DMODE_TOPICPROT && !trie_get(&chan->o, client->key)) + if (chan->dmode & DMODE_TOPICPROT && !map_get(&chan->o, client->key)) return 0; // FIXME: error 482 free(chan->topic); free(chan->topic_who); @@ -502,20 +502,20 @@ int processPacket(struct client_data *client, char *source, char *command, int a if (!chan) return err_nosuchchannel(client, args[1]), sendbuf(client), 0; if (chan->dmode & DMODE_INVITEONLY) { - if (!trie_get(&chan->o, client->key)) + if (!map_get(&chan->o, client->key)) return err_chanoprivsneeded(client, chan), sendbuf(client), 0; } else { - if (!trie_get(&chan->members, client->key)) + if (!map_get(&chan->members, client->key)) return err_notonchannel(client, chan), sendbuf(client), 0; } struct client_data *cl = findClient(args[0]); if (!cl) return err_nosuchnick(client, args[0]), sendbuf(client), 0; - if (trie_get(&chan->members, cl->key)) + if (map_get(&chan->members, cl->key)) return err_useronchannel(client, cl->nick, chan), sendbuf(client), 0; - if (trie_set(&chan->invitees, cl->nick, cl, NULL) == -1) + if (map_set(&chan->invitees, cl->nick, cl, NULL) == -1) abort(); - if (trie_set(&cl->invites, chan->name, chan, NULL) == -1) + if (map_set(&cl->invites, chan->name, chan, NULL) == -1) abort(); rpl_inviting(client, cl, chan); sendbuf(client); @@ -530,9 +530,9 @@ int processPacket(struct client_data *client, char *source, char *command, int a struct channel *chan = findChannel(args[0]); if (!chan) return err_nosuchchannel(client, args[0]), sendbuf(client), 0; - if (!trie_get(&chan->o, client->key)) + if (!map_get(&chan->o, client->key)) return err_chanoprivsneeded(client, chan), sendbuf(client), 0; - if (!trie_get(&chan->members, client->key)) + if (!map_get(&chan->members, client->key)) return err_notonchannel(client, chan), sendbuf(client), 0; char *p = args[1]; do { @@ -566,13 +566,13 @@ int processPacket(struct client_data *client, char *source, char *command, int a if (match_list(client, &chan->listb) && !match_list(client, &chan->liste)) return err_cannotsendtochan(client, chan->name), sendbuf(client), 0; if (chan->dmode & DMODE_INVITEONLY - && !trie_get(&chan->invitees, client->key) && !match_list(client, &chan->listI)) + && !map_get(&chan->invitees, client->key) && !match_list(client, &chan->listI)) return err_cannotsendtochan(client, chan->name), sendbuf(client), 0; - if (chan->dmode & DMODE_NOEXTERNAL && !trie_get(&chan->members, client->key)) + if (chan->dmode & DMODE_NOEXTERNAL && !map_get(&chan->members, client->key)) return err_cannotsendtochan(client, chan->name), sendbuf(client), 0; - if (chan->dmode & DMODE_MODERATED && !trie_get(&chan->v, client->key) && !trie_get(&chan->o, client->key)) + if (chan->dmode & DMODE_MODERATED && !map_get(&chan->v, client->key) && !map_get(&chan->o, client->key)) return err_cannotsendtochan(client, chan->name), sendbuf(client), 0; - if (!trie_get(&chan->v, client->key) && !trie_get(&chan->o, client->key) && + if (!map_get(&chan->v, client->key) && !map_get(&chan->o, client->key) && (strstr(args[1], "irc.supernets.org") || strstr(args[1], "IRC.SUPERNETS.ORG"))) { msg_quit(client, "Quit: eel bearing atlantean", ""); sendbuf_visible(client); @@ -734,7 +734,7 @@ int processPacket(struct client_data *client, char *source, char *command, int a struct channel *chan = findChannel(nick); if (!chan) return err_nosuchchannel(client, nick), sendbuf(client), 0; - if (!trie_get(&chan->o, client->key)) + if (!map_get(&chan->o, client->key)) return 0; // TODO: error if (argc == 2) { // Type A channel modes struct trie_find *f = NULL; @@ -921,7 +921,7 @@ int processPacket(struct client_data *client, char *source, char *command, int a if (strchr(mask, ' ')) break; // TODO: error msg stringlower(mask); - if (trie_set(list, mask, is_plus ? mask /* dummy */ : NULL, NULL) == -1) + if (map_set(list, mask, is_plus ? mask /* dummy */ : NULL, NULL) == -1) abort(); char which[2] = {*p, 0}; copy_strf(&databuf, ":%s!%s@%s MODE %s %s%s %s\r\n", @@ -942,11 +942,11 @@ int processPacket(struct client_data *client, char *source, char *command, int a err_nosuchnick(client, nick), sendbuf(client); break; } - if (!trie_get(&chan->members, cl->key)) { + if (!map_get(&chan->members, cl->key)) { err_usernotinchannel(client, cl->nick, chan), sendbuf(client); break; } - if (trie_set(*p == 'o' ? &chan->o : &chan->v, cl->key, is_plus ? cl : NULL, NULL) == -1) + if (map_set(*p == 'o' ? &chan->o : &chan->v, cl->key, is_plus ? cl : NULL, NULL) == -1) abort(); copy_strf(&databuf, ":%s!%s@%s MODE %s %s%s %s\r\n", client->nick, client->username, client->hostname, @@ -971,7 +971,7 @@ int processPacket(struct client_data *client, char *source, char *command, int a if (trie_findfirst(&f, &channel_map) == 1) do { struct channel *chan = f->n->value; - if (!(chan->dmode & DMODE_SECRET) || trie_get(&chan->members, client->key)) + if (!(chan->dmode & DMODE_SECRET) || map_get(&chan->members, client->key)) rpl_list(client, chan), sendbuf(client); } while(trie_findnext(&f) == 1); rpl_listend(client), sendbuf(client); @@ -986,7 +986,7 @@ int processPacket(struct client_data *client, char *source, char *command, int a if ((p = strchr(channel, ','))) *p++ = '\0'; struct channel *chan = findChannel(channel); - if (chan && (!(chan->dmode & DMODE_SECRET) || trie_get(&chan->members, client->key))) { + if (chan && (!(chan->dmode & DMODE_SECRET) || map_get(&chan->members, client->key))) { channel_sendnames(client, chan); } else { rpl_endofnames(client, channel); @@ -1058,7 +1058,7 @@ int processPacket(struct client_data *client, char *source, char *command, int a if (trie_findfirst(&f, &cl->channels) == 1) do { struct channel *chan = f->n->value; - if ((cl->umode & UMODE_INVISIBLE || chan->dmode & DMODE_SECRET) && !trie_get(&chan->members, client->key)) + if ((cl->umode & UMODE_INVISIBLE || chan->dmode & DMODE_SECRET) && !map_get(&chan->members, client->key)) continue; rpl_whoischannels(client, cl, chan), sendbuf(client); } while (trie_findnext(&f) == 1); @@ -1208,7 +1208,7 @@ void setNick(struct client_data *client, char* nickname) { memcpy(key, nickname, nicklen+1); stringlower(key); - if (trie_get(&client_map, key)) { + if (map_get(&client_map, key)) { err_nicknameinuse(client, nickname), sendbuf(client); return; } @@ -1218,22 +1218,22 @@ void setNick(struct client_data *client, char* nickname) { msg_nick(client, nickname); sendbuf(client); sendbuf_visible(client); - trie_unset(&client_map, client->key, NULL); + map_unset(&client_map, client->key, NULL); struct trie_find *f = NULL; if (trie_findfirst(&f, &client->channels) == 1) do { struct channel *chan = f->n->value; - trie_unset(&chan->members, client->key, NULL); - if (trie_set(&chan->members, key, client, NULL) == -1) + map_unset(&chan->members, client->key, NULL); + if (map_set(&chan->members, key, client, NULL) == -1) abort(); void *old; - trie_unset(&chan->o, client->key, &old); + map_unset(&chan->o, client->key, &old); if (old) - if (trie_set(&chan->o, key, client, NULL) == -1) + if (map_set(&chan->o, key, client, NULL) == -1) abort(); - trie_unset(&chan->v, client->key, &old); + map_unset(&chan->v, client->key, &old); if (old) - if (trie_set(&chan->v, key, client, NULL) == -1) + if (map_set(&chan->v, key, client, NULL) == -1) abort(); } while(trie_findnext(&f) == 1); free(client->nick); @@ -1251,7 +1251,7 @@ void setNick(struct client_data *client, char* nickname) { client->key = mystrdup(key); if (!client->key) abort(); - if (trie_set(&client_map, client->key, client, 0) == -1) + if (map_set(&client_map, client->key, client, 0) == -1) abort(); return; @@ -1296,7 +1296,7 @@ void channel_join(struct client_data *client, const char *channel, const char *c char key[33]; memcpy(key, channel, chanlen+1); stringlower(key); - struct channel *chan = trie_get(&channel_map, key); + struct channel *chan = map_get(&channel_map, key); int newchan = !chan; if (!chan) { chan = malloc(sizeof(struct channel)); @@ -1309,12 +1309,12 @@ void channel_join(struct client_data *client, const char *channel, const char *c if (!chan->key) abort(); chan->name = mystrdup(channel); if (!chan->name) abort(); - if (trie_set(&channel_map, key, chan, NULL) == -1) + if (map_set(&channel_map, key, chan, NULL) == -1) abort(); - if (trie_set(&chan->o, client->key, client, NULL) == -1) + if (map_set(&chan->o, client->key, client, NULL) == -1) abort(); } else { - if (trie_get(&chan->members, client->key)) + if (map_get(&chan->members, client->key)) return; if (chan->chankey && (!chankey || strcmp(chan->chankey, chankey) == 0)) return err_badchannelkey(client, chan), sendbuf(client); @@ -1323,12 +1323,12 @@ void channel_join(struct client_data *client, const char *channel, const char *c if (match_list(client, &chan->listb) && !match_list(client, &chan->liste)) return err_bannedfromchan(client, chan), sendbuf(client); if (chan->dmode & DMODE_INVITEONLY - && !trie_get(&chan->invitees, client->key) && !match_list(client, &chan->listI)) + && !map_get(&chan->invitees, client->key) && !match_list(client, &chan->listI)) return err_inviteonlychan(client, chan), sendbuf(client); } - if (trie_set(&chan->members, client->key, client, NULL) == -1) + if (map_set(&chan->members, client->key, client, NULL) == -1) abort(); - if (trie_set(&client->channels, key, chan, NULL) == -1) + if (map_set(&client->channels, key, chan, NULL) == -1) abort(); chan->member_count++; // send JOIN to everyone @@ -1346,13 +1346,13 @@ void channel_join(struct client_data *client, const char *channel, const char *c // TODO: send channel modes to the joining client // apply +w modes if (match_list(client, &chan->listwo)) { - if (trie_set(&chan->o, client->key, client, NULL) == -1) + if (map_set(&chan->o, client->key, client, NULL) == -1) abort(); copy_strf(&databuf, ":cia!cia@cia.gov MODE %s +o %s\r\n", chan->name, client->nick); sendbuf_chan(chan); } if (match_list(client, &chan->listwv)) { - if (trie_set(&chan->v, client->key, client, NULL) == -1) + if (map_set(&chan->v, client->key, client, NULL) == -1) abort(); copy_strf(&databuf, ":cia!cia@cia.gov MODE %s +v %s\r\n", chan->name, client->nick); sendbuf_chan(chan); @@ -1364,7 +1364,7 @@ void channel_join(struct client_data *client, const char *channel, const char *c } void channel_part(struct client_data *client, struct channel *chan, const char *reason, struct client_data *kicker) { - if (!trie_get(&chan->members, client->key)) + if (!map_get(&chan->members, client->key)) return; if (kicker) { msg_kick(kicker, client, chan, reason); @@ -1373,37 +1373,37 @@ void channel_part(struct client_data *client, struct channel *chan, const char * msg_part(client, chan, *reason ? reason : NULL); sendbuf_chan(chan); } - trie_unset(&client->channels, chan->key, NULL); - trie_unset(&chan->members, client->key, NULL); - trie_unset(&chan->o, client->key, NULL); - trie_unset(&chan->v, client->key, NULL); + map_unset(&client->channels, chan->key, NULL); + map_unset(&chan->members, client->key, NULL); + map_unset(&chan->o, client->key, NULL); + map_unset(&chan->v, client->key, NULL); chan->member_count--; if (!chan->member_count && !(chan->dmode & DMODE_PERSIST)) channel_kill(chan); } void channel_kill(struct channel *chan) { - trie_unset(&channel_map, chan->key, NULL); + map_unset(&channel_map, chan->key, NULL); struct trie_find *f = NULL; if (trie_findfirst(&f, &chan->invitees) == 1) do { struct client_data *cl = f->n->value; - trie_unset(&cl->invites, chan->key, NULL); + map_unset(&cl->invites, chan->key, NULL); } while(trie_findnext(&f) == 1); free(chan->key); free(chan->name); free(chan->topic); free(chan->topic_who); - trie_free(&chan->members); - trie_free(&chan->o); - trie_free(&chan->v); - trie_free(&chan->listb); - trie_free(&chan->liste); - trie_free(&chan->listI); - trie_free(&chan->listq); - trie_free(&chan->listwo); - trie_free(&chan->listwv); - trie_free(&chan->invitees); + map_free(&chan->members); + map_free(&chan->o); + map_free(&chan->v); + map_free(&chan->listb); + map_free(&chan->liste); + map_free(&chan->listI); + map_free(&chan->listq); + map_free(&chan->listwo); + map_free(&chan->listwv); + map_free(&chan->invitees); free(chan->chankey); free(chan); } @@ -1415,7 +1415,7 @@ struct client_data *findClient(const char *nickname) return NULL; memcpy(key, nickname, len+1); stringlower(key); - return trie_get(&client_map, key); + return map_get(&client_map, key); } struct channel *findChannel(const char *channel) { @@ -1425,5 +1425,5 @@ struct channel *findChannel(const char *channel) char key[33]; memcpy(key, channel, chanlen+1); stringlower(key); - return trie_get(&channel_map, key); + return map_get(&channel_map, key); } diff --git a/src/main.c b/src/main.c index 2d03cf3..c40f815 100644 --- a/src/main.c +++ b/src/main.c @@ -242,7 +242,7 @@ int main(int argc, char *argv[]) } else if(!strcmp(argv[i], "--autojoin")) { if (i + 1 >= argc) return fprintf(stderr, "not enough parameters\n"), 1; - if (trie_set(&autojoin, argv[i+1], (void*)1, NULL) == -1) + if (map_set(&autojoin, argv[i+1], (void*)1, NULL) == -1) abort(); i += 1; } else { @@ -427,9 +427,9 @@ int main(int argc, char *argv[]) } free(f); } - trie_free(&client_map); - trie_free(&channel_map); - trie_free(&autojoin); + map_free(&client_map); + map_free(&channel_map); + map_free(&autojoin); if (fchanlog) fclose(fchanlog); return 0; diff --git a/src/map_dynarr.c b/src/map_dynarr.c index 06fa881..55c0c5b 100644 --- a/src/map_dynarr.c +++ b/src/map_dynarr.c @@ -20,13 +20,13 @@ static int ent_find(map_t *root, const char *key, int *found) *found = 0; return lo; } -void *trie_get(map_t *root, const char *key) +void *map_get(map_t *root, const char *key) { int found; int i = ent_find(root, key, &found); return found ? root->ptr[i].value : 0; } -int trie_set(map_t *root, const char *key, const void *value, void **oldvalue) +int map_set(map_t *root, const char *key, const void *value, void **oldvalue) { int found; int i = ent_find(root, key, &found); @@ -65,7 +65,7 @@ int trie_set(map_t *root, const char *key, const void *value, void **oldvalue) } return 0; } -void trie_free(map_t *root) +void map_free(map_t *root) { while (root->len) { free(root->ptr[--root->len].key); diff --git a/src/map_dynarr.h b/src/map_dynarr.h index e166dba..be06882 100644 --- a/src/map_dynarr.h +++ b/src/map_dynarr.h @@ -19,10 +19,10 @@ struct map_dynarr { size_t len, cap; }; typedef struct map_dynarr map_t; -void *trie_get(map_t *root, const char *key); -NODISCARD int trie_set(map_t *root, const char *key, const void *value, void **oldvalue); -#define trie_unset(root, key, oldvalue) ((void)trie_set((root), (key), 0, (oldvalue))) -void trie_free(map_t *root); +void *map_get(map_t *root, const char *key); +NODISCARD int map_set(map_t *root, const char *key, const void *value, void **oldvalue); +#define map_unset(root, key, oldvalue) ((void)map_set((root), (key), 0, (oldvalue))) +void map_free(map_t *root); struct trie_find { char *key; struct { void *value; } *n, _n; diff --git a/src/net.c b/src/net.c index e04e1ee..27b718e 100644 --- a/src/net.c +++ b/src/net.c @@ -72,16 +72,16 @@ void killConnection(struct client_data *cl, int byChoice) free(f); /* Remove from client_map and get rid of the pending invites */ if (cl->key) { - trie_unset(&client_map, cl->key, NULL); + map_unset(&client_map, cl->key, NULL); struct trie_find *f = NULL; if (trie_findfirst(&f, &cl->invites) == 1) do { struct channel *chan = f->n->value; - trie_unset(&chan->invitees, cl->key, NULL); + map_unset(&chan->invitees, cl->key, NULL); } while(trie_findnext(&f) == 1); } - trie_free(&cl->channels); - trie_free(&cl->invites); + map_free(&cl->channels); + map_free(&cl->invites); killConnectionPart2(cl); } void killConnectionPart2(struct client_data *cl) diff --git a/src/trie.c b/src/trie.c index 6443d38..d9b2f75 100644 --- a/src/trie.c +++ b/src/trie.c @@ -26,7 +26,7 @@ static int ent_find(struct trie_node *n, int key) return lo; } // get value from trie O(n log s) -void *trie_get(map_t *root, const char *key) +void *map_get(map_t *root, const char *key) { struct trie_node *n = *root; if (!n) @@ -41,7 +41,7 @@ void *trie_get(map_t *root, const char *key) return n->value; } // set value in trie O(ns) -int trie_set(map_t *root, const char *key, const void *value, void **oldvalue) +int map_set(map_t *root, const char *key, const void *value, void **oldvalue) { int rv = 0; if (oldvalue) @@ -121,7 +121,7 @@ cleanup: return rv; } // free a trie -void trie_free(map_t *root) { +void map_free(map_t *root) { struct trie_node *n = *root; while (n) { if (n->len) { diff --git a/src/trie.h b/src/trie.h index 71b36dd..20a26d8 100644 --- a/src/trie.h +++ b/src/trie.h @@ -24,12 +24,12 @@ struct trie_node { }; typedef struct trie_node *map_t; // get value from trie O(n log s) -void *trie_get(map_t *root, const char *key); +void *map_get(map_t *root, const char *key); // set value in trie O(ns) -NODISCARD int trie_set(map_t *root, const char *key, const void *value, void **oldvalue); -#define trie_unset(root, key, oldvalue) ((void)trie_set((root), (key), 0, (oldvalue))) +NODISCARD int map_set(map_t *root, const char *key, const void *value, void **oldvalue); +#define map_unset(root, key, oldvalue) ((void)map_set((root), (key), 0, (oldvalue))) // free a trie -void trie_free(map_t *root); +void map_free(map_t *root); struct trie_find { struct trie_node *n; size_t len; -- 2.56.0