/*
 * FreeAdhocUDF - copyright (c) 2004 - 2006 adhoc dataservice GmbH
 * Christoph Theuring / Peter Mandrella / Georg Horn
 * based on FreeUDFLibC - copyright (c) 1999 Gregory Deatz
 *
 * File Name:	math_functions.c
 * Description: this module contains UDFs to generate and manipulate math functions
 *
 * for exported functions list see math_functions.h
 *
 * Software distributed under the License is distributed on an
 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
*****************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#ifdef __BORLANDC__
#include <process.h>
#endif

#include "global.h"
#include "math_functions.h"

static int get_value_of (char, char *);

static int get_value_of (c, digits)
     char c;
     char * digits;
{
  int i;
  
  for (i = 0 ;
       (digits[i] != (char) 0) && (digits[i] != c) ;
       i++) ;
  return i;
}

int convertfrom33 (sz)
     char * sz;
{
  int base = 33;
  
  return convertfrombase (sz, &base, "123456789ABCDEFGHIJKLMNPQRSTUVWXYZ");
}

char * convertto33 (value)
     int * value;
{
  int base = 33;

  return converttobase (value, &base, "123456789ABCDEFGHIJKLMNPQRSTUVWXYZ");
}

int convertfrombase (sz, base, digits)
     char * sz;
     int * base;
     char * digits;
{
  int i;
  int res = 0;
  int len = strlen(sz);
  
  for (i = 0 ; i < len ; i++)
    res = (*base * res) + get_value_of(sz[i], digits);
  return res;
}

char * converttobase (value, base, digits)
     int * value;
     int * base;
     char * digits;
{
  char sz_res[LOC_STRING_SIZE + 1];
  char * sz_result = (char *) MALLOC (LOC_STRING_SIZE + 1);
  int i = 0;
  int j, r, len_sz_res;
  int first_time = 1;

  for ( j = *value ;
       (j != 0) || first_time;
	j = j / *base, first_time = 0){
    r = j % *base;
    sz_res[i++] = digits[r];
  }
  sz_res[i] = (char) 0;
  sz_result[i] = (char) 0;
  len_sz_res = i;
  
  for (i = 0 ; i < len_sz_res ; i++)
    sz_result[i] = sz_res[len_sz_res - i - 1];
  return sz_result;
}

char * converttodollar (value)
     double * value;
{
  char * sz_result = (char *) MALLOC (LOC_STRING_SIZE + 1);

  sprintf(sz_result, "$%.2f", (*value));

  return sz_result;
}

double doubleabs (value)
     double * value;
{
  if (*value < 0)
    return -*value;
  else
    return *value;
}

char * fixedpoint (value, decimals)
     double * value;
     int * decimals;
{
  char * sz_result = MALLOC (LOC_STRING_SIZE + 1);

  sprintf(sz_result, "%.*f", *decimals, *value);

  return sz_result;
}

// pm 21.08.04
char * fixedpointlang (double *value, int *nk, char *comma, char *tsep)
{
  char buffer[128];
  double val = *value;
  char * sz_result = MALLOC (LOC_STRING_SIZE + 1);
  char * resp = sz_result;
  char * pc;
  int i, bl;

  // Vorkommastellen
  if (*nk <= 0)
    val += 0.5;
  bl = sprintf(buffer, "%.0f", floor(val));
  for (i = 0; i < bl; i++)
  {
    *resp++ = buffer[i];
    if (*tsep && i>0 && i<bl-1 && ((bl-1-i) % 3) == 0)
    {
      // Tausender-Separator
      pc = tsep;
      while (*pc)
        *resp++ = *pc++;
    }
  }

  if (*nk > 0)
  {
    // Dezimalpunkt
    pc = comma;
    while (*pc)
    {
      *resp = *pc;
      resp++, pc++;
    }

    // Nachkommastellen
    for (i=*nk; i>1; i--)
    {
      val = 10 * (val - floor(val));
      *resp = (char)(floor(val) + '0');
      resp++;
    }
    *resp = (char)(floor(10 * (val - floor(val)) + 0.5) + '0');
    resp++;
  }

  // terminate
  *resp = '\0';
  return sz_result;
}


int integerabs (value)
     int * value;
{
  if (*value < 0)
    return -*value;
  else
    return *value;
}


// pm 21.08.04
double maxnum (double *d1, double *d2)
{
  if (*d1 >= *d2) return *d1;
  else return *d2;
}


// pm 21.08.04
double minnum (double *d1, double *d2)
{
  if (*d1 <= *d2) return *d1;
  else return *d2;
}


int modulo (numerator, denominator)
     int * numerator;
     int * denominator;
{
  if (*denominator == 0)
    return 0;
  else
    return *numerator % *denominator;
}

// pm 21.08.04
int isdivisibleby (int* numerator, int* denominator)
{
  if (*denominator == 0)
    return 0;
  else
    return ((*numerator % *denominator) == 0);
}


int round (value)
     double * value;
{
  if (*value < 0)
    return (int)(*value - 0.5);
  else
    return (int)(*value + 0.5);
}

int truncate (value)
     double * value;
{
  return (int) (*value);
}

// pm 21.08.04
double fact (int *n)
{
  if (*n<1 || *n>100)
    return 0.0;
  else
  {
    int i;
    double f = 1.0;
    for (i=2; i<=*n; i++)
      f *= i;
    return f;
  }
};


// pm 03.10.05

int equalfloat (double *d1, double *d2)
{
  return (*d1 == *d2);
}


// pm 03.10.05

int equalinteger (int *i1, int *i2)
{
  return (*i1 == *i2);
}


// pm 03.10.05

double zahlrunden (double *d1, int *nk)
{
  double sign = (*d1 < 0 ? -1 : 1);
  double result = *d1;
  int n = *nk;

  while (n--) result *= 10;
  result = floor(result + 0.5);
  n = *nk;
  while (n--) result /= 10;

  return sign * result;
}


// pm 22.10.05

double roundfloat (double *d1, double *d2)
{
  if (*d2 == 0)
    return 0;
  else
    return floor ((*d1 / *d2) + 0.5) * *d2;
}


// pm 03.10.05

double prozente (double *d1, double *d2)
{
  return *d2 / *d1 * 100;
}


// pm 03.10.05

char * inttohex (int *n, int *digits)
{
  char * sz_result = (char*) MALLOC (*digits + 1);
  char * pc = sz_result;

  sprintf(sz_result,"%*x", *digits, *n);

  while (*pc)
  {
    if (*pc == ' ') *pc = '0';
    pc++;
  }

  return sz_result;
}


// pm 03.10.05

int hextoint (char *sz)
{
  int result = 0;

  while (*sz)
  {
    result *= 16;
    if (isdigit(*sz)) {
      result += (*sz - '0');
    } else if (isalpha(*sz)) {
      if (*sz <= 'Z')
        result += ((*sz - 'A') + 10);
      else
        result += ((*sz - 'a') + 10);
    }
    sz++;
  }

  return result;
}


// pm 03.10.05

double degtorad (double *d)
{
  return *d / 180.0 * M_PI;
}


// pm 03.10.05

double radtodeg (double *d)
{
  return *d / M_PI * 180.0;
}


// gh 14.10.06
/* integer random number in range a <= x <= e */
int intrandom (int *a, int *e)
{
    static time_t t = 0;
    double r = *e - *a + 1;

    if (t == 0) {
	srand((unsigned)time(&t) + getpid());
    }
    return *a + (int)(r * rand() / (RAND_MAX + 1.0));
}

