slang_facts_places.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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_facts_place {
  19. struct slang_facts_place *next;
  20. int place;
  21. struct slang_multitext *mtext;
  22. };
  23. static struct slang_facts_place *slang_facts_place_add(struct slang_facts_place *, int, char *);
  24. //static int slang_facts_place_expmem(struct slang_facts_place *);
  25. static void slang_facts_place_free(struct slang_facts_place *);
  26. //static char *slang_facts_place_getfirst(struct slang_facts_place *);
  27. //static char *slang_facts_place_getnext();
  28. static char *slang_facts_place_get(struct slang_facts_place *, int, int);
  29. static struct slang_facts_place *slang_facts_place_add(struct slang_facts_place *where, int place, char *text)
  30. {
  31. struct slang_facts_place *newitem, *target;
  32. newitem = NULL;
  33. if (where) {
  34. for (newitem = where; newitem; newitem = newitem->next)
  35. if (newitem->place == place)
  36. break;
  37. }
  38. if (!newitem) {
  39. newitem = nmalloc(sizeof(struct slang_facts_place));
  40. newitem->place = place;
  41. newitem->mtext = NULL;
  42. newitem->next = NULL;
  43. for (target = where; target && target->next; target = target->next);
  44. if (target)
  45. target->next = newitem;
  46. else
  47. where = newitem;
  48. }
  49. newitem->mtext = slang_mtext_add(newitem->mtext, text);
  50. return where;
  51. }
  52. /*static int slang_facts_place_expmem(struct slang_facts_place *what)
  53. {
  54. int size = 0;
  55. for (; what; what = what->next) {
  56. size += sizeof(struct slang_facts_place);
  57. size += slang_multitext_expmem(what->mtext);
  58. }
  59. return size;
  60. }*/
  61. static void slang_facts_place_free(struct slang_facts_place *what)
  62. {
  63. struct slang_facts_place *next;
  64. while (what) {
  65. next = what->next;
  66. slang_multitext_free(what->mtext);
  67. nfree(what);
  68. what = next;
  69. }
  70. }
  71. /*
  72. static struct slang_facts_place *glob_fact_place;
  73. static char *slang_facts_place_getfirst(struct slang_facts_place *where)
  74. {
  75. int itype, pitype;
  76. locstats *ls;
  77. if (!glob_globstats || !glob_fact)
  78. return 0;
  79. if (!glob_globstats->local)
  80. return 0;
  81. itype = glob_fact->sorting;
  82. pitype = (itype * -1) + (TOTAL_TYPES - 1);
  83. glob_sorting = itype;
  84. for (glob_fact_place = what; glob_fact_place; glob_fact_place = glob_fact_place->next) {
  85. glob_place = 0;
  86. for (ls = glob_globstats->slocal[S_TODAY][pitype]; ls; ls = ls->snext[S_TODAY][pitype]) {
  87. // skip this fact if the value seems to be 0
  88. if (itype >= 0) {
  89. if (!glob_globstats->slocal[S_TODAY][itype]->values[S_TODAY][itype])
  90. break;
  91. } else {
  92. if ((itype == T_WPL) && (!ls->values[S_DAILY][T_WORDS]
  93. || !ls->values[S_DAILY][T_LINES]))
  94. break;
  95. else if ((itype == T_IDLE) && (!ls->values[S_DAILY][T_MINUTES]
  96. || !ls->values[S_DAILY][T_LINES]))
  97. break;
  98. else if ((itype == T_VOCABLES) && !ls->vocables)
  99. break;
  100. }
  101. glob_place++;
  102. if (glob_place == glob_facts_place->place) {
  103. glob_locstats = ls;
  104. return slang_multitext_get(glob_facts_place->mtext);
  105. }
  106. }
  107. }
  108. return NULL;
  109. }
  110. static char *slang_facts_place_getnext()
  111. {
  112. int itype, pitype;
  113. locstats *ls;
  114. if (!glob_globstats || !glob_fact)
  115. return 0;
  116. if (!glob_globstats->local)
  117. return 0;
  118. itype = glob_fact->sorting;
  119. pitype = (itype * -1) + (TOTAL_TYPES - 1);
  120. glob_sorting = itype;
  121. for (; glob_fact_place; glob_fact_place = glob_fact_place->next) {
  122. glob_place = 0;
  123. for (ls = glob_globstats->slocal[S_TODAY][pitype]; ls; ls = ls->snext[S_TODAY][pitype]) {
  124. // skip this fact if the value seems to be 0
  125. if (itype >= 0) {
  126. if (!glob_globstats->slocal[S_TODAY][itype]->values[S_TODAY][itype])
  127. break;
  128. } else {
  129. if ((itype == T_WPL) && (!ls->values[S_DAILY][T_WORDS]
  130. || !ls->values[S_DAILY][T_LINES]))
  131. break;
  132. else if ((itype == T_IDLE) && (!ls->values[S_DAILY][T_MINUTES]
  133. || !ls->values[S_DAILY][T_LINES]))
  134. break;
  135. else if ((itype == T_VOCABLES) && !ls->vocables)
  136. break;
  137. }
  138. glob_place++;
  139. if (glob_place == glob_facts_place->place) {
  140. glob_locstats = ls;
  141. return slang_multitext_get(glob_facts_place->mtext);
  142. }
  143. }
  144. }
  145. return NULL;
  146. }
  147. */
  148. static char *slang_facts_place_get(struct slang_facts_place *where, int itype, int place)
  149. {
  150. struct slang_facts_place *fp;
  151. locstats *ls;
  152. int pitype;
  153. if (!glob_globstats || !place)
  154. return NULL;
  155. if (itype < 0)
  156. pitype = (itype * -1) + (TOTAL_TYPES - 1);
  157. else
  158. pitype = itype;
  159. glob_sorting = itype;
  160. for (fp = where; fp; fp = fp->next) {
  161. if (fp->place == place) {
  162. glob_place = 0;
  163. for (ls = glob_globstats->slocal[S_TODAY][pitype]; ls; ls = ls->snext[S_TODAY][pitype]) {
  164. if (itype >= 0) {
  165. if (!glob_globstats->slocal[S_TODAY][itype]->values[S_TODAY][itype])
  166. return NULL;
  167. } else {
  168. if ((itype == T_WPL) && (!ls->values[S_DAILY][T_WORDS]
  169. || !ls->values[S_DAILY][T_LINES]))
  170. return NULL;
  171. else if ((itype == T_IDLE) && (!ls->values[S_DAILY][T_MINUTES]
  172. || !ls->values[S_DAILY][T_LINES]))
  173. return NULL;
  174. else if ((itype == T_VOCABLES) && !ls->vocables)
  175. return NULL;
  176. }
  177. glob_place++;
  178. if (glob_place == fp->place) {
  179. glob_locstats = ls;
  180. glob_timerange = S_TODAY;
  181. glob_toptype = itotype(itype);
  182. return slang_multitext_getrandomtext(fp->mtext);
  183. }
  184. }
  185. }
  186. }
  187. return NULL;
  188. }