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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* Copyright (C) 2018 Curtis 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 NONE "-none-"
  22. #define STATUS "-status-"
  23. #define RAW "-raw-"
  24. const struct Tag TagNone = { 0, NONE };
  25. const struct Tag TagStatus = { 1, STATUS };
  26. const struct Tag TagRaw = { 2, RAW };
  27. static struct {
  28. char *name[TagsLen];
  29. size_t len;
  30. } tags = {
  31. .name = { NONE, STATUS, RAW },
  32. .len = 3,
  33. };
  34. struct Tag tagFind(const char *name) {
  35. for (size_t id = 0; id < tags.len; ++id) {
  36. if (strcmp(tags.name[id], name)) continue;
  37. return (struct Tag) { id, tags.name[id] };
  38. }
  39. return TagNone;
  40. }
  41. struct Tag tagFor(const char *name) {
  42. struct Tag tag = tagFind(name);
  43. if (tag.id != TagNone.id) return tag;
  44. if (tags.len == TagsLen) return TagStatus;
  45. size_t id = tags.len++;
  46. tags.name[id] = strdup(name);
  47. if (!tags.name[id]) err(EX_OSERR, "strdup");
  48. return (struct Tag) { id, tags.name[id] };
  49. }