/* Copyright (C) 2000, 2001  SWsoft, Singapore
 *
 *  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
 */

/*  $Id: templates.h,v 1.21 2001/05/16 14:10:29 kir Exp $
	Author: Alexander F. Avdonkin
	Uses parts of UdmSearch code
*/

#ifndef _TEMPLATE_H_
#define _TEMPLATE_H_

#include "maps.h"
#include "defines.h"
#include "documents.h"
#ifdef UNICODE
#include "ucharset.h"
#endif
#include <stdio.h>
#include <string>
#include <hash_set>

class CStringSet;

typedef vector<string> CStringVector;
class CStringStringVector : public vector<CStringVector*>
{
public:
	~CStringStringVector()
	{
		for (CStringVector** v = begin(); v != end(); v++)
		{
			if (*v) delete *v;
		}
	}
};

class CUrlW
{
public:
	ULONG m_urlID;
	ULONG m_siteID;
	ULONG m_count;
	ULONG m_weight;
};

class CPUrlW
{
public:
	CUrlW* m_purlw;

public:
	int operator<(const CPUrlW& uw) const
	{
		if (m_purlw->m_weight == uw.m_purlw->m_weight)
		{
			int res = (int)(m_purlw->m_count >> 24) - (int)(uw.m_purlw->m_count >> 24);
			if (res == 0)
			{
				res = m_purlw->m_urlID - uw.m_purlw->m_urlID;
			}
			return res;
		}
		else
		{
			return m_purlw->m_weight > uw.m_purlw->m_weight;
		}
	}
};

class CPUrlWM
{
public:
	CUrlW* m_purlw;

public:
	int operator<(const CPUrlWM& uw) const
	{
		return (m_purlw->m_count >> 24) < (uw.m_purlw->m_count >> 24);
	}
};

class CBufferedSocket;

class CSrcDocumentS : public CSrcDocument
{
public:
	void Send(CBufferedSocket& socket);
	void Recv(CBufferedSocket& socket);
#ifdef UNICODE
	void RecodeTo(CCharsetB* charset);
	void GetHilightPos(CCharsetB* charset, CStringSet& lightWords);
#endif
};

enum
{
	SEARCH_MODE_NONE,
	SEARCH_MODE_EXACT,
	SEARCH_MODE_FORMS
};

enum
{
	ACCOUNT_DISTANCE_UNK,
	ACCOUNT_DISTANCE_YES,
	ACCOUNT_DISTANCE_NO
};

enum
{
	SORT_BY_RATE,
	SORT_BY_DATE
};

class CCgiQuery
{
public:
	string m_query_words;
	string m_results;
	string m_cached;
	string m_include_words;
	string m_exclude_words;
	string m_includeSites;
	string m_excludeSites;
	string m_ul_unescaped;
	string m_tmplp;
	string m_tmpl_url_escaped;
	string m_query_url_escaped;
	string m_query_form_escaped;
	string m_qr_form_escaped;
	string m_qr_url_escaped;
	string m_charset;
	/// What languages to use in FindForms (comma-separated list). Passed in fm=
	string m_wordform_langs;	
	int m_format;
	int m_np;
	int m_ps;
	int m_inres;
	int m_posmask;
	int m_take;
	int m_iph;
	int m_xph;
	int m_gr;
	int m_site;
	int m_search_mode;
	int m_sort_by;
	int m_accDist;
	time_t m_t_from;	//< time filter - from time
	time_t m_t_to;		//< time filter - to time
	CULONGSet m_spaces;
	char m_cspaces[300];

public:
	CCgiQuery()
	{
		m_posmask = 0;
		m_format = 0;
		m_inres = 0;
		m_take = 0;
		m_iph = 0;
		m_xph = 0;
		m_np = 0;
		m_ps = 0;
		m_gr = 1;
		m_site = 0;
		m_cspaces[0] = 0;
		m_search_mode = SEARCH_MODE_NONE;
		m_sort_by = SORT_BY_RATE;
		m_t_from = 0;
		m_t_to = 0;
		m_accDist = ACCOUNT_DISTANCE_UNK;
	}
	void Send(CBufferedSocket& sock);
	int Recv(CBufferedSocket& sock);
	ULONG MakeQueryKey(char* query);
};

class CCgiQueryC : public CCgiQuery
{
public:
	char m_r_str[STRSIZ];
	char m_ul_str[STRSIZ];

public:
	CCgiQueryC()
	{
		m_r_str[0] = 0;
		m_ul_str[0] = 0;
	}
	void FormQuery();
	void FormHrefs(char* href, char* hrefwg);
	int ParseCgiQuery(char* query, char* templ);
};

extern char self [STRSIZ];
extern char wordinfo[STRSIZ];
extern char href[STRSIZ];
extern char error[BUFSIZ];
extern char hrefwg[STRSIZ];
//extern string query_form_escaped, qr_form_escaped, qr_url_escaped;
//extern string tmpl_url_escaped;
extern ULONG icolor;

extern char defaultw[];

class CDataSource;

int LoadTemplate(char *name, const char *query, char* default_where = defaultw);
int PrintTemplate(CDataSource& database, char* Target, const char* where, const char *url, const char *content_type, const char *last_modified);
int PrintTemplate(CDataSource& database, const char* where, const char *url, const char *content_type, const char *last_modified);
int PrintOneTemplate(CDataSource& database, char* Target, const char *s, const char* where, const char *url, const char *content_type, const char *last_modified);
int PrintOption(CDataSource& database, char* Target, const char* where, const char* option);
void ClearTemplates();
char * UnescapeCGIQuery(char *d, char *s);
char* EscapeHtml(char *d, const char *s, char what);
char* EscapeURL(char *d, const char *s);
void HtmlSpecialChars(const char *str, char* p);
int InitRand();
int Match(const char* string, const char* pattern);
void PrepareHighlite();

void SendString(int socket, const char* string, int len);
void SendString(int socket, const char* string);
int RecvString(int socket, char* string);
int RecvString(int socket, string& str);
int RecvULONG(int socket, ULONG& val);
int RecvULONG(int socket, long& val);
void SendULONG(int socket, ULONG val);
void InitErrorMes();

void AddIE(string& qwords, string& is, string& xs);
string CombineIE(string& include, string& exclude, int iphrase, int xphrase);

extern int pps, use_clones;
extern int found;
extern ULONG docnum, first, last;
//extern unsigned int format;
extern char *mode;
//extern CULONGSet spaces;
//extern int gr;
extern double ttime;
extern CUrlW* urlw;
extern int  size;
extern char title[MAXTITLESIZE * 5];
extern CStringVector texts;
extern char descr[MAXDESCSIZE * 5];
extern char keywords[MAXKEYWORDSIZE * 5];
extern int num_pages, navpage, is_next;
//extern string ul_unescaped;
extern const char *crc;

extern int Port;
//extern unsigned long DaemonAddress;
extern int MaxExcerpts;
extern ULONG MaxExcerptLen;

extern string LocalCharset;

#define MAX_PS		100
#define DEFAULT_PS	20

enum
{
	GET_SITE = 1,
	GET_DOCUMENT = 2,
	GET_CLONELIST = 3,
	ADD_STAT = 4,
	GET_ERROR = 5,
	GET_DOCUMENTS = 6
};

extern CCgiQueryC cgiQuery;

#ifdef UNICODE
extern CULONGVector* titleH;
extern CULONGVector* textH;
extern CULONGVector* descrH;
extern CULONGVector* keywordsH;
#endif

#endif

