class.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2020 Igor van den Hoven *
  5. * *
  6. * TinTin++ is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  18. ******************************************************************************/
  19. /******************************************************************************
  20. * T I N T I N + + *
  21. * *
  22. * coded by Igor van den Hoven 2004 *
  23. ******************************************************************************/
  24. #include "tintin.h"
  25. DO_COMMAND(do_class)
  26. {
  27. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE], arg3[BUFFER_SIZE];
  28. int i;
  29. struct listroot *root;
  30. struct listnode *node;
  31. root = ses->list[LIST_CLASS];
  32. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  33. arg = sub_arg_in_braces(ses, arg, arg2, GET_ONE, SUB_VAR|SUB_FUN);
  34. arg = sub_arg_in_braces(ses, arg, arg3, GET_ONE, SUB_VAR|SUB_FUN);
  35. if (*arg1 == 0)
  36. {
  37. tintin_header(ses, " CLASSES ");
  38. for (root->update = 0 ; root->update < root->used ; root->update++)
  39. {
  40. node = root->list[root->update];
  41. tintin_printf2(ses, "%-20s %4d %6s %6s %3d", node->arg1, count_class(ses, node), !strcmp(ses->group, node->arg1) ? "ACTIVE" : "", atoi(node->arg3) ? "OPEN" : "CLOSED", atoi(node->arg3));
  42. }
  43. }
  44. else if (*arg2 == 0)
  45. {
  46. class_list(ses, NULL, arg1, arg2);
  47. }
  48. else
  49. {
  50. for (i = 0 ; *class_table[i].name ; i++)
  51. {
  52. if (is_abbrev(arg2, class_table[i].name))
  53. {
  54. break;
  55. }
  56. }
  57. if (*class_table[i].name == 0)
  58. {
  59. show_error(ses, LIST_COMMAND, "#SYNTAX: CLASS {name} {OPEN|CLOSE|READ|SIZE|WRITE|KILL}.", arg1, capitalize(arg2));
  60. }
  61. else
  62. {
  63. node = search_node_list(ses->list[LIST_CLASS], arg1);
  64. if (node == NULL)
  65. {
  66. check_all_events(ses, SUB_ARG, 0, 1, "CLASS CREATED", arg1);
  67. node = update_node_list(ses->list[LIST_CLASS], arg1, "", arg3, "");
  68. }
  69. class_table[i].fun(ses, node, arg1, arg3);
  70. }
  71. }
  72. return ses;
  73. }
  74. int count_class(struct session *ses, struct listnode *group)
  75. {
  76. int list, cnt, index;
  77. for (cnt = list = 0 ; list < LIST_MAX ; list++)
  78. {
  79. if (!HAS_BIT(ses->list[list]->flags, LIST_FLAG_CLASS))
  80. {
  81. continue;
  82. }
  83. for (index = 0 ; index < ses->list[list]->used ; index++)
  84. {
  85. if (!strcmp(ses->list[list]->list[index]->group, group->arg1))
  86. {
  87. cnt++;
  88. }
  89. }
  90. }
  91. return cnt;
  92. }
  93. DO_CLASS(class_clear)
  94. {
  95. int type, index;
  96. for (type = 0 ; type < LIST_MAX ; type++)
  97. {
  98. if (!HAS_BIT(ses->list[type]->flags, LIST_FLAG_CLASS))
  99. {
  100. continue;
  101. }
  102. for (index = 0 ; index < ses->list[type]->used ; index++)
  103. {
  104. if (!strcmp(ses->list[type]->list[index]->group, arg1))
  105. {
  106. delete_index_list(ses->list[type], index--);
  107. }
  108. }
  109. }
  110. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN CLEARED.", arg1);
  111. if (!strcmp(ses->group, arg1))
  112. {
  113. class_close(ses, node, arg1, arg2);
  114. }
  115. return ses;
  116. }
  117. DO_CLASS(class_close)
  118. {
  119. if (atoi(node->arg3) == 0)
  120. {
  121. show_message(ses, LIST_CLASS, "#CLASS {%s} IS ALREADY CLOSED.", arg1);
  122. }
  123. else
  124. {
  125. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN CLOSED.", arg1);
  126. update_node_list(ses->list[LIST_CLASS], arg1, "", "0","");
  127. if (!strcmp(ses->group, arg1))
  128. {
  129. check_all_events(ses, SUB_ARG, 0, 1, "CLASS DEACTIVATED", ses->group);
  130. check_all_events(ses, SUB_ARG, 1, 1, "CLASS DEACTIVATED %s", ses->group, ses->group);
  131. node = ses->list[LIST_CLASS]->list[0];
  132. if (atoi(node->arg3))
  133. {
  134. RESTRING(ses->group, node->arg1);
  135. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN ACTIVATED.", node->arg1);
  136. check_all_events(ses, SUB_ARG, 0, 1, "CLASS ACTIVATED", node->arg1);
  137. check_all_events(ses, SUB_ARG, 1, 1, "CLASS ACTIVATED %s", arg1, arg1);
  138. }
  139. else
  140. {
  141. RESTRING(ses->group, "");
  142. }
  143. }
  144. }
  145. return ses;
  146. }
  147. DO_CLASS(class_list)
  148. {
  149. int i, j;
  150. if (search_node_list(ses->list[LIST_CLASS], arg1))
  151. {
  152. tintin_header(ses, " %s ", arg1);
  153. for (i = 0 ; i < LIST_MAX ; i++)
  154. {
  155. if (!HAS_BIT(ses->list[i]->flags, LIST_FLAG_CLASS))
  156. {
  157. continue;
  158. }
  159. if (*arg2 && !is_abbrev(arg2, list_table[i].name) && !is_abbrev(arg2, list_table[i].name_multi))
  160. {
  161. continue;
  162. }
  163. for (j = 0 ; j < ses->list[i]->used ; j++)
  164. {
  165. if (!strcmp(ses->list[i]->list[j]->group, arg1))
  166. {
  167. show_node(ses->list[i], ses->list[i]->list[j], 0);
  168. }
  169. }
  170. }
  171. }
  172. else
  173. {
  174. show_error(ses, LIST_CLASS, "#CLASS {%s} DOES NOT EXIST.", arg1);
  175. }
  176. return ses;
  177. }
  178. DO_CLASS(class_kill)
  179. {
  180. int group;
  181. class_clear(ses, node, arg1, arg2);
  182. group = search_index_list(ses->list[LIST_CLASS], arg1, NULL);
  183. delete_index_list(ses->list[LIST_CLASS], group);
  184. check_all_events(ses, SUB_ARG, 0, 1, "CLASS DESTROYED", arg1);
  185. check_all_events(ses, SUB_ARG, 1, 1, "CLASS DESTROYED %s", arg1, arg1);
  186. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN KILLED.", arg1);
  187. return ses;
  188. }
  189. DO_CLASS(class_load)
  190. {
  191. FILE *file;
  192. if (node->data == NULL)
  193. {
  194. show_error(ses, LIST_CLASS, "#CLASS {%s} DOES NOT HAVE ANY DATA SAVED.", arg1);
  195. return ses;
  196. }
  197. file = fmemopen(node->data, node->val32[1], "r");
  198. read_file(ses, file, arg1);
  199. return ses;
  200. }
  201. DO_CLASS(class_open)
  202. {
  203. int count;
  204. if (!strcmp(ses->group, arg1))
  205. {
  206. show_message(ses, LIST_CLASS, "#CLASS {%s} IS ALREADY OPENED AND ACTIVATED.", arg1);
  207. }
  208. else
  209. {
  210. if (*ses->group)
  211. {
  212. check_all_events(ses, SUB_ARG, 0, 1, "CLASS DEACTIVATED", ses->group);
  213. check_all_events(ses, SUB_ARG, 1, 1, "CLASS DEACTIVATED %s", ses->group, ses->group);
  214. }
  215. RESTRING(ses->group, arg1);
  216. count = atoi(ses->list[LIST_CLASS]->list[0]->arg3);
  217. update_node_list(ses->list[LIST_CLASS], arg1, "", ntos(--count), "");
  218. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN OPENED AND ACTIVATED.", arg1);
  219. check_all_events(ses, SUB_ARG, 0, 1, "CLASS ACTIVATED", arg1);
  220. check_all_events(ses, SUB_ARG, 1, 1, "CLASS ACTIVATED %s", arg1, arg1);
  221. }
  222. return ses;
  223. }
  224. DO_CLASS(class_read)
  225. {
  226. class_open(ses, node, arg1, arg2);
  227. do_read(ses, arg2);
  228. class_close(ses, node, arg1, arg2);
  229. return ses;
  230. }
  231. DO_CLASS(class_save)
  232. {
  233. FILE *file;
  234. int list, index;
  235. str_cpy(&node->arg4, "");
  236. file = open_memstream(&node->data, (size_t *) &node->val32[1]);
  237. fprintf(file, "%cCLASS {%s} OPEN\n\n", gtd->tintin_char, arg1);
  238. for (list = 0 ; list < LIST_MAX ; list++)
  239. {
  240. if (!HAS_BIT(ses->list[list]->flags, LIST_FLAG_CLASS))
  241. {
  242. continue;
  243. }
  244. for (index = 0 ; index < ses->list[list]->used ; index++)
  245. {
  246. if (!strcmp(ses->list[list]->list[index]->group, arg1))
  247. {
  248. write_node(ses, list, ses->list[list]->list[index], file);
  249. }
  250. }
  251. }
  252. fprintf(file, "\n%cCLASS {%s} CLOSE\n", gtd->tintin_char, arg1);
  253. fclose(file);
  254. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN SAVED TO MEMORY (%d BYTES) (%s).", arg1, node->val32[1], node->data);
  255. return ses;
  256. }
  257. DO_CLASS(class_size)
  258. {
  259. if (*arg1 == 0 || *arg2 == 0)
  260. {
  261. show_error(ses, LIST_CLASS, "#SYNTAX: #CLASS {<class name>} SIZE {<variable>}.");
  262. return ses;
  263. }
  264. set_nest_node_ses(ses, arg2, "%d", count_class(ses, node));
  265. return ses;
  266. }
  267. DO_CLASS(class_write)
  268. {
  269. FILE *file;
  270. int list, index;
  271. if (*arg2 == 0 || (file = fopen(arg2, "w")) == NULL)
  272. {
  273. show_error(ses, LIST_CLASS, "#ERROR: #CLASS WRITE {%s} - COULDN'T OPEN FILE TO WRITE.", arg2);
  274. return ses;
  275. }
  276. fprintf(file, "%cCLASS {%s} OPEN\n\n", gtd->tintin_char, arg1);
  277. for (list = 0 ; list < LIST_MAX ; list++)
  278. {
  279. if (!HAS_BIT(ses->list[list]->flags, LIST_FLAG_CLASS))
  280. {
  281. continue;
  282. }
  283. for (index = 0 ; index < ses->list[list]->used ; index++)
  284. {
  285. if (!strcmp(ses->list[list]->list[index]->group, arg1))
  286. {
  287. write_node(ses, list, ses->list[list]->list[index], file);
  288. }
  289. }
  290. }
  291. fprintf(file, "\n%cCLASS {%s} CLOSE\n", gtd->tintin_char, arg1);
  292. fclose(file);
  293. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN WRITTEN TO FILE.", arg1);
  294. return ses;
  295. }