#include "CLucene.h" After a rather long search for the cause Strigi crash on PPC (e.g. Mac)use the flag -ansi when compiling c++ code

Common CLucene problems


After a rather long search for the cause Strigi crash on PPC (e.g. Mac Mini), I found out that it is a good idea to always use the flag -ansi when compiling c++ code.

What happened?

Strigi uses CLucene as an index backend. By default it is compiled without -ansi. Strigi has -ansi as a default flag. This causes a problem when using the math.h header. This file, /usr/lib/gcc/powerpc-linux-gnu/4.1.2/include/bits/mathdef.h, defines the type float_t differently depending on the -ansi flag.
# ifdef __GNUC__

#  if  defined(__STRICT_ANSI__)

typedef float float_t;

typedef double double_t;

#  else

typedef double float_t;

typedef double double_t;

#  endif

# else

typedef double float_t;

typedef double double_t;

# endif
Putting such macro magic in public header files is madness and can cause nasty bugs. For example, you can have a class like this:
class Planet {

float_t mass;

};
If you new allocate this class with 'new Planet()' you allocate either 4, 8 or 16 bytes depending on your flags and architecture. If this class is
in a library, and you compile the code that uses the library with different flags than the ones with which you compiled the library, weird things
will happen. Since your objects are smaller or bigger than you expect you will start writing in the wrong memory positions when working with the
objects.

I just spent quite some time tracking down a bug like this and thought it worthwhile to tell you about it so you are aware of this issue and can
hopefully fix it faster then I did when you encounter it.

How do I use the multi field search?

#include "CLucene.h"

#include "CLucene/util/Reader.h"

#include <iostream>

#include <conio.h>

using namespace lucene::index;

using namespace lucene::analysis;

using namespace lucene::util;

using namespace lucene::store;

using namespace lucene::document;

using namespace lucene::search;

using namespace lucene::queryParser;

 

#include "CLucene/queryParser/MultiFieldQueryParser.h"

 

void searchMultiFields(char_t *dir, const char_t* qry, const char_t** fields, const int fieldsLen){

 

l_byte_t* flags = new l_byte_t[fieldsLen];

 

for ( int i=0;i<fieldsLen;i++ )

flags[i] = lucene::queryParser::MultiFieldQueryParser::NORMAL_FIELD;

 

standard::StandardAnalyzer analyzer;

Query &q = MultiFieldQueryParser::Parse(qry,fields,fieldsLen,flags,analyzer);

IndexReader& reader = IndexReader::open(dir);

IndexSearcher searcher(reader);

Hits& hits = searcher.search(q);

 

const char_t toks[] = {','};

 

for ( int i=0;i<hits.Length();i++ )

{

Document& doc = hits.doc(i);

if ( &doc != NULL )

{

char_t* token = stringToken(_T("path"), toks);

while ( token != NULL )

{

const char_t* f = doc.get(token);

if(f)

_cout << token << _T(": ") << f << endl;

token = stringToken( NULL, toks );

}

}

}

 

delete[] flags;

}

 

 

int main()

{

wchar_t **fields = new wchar_t*[2];

fields[0] = new wchar_t[10];

fields[1] = new wchar_t[10];

 

stringCopy(fields[0],_T("Title"));

stringCopy(fields[1],_T("contents"));

 

searchMultiFields(_T("YOUR INDEX PATH"), _T("QUERY STRING") , (const char_t**) fields, 2);

 

for ( int i=0;i<2;i++ )

{

delete[] fields[i];

}

delete[] fields;

 

return 0;

}

你可能感兴趣的:(include)