/* This is -*- C -*- */
/* vim: set sw=2: */
/* $Id$ */

/*
 * scanner.c
 *
 * Copyright (C) 2003 The Free Software Foundation, Inc.
 *
 * Developed by Jon Trowbridge <trow@gnu.org>
 */

/*
 * 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.
 */

#ifdef CONFIG_H
#include <config.h>
#endif
#include "scanner.h"

#include <stdio.h>

void
scanner_free (Scanner *scan)
{
  if (scan->dealloc)
    scan->dealloc (scan);
}

void
scanner_load_file (Scanner *scan,
		   const char *filename,
		   ScannerAddWordFn add_word_fn,
		   gpointer user_data)
{
  FILE *in;
  char line_buffer[1024];

  g_return_if_fail (scan != NULL);
  g_return_if_fail (filename && *filename);
  g_return_if_fail (scan->scan_line);
  g_return_if_fail (add_word_fn != NULL);

  in = fopen (filename, "r");
  g_return_if_fail (in != NULL);

  while (fgets (line_buffer, sizeof (line_buffer), in)) {
    scan->scan_line (scan, line_buffer, add_word_fn, user_data);
  }

  fclose (in);
}

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

typedef struct _PyScanner PyScanner;
struct _PyScanner {
  PyObject_HEAD;
  Scanner *scan;
};

static void
py_add_cb (Scanner *scan,
	   const char *pos_tag,
	   const char *word,
	   gboolean    is_terminal,
	   gpointer user_data)
{
  PyObject *callback = user_data;
  PyObject *args = Py_BuildValue ("ssi", pos_tag, word, is_terminal);
  PyObject *results;

  results = PyEval_CallObject (callback, args);
  if (results == NULL) {
    PyErr_Print ();
  } else {
    Py_DECREF (results);
  }
  Py_DECREF (args);
}

static PyObject *
py_scanner_load_file (PyObject *self, PyObject *args)
{
  Scanner *scan = scanner_from_py (self);
  char *filename;
  PyObject *callback;

  if (! PyArg_ParseTuple (args, "sO", &filename, &callback))
    return NULL;

  scanner_load_file (scan, filename, py_add_cb, callback);

  Py_INCREF (Py_None);
  return Py_None;
}

static PyMethodDef py_scanner_methods[] = {
  { "load_file", py_scanner_load_file, METH_VARARGS,
    "Use the scanner to load a file." },
  { NULL, NULL, 0, NULL }
};

static PyObject *
py_scanner_getattr (PyObject *self, char *name)
{
  return Py_FindMethod (py_scanner_methods, self, name);
}

static void
py_scanner_dealloc (PyObject *self)
{
  Scanner *scan = scanner_from_py (self);
  scanner_free (scan);
  PyObject_Del (self);
}

static PyTypeObject py_scanner_type_info = {
  PyObject_HEAD_INIT(NULL)
  0,
  "Scanner",
  sizeof(PyScanner),
  0,
  py_scanner_dealloc,  /*tp_dealloc*/
  NULL,                 /*tp_print*/
  py_scanner_getattr,  /*tp_getattr*/
  NULL,                 /*tp_setattr*/
  NULL,                 /*tp_compare*/
  NULL,                 /*tp_repr*/
  NULL,                 /*tp_as_number*/
  NULL,                 /*tp_as_sequence*/
  NULL,                 /*tp_as_mapping*/
  NULL,                 /*tp_hash */
};

PyObject *
scanner_to_py (Scanner *scan)
{
  PyScanner *py_scan;

  if (scan == NULL) {
    Py_INCREF (Py_None);
    return Py_None;
  }

  py_scan = PyObject_New (PyScanner,
			  &py_scanner_type_info);
  py_scan->scan = scan;

  return (PyObject *) py_scan;
}

Scanner *
scanner_from_py (PyObject *obj)
{
  return ((PyScanner *) obj)->scan;
}

