//////////////////
/*

The CLucene dll is an easy way of dealing with a clucene index. It is not
object oriented, so can easily be implemented in other languages, however
this is at the sacrifice of some flexibility


Configuration:

Methods Macros:
---------------

Global methods:

Cleanup all resources
CL_T_CLEANUP (  )

Return the last error from the specified resource
CL_T_ERRSTR ( resourceID, Char_t* ret, int ret_len )

Return the last non-resource specific error
CL_T_ERRSTRGLOBAL ( Char_t* ret, int ret_len )

Unlock the specified directory (in case an error occurred and left the index locked)
CL_T_UNLOCK ( char* directory)


Resource methods:
Open the specified directory, create it if it doesn't exist
CL_T_OPEN (char* dir, bool create)

Close the resource
CL_T_CLOSE ( resourceID )

Reload the resources reader to reflect changes to the index
CL_T_RELOAD ( resourceID )

Optimize the index
CL_T_OPTIMIZE ( resourceID )

Perform a search using the specified query on the specified field.
Delete all documents that is returned from the search
CL_T_DELETE ( resourceID, Char_t query, Char_t field )


Adding documents:
Start a new document
CL_T_NEW_DOCUMENT ( resourceID )

Adds the specified value to the current document
Field specifies the name of the field within the document
Store: Store the value in the index. This makes the index bigger in size,
       so would only usually be true for the unique identifier to the document
	   (such as the url, or the database id)
Index: Index the field to make it searchable. (usually true)
Tokenize: Split the value into words using the StandardAnalyzer. If the field
	      is a reference, then this should be false, else usually true
CL_T_ADD_FIELD ( resourceID, Char_t* field, Char_t* value, int value_length, bool store, bool index, bool tokenize )


Adds the contents of the specified file.
CL_T_ADD_FILE ( resourceID, Char_t* field, char* filename,TCHAR* encoding_scheme,bool store, bool index, bool tokenize )

Adds a date to the field.
CL_T_ADD_DATE ( resourceID, Char_t* field, long time, bool store, bool index, bool tokenize )

//returns an overview of the document about to be added
CL_T_DOCUMENT_INFO

Insert the current document
CL_T_INSERT_DOCUMENT ( resourceID )


Searching:

Search within the specified field using the specified query
CL_T_SEARCH ( resourceID, Char_t* query, Char_t* field, byte* flags )

Search within the specified fields using the specified query
Flags is an array matching each field with a value of:
    byte NORMAL FIELD     = 0;
    byte REQUIRED FIELD   = 1;
    byte PROHIBITED FIELD = 2;
CL_T_SEARCHMULTIFIELDSFLAGGED 

Search within the specified fields using the specified query
Each field is set as a normal field
CL_T_SEARCHMULTIFIELDS ( resourceID, Char_t* query, Char_t** fields, int fields_len, byte* flags )

Returns an overview of the query given to the search
CL_T_SEARCHINFO ( resourceID, Char_t* ret, int ret_len )

Return the hitcount of the last search
CL_T_HITCOUNT CL_T_HitCount ( resourceID )

Go to the next hit
CL_T_NEXTHIT CL_T_NextHit ( resourceID )

Go to the specified hit
CL_T_GOTOHIT CL_T_GotoHit ( resourceID, number )

Return the HitScore of the current search result Document
float CL_T_HITSCORE CL_T_HitScore ( resourceID )

Return a the specified field as a date
long CL_T_GETDATEFIELD ( resourceID, Char_t* field )

Return a the specified field
A Char_t array is created for return, with ret_len length
This should be cleaned up after use
CL_T_GETFIELD ( resourceID, Char_t* field, Char_t** ret, int* ret_len )

Frees the current search
CL_T_CLEARSEARCH CL_T_ClearSearch ( resourceID )


Unicode:
* If _UNICODE is defined, the MACROS listed above will be the unicode equivalents.
* Char_t will be defined as wchar_t. The macros listed above will point to their
  unicode equivalents. The unicode mode will be CL_T_U_MethodName.
* CL_T_METHODNAME_OTHER will point to the non-unicode function.

* If _UNICODE is NOT defined, the MACROS listed above will be the non-unicode equivalents.
* Char_t will be defined as char. The macros listed above will point to their
  non-unicode equivalents. The non-unicode method will be CL_T_N_MethodName.
* If Possible, CL_T_METHOD_OTHER will point to the unicode function


*/
//////////////////

