home *** CD-ROM | disk | FTP | other *** search
/ PC Open 48 / pcopen48.iso / Internet / HtTrack / DATA1.CAB / Sources / src / htshash.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-21  |  9.8 KB  |  337 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche, Yann Philippot
  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.  
  31. Please visit our Website: http://www.httrack.com
  32. */
  33.  
  34.  
  35. /* ------------------------------------------------------------ */
  36. /* File: httrack.c subroutines:                                 */
  37. /*       hash table system (fast index)                         */
  38. /* Author: Xavier Roche                                         */
  39. /* ------------------------------------------------------------ */
  40.  
  41. #include "htshash.h"
  42.  
  43. /* specific definitions */
  44. #include "htsbase.h"
  45. #include "htsmd5.h"
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. /* END specific definitions */
  50.  
  51. // GESTION DES TABLES DE HACHAGE
  52. // MΘthode α 2 clΘs (adr+fil), 2e cle facultative
  53. // hash[no_enregistrement][pos]->hash est un index dans le tableau gΘnΘral liens
  54. // #define HTS_HASH_SIZE 8191  (premier si possible!)
  55. // type: numero enregistrement - 0 est case insensitive (sav) 1 (adr+fil) 2 (former_adr+former_fil)
  56. #if HTS_HASH
  57. // recherche dans la table selon nom1,nom2 et le no d'enregistrement
  58. // retour: position ou -1 si non trouvΘ
  59. int hash_read(hash_struct* hash,char* nom1,char* nom2,int type) {
  60.   unsigned int cle;
  61.   int pos; 
  62.   // calculer la clΘ de recherche, non modulΘe
  63.   if (type)
  64.     cle = hash_cle(nom1,nom2);
  65.   else
  66.     cle = hash_cle(convtolower(nom1),nom2);         // case insensitive
  67.   // la position se calcule en modulant
  68.   pos = (int) (cle%HTS_HASH_SIZE);
  69.   // entrΘe trouvΘe?
  70.   if (hash->hash[type][pos] >= 0) {             // un enregistrement avec une telle clΘ existe..
  71.     // tester table de raccourcis (hash)
  72.     // pos est maintenant la position recherchΘe dans liens
  73.     pos = hash->hash[type][pos];
  74.     while (pos>=0) {              // parcourir la chaine
  75.       switch (type) {
  76.       case 0:         // sav
  77.         if (strfield2(nom1,hash->liens[pos]->sav)) {  // case insensitive
  78. #if DEBUG_HASH==2
  79.           printf("hash: found shortcut at %d\n",pos);
  80. #endif
  81.           return pos;
  82.         }
  83.         break;
  84.       case 1:         // adr+fil
  85.         if ((strcmp(nom1,jump_identification(hash->liens[pos]->adr))==0) && (strcmp(nom2,hash->liens[pos]->fil)==0)) {
  86. #if DEBUG_HASH==2
  87.           printf("hash: found shortcut at %d\n",pos);
  88. #endif
  89.           return pos;
  90.         }
  91.         break;
  92.       case 2:         // former_adr+former_fil
  93.         if (hash->liens[pos]->former_adr)
  94.         if ((strcmp(nom1,jump_identification(hash->liens[pos]->former_adr))==0) && (strcmp(nom2,hash->liens[pos]->former_fil)==0)) {
  95. #if DEBUG_HASH==2
  96.           printf("hash: found shortcut at %d\n",pos);
  97. #endif
  98.           return pos;
  99.         }
  100.         break;
  101.       }
  102.       // calculer prochaine position dans la chaine
  103.       {
  104.         int old=pos;
  105.         pos=hash->liens[pos]->hash_next[type];   // sinon prochain dans la chaine
  106.         if (old==pos)
  107.           pos=-1;         // erreur de bouclage (ne devrait pas arriver)
  108.       }
  109.     }
  110.     
  111.     // Ok va falloir chercher alors..
  112.     /*pos=hash->max_lien;    // commencer α max_lien
  113.     switch (type) {
  114.     case 0:         // sav
  115.       while(pos>=0) {
  116.         if (hash->liens[pos]->hash_sav == cle ) {
  117.           if (strcmp(nom1,hash->liens[pos]->sav)==0) {
  118.             hash->hash[type][(int) (cle%HTS_HASH_SIZE)] = pos;    // noter plus rΘcent dans shortcut table
  119. #if DEBUG_HASH==2
  120.             printf("hash: found long search at %d\n",pos);
  121. #endif
  122.             return pos;
  123.           }
  124.         }
  125.         pos--;
  126.       }
  127.       break;
  128.     case 1:         // adr+fil
  129.       while(pos>=0) {
  130.         if (hash->liens[pos]->hash_adrfil == cle ) {
  131.           if ((strcmp(nom1,hash->liens[pos]->adr)==0) && (strcmp(nom2,hash->liens[pos]->fil)==0)) {
  132.             hash->hash[type][(int) (cle%HTS_HASH_SIZE)] = pos;    // noter plus rΘcent dans shortcut table
  133. #if DEBUG_HASH==2
  134.             printf("hash: found long search at %d\n",pos);
  135. #endif
  136.             return pos;
  137.           }
  138.         }
  139.         pos--;
  140.       }
  141.       break;
  142.     case 2:         // former_adr+former_fil
  143.       while(pos>=0) {
  144.         if (hash->liens[pos]->hash_fadrfil == cle ) {
  145.           if (hash->liens[pos]->former_adr)
  146.             if ((strcmp(nom1,hash->liens[pos]->former_adr)==0) && (strcmp(nom2,hash->liens[pos]->former_fil)==0)) {
  147.             hash->hash[type][(int) (cle%HTS_HASH_SIZE)] = pos;    // noter plus rΘcent dans shortcut table
  148. #if DEBUG_HASH==2
  149.             printf("hash: found long search at %d\n",pos);
  150. #endif
  151.             return pos;
  152.           }
  153.         }
  154.         pos--;
  155.       }
  156.     }*/
  157. #if DEBUG_HASH==1
  158.     printf("hash: not found after test %s%s\n",nom1,nom2);
  159. #endif
  160.     return -1;    // non trouvΘ
  161.   } else {
  162. #if DEBUG_HASH==2
  163.     printf("hash: not found %s%s\n",nom1,nom2);
  164. #endif
  165.     return -1;    // non trouvΘ : clΘ non entrΘe (mΩme une fois)
  166.   }
  167. }
  168.  
  169. // enregistrement lien lpos dans les 3 tables hash1..3
  170. void hash_write(hash_struct* hash,int lpos) {
  171.   unsigned int cle;
  172.   int pos; 
  173.   int* ptr;
  174.   //
  175.   if (hash->liens[lpos]) {                       // on sait jamais..
  176.     hash->max_lien = max(hash->max_lien,lpos);
  177. #if DEBUG_HASH
  178.     hashnumber=hash->max_lien;
  179. #endif
  180.     // ΘlΘment actuel sur -1 (fin de chaine)
  181.     hash->liens[lpos]->hash_next[0]=hash->liens[lpos]->hash_next[1]=hash->liens[lpos]->hash_next[2]=-1;
  182.     //
  183.     cle = hash_cle(convtolower(hash->liens[lpos]->sav),"");    // CASE INSENSITIVE
  184.     pos = (int) (cle%HTS_HASH_SIZE);
  185.     ptr = hash_calc_chaine(hash,0,pos);         // calculer adresse chaine
  186.     *ptr = lpos;                   // noter dernier enregistrΘ
  187. #if DEBUG_HASH==3
  188.     printf("[%d",pos);
  189. #endif
  190.     //
  191.     cle = hash_cle(jump_identification(hash->liens[lpos]->adr),hash->liens[lpos]->fil);
  192.     pos = (int) (cle%HTS_HASH_SIZE);
  193.     ptr = hash_calc_chaine(hash,1,pos);         // calculer adresse chaine
  194.     *ptr = lpos;                   // noter dernier enregistrΘ
  195. #if DEBUG_HASH==3
  196.     printf(",%d",pos);
  197. #endif
  198.     //
  199.     if (hash->liens[lpos]->former_adr) {         // former_adr existe?
  200.       cle = hash_cle(jump_identification(hash->liens[lpos]->former_adr),hash->liens[lpos]->former_fil);
  201.       pos = (int) (cle%HTS_HASH_SIZE);
  202.       ptr = hash_calc_chaine(hash,2,pos);         // calculer adresse chaine
  203.       *ptr = lpos;                   // noter dernier enregistrΘ
  204. #if DEBUG_HASH==3
  205.       printf(",%d",pos);
  206. #endif
  207.     }
  208. #if DEBUG_HASH==3
  209.     printf("] "); fflush(stdout);
  210. #endif
  211.   }
  212. #if DEBUT_HASH
  213.   else {
  214.     printf("* hash_write=0!!\n");
  215.     exit(1);
  216.   }
  217. #endif
  218.   //
  219. }
  220.  
  221. // calcul clΘ
  222. // il n'y a pas de formule de hashage universelle, celle-ci semble acceptable..
  223. unsigned long int hash_cle(char* nom1,char* nom2) {
  224.   /*
  225.   unsigned int sum=0;
  226.   int i=0;
  227.   while(*nom1) {
  228.     sum += 1;
  229.     sum += (unsigned int) *(nom1);
  230.     sum *= (unsigned int) *(nom1++);
  231.     sum += (unsigned int) i;
  232.     i++;
  233.   }
  234.   while(*nom2) {
  235.     sum += 1;
  236.     sum += (unsigned int) *(nom2);
  237.     sum *= (unsigned int) *(nom2++);
  238.     sum += (unsigned int) i;
  239.     i++;
  240.   }
  241.   */
  242.   return md5sum32(nom1)
  243.         +md5sum32(nom2);
  244. }
  245.  
  246. // calcul de la position finale dans la chaine des elements ayant la mΩme clΘ
  247. int* hash_calc_chaine(hash_struct* hash,int type,int pos) {
  248. #if DEBUG_HASH
  249.   int count=0;
  250. #endif
  251.   if (hash->hash[type][pos] == -1)
  252.     return &(hash->hash[type][pos]);    // premier ΘlΘment dans la chaine
  253.   pos=hash->hash[type][pos];
  254.   while(hash->liens[pos]->hash_next[type] != -1) {
  255.     pos = hash->liens[pos]->hash_next[type];
  256. #if DEBUG_HASH
  257.     count++;
  258. #endif
  259.   }
  260. #if DEBUG_HASH
  261.   count++;
  262.   longest_hash[type]=max(longest_hash[type],count);
  263. #endif
  264.   return &(hash->liens[pos]->hash_next[type]);
  265. }
  266. #endif
  267. // FIN GESTION DES TABLES DE HACHAGE
  268.  
  269. // --
  270.  
  271. void inthash_add(hash_chain** hash,int hash_size,char* str,long int e) {
  272.   int pos = (hash_cle(str,"") % hash_size);
  273.   hash_chain** h=&hash[pos];
  274.  
  275.   while (*h)
  276.     h=&((*h)->next);
  277.   *h=(hash_chain*)calloc(1,sizeof(hash_chain));
  278.   if (h) {
  279.     (*h)->next=NULL;
  280.     strcpy((*h)->value,str);
  281.     (*h)->pos=e;
  282.   }
  283. }
  284.  
  285. int inthash_read(hash_chain** hash,int hash_size,char* str,long int* e) {
  286.   int pos = (hash_cle(str,"") % hash_size);
  287.   hash_chain* h=hash[pos];
  288.   while (h) {
  289.     if (strcmp(h->value,str)==0) {
  290.       *e=h->pos;
  291.       return 1;
  292.     }
  293.     h=h->next;
  294.   }
  295.   return 0;
  296. }
  297.  
  298. void inthash_init(hash_chain** hash,int hash_size) {
  299.   int i;
  300.   for(i=0;i<hash_size;i++) {
  301.     hash[i]=NULL;
  302.   }
  303. }
  304.  
  305. void inthash_del(hash_chain** hash,int hash_size) {
  306.   if (hash) {
  307.     int i;
  308.     for(i=0;i<hash_size;i++) {
  309.       inthash_delchain(hash[i],0);
  310.       hash[i]=NULL;
  311.     }
  312.   }
  313. }
  314.  
  315. void inthash_mdel(hash_chain** hash,int hash_size) {
  316.   if (hash) {
  317.     int i;
  318.     for(i=0;i<hash_size;i++) {
  319.       inthash_delchain(hash[i],1);
  320.       hash[i]=NULL;
  321.     }
  322.   }
  323. }
  324.  
  325. void inthash_delchain(hash_chain* hash,int free_int) {
  326.   if (hash) {
  327.     inthash_delchain(hash->next,free_int);
  328.     if (free_int) {     // pos is a malloc() block, delete it!
  329.       if (hash->pos)
  330.         free((void*)hash->pos);
  331.       hash->pos=0;
  332.     }
  333.     free(hash);
  334.   }
  335. }
  336.  
  337.