home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 71 / CDROM71.ISO / internet / navoff / data1.cab / Sources / src / htsindex.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-04-01  |  12.4 KB  |  435 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche, Yann Philippot and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or 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 this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. This project has been developed by Xavier Roche and Yann Philippot,
  29. from the company Serianet at Caen, France (http://www.serianet.com)
  30. and other contributors (see the greetings file)
  31.  
  32. Please visit our Website: http://www.httrack.com
  33. */
  34.  
  35.  
  36. /* ------------------------------------------------------------ */
  37. /* File: htsindex.c                                             */
  38. /*       keyword indexing system (search index)                 */
  39. /* Author: Xavier Roche                                         */
  40. /* ------------------------------------------------------------ */
  41.  
  42.  
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include "htsindex.h"
  46. #include "htsglobal.h"
  47. #include "htslib.h"
  48.  
  49. #if HTS_MAKE_KEYWORD_INDEX
  50. #include "htshash.h"
  51.  
  52.  
  53. /* Keyword Indexer Parameters */
  54.  
  55. // Maximum length for a keyword
  56. #define KEYW_LEN             50
  57. // Minimum length for a keyword - MUST NOT BE NULL!!!
  58. #define KEYW_MIN_LEN         3
  59. // What characters to accept? - MUST NOT BE EMPTY AND MUST NOT CONTAIN THE SPACE (32) CHARACTER!!!
  60. #define KEYW_ACCEPT          "abcdefghijklmnopqrstuvwxyz0123456789-_."
  61. // Convert A to a, and so on.. to avoid case problems in indexing
  62. // This can be a generic table, containing characters that are in fact not accepted by KEYW_ACCEPT
  63. // MUST HAVE SAME SIZES!!
  64. #define KEYW_TRANSCODE_FROM  (\
  65.                                "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
  66.                                "αΓΣ" \
  67.                                "└┬─" \
  68.                                "ΘΦΩδ" \
  69.                                "╚╚╩╦" \
  70.                                "∞ε∩" \
  71.                                "╠╬╧" \
  72.                                "≥⌠÷" \
  73.                                "╥╘╓" \
  74.                                "∙√ⁿ" \
  75.                                "┘█▄" \
  76.                                " " \
  77.                              )
  78. #define KEYW_TRANSCODE_TO    ( \
  79.                                "abcdefghijklmnopqrstuvwxyz" \
  80.                                "aaa" \
  81.                                "aaa" \
  82.                                "eeee" \
  83.                                "eeee" \
  84.                                "iii" \
  85.                                "iii" \
  86.                                "ooo" \
  87.                                "ooo" \
  88.                                "uuu" \
  89.                                "uuu" \
  90.                                "y" \
  91.                              )
  92. // These (accepted) characters will be ignored at begining of a keyword
  93. #define KEYW_IGNORE_BEG       "-_."
  94. // These (accepted) characters will be stripped if at the end of a keyword
  95. #define KEYW_STRIP_END       "-_."
  96. // Words begining with these (accepted) characters will be ignored
  97. #define KEYW_NOT_BEG         "0123456789"
  98. // Treat these characters as space characters - MUST NOT BE EMPTY!!!
  99. #define KEYW_SPACE           " ',;:!?\"\x0d\x0a\x09\x0c"
  100. // Common words (the,for..) detector
  101. // If a word represents more than KEYW_USELESS1K (%1000) of total words, then ignore it
  102. // 5 (0.5%)
  103. #define KEYW_USELESS1K       5
  104. // If a word is present in more than KEYW_USELESS1KPG (%1000) pages, then ignore it
  105. // 800 (80%)
  106. #define KEYW_USELESS1KPG     800
  107. // This number will be reduced by index hit for sorting purpose
  108. // leave it as it is here if you don't REALLY know what you are doing
  109. // Yes, I may be the only person, maybe
  110. #define KEYW_SORT_MAXCOUNT 999999999
  111.  
  112. /* End of Keyword Indexer Parameters */
  113.  
  114. int strcpos(char* adr,char c);
  115. int mystrcmp(const void* _e1,const void* _e2);
  116.  
  117. // Global variables
  118. int hts_index_init=1;
  119. int hts_primindex_size=0;
  120. FILE* fp_tmpproject=NULL;
  121. int hts_primindex_words=0;
  122.  
  123. #endif
  124.  
  125. /* 
  126.   Init index 
  127. */
  128. void index_init(const char* indexpath) {
  129. #if HTS_MAKE_KEYWORD_INDEX
  130.   /* remove(concat(indexpath,"index.txt")); */
  131.   hts_index_init=1;
  132.   hts_primindex_size=0;
  133.   hts_primindex_words=0;
  134.   fp_tmpproject=tmpfile();
  135. #endif
  136. }
  137.  
  138.  
  139. /* 
  140.    Indexing system
  141.    A little bit dirty, (quick'n dirty, in fact)
  142.    But should be okay on most cases
  143.    Tags and javascript handled (ignored)
  144. */
  145. int index_keyword(const char* html_data,LLint size,const char* mime,const char* filename,const char* indexpath) {
  146. #if HTS_MAKE_KEYWORD_INDEX
  147.   int intag=0,inscript=0,incomment=0;
  148.   char keyword[KEYW_LEN+32];
  149.   int i=0;
  150.   //
  151.   int WordIndexSize=1024;
  152.   hash_chain** WordIndexHash=NULL;
  153.   FILE *tmpfp=NULL;
  154.   //
  155.  
  156.   // Check parameters
  157.   if (!html_data)
  158.     return 0;
  159.   if (!size)
  160.     return 0;
  161.   if (!mime)
  162.     return 0;
  163.   if (!filename)
  164.     return 0;
  165.  
  166.   // Init ?
  167.   if (hts_index_init) {
  168.     remove(concat(indexpath,"index.txt"));
  169.     hts_index_init=0;
  170.   }
  171.  
  172.   // Check MIME type
  173.   if (strfield2(mime,"text/html")) {
  174.     inscript=0;
  175.   } 
  176.   else if (strfield2(mime,"application/x-javascript")) {
  177.     inscript=1;
  178.   } else
  179.     return 0;
  180.  
  181.   // Temporary file
  182.   tmpfp = tmpfile();
  183.   if (!tmpfp)
  184.     return 0;
  185.  
  186.     // Create hash structure
  187.   // Hash tables rulez da world!
  188.   WordIndexHash=(hash_chain**)calloc(WordIndexSize,sizeof(hash_chain*));
  189.   if (!WordIndexHash)
  190.     return 0;
  191.   inthash_init(WordIndexHash,WordIndexSize);
  192.  
  193.  
  194.   // Start indexing this page
  195.   keyword[0]='\0';
  196.   while(i<size) {
  197.     if (strfield(html_data + i , "<script")) {
  198.       inscript=1;
  199.     } 
  200.     else if (strfield(html_data + i , "<!--")) {
  201.       incomment=1;
  202.     }
  203.     else if (strfield(html_data + i , "</script")) {
  204.       if (!incomment)
  205.         inscript=0;
  206.     } 
  207.     else if (strfield(html_data + i , "-->")) {
  208.       incomment=0;
  209.     }
  210.     else if (html_data[i]=='<') {
  211.       if (!inscript)
  212.         intag=1;
  213.     }    
  214.     else if (html_data[i]=='>') {
  215.       intag=0;
  216.     }    
  217.     else {    
  218.       // Okay, parse keywords
  219.       if ( (!inscript) && (!incomment) && (!intag) ) {
  220.         char cchar=html_data[i];
  221.         int pos;
  222.         int len=strlen(keyword);
  223.         
  224.         // Replace (ignore case, and so on..)
  225.         if ((pos=strcpos(KEYW_TRANSCODE_FROM,cchar))>=0)
  226.           cchar=KEYW_TRANSCODE_TO[pos];
  227.         
  228.         if (strchr(KEYW_ACCEPT,cchar)) {
  229.           /* Ignore some characters at begining */
  230.           if ((len>0) || (!strchr(KEYW_IGNORE_BEG,cchar))) {
  231.             keyword[len++]=cchar;
  232.             keyword[len]='\0';
  233.           }
  234.         } else if ( (strchr(KEYW_SPACE,cchar)) || (!cchar) ) {
  235.  
  236.  
  237.           /* Avoid these words */
  238.           if (len>0) {
  239.             if (strchr(KEYW_NOT_BEG,keyword[0])) {
  240.               keyword[(len=0)]='\0';
  241.             }
  242.           }
  243.  
  244.           /* Strip ending . and so */
  245.           {
  246.             int ok=0;
  247.             while((len=strlen(keyword)) && (!ok)) {
  248.               if (strchr(KEYW_STRIP_END,keyword[len-1])) {      /* strip it */
  249.                 keyword[len-1]='\0';
  250.               } else
  251.                 ok=1;
  252.             }
  253.           }
  254.           
  255.           /* Store it ? */
  256.           if (len >= KEYW_MIN_LEN ) {
  257.             hts_primindex_words++;
  258.             if (inthash_inc(WordIndexHash,WordIndexSize,keyword)) {   /* added new */
  259.               fprintf(tmpfp,"%s\n",keyword);
  260.             }
  261.           }
  262.           keyword[(len=0)]='\0';
  263.         } else      /* Invalid */
  264.           keyword[(len=0)]='\0';
  265.  
  266.         if (len>KEYW_LEN) {
  267.           keyword[(len=0)]='\0';
  268.         }
  269.       }
  270.       
  271.     }
  272.     
  273.     i++;
  274.   }
  275.  
  276.   // Reset temp file
  277.   fseek(tmpfp,0,SEEK_SET);
  278.  
  279.   // Process indexing for this page
  280.   {
  281.     //FILE* fp=NULL;
  282.     //fp=fopen(concat(indexpath,"index.txt"),"ab");
  283.     if (fp_tmpproject) {
  284.       while(!feof(tmpfp)) {
  285.         char line[KEYW_LEN + 32];
  286.         linput(tmpfp,line,KEYW_LEN + 2);
  287.         if (strnotempty(line)) {
  288.           unsigned long int e=0;
  289.           if (inthash_read(WordIndexHash,WordIndexSize,line,&e)) {
  290.             //if (e) {
  291.             char savelst[HTS_URLMAXSIZE*2];
  292.             e++;          /* 0 means "once" */
  293.             
  294.             if (strncmp((const char*)fslash((char*)indexpath),filename,strlen(indexpath))==0)  // couper
  295.               strcpy(savelst,filename+strlen(indexpath));
  296.             else
  297.               strcpy(savelst,filename);
  298.             
  299.             // Add entry for this file and word
  300.             fprintf(fp_tmpproject,"%s %d %s\n",line,(int) (KEYW_SORT_MAXCOUNT - e),savelst);
  301.             hts_primindex_size++;
  302.             //}
  303.           }
  304.         }
  305.       }
  306.       //fclose(fp);
  307.     }
  308.   }
  309.  
  310.   // Delete temp file
  311.   fclose(tmpfp);
  312.   tmpfp=NULL;
  313.  
  314.   // Clear hash table
  315.   inthash_del(WordIndexHash,WordIndexSize);
  316. #endif
  317.   return 1;
  318. }
  319.  
  320. /*
  321.   Sort index!
  322. */
  323. void index_finish(const char* indexpath) {
  324. #if HTS_MAKE_KEYWORD_INDEX
  325.   char** tab;
  326.   char* blk;
  327.   int size;
  328.   
  329.   size=fpsize(fp_tmpproject);
  330.   if (size>0) {
  331.     //FILE* fp=fopen(concat(indexpath,"index.txt"),"rb");
  332.     if (fp_tmpproject) {
  333.       tab=(char**)malloc(sizeof(char*) * (hts_primindex_size+2) );
  334.       if (tab) {
  335.         blk = malloc(size+4);
  336.         if (blk) {
  337.           fseek(fp_tmpproject,0,SEEK_SET);
  338.           if ((int)fread(blk,1,size,fp_tmpproject) == size) {
  339.             char *a=blk,*b;
  340.             int index=0;
  341.             int i;
  342.             FILE* fp;
  343.  
  344.             while( (b=strchr(a,'\n')) && (index < hts_primindex_size) ) {
  345.               tab[index++]=a;
  346.               *b='\0';
  347.               a=b+1;
  348.             }
  349.             
  350.             // Sort it!
  351.             qsort(tab,index,sizeof(char*),mystrcmp);
  352.  
  353.             // Delete fp_tmpproject
  354.             fclose(fp_tmpproject);
  355.             fp_tmpproject=NULL;
  356.  
  357.             // Write new file
  358.             fp=fopen(concat(indexpath,"index.txt"),"wb");
  359.             if (fp) {
  360.               char current_word[KEYW_LEN + 32];
  361.               char word[KEYW_LEN + 32];
  362.               int hit;
  363.               int total_hit=0;
  364.               int total_line=0;
  365.               int last_pos=0;
  366.               current_word[0]='\0';
  367.               for(i=0;i<index;i++) {
  368.                 if (sscanf(tab[i],"%s %d",word,&hit) == 2) {
  369.                   char*  a=strchr(tab[i],' ');
  370.                   if (a) a=strchr(a+1,' ');
  371.                   if (a++) {                            /* Yes, a++, not ++a :) */
  372.                     hit=KEYW_SORT_MAXCOUNT-hit;
  373.                     if (strcmp(word,current_word)) {    /* New word */
  374.                       if (total_hit) {
  375.                         fprintf(fp,"\t=%d\r\n",total_hit);
  376.                         if ( 
  377.                               ( ((total_hit*1000 ) / hts_primindex_words) >= KEYW_USELESS1K   )
  378.                             ||
  379.                               ( ((total_line*1000) / index              ) >= KEYW_USELESS1KPG )
  380.                           ) {
  381.                           fseek(fp,last_pos,SEEK_SET);
  382.                           fprintf(fp,"\tignored (%d)\r\n",((total_hit*1000)/hts_primindex_words));
  383.                         }
  384.                         else
  385.                           fprintf(fp,"\t(%d)\r\n",((total_hit*1000)/hts_primindex_words));
  386.                       }
  387.                       fprintf(fp,"%s\r\n",word);
  388.                       fflush(fp); last_pos=ftell(fp);
  389.                       strcpy(current_word,word);
  390.                       total_hit=total_line=0;
  391.                     }
  392.                     total_hit+=hit;
  393.                     total_line++;
  394.                     fprintf(fp,"\t%d %s\r\n",hit,a);
  395.                   }
  396.                 }
  397.               }
  398.               fclose(fp);
  399.             }
  400.             
  401.           }
  402.           free(blk);
  403.         }
  404.         free(tab);
  405.       }
  406.  
  407.     }
  408.     //qsort
  409.   }
  410.   if (fp_tmpproject)
  411.     fclose(fp_tmpproject);
  412.   fp_tmpproject=NULL;
  413. #endif
  414. }
  415.  
  416.  
  417. /* Subroutines */
  418.  
  419. #if HTS_MAKE_KEYWORD_INDEX
  420. int strcpos(char* adr,char c) {
  421.   char* apos=strchr(adr,c);
  422.   if (apos)
  423.     return (int)(apos-adr);
  424.   else
  425.     return -1;
  426. }
  427.  
  428. int mystrcmp(const void* _e1,const void* _e2) {
  429.   char** e1=(char**)_e1;
  430.   char** e2=(char**)_e2;
  431.   return strcmp(*e1,*e2);
  432. }
  433. #endif
  434.  
  435.