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
|
/* Copyright (C) 2000-2006 Lavtech.com corp. All rights reserved.
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.
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 "udm_config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "udm_common.h"
#include "udm_spell.h"
#include "udm_hrefs.h"
#include "udm_utils.h"
#include "udm_xmalloc.h"
#include "udm_sgml.h"
#include "udm_url.h"
#include "udm_vars.h"
/* Max URLs in cache: 4K URLs will use about 200K of RAM */
/* This should be a configurable parameter but we'll use 4K now */
#define HSIZE 256 /* Length of buffer increment TUNE */
#define RESORT_HREFS 256 /* Max length of unsorted part TUNE */
UDM_HREF *UdmHrefInit(UDM_HREF * H)
{
bzero((void*)H, sizeof(*H));
return(H);
}
void UdmHrefFree(UDM_HREF * H)
{
if (H->url)
UdmFree(H->url);
UdmVarListFree(&H->Vars);
}
/* Function to sort URLs in alphabetic order */
static int cmphrefs(const void * v1, const void * v2)
{
return(strcmp(((const UDM_HREF*)v1)->url,((const UDM_HREF*)v2)->url));
}
__C_LINK int __UDMCALL UdmHrefListAdd(UDM_HREFLIST * HrefList,UDM_HREF * Href)
{
int l,r,c,res;
size_t len;
char *ehref;
/* Don't add an empty link */
if (!(len= strlen(Href->url)) ||
!(ehref = (char*)UdmMalloc(3*len + 1)))
return 0;
UdmURLCanonize(Href->url, ehref, 3*len+1);
UdmSGMLUnescape(ehref);
/* Find current URL in sorted part of list */
l=0;r=HrefList->shrefs-1;
while(l<=r)
{
c=(l+r)/2;
if(!(res=strcmp(HrefList->Href[c].url,ehref)))
{
HrefList->Href[c].stored = Href->stored;
HrefList->Href[c].referrer = Href->referrer;
HrefList->Href[c].hops = Href->hops;
HrefList->Href[c].method = Href->method;
HrefList->Href[c].stored = Href->stored;
HrefList->Href[c].site_id = Href->site_id;
HrefList->Href[c].server_id = Href->server_id;
HrefList->Href[c].rec_id = Href->rec_id;
HrefList->Href[c].max_doc_per_site = Href->max_doc_per_site;
HrefList->Href[c].collect_links= Href->collect_links;
UdmVarListFree(&HrefList->Href[c].Vars);
UdmVarListInit(&HrefList->Href[c].Vars);
UdmVarListReplaceLst(&HrefList->Href[c].Vars, &Href->Vars, NULL, "*");
UDM_FREE(ehref);
return(0);
}
if(res<0)
l=c+1;
else
r=c-1;
}
/* Find in unsorted part */
for(c= HrefList->shrefs; c < HrefList->nhrefs; c++)
{
if(!strcmp(HrefList->Href[c].url,ehref))
{
HrefList->Href[c].stored = Href->stored;
HrefList->Href[c].referrer = Href->referrer;
HrefList->Href[c].hops = Href->hops;
HrefList->Href[c].method = Href->method;
HrefList->Href[c].stored = Href->stored;
HrefList->Href[c].site_id = Href->site_id;
HrefList->Href[c].server_id = Href->server_id;
HrefList->Href[c].rec_id = Href->rec_id;
HrefList->Href[c].max_doc_per_site = Href->max_doc_per_site;
HrefList->Href[c].collect_links= Href->collect_links;
UdmVarListFree(&HrefList->Href[c].Vars);
UdmVarListInit(&HrefList->Href[c].Vars);
UdmVarListReplaceLst(&HrefList->Href[c].Vars, &Href->Vars, NULL, "*");
UDM_FREE(ehref);
return(0);
}
}
if(HrefList->nhrefs>=HrefList->mhrefs)
{
HrefList->mhrefs+=HSIZE;
HrefList->Href=(UDM_HREF *)UdmRealloc(HrefList->Href,HrefList->mhrefs*sizeof(UDM_HREF));
}
HrefList->Href[HrefList->nhrefs].url = (char*)UdmStrdup(ehref);
HrefList->Href[HrefList->nhrefs].referrer=Href->referrer;
HrefList->Href[HrefList->nhrefs].hops=Href->hops;
HrefList->Href[HrefList->nhrefs].method=Href->method;
HrefList->Href[HrefList->nhrefs].stored=Href->stored;
HrefList->Href[HrefList->nhrefs].site_id = Href->site_id;
HrefList->Href[HrefList->nhrefs].server_id = Href->server_id;
HrefList->Href[HrefList->nhrefs].rec_id = Href->rec_id;
HrefList->Href[HrefList->nhrefs].max_doc_per_site= Href->max_doc_per_site;
HrefList->Href[HrefList->nhrefs].collect_links= Href->collect_links;
UdmVarListInit(&HrefList->Href[HrefList->nhrefs].Vars);
UdmVarListReplaceLst(&HrefList->Href[HrefList->nhrefs].Vars,
&Href->Vars, NULL, "*");
HrefList->nhrefs++;
/* Sort unsorted part */
if((HrefList->nhrefs-HrefList->shrefs)>RESORT_HREFS)
{
UdmSort(HrefList->Href,HrefList->nhrefs,sizeof(UDM_HREF),cmphrefs);
/* Remember count of sorted URLs */
HrefList->shrefs=HrefList->nhrefs;
/* Count of stored URLs became 0 */
HrefList->dhrefs=0;
}
UDM_FREE(ehref);
return(1);
}
extern __C_LINK void __UDMCALL UdmHrefListFree(UDM_HREFLIST * HrefList)
{
size_t i;
for(i=0;i<HrefList->nhrefs;i++)
UdmHrefFree(&HrefList->Href[i]);
UDM_FREE(HrefList->Href);
bzero((void*)HrefList, sizeof(*HrefList));
}
__C_LINK UDM_HREFLIST * __UDMCALL UdmHrefListInit(UDM_HREFLIST * Hrefs)
{
bzero((void*)Hrefs, sizeof(*Hrefs));
return(Hrefs);
}
|