/***************************************************************************
 *
 * Copyright (c) 2000, 2001 BalaBit IT Ltd, Budapest, Hungary
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: pycfg.c,v 1.6.4.2 2001/09/19 19:22:17 bazsi Exp $
 *
 * Author  : Bazsi
 * Auditor : kisza
 * Last version: 1.2
 * Notes:
 *   - comments
 *
 ***************************************************************************/

#include <zorp/cfg.h>
#include <zorp/zpython.h>
#include <zorp/policy.h>

static PyObject *settings;

int
z_config_init(void)
{
  
  z_policy_lock(z_python_global_state);
  
  settings = PyImport_AddModule("Zorp.Zorp");
  Py_XINCREF(settings);
  z_policy_unlock(z_python_global_state);
  return TRUE;
}

void
z_config_destroy(void)
{
  z_policy_lock(z_python_global_state);
  Py_XDECREF(settings);
  settings = NULL;
  z_policy_unlock(z_python_global_state);
}

gboolean 
z_config_int(gchar *name, gint *value)
{
  PyObject *o;
  
  z_policy_lock(z_python_global_state);
  o = PyObject_GetAttrString(settings, name);
  if (o && PyInt_Check(o))
    {
      *value = PyInt_AsLong(o);
      Py_XDECREF(o);
      z_policy_unlock(z_python_global_state);
      return TRUE;
    }
  if (!o)
    {
      PyErr_Clear();
    }
  Py_XDECREF(o);
  z_policy_unlock(z_python_global_state);
  return FALSE;
}

gboolean 
z_config_str(gchar *name, gchar *value, gint size)
{
  PyObject *o;
  
  z_policy_lock(z_python_global_state);
  o = PyObject_GetAttrString(settings, name);
  if (o && PyString_Check(o))
    {
      strncpy(value, PyString_AsString(o), size);
      Py_XDECREF(o);
      z_policy_unlock(z_python_global_state);
      return TRUE;
    }
  if (!o)
    {
      PyErr_Clear();
    }
  Py_XDECREF(o);
  z_policy_unlock(z_python_global_state);
  return FALSE;
}
