File: xml_output.c

package info (click to toggle)
webdruid 0.5.4-12.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 3,360 kB
  • sloc: ansic: 10,823; sh: 181; makefile: 112
file content (994 lines) | stat: -rw-r--r-- 32,076 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
/*
    xml_output.c - XML output dor The WebDruid

    Copyright (C) 2003-2004 Fabien Chevalier (fabien@juliana-multimedia.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version, and provided that the above
    copyright and permission notice is included with all distributed
    copies of this or derived software.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/

#include "config.h"

/* ensure sys/types */
#ifndef _SYS_TYPES_H
#include <sys/types.h>
#endif

/* some systems need this */
#ifdef HAVE_MATH_H
#include <math.h>
#endif

#include <time.h>

/* SunOS 4.x Fix */
#ifndef CLK_TCK
#define CLK_TCK _SC_CLK_TCK
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "webdruid.h"
#include "lang.h"
#include "hashtab.h"
#include "linklist.h"
#include "sengine.h"
#include "utils.h"

/* TODO: delete this when it is time...*/
#include "preserve.h"

/*********************************************/
/*  local defines                            */
/*********************************************/

/*
   We won't output more than LIST_MAX items
   in our sorted lists, unless dump_xxx or
   all_xxx is specified
*/
#define LIST_MAX 250


/*********************************************/
/*  local variables                          */
/*********************************************/

/* sort arrays */

static UNODEPTR *u_array      = NULL;                /* Sort array for URL's     */
static HNODEPTR *h_array      = NULL;                /* hostnames (sites)        */
static RNODEPTR *r_array      = NULL;                /* referrers                */
static ANODEPTR *a_array      = NULL;                /* user agents              */
static INODEPTR *i_array      = NULL;                /* ident strings (username) */
static u_long   a_ctr         = 0;                   /* counter for sort array   */

/*********************************************/
/*  local functions                          */
/*********************************************/

static int  common_header(FILE * out_fp);        /* write xml header file         */
static int  write_settings(FILE * out_fp);       /* write common settings in file */
static void output_setting_string(FILE * out_fp, /* write string setting          */
          const char *name, const char *value);

static u_long  load_url_array(  UNODEPTR *);               /* load URL array      */
static u_long  load_site_array( HNODEPTR *);               /* load Site array     */
static u_long  load_ref_array(  RNODEPTR *);               /* load Refs array     */
static u_long  load_agent_array(ANODEPTR *);               /* load Agents array   */
static u_long  load_ident_array(INODEPTR *);               /* load ident array    */
static u_long  load_search_array(SNODEPTR array[],
                  SNODEPTR source_htab[]);
static int qs_url_cmph( const void*, const void*);         /* compare by hits     */
static int qs_url_cmpk( const void*, const void*);         /* compare by kbytes   */
static int qs_url_cmpn( const void*, const void*);         /* compare by entrys   */
static int qs_url_cmpx( const void*, const void*);         /* compare by exits    */
static int qs_site_cmph(const void*, const void*);         /* compare by hits     */
static int qs_site_cmpk(const void*, const void*);         /* compare by kbytes   */
static int qs_ref_cmph( const void*, const void*);         /* compare by hits     */
static int qs_agnt_cmph(const void*, const void*);         /* compare by hits     */
static int qs_ident_cmph(const void*, const void*);        /* compare by hits     */
static int qs_snode_cmp_count(const void *cp1, const void *cp2);    /* compare by hits     */
static int qs_sengines_cmp_count(const void *cp1, const void *cp2);    /* compare by hits     */


/*********************************************/
/*  Writes global statistics                 */
/*********************************************/

int write_history_xml()
{
   int i;
   int year = 0;
   const char *xml_fname = "history.xml";
   FILE * out_fp;

   if (verbose>1)
     printf("%s\n",_("Generating shared XML document..."));

   /* fill in filename */

   /* first, open the file */
   if ( (out_fp = open_out_file_in_dir("xml", xml_fname)) == NULL )
    return 1;

    /* Let's gogogo... */

   if(common_header(out_fp))
   {
      fclose(out_fp);
      return 1;
   };

   fprintf(out_fp, "<report xmlns=\"http://www.webdruid.org/history\">\n");

   if(write_settings(out_fp))
   {
      fclose(out_fp);
      return 1;
   };

   /* Write some months/years values */

   for(i = 0; i < 12; i++)
   {
      if(year != hist_year[i])
      {
         if(year)
         {
            fprintf(out_fp, "</year>\n");
         }

         year = hist_year[i];

         fprintf(out_fp, "<year value=\"%04d\">\n", year);
      }
      if(hist_month[i]){
         fprintf(out_fp, "<month value=\"%02d\"/>\n", hist_month[i]);
      }
   }

   fprintf(out_fp, "</year>\n");

   fprintf(out_fp, "</report>\n");

   fclose(out_fp);
   return 0;
}

