/* 
 * The Spar Library - modular math parser 
 * Copyright (C) 2000,2001 Davide Angelocola <davide178@inwind.it>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 *
 */

#include <spar/sl_constant.h>
#include <spar/sl_parser.h>
#include <spar/sl_math.h>
#include <spar/sl_io.h>
#include <spar/sl_sort.h>
#include <spar/sl_table.h>

sl_constant sl_constant_TABLE[] = {
  {
   "e",
   M_E,
   "natural base logarithm"}
  ,

  {
   "epsi",
   SL_DOUBLE_EPSILON,
   "epsilon"}
  ,

  {
   "zero",
   ZERO,
   "zero"}
  ,

  {
   "pi",
   M_PI,
   "pi"}
  ,
  {
   "log2",
   M_LN2,
   "log(2)"}
  ,


  {
   "sqrt2",
   M_SQRT2,
   "sqrt(2)"}
};

#define SL_CONSTANT_TABLE_LENGTH (sizeof(sl_constant_TABLE) / sizeof(sl_constant_TABLE[0]))

int
__constant_cmp (const sl_constant * c1, const sl_constant * c2)
{
  return sl_strncmp (c1->name, c2->name, SL_IDENTIFIER_LENGHT);
}

int
sl_constant_init (void)
{
  return sl_qsort (sl_constant_TABLE,
		   SL_CONSTANT_TABLE_LENGTH,
		   sizeof (sl_constant),
		   (int (*)(const void *, const void *)) __constant_cmp);
}

int
sl_constant_get (const char *name, double *value)
{
  sl_constant *r;

  r = sl_bsearch (name,
		  sl_constant_TABLE,
		  SL_CONSTANT_TABLE_LENGTH,
		  sizeof (sl_constant),
		  (int (*)(const void *, const void *)) __constant_cmp);

  if (r)
    {
      /*
       *  Copy the constant value into the variable pointed by value
       */
      *value = r->value;

      return SL_SUCCESS;
    }
  else
    return SL_SYMBOL_NOT_FOUND;
}

void
sl_constant_print_all (void)
{
  sl_table_column constant_cols[] = {
    {"Name", 6},
    {"Value", 15},
    {"Use", SL_MESSAGE_LENGHT},

    {{0}}
  };

  sl_table_create ("Constants",
		   constant_cols,
		   SL_CONSTANT_TABLE_LENGTH, sl_table_print_constant);
}


syntax highlighted by Code2HTML, v. 0.9.1