/* -*- mode: c; coding: koi8-r -*- */

%module fb2wrap


//%typemap(python,ignore) int *OutValue(int temp) {
//  $target = &temp;
//}

//%typemap(python,argout) int *OutValue {
//  PyObject *o;
//  o = PyInt_FromLong(*$source);
//  if ((!$target) || ($target == Py_None)) {
//    $target = o;
//  } else {
//    if (!PyList_Check($target)) {
//      PyObject *o2 = $target;
//      $target = PyList_New(0);
//      PyList_Append($target,o2);
//      Py_XDECREF(o2);
//    }
//    PyList_Append($target,o);
//    Py_XDECREF(o);
//  }
//}

%{
#include "fb2parser.h"
%}

%include fb2parser.h

%include typemaps.i

/*
%typemap(python,ignore) char **OUTPUT(char *temp) {
  $target = &temp;
}

%typemap(python,argout) char **OUTPUT {
  PyObject *o;
  o = PyString_FromString(*$source);
  if ((!$target) || ($target == Py_None)) {
    $target = o;
  } else {
    if (!PyList_Check($target)) {
      PyObject *o2 = $target;
      $target = PyList_New(0);
      PyList_Append($target,o2);
      Py_XDECREF(o2);
    }
    PyList_Append($target,o);
    Py_XDECREF(o);
  }
}

%apply char **OUTPUT { char** content, char **id, char **content_type };
*/


%apply int *OUTPUT { int *offset, int *length, int *type };
%apply int *OUTPUT { int *start, int *end, int *link_start, int *link_end, int *type };


%addmethods FB2Content{

  FB2Content (char *filename) {
    return parseFile(filename);
  };

  ~FB2Content () {
    freeFB2(self);
  };

  /*int get_bookmarks ()*/


  int get_mark (int n, int *offset, int *length, int *type) {
    int len;

    if (!self) return -1;
    if (n >= self->num_marks || n < 0) return -1;

    *offset = self->marks[n]->start;
    len = self->marks[n]->end - self->marks[n]->start;
    if (len < 0) *length = 0;
    else *length = len;
    *type = self->marks[n]->type;

    return 0;
  };

  int get_link (int n, int *start, int *end,
		int *link_start, int *link_end, int *type) {

    if (!self) return -1;
    if (n >= self->num_marks || n < 0) return -1;

    *start = self->marks[n]->start;
    *end = self->marks[n]->end;
    *link_start = self->marks[n]->link_start;
    *link_end = self->marks[n]->link_end;
    *type = self->marks[n]->type;

    return 0;
  };

  char *get_genre (int n) {
    if (!self) return NULL;
    if (n >= self->num_genres || n < 0) return NULL;
    return self->genres[n];
  };


/*
  int get_binary(int n, char** content, char **id, char **content_type) {
    //static char *out[3];
    if (!self) return -1;
    if (n >= self->num_binaries || n < 0) return -1;
    *content = self->binaries[n]->buffer;
    *id = self->binaries[n]->id;
    *content_type = self->binaries[n]->content_type;
    return 0;
  };
*/


  char *get_binary_data (int n) {
    if (!self) return NULL;
    if (n >= self->num_binaries || n < 0) return NULL;
    return self->binaries[n]->buffer;
  };

  char *get_binary_id (int n) {
    if (!self) return NULL;
    if (n >= self->num_binaries || n < 0) return NULL;
    return self->binaries[n]->id;
  };

  char *get_binary_content_type (int n) {
    if (!self) return NULL;
    if (n >= self->num_binaries || n < 0) return NULL;
    return self->binaries[n]->content_type;
  };

  int get_binary_offset (int n) {
    int i, mi;

    if (!self) return -1;
    if (n >= self->num_binaries || n < 0) return -1;

    /* search mark index */
    mi = -1;
    for (i=0; i<BUF_SIZE-1; i++) {
      FB2Mark *m;

      m = self->marks[i];
      if (m == NULL) {
	break;
      }
      if (m->type == IMAGE_TYPE) {
	//printf("%s -- %s\n", self->binaries[n]->id, m->id);
	if (!strcmp(self->binaries[n]->id, m->id+1)) { /* skip leading "#" */
	  mi = i;
	  break;
	}
      }
    }

    if (mi>=0)
      return self->marks[mi]->start;
    else
      return -1;

  };


};



