File: doc.c

package info (click to toggle)
mnogosearch 3.3.7-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 17,484 kB
  • ctags: 4,565
  • sloc: ansic: 94,097; xml: 16,864; sh: 8,915; makefile: 1,727; perl: 801; php: 561; sql: 15
file content (156 lines) | stat: -rw-r--r-- 4,053 bytes parent folder | download
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
/* 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 <sys/types.h>
#include <stdlib.h>
#include <string.h>

#include "udm_common.h"
#include "udm_doc.h"
#include "udm_utils.h"
#include "udm_doc.h"
#include "udm_word.h"
#include "udm_crossword.h"
#include "udm_hrefs.h"
#include "udm_guesser.h"
#include "udm_vars.h"
#include "udm_textlist.h"
#include "udm_parsehtml.h"
#include "udm_xmalloc.h"
#include "udm_url.h"

UDM_DOCUMENT * __UDMCALL UdmDocInit(UDM_DOCUMENT * Doc)
{
  if (!Doc)
  {
    Doc= (UDM_DOCUMENT*) UdmMalloc(sizeof(UDM_DOCUMENT));
    bzero((void*)Doc, sizeof(UDM_DOCUMENT));
    Doc->freeme=1;
  }
  else
  {
    bzero((void*)Doc, sizeof(UDM_DOCUMENT));
  }
  Doc->Spider.read_timeout= UDM_READ_TIMEOUT;
  Doc->Spider.doc_timeout= UDM_DOC_TIMEOUT;
  Doc->Spider.net_error_delay_time= UDM_DEFAULT_NET_ERROR_DELAY_TIME;
  Doc->connp.connp= (UDM_CONN*) UdmXmalloc(sizeof(UDM_CONN)); /* need UdmXmalloc to avoid a trap */
  UdmURLInit(&Doc->CurURL);
  return Doc;
}


void __UDMCALL UdmDocFree(UDM_DOCUMENT *Result)
{
  if(!Result)return;
  UDM_FREE(Result->Buf.buf);
  UDM_FREE(Result->connp.hostname);
  UDM_FREE(Result->connp.user);
  UDM_FREE(Result->connp.pass);
  UDM_FREE(Result->connp.connp);
  
  UdmHrefListFree(&Result->Hrefs);
  UdmWordListFree(&Result->Words);
  UdmCrossListFree(&Result->CrossWords);
  UdmVarListFree(&Result->RequestHeaders);
  UdmVarListFree(&Result->Sections);
  UdmTextListFree(&Result->TextList);
  
  UdmURLFree(&Result->CurURL);

  if(Result->freeme)
  {
    UDM_FREE(Result);
  }
  else
  {
    bzero((void*)Result, sizeof(UDM_DOCUMENT));
  }
}


#define nonul(x)  ((x)?(x):"")

int UdmDocToTextBuf(UDM_DOCUMENT * Doc,char *textbuf,size_t len)
{
  size_t  i;
  char  *end;
  
  textbuf[0]='\0';
  
  udm_snprintf(textbuf, len, "<DOC");
  end=textbuf+strlen(textbuf);
  
  for(i=0;i<Doc->Sections.nvars;i++)
  {
    UDM_VAR  *S=&Doc->Sections.Var[i];
    if(!S->name || !S->val ||!S->val[0])
      continue;
    if(!S->section && 
       strcasecmp(S->name,"ID") &&
       strcasecmp(S->name,"URL") &&
       strcasecmp(S->name,"Status") &&
       strcasecmp(S->name,"Content-Type") &&
       strcasecmp(S->name,"Content-Length") &&
       strcasecmp(S->name,"Content-Language") &&
       strcasecmp(S->name,"Last-Modified") &&
       strcasecmp(S->name,"Tag") &&
       strcasecmp(S->name,"Category"))
      continue;
    
    udm_snprintf(end, len - (end - textbuf), "\t%s=\"%s\"", S->name, S->val);
    end = end + strlen(end);
  }
  if (len - (end - textbuf) > 1) strcpy(end, ">");
  return UDM_OK;
}


int __UDMCALL UdmDocFromTextBuf(UDM_DOCUMENT * Doc,const char *textbuf)
{
  const char  *htok, *last;
  UDM_HTMLTOK  tag;
  size_t    i;
  
  if (textbuf == NULL) return UDM_OK;
  
  UdmHTMLTOKInit(&tag);
  
  htok=UdmHTMLToken(textbuf,&last,&tag);
  
  if(!htok || tag.type!=UDM_HTML_TAG)
    return UDM_OK;
  
  for(i=1;i<tag.ntoks;i++)
  {
    size_t  nlen=tag.toks[i].nlen;
    size_t  vlen=tag.toks[i].vlen;
    char  *name = UdmStrndup(tag.toks[i].name,nlen);
    char  *data = UdmStrndup(tag.toks[i].val,vlen);
    UDM_VAR  Sec;
    bzero((void*)&Sec, sizeof(Sec));
    Sec.name=name;
    Sec.val=data;
    UdmVarListReplace(&Doc->Sections, &Sec);
    UDM_FREE(name);
    UDM_FREE(data);
  }
  return 0;
}