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.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. #define GLOBAL_TAGS \
  22. X(TagNone, 0, "<none>") \
  23. X(TagStatus, 1, "<status>") \
  24. X(TagRaw, 2, "<raw>")
  25. enum { GlobalTagsLen = 3 };
  26. #define X(tag, id, name) const struct Tag tag = { id, name };
  27. GLOBAL_TAGS
  28. #undef X
  29. static struct {
  30. char *name[TagsLen];
  31. size_t len;
  32. } tags = {
  33. .name = {
  34. #define X(_, id, name) [id] = name,
  35. GLOBAL_TAGS
  36. #undef X
  37. },
  38. .len = GlobalTagsLen,
  39. };
  40. struct Tag tagFind(const char *name) {
  41. for (size_t id = 0; id < tags.len; ++id) {
  42. if (strcmp(tags.name[id], name)) continue;
  43. return (struct Tag) { id, tags.name[id] };
  44. }
  45. return TagNone;
  46. }
  47. struct Tag tagFor(const char *name) {
  48. struct Tag tag = tagFind(name);
  49. if (tag.id != TagNone.id) return tag;
  50. if (tags.len == TagsLen) return TagStatus;
  51. size_t id = tags.len++;
  52. tags.name[id] = strdup(name);
  53. if (!tags.name[id]) err(EX_OSERR, "strdup");
  54. return (struct Tag) { id, tags.name[id] };
  55. }