#ifndef _DLL_CLUCENE
#define _DLL_CLUCENE
#include "CLucene/CLConfig.h"
# if defined(_MSC_VER) && !defined(__MWERKS__) && !defined (__ICL) && !defined (__COMO__)
	typedef __int64 type_long;

	#ifdef CLUCENEDLL_EXPORTS
		#define CLUCENEDLL_API __declspec(dllexport)
	#elif CLUCENEDLL_INCLUDED
		#define CLUCENEDLL_API
	#else
		#define CLUCENEDLL_API __declspec(dllimport)
	#endif
#else
	typedef long long type_long;

	#define CLUCENEDLL_API
#endif

#define LUCENE_QUERY_TERM 1
#define LUCENE_QUERY_FUZZY 2
#define LUCENE_QUERY_PHRASE 3
#define LUCENE_QUERY_PREFIX 4
#define LUCENE_QUERY_RANGEDATE 5
#define LUCENE_QUERY_WILDCARD 6

#define MAX_CLUCENE_DLL_RESOURCES 10


#ifdef  __cplusplus
extern "C" {
#endif
	#define errlen 500
	
	//CLUCENEDLL_API void CL_T_CallBack_Error ( char* error );

	CLUCENEDLL_API void CL_DeleteMemory(void* data);
	CLUCENEDLL_API void CL_DeleteArrayMemory(void* data);

	//code for both unicode and ascii
#ifndef CLUCENE_LITE
	CLUCENEDLL_API int CL_T_Optimize(const int resource);
	CLUCENEDLL_API int CL_T_New_Document(const int resource);
	CLUCENEDLL_API int CL_T_Insert_Document(const int resource);
	CLUCENEDLL_API void  CL_T_DeleteCurrentDocument(const int resource);
#endif

	CLUCENEDLL_API int CL_A_Open(const char* dir, int create);
	#ifdef _UNICODE
		CLUCENEDLL_API int CL_T_Open(const wchar_t* dir, int create);
	#endif

	CLUCENEDLL_API int CL_T_Close(const int resource);
	CLUCENEDLL_API int CL_T_Reload(const int resource);
	CLUCENEDLL_API void CL_T_Cleanup();
	CLUCENEDLL_API bool CL_T_TermDocsNext(const int resource);

	CLUCENEDLL_API int   CL_T_HitCount(const int resource);
	CLUCENEDLL_API int   CL_T_NextHit(const int resource);
	CLUCENEDLL_API int   CL_T_GotoHit(const int resource, const int number);
	CLUCENEDLL_API float CL_T_HitScore(const int resource);
	CLUCENEDLL_API void  CL_T_ClearSearch(const int resource);
	CLUCENEDLL_API int   CL_T_SearchCurrentQuery(const int resource);
	CLUCENEDLL_API bool  CL_T_TermDocsNext(const int resource);
 

	CLUCENEDLL_API void CL_T_Document_Info(const int resource, wchar_t* info,  const int len);

	CLUCENEDLL_API int CL_T_Search(const int resource, const wchar_t* query, const wchar_t* field, bool execute);
	CLUCENEDLL_API int CL_T_SearchMultiFields(const int resource, const wchar_t* qry, const wchar_t** fields, const int fieldsLen, bool execute);
	CLUCENEDLL_API int CL_T_SearchMultiFieldsFlagged(const int resource, const wchar_t* qry, const wchar_t** fields, const int fieldsLen, const unsigned char * flags, bool execute);
	CLUCENEDLL_API void CL_T_SearchInfo(const int resource, wchar_t* pl, size_t len);
	CLUCENEDLL_API int CL_T_GetField(const int resource, const wchar_t* field, wchar_t** value, size_t* value_len);
	CLUCENEDLL_API type_long CL_T_GetDateField(const int resource, const wchar_t* field);

	CLUCENEDLL_API void CL_A_ErrStr(const int resource, char* pl, size_t len);
#ifdef _UNICODE
	CLUCENEDLL_API void CL_T_ErrStr(const int resource, wchar_t* pl, size_t len);
#endif

#ifdef _UNICODE
	CLUCENEDLL_API void CL_T_ErrStrGlobal(wchar_t* pl, size_t len);
#endif
	CLUCENEDLL_API void CL_A_ErrStrGlobal(char* pl, size_t len);

	CLUCENEDLL_API void CL_T_FilterDate(const int resource, const wchar_t* field, const type_long date, const bool after);
	CLUCENEDLL_API void CL_T_FilterDate_ex(const int resource, const wchar_t* field, const type_long from, const type_long to);
	CLUCENEDLL_API void CL_T_AddSortField(const int resource, const wchar_t* field, const int type, const bool reverse);
	CLUCENEDLL_API void CL_T_AddSortGrouping(const int resource, const wchar_t* field, const wchar_t** groups );
	CLUCENEDLL_API void CL_T_GetTermDocs(const int resource, const wchar_t* field, const wchar_t* value);
	CLUCENEDLL_API void CL_T_AddQuery(const int resource, const wchar_t* field, const wchar_t* text, const int query_type, const bool required, const bool prohibited);

#ifndef CLUCENE_LITE
	CLUCENEDLL_API int CL_A_Unlock(const char* dir);
	#ifdef _UNICODE
		CLUCENEDLL_API int CL_T_Unlock(const wchar_t* dir);
	#endif

	CLUCENEDLL_API int CL_T_Delete(const int resource, const wchar_t* query, const wchar_t* field);

	CLUCENEDLL_API int CL_T_Add_Field(const int resource, const wchar_t* field, const wchar_t* value, const int value_length, const int store, const int index, const int token);
	CLUCENEDLL_API int CL_T_Add_Date(const int resource, const wchar_t* field, const type_long value, const int store, const int index, const int token);

	CLUCENEDLL_API int CL_A_Add_File(const int resource, const char* field, const char* filename, const char* encoding_scheme, const int store, const int index, const int token);
	#ifdef _UNICODE
		CLUCENEDLL_API int CL_T_Add_File(const int resource, const wchar_t* field, const wchar_t* filename, const wchar_t* encoding_scheme, const int store, const int index, const int token);
	#endif
#endif

#ifdef HIGHLIGHTING
	CLUCENEDLL_API void CL_T_HighLight(const int resource, const wchar_t* text, const int text_is_filename, wchar_t* ret, const size_t ret_len);
	CLUCENEDLL_API void CL_T_HighLight_X(const int resource, const wchar_t* text, const int text_is_filename, wchar_t* ret, const size_t ret_len, const wchar_t* seperator, const int max_fragments, const int fragment_size, const int type, const wchar_t* d1, const wchar_t* d2);
#endif

#ifdef  __cplusplus
}
#endif

