IRC client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tag.c 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* Copyright (C) 2018 C. McEnroe <june@causal.agency>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <err.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sysexits.h>
  20. #include "chat.h"
  21. static struct {
  22. char *name[TagsLen];
  23. size_t len;
  24. } tags = {
  25. .name = { "<none>", "<status>", "<raw>" },
  26. .len = 3,
  27. };
  28. const struct Tag TagNone = { 0, "<none>" };
  29. const struct Tag TagStatus = { 1, "<status>" };
  30. const struct Tag TagRaw = { 2, "<raw>" };
  31. struct Tag tagFind(const char *name) {
  32. for (size_t id = 0; id < tags.len; ++id) {
  33. if (strcmp(tags.name[id], name)) continue;
  34. return (struct Tag) { id, tags.name[id] };
  35. }
  36. return TagNone;
  37. }
  38. struct Tag tagFor(const char *name) {
  39. struct Tag tag = tagFind(name);
  40. if (tag.id != TagNone.id) return tag;
  41. if (tags.len == TagsLen) return TagStatus;
  42. size_t id = tags.len++;
  43. tags.name[id] = strdup(name);
  44. if (!tags.name[id]) err(EX_OSERR, "strdup");
  45. return (struct Tag) { id, tags.name[id] };
  46. }