llists.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (C) 2001 Florian Sander
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. struct llist_2string {
  19. struct llist_2string *next;
  20. char *s1;
  21. char *s2;
  22. };
  23. static struct llist_2string *llist_2string_add(struct llist_2string *where, char *s1, char *s2)
  24. {
  25. struct llist_2string *newitem;
  26. newitem = (struct llist_2string *) nmalloc(sizeof(struct llist_2string));
  27. newitem->next = NULL;
  28. newitem->s1 = (char *) nmalloc(strlen(s1) + 1);
  29. strcpy(newitem->s1, s1);
  30. newitem->s2 = (char *) nmalloc(strlen(s2) + 1);
  31. strcpy(newitem->s2, s2);
  32. newitem->next = where;
  33. return newitem;
  34. }
  35. static int llist_2string_expmem(struct llist_2string *what)
  36. {
  37. int size = 0;
  38. while (what) {
  39. size += sizeof(struct llist_2string);
  40. size += strlen(what->s1) + 1;
  41. size += strlen(what->s2) + 1;
  42. what = what->next;
  43. }
  44. return size;
  45. }
  46. static void llist_2string_free(struct llist_2string *what)
  47. {
  48. struct llist_2string *next;
  49. while (what) {
  50. next = what->next;
  51. nfree(what->s1);
  52. nfree(what->s2);
  53. nfree(what);
  54. what = next;
  55. }
  56. }
  57. static char *llist_2string_get_s2(struct llist_2string *where, char *s1)
  58. {
  59. for (; where; where = where->next)
  60. if (!strcasecmp(where->s1, s1))
  61. return where->s2;
  62. return NULL;
  63. }
  64. /******************************/
  65. struct llist_1string {
  66. struct llist_1string *next;
  67. char *s1;
  68. };
  69. static struct llist_1string *llist_1string_add(struct llist_1string *where, char *s1)
  70. {
  71. struct llist_1string *newitem, *target;
  72. newitem = nmalloc(sizeof(struct llist_1string));
  73. newitem->s1 = nmalloc(strlen(s1) + 1);
  74. strcpy(newitem->s1, s1);
  75. newitem->next = NULL;
  76. target = where;
  77. while (target && target->next)
  78. target = target->next;
  79. if (target)
  80. target->next = newitem;
  81. else
  82. return newitem;
  83. return where;
  84. }
  85. static int llist_1string_expmem(struct llist_1string *what)
  86. {
  87. int size = 0;
  88. while (what) {
  89. size += sizeof(struct llist_1string);
  90. size += strlen(what->s1) + 1;
  91. what = what->next;
  92. }
  93. return size;
  94. }
  95. static void llist_1string_free(struct llist_1string *what)
  96. {
  97. struct llist_1string *next;
  98. while (what) {
  99. next = what->next;
  100. nfree(what->s1);
  101. nfree(what);
  102. what = next;
  103. }
  104. }