//methods that use ascii at least once
#define CL_UNLOCK_OTHER CL_A_Unlock
#define CL_OPEN_OTHER CL_A_Open
#define CL_ADD_FILE_OTHER CL_A_Add_File
//and their equivalent full tchar equivalent
#define CL_UNLOCK CL_T_Unlock
#define CL_OPEN CL_T_Open
#define CL_ADD_FILE CL_T_Add_File


#define CL_DOCUMENT_INFO CL_T_Document_Info
#define CL_ADD_FIELD CL_T_Add_Field
#define CL_ADD_DATE CL_T_Add_Date
#define CL_SEARCHMULTIFIELDSFLAGGED CL_T_SearchMultiFieldsFlagged
#define CL_SEARCHMULTIFIELDS CL_T_SearchMultiFields
#define CL_SEARCH CL_T_Search
#define CL_SEARCHINFO CL_T_SearchInfo
#define CL_GETDATEFIELD CL_T_GetDateField
#define CL_GETFIELD CL_T_GetField
#define CL_DELETE CL_T_Delete
#define CL_ERRSTR CL_T_ErrStr
#define CL_ERRSTRGLOBAL CL_T_ErrStrGlobal
#define CL_HIGHLIGHT CL_T_HighLight
#define CL_HIGHLIGHT_X CL_T_HighLight_X
#define CL_FILTERDATE CL_T_FilterDate
#define CL_FILTERDATE_EX CL_T_FilterDate_ex
#define CL_ADDSORTFIELD CL_T_AddSortField
#define CL_ADDSORTGROUPING CL_T_AddSortGrouping
#define CL_GET_TERMDOCS CL_T_GetTermDocs
#define CL_TERMDOCSNEXT CL_T_TermDocsNext
#define CL_ADDQUERY CL_T_AddQuery

//neutral items
#define CL_TERMDOCS_NEXT CL_T_TermDocsNext
#define CL_CLOSE CL_T_Close
#define CL_RELOAD CL_T_Reload
#define CL_CLEANUP CL_T_Cleanup
#define CL_OPTIMIZE CL_T_Optimize
#define CL_NEW_DOCUMENT CL_T_New_Document
#define CL_INSERT_DOCUMENT CL_T_Insert_Document
#define CL_HITCOUNT CL_T_HitCount
#define CL_NEXTHIT CL_T_NextHit
#define CL_GOTOHIT CL_T_GotoHit
#define CL_HITSCORE CL_T_HitScore
#define CL_CLEARSEARCH CL_T_ClearSearch
#define CL_SEARCHCURRENTQUERY CL_T_SearchCurrentQuery
#define CL_DELETECURRENTDOCUMENT CL_T_DeleteCurrentDocument

#endif
