/* File : swish_e.c */
 
#include <string>
#include <vector>
#include <stdio.h>
 
using namespace std;

extern "C" {
  // http://www.swish-e.org/current/docs/SWISH-LIBRARY.html
  void *SwishInit(const char* file);
  void *SwishError(void *handle);
  void  SwishClose(void *handle);
  void *SwishNextResult(void *handle);
  void *SwishQuery(void *handle,const char *);
  char *SwishResultPropertyStr(void *result, char *property);
  char *SwishErrorString(void *handle);
}

vector<string> search(string index, string query) {
  vector<string> result;

  void *handle=SwishInit(index.c_str());
  if (SwishError(handle)) {
    fprintf(stderr, "Failed to connect.  %s\n", SwishErrorString(handle));
  } else { 
    void *results, *match;
    results=SwishQuery(handle, query.c_str());
    while ((match=SwishNextResult(results))) {
      result.push_back(SwishResultPropertyStr(match, "swishdocpath"));
    }
    SwishClose(handle);
  }

  return result;
}