/*********************************************/
/*  Writes monthly statistics                */
/*********************************************/

int write_month_xml()
{
   unsigned long i, j;
   char xml_fname[256];           /* filename storage area...       */
   FILE * out_fp;
   struct sengine *sp_sorted_table; /* contains the array of array of sorted search phrases */

   if (verbose>1)
     printf("%s %s %d...\n",_("Generating XML document for"), _(l_month[cur_month-1]), cur_year);

   /* fill in filename */
   snprintf(xml_fname,sizeof(xml_fname),"monthly_%04d_%02d.xml",
            cur_year,cur_month);

   /* first, open the file */
   if ( (out_fp = open_out_file_in_dir("xml", xml_fname)) == NULL )
    return 1;

   /* Let's gogogo... */

   if(common_header(out_fp))
   {
      fclose(out_fp);
      return 1;
   };

   fprintf(out_fp, "<report xmlns=\"http://www.webdruid.org/stats\">\n");

   if(write_settings(out_fp))
   {
      fclose(out_fp);
      return 1;
   };

   /* Keep track of what kind of report it is... */
   fprintf(out_fp, "<grain>month</grain>\n");
   /* Keep track of what month we are... */
   fprintf(out_fp, "<year>%04d</year>\n", cur_year);
   fprintf(out_fp, "<month>%02d</month>\n", cur_month);

   /* Do URL related stuff here, sorting appropriately                      */
   if ((a_ctr = load_url_array(NULL)) != 0)
   {
      if ( (u_array = malloc(sizeof(UNODEPTR)*(a_ctr))) != 0 )
      {
         unsigned long max_output; /* number of items to output */
         unsigned long total_hits = 0;
         unsigned long total_kilobytes = 0;
         unsigned long total_exit = 0;
         unsigned long total_entry = 0;

         a_ctr = load_url_array(u_array);        /* load up our sort array        */

         for(i = 0; i < a_ctr; i++)
         {
            total_hits += u_array[i]->count;
            total_kilobytes += u_array[i]->xfer/1024;
            if (u_array[i]->flag == OBJ_REG)
            {
               if (u_array[i]->entry != 0)
                  total_entry += u_array[i]->entry;
               if (u_array[i]->exit != 0)
                  total_exit += u_array[i]->exit;
            }
         }

         fprintf(out_fp, "<urls>\n");

         fprintf(out_fp, "<count value=\"%lu\"/>\n",
            a_ctr);

         fprintf(out_fp, "<sums hits=\"%lu\" kilobytes=\"%lu\" entries=\"%lu\" exits=\"%lu\"/>\n",
            total_hits, total_kilobytes, total_entry, total_exit);

         if(all_urls || dump_urls)
         {
            fprintf(out_fp, "<list key=\"hits\" all=\"yes\">\n");
            max_output = a_ctr;
         }
         else
         {
            fprintf(out_fp, "<list key=\"hits\">\n");
            max_output = MIN(LIST_MAX, a_ctr);
         }

         qsort(u_array, a_ctr, sizeof(UNODEPTR), qs_url_cmph);
         for(i = 0; i < max_output; i++)
         {
            fprintf(out_fp, "<url name=\"%s\" hits=\"%lu\" kilobytes=\"%.0f\" entries=\"%lu\" exits=\"%lu\"/>\n",
            u_array[i]->string, u_array[i]->count, u_array[i]->xfer/1024, u_array[i]->entry, u_array[i]->exit);
         }

         fprintf(out_fp, "</list>\n");

         /* Top URL's (by kbytes)         */
         fprintf(out_fp, "<list key=\"kilobytes\">\n");
         qsort(u_array,a_ctr,sizeof(UNODEPTR),qs_url_cmpk);
         max_output = MIN(LIST_MAX, a_ctr);
         for(i = 0; i < max_output; i++)
         {
            fprintf(out_fp, "<url name=\"%s\" hits=\"%lu\" kilobytes=\"%.0f\" entries=\"%lu\" exits=\"%lu\"/>\n",
            u_array[i]->string, u_array[i]->count, u_array[i]->xfer/1024, u_array[i]->entry, u_array[i]->exit);
         }
         fprintf(out_fp, "</list>\n");

         /* Top Entry Pages               */
         fprintf(out_fp, "<list key=\"entries\">\n");
         qsort(u_array, a_ctr, sizeof(UNODEPTR), qs_url_cmpn);
         for(i = 0, j = 0; i < a_ctr && j < LIST_MAX; i++)
         {
            if(u_array[i]->flag != OBJ_HIDE)
            {
               fprintf(out_fp, "<url name=\"%s\" hits=\"%lu\" kilobytes=\"%.0f\" entries=\"%lu\" exits=\"%lu\"/>\n",
               u_array[i]->string, u_array[i]->count, u_array[i]->xfer/1024, u_array[i]->entry, u_array[i]->exit);
               j++;
            }
         }
         fprintf(out_fp, "</list>\n");

         /* Top Exit Pages               */
         fprintf(out_fp, "<list key=\"exits\">\n");
         qsort(u_array, a_ctr, sizeof(UNODEPTR), qs_url_cmpx);
         for(i = 0; i < a_ctr && j < LIST_MAX; i++)
         {
            if(u_array[i]->flag != OBJ_HIDE)
            {
               fprintf(out_fp, "<url name=\"%s\" hits=\"%lu\" kilobytes=\"%.0f\" entries=\"%lu\" exits=\"%lu\"/>\n",
               u_array[i]->string, u_array[i]->count, u_array[i]->xfer/1024, u_array[i]->entry, u_array[i]->exit);
               j++;
            }
         }
         fprintf(out_fp, "</list>\n");

         free(u_array);

         fprintf(out_fp, "</urls>\n");
      }
      else if (verbose) fprintf(stderr,"%s [u_array]\n",_("Can't allocate enough memory, Top URLs disabled!")); /* err */
   }

   /* do hostname (sites) related stuff here, sorting appropriately...      */
   if ((a_ctr = load_site_array(NULL)) != 0)
   {
      if ( (h_array = malloc(sizeof(HNODEPTR)*(a_ctr))) != 0 )
      {
         unsigned long max_output; /* number of items to output */
         unsigned long total_hits = 0;
         unsigned long total_files = 0;
         unsigned long total_visits = 0;
         unsigned long total_kilobytes = 0;

         a_ctr = load_site_array(h_array);       /* load up our sort array        */

         for(i = 0; i < a_ctr; i++)
         {
            total_hits += h_array[i]->count;
            total_files += h_array[i]->files;
            total_kilobytes += h_array[i]->xfer/1024;
            total_visits += h_array[i]->visit;
         }

         fprintf(out_fp, "<sites>\n");

         fprintf(out_fp, "<count value=\"%lu\"/>\n",
            a_ctr);

         fprintf(out_fp, "<sums hits=\"%lu\" files=\"%lu\" kilobytes=\"%lu\" visits=\"%lu\"/>\n",
            total_hits, total_files, total_kilobytes, total_visits);

         if(all_sites || dump_sites)
         {
            fprintf(out_fp, "<list key=\"hits\" all=\"yes\">\n");
            max_output = a_ctr;
         }
         else
         {
            fprintf(out_fp, "<list key=\"hits\">\n");
            max_output = MIN(LIST_MAX, a_ctr);
         }

         qsort(h_array,a_ctr,sizeof(HNODEPTR),qs_site_cmph);
         for(i = 0; i < max_output; i++)
         {
               fprintf(out_fp, "<site hostname=\"%s\" hits=\"%lu\" files=\"%lu\" kilobytes=\"%.0f\" visits=\"%lu\"/>\n",
               h_array[i]->string, h_array[i]->count, h_array[i]->files, h_array[i]->xfer/1024, h_array[i]->visit);
         }

         fprintf(out_fp, "</list>\n");

         /* Top Sites table (by kbytes)   */
         fprintf(out_fp, "<list key=\"kilobytes\">\n");
         qsort(h_array,a_ctr,sizeof(HNODEPTR),qs_site_cmpk);
         max_output = MIN(LIST_MAX, a_ctr);
         for(i = 0; i < max_output; i++)
         {
            fprintf(out_fp, "<site hostname=\"%s\" hits=\"%lu\" files=\"%lu\" kilobytes=\"%.0f\" visits=\"%lu\"/>\n",
            h_array[i]->string, h_array[i]->count, h_array[i]->files, h_array[i]->xfer/1024, h_array[i]->visit);
         }
         fprintf(out_fp, "</list>\n");

         free(h_array);

         fprintf(out_fp, "</sites>\n");
      }
      else if (verbose) fprintf(stderr,"%s [h_array]\n",_("Can't allocate enough memory, Top Sites disabled!")); /* err */
   }

   /* do referrer related stuff here, sorting appropriately...              */
   if ((a_ctr = load_ref_array(NULL)) != 0)
   {
      if ((r_array = malloc(sizeof(RNODEPTR)*(a_ctr))) != 0)
      {
         unsigned long max_output; /* number of items to output */
         unsigned long total_hits = 0;

         a_ctr = load_ref_array(r_array);       /* load up our sort array        */

         for(i = 0; i < a_ctr; i++)
            total_hits += r_array[i]->count;

         fprintf(out_fp, "<referrers>\n");

         fprintf(out_fp, "<count value=\"%lu\"/>\n",
            a_ctr);

         fprintf(out_fp, "<sums hits=\"%lu\"/>\n", total_hits);

         if(all_refs || dump_refs)
         {
            fprintf(out_fp, "<list key=\"hits\" all=\"yes\">\n");
            max_output = a_ctr;
         }
         else
         {
            fprintf(out_fp, "<list key=\"hits\">\n");
            max_output = MIN(LIST_MAX, a_ctr);
         }

         qsort(r_array, a_ctr, sizeof(RNODEPTR), qs_ref_cmph);
         for(i = 0; i < max_output; i++)
         {
            fprintf(out_fp, "<referrer url=\"%s\" hits=\"%lu\"/>\n",
            r_array[i]->string, r_array[i]->count);
         }
         fprintf(out_fp, "</list>\n");

         free(r_array);

         fprintf(out_fp, "</referrers>\n");
     }
        else if (verbose) fprintf(stderr,"%s [r_array]\n",_("Can't allocate enough memory, Top Referrers disabled!")); /* err */
   }

   /* do ident (username) related stuff here, sorting appropriately...      */
   if((a_ctr = load_ident_array(NULL)) != 0)
   {
      if ((i_array = malloc(sizeof(RNODEPTR)*(a_ctr))) != 0)
      {
         unsigned long max_output; /* number of items to output */
         unsigned long total_hits = 0;

         a_ctr = load_ident_array(i_array);       /* load up our sort array        */

         for(i = 0; i < a_ctr; i++)
            total_hits += i_array[i]->count;

         fprintf(out_fp, "<usernames>\n");

         fprintf(out_fp, "<count value=\"%lu\"/>\n",
            a_ctr);

         fprintf(out_fp, "<sums hits=\"%lu\"/>\n", total_hits);

         if(all_users || dump_users)
         {
            fprintf(out_fp, "<list key=\"hits\" all=\"yes\">\n");
            max_output = a_ctr;
         }
         else
         {
            fprintf(out_fp, "<list key=\"hits\">\n");
            max_output = MIN(LIST_MAX, a_ctr);
         }

         qsort(i_array, a_ctr, sizeof(INODEPTR), qs_ident_cmph);
         for(i = 0; i < max_output; i++)
         {
            fprintf(out_fp, "<username url=\"%s\" hits=\"%lu\"/>\n",
            i_array[i]->string, i_array[i]->count);
         }
         fprintf(out_fp, "</list>\n");

         free(i_array);

         fprintf(out_fp, "</usernames>\n");
     }
         else if (verbose) fprintf(stderr,"%s [i_array]\n",_("Can't allocate enough memory, Top Usernames disabled!")); /* err */
   }

   /* do user agent related stuff here, sorting appropriately...            */
   if ((a_ctr = load_agent_array(NULL)) != 0)
   {
      if ((a_array = malloc(sizeof(ANODEPTR)*(a_ctr))) != 0)
      {
         unsigned long max_output; /* number of items to output */
         unsigned long total_hits = 0;

         a_ctr = load_agent_array(a_array);       /* load up our sort array        */

         for(i = 0; i < a_ctr; i++)
            total_hits += a_array[i]->count;

         fprintf(out_fp, "<agents>\n");

         fprintf(out_fp, "<count value=\"%lu\"/>\n",
            a_ctr);

         fprintf(out_fp, "<sums hits=\"%lu\"/>\n", total_hits);

         if(all_agents || dump_agents)
         {
            fprintf(out_fp, "<list key=\"hits\" all=\"yes\">\n");
            max_output = a_ctr;
         }
         else
         {
            fprintf(out_fp, "<list key=\"hits\">\n");
            max_output = MIN(LIST_MAX, a_ctr);
         }

         qsort(a_array, a_ctr, sizeof(ANODEPTR), qs_agnt_cmph);
         for(i = 0; i < max_output; i++)
         {
            fprintf(out_fp, "<username url=\"%s\" hits=\"%lu\"/>\n",
            a_array[i]->string, a_array[i]->count);
         }
         fprintf(out_fp, "</list>\n");

         free(a_array);

         fprintf(out_fp, "</agents>\n");
     }
         else if (verbose) fprintf(stderr,"%s [a_array]\n",_("Can't allocate enough memory, Top User Agents disabled!")); /* err */
   }

   /* let's malloc it -- and do it's initialisation with a copy of sp_table */
   sp_sorted_table = malloc(sizeof(struct sengine) * n_sengines);

   if(sp_sorted_table == NULL)
   {
      if (verbose)
         fprintf(stderr,"[generate_sengines_info] %s\n",_("Can't allocate enough memory, Search engines report disabled!")); /* err */
   }
   else
   {
      unsigned long total_searches = 0;

      memcpy(sp_sorted_table, sp_table, sizeof(struct sengine) * n_sengines);

      /* compute the number of searches for all engines */
      for(i=0; i<n_sengines; i++)
         total_searches += sp_table[i].count;

      /* Let's fill our array of array */
      for(i=0; i<n_sengines; i++)
      {
         if(sp_table[i].count)
         {
            /* Do load the array */
            u_long size = load_search_array(NULL, sp_table[i].s_htab);

            sp_sorted_table[i].s_htab = malloc((size + 1) * sizeof(SNODEPTR));
            if(sp_sorted_table[i].s_htab == NULL) /* no more memory */
            {
               if (verbose)
                  fprintf(stderr,"[generate_sengines_info] %s\n",_("Can't allocate enough memory, Search engines report will be incomplete!")); /* err */
               /* force it to be ignored */
               sp_sorted_table[i].s_htab = NULL;
               sp_sorted_table[i].count = 0;
            }
            else
            {
               /* if memory allocated, fill it and sort it */
               load_search_array(sp_sorted_table[i].s_htab, sp_table[i].s_htab);
               qsort(sp_sorted_table[i].s_htab, size, sizeof(SNODEPTR), qs_snode_cmp_count);
               /* end array with null */
               sp_sorted_table[i].s_htab[size] = NULL;
            }
         }
         else
            sp_sorted_table[i].s_htab = NULL;
      }

      /* now we sort the array of array */

      qsort(sp_sorted_table, n_sengines, sizeof(struct sengine), qs_sengines_cmp_count);

      fprintf(out_fp, "<sengines>\n");
      fprintf(out_fp, "<sums hits=\"%lu\"/>\n", total_searches);
      /* iterate through sengines */
      for(i = 0; i < n_sengines; i++)
      {
         if(sp_sorted_table[i].count != 0)
         {
            int j;
            LISTPTR lptr;

            fprintf(out_fp, "<sengine name=\"%s\">\n", sp_sorted_table[i].display_name);
            fprintf(out_fp, "<sums hits=\"%lu\"/>\n", sp_sorted_table[i].count);

            /* iterate through search phrases */
            for(j = 0; j < LIST_MAX && sp_sorted_table[i].s_htab[j] != NULL; j++)
            {
               fprintf(out_fp, "<search phrase=\"%s\">\n",
                  sp_sorted_table[i].s_htab[j]->string);
               fprintf(out_fp, "<sums hits=\"%lu\"/>\n",
                  sp_sorted_table[i].s_htab[j]->count);
               /* iterate through search urls */
               lptr = sp_sorted_table[i].s_htab[j]->urls;
               while(lptr != NULL)
               {
                  fprintf(out_fp, "<url name=\"%s\"/>\n", (char *)lptr->item);
                  lptr = lptr->next;
               }
               fprintf(out_fp, "</search>\n");
            }
            fprintf(out_fp, "</sengine>\n");
         }
      }
      fprintf(out_fp, "</sengines>\n");

      /* cleanup */
      for(i=0; i < n_sengines; i++)
         free(sp_sorted_table[i].s_htab);

      free(sp_sorted_table);
   }

   fprintf(out_fp, "</report>\n");

   fclose(out_fp);                        /* close the file                 */
   return 0;                              /* done...                        */
}


