data_sorting.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. static void sortstats(struct stats_global *gs, int itype, int today)
  19. {
  20. int again = 1;
  21. struct stats_local *last, *p, *c, *n;
  22. int a, b, pitype;
  23. Context;
  24. Assert(gs);
  25. again = 1;
  26. last = NULL;
  27. if (itype < 0) {
  28. // switch to the special sorting function
  29. switch (itype) {
  30. case T_WPL:
  31. sortstats_wpl(gs, today);
  32. break;
  33. case T_VOCABLES:
  34. sortstats_vocables(gs, today);
  35. break;
  36. case T_WORD:
  37. sortstats_word(gs, today);
  38. break;
  39. case T_IDLE:
  40. sortstats_idle(gs, today);
  41. break;
  42. default:
  43. debug1("Missing sorting algorithm for \"%d\"!!!", itype);
  44. }
  45. return;
  46. }
  47. // if (itype < 0) pitype = (TOTAL_TYPES - 1) + (itype * -1);
  48. // not needed here
  49. pitype = itype;
  50. while ((gs->slocal[today][pitype] != last) && (again)) {
  51. p = NULL;
  52. c = gs->slocal[today][pitype];
  53. n = c->snext[today][pitype];
  54. again = 0;
  55. while (n != last) {
  56. if (!c || !n)
  57. a = b = 0;
  58. else {
  59. a = c->values[today][itype];
  60. b = n->values[today][itype];
  61. }
  62. if (a < b) {
  63. again = 1;
  64. c->snext[today][pitype] = n->snext[today][pitype];
  65. n->snext[today][pitype] = c;
  66. if (p == NULL)
  67. gs->slocal[today][pitype] = n;
  68. else
  69. p->snext[today][pitype] = n;
  70. }
  71. p = c;
  72. c = n;
  73. n = n->snext[today][pitype];
  74. }
  75. last = c;
  76. }
  77. Context;
  78. return;
  79. }
  80. static void sortstats_wpl(struct stats_global *gs, int today)
  81. {
  82. int again = 1;
  83. struct stats_local *last, *p, *c, *n;
  84. int a, b, pitype;
  85. Context;
  86. again = 1;
  87. last = NULL;
  88. pitype = (T_WPL * (-1)) + TOTAL_TYPES - 1;
  89. while ((gs->slocal[today][pitype] != last) && (again)) {
  90. p = NULL;
  91. c = gs->slocal[today][pitype];
  92. n = c->snext[today][pitype];
  93. again = 0;
  94. while (n != last) {
  95. if (!c || !n)
  96. a = b = 0;
  97. else {
  98. if (c->values[today][T_LINES] >= min_lines)
  99. a = (int) (((float) c->values[today][T_WORDS] / (float) c->values[today][T_LINES]) * 100.0);
  100. else
  101. a = 0;
  102. if (n->values[today][T_LINES] >= min_lines)
  103. b = (int) (((float) n->values[today][T_WORDS] / (float) n->values[today][T_LINES]) * 100.0);
  104. else
  105. b = 0;
  106. }
  107. if (a < b) {
  108. again = 1;
  109. c->snext[today][pitype] = n->snext[today][pitype];
  110. n->snext[today][pitype] = c;
  111. if (p == NULL)
  112. gs->slocal[today][pitype] = n;
  113. else
  114. p->snext[today][pitype] = n;
  115. }
  116. p = c;
  117. c = n;
  118. n = n->snext[today][pitype];
  119. }
  120. last = c;
  121. }
  122. Context;
  123. return;
  124. }
  125. static void sortstats_vocables(struct stats_global *gs, int today)
  126. {
  127. int again = 1;
  128. struct stats_local *last, *p, *c, *n;
  129. int a, b, pitype;
  130. Context;
  131. again = 1;
  132. last = NULL;
  133. countvocables(gs);
  134. pitype = (T_VOCABLES * (-1)) + TOTAL_TYPES - 1;
  135. while ((gs->slocal[today][pitype] != last) && (again)) {
  136. p = NULL;
  137. c = gs->slocal[today][pitype];
  138. n = c->snext[today][pitype];
  139. again = 0;
  140. while (n != last) {
  141. if (!c || !n)
  142. a = b = 0;
  143. else {
  144. a = c->vocables;
  145. b = n->vocables;
  146. }
  147. if (a < b) {
  148. again = 1;
  149. c->snext[today][pitype] = n->snext[today][pitype];
  150. n->snext[today][pitype] = c;
  151. if (p == NULL)
  152. gs->slocal[today][pitype] = n;
  153. else
  154. p->snext[today][pitype] = n;
  155. }
  156. p = c;
  157. c = n;
  158. n = n->snext[today][pitype];
  159. }
  160. last = c;
  161. }
  162. Context;
  163. return;
  164. }
  165. static void sortstats_word(struct stats_global *gs, int today)
  166. {
  167. int again = 1;
  168. struct stats_local *last, *p, *c, *n;
  169. int a, b, pitype;
  170. Context;
  171. debug1("sortstats_word: today == %d", today);
  172. again = 1;
  173. last = NULL;
  174. pitype = (T_WORD * (-1)) + TOTAL_TYPES - 1;
  175. debug1("pitype: %d", pitype);
  176. while ((gs->slocal[today][pitype] != last) && (again)) {
  177. p = NULL;
  178. c = gs->slocal[today][pitype];
  179. n = c->snext[today][pitype];
  180. again = 0;
  181. while (n != last) {
  182. if (!c || !n)
  183. a = b = 0;
  184. else {
  185. if (c->word)
  186. a = c->word->nr;
  187. else
  188. a = 0;
  189. if (n->word)
  190. b = n->word->nr;
  191. else
  192. b = 0;
  193. }
  194. if (a < b) {
  195. again = 1;
  196. c->snext[today][pitype] = n->snext[today][pitype];
  197. n->snext[today][pitype] = c;
  198. if (p == NULL)
  199. gs->slocal[today][pitype] = n;
  200. else
  201. p->snext[today][pitype] = n;
  202. }
  203. p = c;
  204. c = n;
  205. n = n->snext[today][pitype];
  206. }
  207. last = c;
  208. }
  209. Context;
  210. return;
  211. }
  212. // sort stats by idle-factor (minutes/lines)
  213. static void sortstats_idle(struct stats_global *gs, int today)
  214. {
  215. int again = 1;
  216. struct stats_local *last, *p, *c, *n;
  217. int a, b, pitype;
  218. Context;
  219. again = 1;
  220. last = NULL;
  221. pitype = (T_IDLE * (-1)) + TOTAL_TYPES - 1;
  222. while ((gs->slocal[today][pitype] != last) && (again)) {
  223. p = NULL;
  224. c = gs->slocal[today][pitype];
  225. n = c->snext[today][pitype];
  226. again = 0;
  227. while (n != last) {
  228. if (!c || !n)
  229. a = b = 0;
  230. else {
  231. if (c->values[today][T_LINES] >= min_lines)
  232. a = (int) (((float) c->values[today][T_MINUTES] / (float) c->values[today][T_LINES]) * 100.0);
  233. else
  234. a = 0;
  235. if (n->values[today][T_LINES] >= min_lines)
  236. b = (int) (((float) n->values[today][T_MINUTES] / (float) n->values[today][T_LINES]) * 100.0);
  237. else
  238. b = 0;
  239. }
  240. if (a < b) {
  241. again = 1;
  242. c->snext[today][pitype] = n->snext[today][pitype];
  243. n->snext[today][pitype] = c;
  244. if (p == NULL)
  245. gs->slocal[today][pitype] = n;
  246. else
  247. p->snext[today][pitype] = n;
  248. }
  249. p = c;
  250. c = n;
  251. n = n->snext[today][pitype];
  252. }
  253. last = c;
  254. }
  255. Context;
  256. return;
  257. }
  258. static void sortwordstats(locstats *ls, globstats *gs)
  259. {
  260. int again = 1;
  261. wordstats *last, *p, *c, *n, *tmp;
  262. int a, b;
  263. Context;
  264. again = 1;
  265. last = NULL;
  266. if (ls)
  267. tmp = ls->words;
  268. else
  269. tmp = gs->words;
  270. while ((tmp != last) && (again)) {
  271. p = NULL;
  272. if (ls)
  273. c = ls->words;
  274. else
  275. c = gs->words;
  276. n = c->next;
  277. again = 0;
  278. while (n != last) {
  279. if (!c || !n)
  280. a = b = 0;
  281. else {
  282. a = c->nr;
  283. b = n->nr;
  284. }
  285. if (a < b) {
  286. again = 1;
  287. c->next = n->next;
  288. n->next = c;
  289. if (p == NULL) {
  290. if (ls)
  291. ls->words = n;
  292. else
  293. gs->words = n;
  294. tmp = n;
  295. } else
  296. p->next = n;
  297. }
  298. p = c;
  299. c = n;
  300. n = n->next;
  301. }
  302. last = c;
  303. }
  304. Context;
  305. return;
  306. }
  307. static void sorthosts(struct stats_global *gs)
  308. {
  309. int again = 1;
  310. hoststr *last, *p, *c, *n;
  311. int a, b;
  312. Context;
  313. again = 1;
  314. last = NULL;
  315. while ((gs->hosts != last) && (again)) {
  316. p = NULL;
  317. c = gs->hosts;
  318. n = c->next;
  319. again = 0;
  320. while (n != last) {
  321. if (!c || !n)
  322. a = b = 0;
  323. else {
  324. a = c->nr;
  325. b = n->nr;
  326. }
  327. if (a < b) {
  328. again = 1;
  329. c->next = n->next;
  330. n->next = c;
  331. if (p == NULL)
  332. gs->hosts = n;
  333. else
  334. p->next = n;
  335. }
  336. p = c;
  337. c = n;
  338. n = n->next;
  339. }
  340. last = c;
  341. }
  342. Context;
  343. return;
  344. }
  345. static void sort_stats_alphabetically(globstats *gs)
  346. {
  347. locstats *as, *bs, *l, *last;
  348. int a, b, again = 1;
  349. char *astr, *bstr, n[2];
  350. Context;
  351. n[0] = n[1] = 0;
  352. last = NULL;
  353. while ((gs->local != last) && again) {
  354. again = 0;
  355. l = NULL;
  356. as = gs->local;
  357. bs = gs->local->next;
  358. while(bs) {
  359. if (!as)
  360. astr = n;
  361. else
  362. astr = as->user;
  363. if (!bs)
  364. bstr = n;
  365. else
  366. bstr = bs->user;
  367. a = (int) tolower(astr[0]);
  368. b = (int) tolower(bstr[0]);
  369. while ((a == b) && a && b) {
  370. astr++;
  371. bstr++;
  372. a = (int) tolower(astr[0]);
  373. b = (int) tolower(bstr[0]);
  374. }
  375. if (a > b) {
  376. if (!l)
  377. gs->local = bs;
  378. else
  379. l->next = bs;
  380. as->next = bs->next;
  381. bs->next = as;
  382. again = 1;
  383. if (l == NULL)
  384. gs->local = bs;
  385. else
  386. l = bs;
  387. }
  388. l = as;
  389. as = bs;
  390. bs = bs->next;
  391. }
  392. last = as;
  393. }
  394. Context;
  395. }