slang_multitext.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (C) 2000,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 slang_mt_content {
  19. struct slang_mt_content *next;
  20. struct slang_text *text;
  21. };
  22. struct slang_multitext {
  23. int nr;
  24. struct slang_mt_content *contents;
  25. };
  26. static struct slang_multitext *slang_mtext_add(struct slang_multitext *, char *);
  27. //static int slang_multitext_expmem(struct slang_multitext *);
  28. static void slang_multitext_free(struct slang_multitext *);
  29. static char *slang_multitext_getrandomtext(struct slang_multitext *);
  30. #ifndef SLANG_NOTYPES
  31. static struct slang_text *slang_multitext_find(struct slang_multitext *, char *);
  32. #endif
  33. #ifndef SLANG_NOGETALL
  34. static char *slang_multitext_get_first(struct slang_multitext *);
  35. static char *slang_multitext_get_next();
  36. #endif
  37. static struct slang_multitext *slang_mtext_add(struct slang_multitext *where, char *text)
  38. {
  39. struct slang_mt_content *oc, *nc;
  40. if (!where) {
  41. where = nmalloc(sizeof(struct slang_multitext));
  42. where->nr = 0;
  43. where->contents = NULL;
  44. }
  45. nc = nmalloc(sizeof(struct slang_mt_content));
  46. nc->next = NULL;
  47. nc->text = slang_text_parse(text);
  48. for (oc = where->contents; oc && oc->next; oc = oc->next);
  49. if (oc) {
  50. Assert(!oc->next);
  51. oc->next = nc;
  52. } else
  53. where->contents = nc;
  54. where->nr++;
  55. return where;
  56. }
  57. /*static int slang_multitext_expmem(struct slang_multitext *what)
  58. {
  59. struct slang_mt_content *content;
  60. int size = 0;
  61. if (!what) {
  62. debug0("WARNING! slang_multitext_expmem() called with NULL pointer!");
  63. return 0;
  64. }
  65. size += sizeof(struct slang_multitext);
  66. for (content = what->contents; content; content = content->next) {
  67. size += sizeof(struct slang_mt_content);
  68. size += slang_text_expmem(content->text);
  69. }
  70. return size;
  71. }*/
  72. static void slang_multitext_free(struct slang_multitext *what)
  73. {
  74. struct slang_mt_content *content, *next;
  75. if (!what) {
  76. debug0("WARNING! slang_multitext_free() called with NULL pointer!");
  77. return;
  78. }
  79. content = what->contents;
  80. while (content) {
  81. next = content->next;
  82. slang_text_free(content->text);
  83. nfree(content);
  84. content = next;
  85. }
  86. nfree(what);
  87. }
  88. static char *slang_multitext_getrandomtext(struct slang_multitext *where)
  89. {
  90. struct slang_mt_content *content;
  91. unsigned long x;
  92. if (!where)
  93. return NULL;
  94. x = random() % where->nr;
  95. for (content = where->contents; content; content = content->next)
  96. if (!x)
  97. return slang_text_get(content->text);
  98. else
  99. x--;
  100. // we should never reach this part
  101. debug0("warning: getrandomtext didn't find anything!");
  102. return NULL;
  103. }
  104. #ifndef SLANG_NOTYPES
  105. static struct slang_text *slang_multitext_find(struct slang_multitext *where, char *what)
  106. {
  107. struct slang_mt_content *content;
  108. Assert(where);
  109. for (content = where->contents; content; content = content->next) {
  110. Assert(content->text);
  111. if (!slang_text_strcasecmp(content->text, what))
  112. return content->text;
  113. }
  114. return NULL;
  115. }
  116. #endif
  117. #ifndef SLANG_NOGETALL
  118. static struct slang_mt_content *glob_mtext_content;
  119. static char *slang_multitext_get_first(struct slang_multitext *where)
  120. {
  121. Assert(where);
  122. glob_mtext_content = where->contents;
  123. if (glob_mtext_content)
  124. return slang_text_get(glob_mtext_content->text);
  125. else
  126. return NULL;
  127. }
  128. static char *slang_multitext_get_next()
  129. {
  130. glob_mtext_content = glob_mtext_content->next;
  131. if (glob_mtext_content)
  132. return slang_text_get(glob_mtext_content->text);
  133. else
  134. return NULL;
  135. }
  136. #endif