/*********************************************/
/*  WRITE_SETTINGS                           */
/*********************************************/

/*
   Outputs settings
*/

static int write_settings(FILE * out_fp)
{
   /* Record WebDruid's settings */
   fprintf(out_fp, "<settings>\n");

   /* Give hostname */
   output_setting_string(out_fp, "HostName", hname);

   /* Give version */
   output_setting_string(out_fp, "Version", version);

   fprintf(out_fp, "</settings>\n");

   return 0;
}

/*********************************************/
/*  COMMON_HEADER                            */
/*********************************************/

/*
   Outputs header in xml file
*/

static int common_header(FILE * out_fp)
{
   time_t curtime = time(0);

   fprintf(out_fp, "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
   fprintf(out_fp,"<!-- Generated by The WebDruid  v %s       -->\n",
      version);
   fprintf(out_fp,"<!-- (http://www.webdruid.org)                -->\n");
   fprintf(out_fp,"<!--                                          -->\n");
   fprintf(out_fp,"<!-- Copyright 2003-2004 Fabien Chevalier     -->\n");
   fprintf(out_fp,"<!-- (fabien@juliana-multimedia.com)          -->\n");
   fprintf(out_fp,"<!--                                          -->\n");
   fprintf(out_fp,"<!-- Distributed under the GNU GPL  Version 2 -->\n");
   fprintf(out_fp,"<!--                                          -->\n");
   fprintf(out_fp,"<!--                                          -->\n");
   fprintf(out_fp,"<!-- *** Generated: %s *** -->\n\n", ctime(&curtime));

   return 0;
}

/*********************************************/
/*  output_setting_string  - straightforward */
/*********************************************/

static void output_setting_string(FILE * out_fp, const char *name, const char *value)
{
   fprintf(out_fp, "<setting name=\"%s\"><value>%s</value></setting>\n", name, value);
}

/*********************************************/
/* LOAD_SITE_ARRAY - load up the sort array  */
/*********************************************/

static u_long load_site_array(HNODEPTR *pointer)
{
   HNODEPTR hptr;
   int      i;
   u_long   ctr = 0;

   /* load the array */
   for (i=0;i<MAXHASH;i++)
   {
      hptr=sm_htab[i];
      while (hptr!=NULL)
      {
         if (pointer==NULL) ctr++;       /* fancy way to just count 'em    */
         else *(pointer+ctr++)=hptr;     /* otherwise, really do the load  */
         hptr=hptr->next;
      }
   }
   return ctr;   /* return number loaded */
}

/*********************************************/
/* LOAD_URL_ARRAY - load up the sort array   */
/*********************************************/

static u_long load_url_array(UNODEPTR *pointer)
{
   UNODEPTR uptr;
   int      i;
   u_long   ctr = 0;

   /* load the array */
   for (i=0;i<MAXHASH;i++)
   {
      uptr=um_htab[i];
      while (uptr!=NULL)
      {
         if (pointer==NULL) ctr++;       /* fancy way to just count 'em    */
         else *(pointer+ctr++)=uptr;     /* otherwise, really do the load  */
         uptr=uptr->next;
      }
   }
   return ctr;   /* return number loaded */
}

/*********************************************/
/* LOAD_REF_ARRAY - load up the sort array   */
/*********************************************/

static u_long load_ref_array(RNODEPTR *pointer)
{
   RNODEPTR rptr;
   int      i;
   u_long   ctr = 0;

   /* load the array */
   for (i=0;i<MAXHASH;i++)
   {
      rptr=rm_htab[i];
      while (rptr!=NULL)
      {
         if (pointer==NULL) ctr++;       /* fancy way to just count 'em    */
         else *(pointer+ctr++)=rptr;     /* otherwise, really do the load  */
         rptr=rptr->next;
      }
   }
   return ctr;   /* return number loaded */
}

/*********************************************/
/* LOAD_AGENT_ARRAY - load up the sort array */
/*********************************************/

static u_long load_agent_array(ANODEPTR *pointer)
{
   ANODEPTR aptr;
   int      i;
   u_long   ctr = 0;

   /* load the array */
   for (i=0;i<MAXHASH;i++)
   {
      aptr=am_htab[i];
      while (aptr!=NULL)
      {
         if (pointer==NULL) ctr++;       /* fancy way to just count 'em    */
         else *(pointer+ctr++)=aptr;     /* otherwise, really do the load  */
         aptr=aptr->next;
      }
   }
   return ctr;   /* return number loaded */
}

/*********************************************/
/* LOAD_IDENT_ARRAY - load up the sort array */
/*********************************************/

static u_long load_ident_array(INODEPTR *pointer)
{
   INODEPTR iptr;
   int      i;
   u_long   ctr = 0;

   /* load the array */
   for (i=0;i<MAXHASH;i++)
   {
      iptr=im_htab[i];
      while (iptr!=NULL)
      {
         if (pointer==NULL) ctr++;       /* fancy way to just count 'em    */
         else *(pointer+ctr++)=iptr;     /* otherwise, really do the load  */
         iptr=iptr->next;
      }
   }
   return ctr;   /* return number loaded */
}

/**********************************************/
/* LOAD_SEARCH_ARRAY - load up the sort array */
/**********************************************/

static u_long load_search_array(SNODEPTR array[], SNODEPTR source_htab[])
{
   SNODEPTR sptr;
   int      i;
   u_long   ctr = 0;

   /* load the array */
   for (i=0; i<MAXSPHASH; i++)
   {
      sptr=source_htab[i];
      while (sptr!=NULL)
      {
         if (array==NULL) ctr++;     /* fancy way to just count 'em   */
         else array[ctr++]=sptr;     /* otherwise, really do the load */
         sptr=sptr->next;
      }
   }
   return ctr;   // return number loaded*/
   return 0;
}

/*********************************************/
/* QS_SITE_CMPH - QSort compare site by hits */
/*********************************************/

static int qs_site_cmph(const void *cp1, const void *cp2)
{
   u_long  t1, t2;
   t1=(*(HNODEPTR *)cp1)->count;
   t2=(*(HNODEPTR *)cp2)->count;
   if (t1!=t2) return (t2<t1)?-1:1;
   /* if hits are the same, we sort by hostname instead */
   return strcmp( (*(HNODEPTR *)cp1)->string,
                  (*(HNODEPTR *)cp2)->string );
}

/*********************************************/
/* QS_SITE_CMPK - QSort cmp site by bytes    */
/*********************************************/

static int qs_site_cmpk(const void *cp1, const void *cp2)
{
   double t1, t2;
   t1=(*(HNODEPTR *)cp1)->xfer;
   t2=(*(HNODEPTR *)cp2)->xfer;
   if (t1!=t2) return (t2<t1)?-1:1;
   /* if xfer bytes are the same, we sort by hostname instead */
   return strcmp( (*(HNODEPTR *)cp1)->string,
                  (*(HNODEPTR *)cp2)->string );
}

/*********************************************/
/* QS_URL_CMPH - QSort compare URL by hits   */
/*********************************************/

static int qs_url_cmph(const void *cp1, const void *cp2)
{
   u_long  t1, t2;
   t1=(*(UNODEPTR *)cp1)->count;
   t2=(*(UNODEPTR *)cp2)->count;
   if (t1!=t2) return (t2<t1)?-1:1;
   /* if hits are the same, we sort by url instead */
   return strcmp( (*(UNODEPTR *)cp1)->string,
                  (*(UNODEPTR *)cp2)->string );
}

/*********************************************/
/* QS_URL_CMPK - QSort compare URL by bytes  */
/*********************************************/

static int qs_url_cmpk(const void *cp1, const void *cp2)
{
   double t1, t2;
   t1=(*(UNODEPTR *)cp1)->xfer;
   t2=(*(UNODEPTR *)cp2)->xfer;
   if (t1!=t2) return (t2<t1)?-1:1;
   /* if xfer bytes are the same, we sort by url instead */
   return strcmp( (*(UNODEPTR *)cp1)->string,
                  (*(UNODEPTR *)cp2)->string );
}

/*********************************************/
/* QS_URL_CMPN - QSort compare URL by entry  */
/*********************************************/

static int qs_url_cmpn(const void *cp1, const void *cp2)
{
   double t1, t2;
   t1=(*(UNODEPTR *)cp1)->entry;
   t2=(*(UNODEPTR *)cp2)->entry;
   if (t1!=t2) return (t2<t1)?-1:1;
   /* if xfer bytes are the same, we sort by url instead */
   return strcmp( (*(UNODEPTR *)cp1)->string,
                  (*(UNODEPTR *)cp2)->string );
}

/*********************************************/
/* QS_URL_CMPX - QSort compare URL by exit   */
/*********************************************/

static int qs_url_cmpx(const void *cp1, const void *cp2)
{
   double t1, t2;
   t1=(*(UNODEPTR *)cp1)->exit;
   t2=(*(UNODEPTR *)cp2)->exit;
   if (t1!=t2) return (t2<t1)?-1:1;
   /* if xfer bytes are the same, we sort by url instead */
   return strcmp( (*(UNODEPTR *)cp1)->string,
                  (*(UNODEPTR *)cp2)->string );
}

/*********************************************/
/* QS_REF_CMPH - QSort compare Refs by hits  */
/*********************************************/

static int qs_ref_cmph(const void *cp1, const void *cp2)
{
   u_long  t1, t2;
   t1=(*(RNODEPTR *)cp1)->count;
   t2=(*(RNODEPTR *)cp2)->count;
   if (t1!=t2) return (t2<t1)?-1:1;
   /* if hits are the same, we sort by referrer URL instead */
   return strcmp( (*(RNODEPTR *)cp1)->string,
                  (*(RNODEPTR *)cp2)->string );
}

/*********************************************/
/* QS_AGNT_CMPH - QSort cmp Agents by hits   */
/*********************************************/

static int qs_agnt_cmph(const void *cp1, const void *cp2)
{
   u_long  t1, t2;
   t1=(*(ANODEPTR *)cp1)->count;
   t2=(*(ANODEPTR *)cp2)->count;
   if (t1!=t2) return (t2<t1)?-1:1;
   /* if hits are the same, we sort by agent string instead */
   return strcmp( (*(ANODEPTR *)cp1)->string,
                  (*(ANODEPTR *)cp2)->string );
}

/*********************************************/
/* QS_IDENT_CMPH - QSort cmp ident by hits   */
/*********************************************/

static int qs_ident_cmph(const void *cp1, const void *cp2)
{
   u_long  t1, t2;
   t1=(*(INODEPTR *)cp1)->count;
   t2=(*(INODEPTR *)cp2)->count;
   if (t1!=t2) return (t2<t1)?-1:1;
   /* if hits are the same, sort by ident (username) string instead */
   return strcmp( (*(INODEPTR *)cp1)->string,
                  (*(INODEPTR *)cp2)->string );
}

/*********************************************/
/* QS_SNODE_CMP_COUNT                        */
/*********************************************/

static int qs_snode_cmp_count(const void *cp1, const void *cp2)
{
   u_long  t1, t2;
   t1=(*(SNODEPTR *)cp1)->count;
   t2=(*(SNODEPTR *)cp2)->count;
   if (t1!=t2) return (t2<t1)?-1:1;
   /* if hits are the same, we sort by search string instead */
   return strcmp( (*(SNODEPTR *)cp1)->string,
                  (*(SNODEPTR *)cp2)->string );
}

/*********************************************/
/* QS_SENGINES_CMP_COUNT                     */
/*********************************************/

static int qs_sengines_cmp_count(const void *cp1, const void *cp2)
{
   u_long  t1, t2;
   t1=((struct sengine *)cp1)->count;
   t2=((struct sengine *)cp2)->count;
   if (t1!=t2) return (t2<t1)?-1:1;
   /* if hits are the same, we sort by search string instead */
   return strcmp( ((struct sengine *)cp1)->display_name,
                  ((struct sengine *)cp2)->display_name );
}