/*
This source file is part of Scol
For the latest info, see http://www.scolring.org

Copyright (c) 2010 Stephane Bisaro, aka Iri <iri@irizone.net>

This program 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 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser 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, or go to
http://www.gnu.org/copyleft/lesser.txt

For others informations, please contact us from http://www.scolring.org/
*/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "main_science.h"

void *memrchr(const void *s, int c, size_t n); /* defined in misc/memrchr.c */

void replaceChar (char *s, char cin, char cout)
{
    int i = 0;

    for (i = 0; i < strlen (s); i++)
    {
        if (s[i] == cin)
            s[i] = cout;
    }
    return;
}

/*
    Return the position of a char into a string, NULL if not found
*/
int searchChar (char * word, char c)
{
	int i;
	for (i = 0; word[i] != '\0'; i++)
		if (word[i] == c)
			return i;
	return (int) NULL;
}

/*
    Return the position of a char into a string, NULL if not found
*/
int searchCharMem (const char * word, char c)
{
    char * s;
    s = (char*) memchr (word, c, strlen (word));
    if (s != NULL)
        return s-word+1;
    return -1;
}

/*
    Return the position of a char into a string (from the end), NULL if not found
*/
int searchRCharMem (const char * word, char c)
{
    char * s;

    s = (char*) memrchr (word, c, strlen (word));
    if (s != NULL)
        return strlen (word) - strlen (s);
    return -1;
}


int equaldouble (double a, double b)
{
    int r = 1;
    const double Scolscience_epsilon = 0.00001; /* comparaison de deux doubles */

    if (fabs (a - b) < Scolscience_epsilon)
        r = 0;
    return r;
}


char * removeZeroBefore (char *s)
{
    int i, len;

    len = strlen (s);

    for (i = 0; i < len; i++)
    {
        if (s[0] == '0')
        {
            s = s+1;
            len -= 1;
        }
        else
            break;
    }
    return s;
}


void ReverseString (char *string)
{
    char *c;
    char tmp;

    c = string + strlen (string) - 1;

    while (string < c)
    {
        tmp = *string;
        *string++ = *c;
        *c-- = tmp;
    }
}

char * addPrefix (char *str, const char * prefix)
{
    int len, lenprefix;
    char * newstr;

    len = strlen (str);
    lenprefix = strlen (prefix);
    newstr = (char *) malloc (sizeof (char) * (len + lenprefix + 1));
    str = (char *) realloc (str, sizeof (char) * (len + lenprefix + 1));
    snprintf (newstr, len+lenprefix+1, "%s%s", prefix, str);
    newstr[len+lenprefix] = '\0';
    strncpy (str, newstr, len+lenprefix);
    str[len+lenprefix] = '\0';
    free (newstr);
    return str;
}

char * addPrefixChar (char * str, const char prefix)
{
    int len;
    char *tmp;

    len = strlen (str);
    tmp = (char*) malloc (sizeof (char) * (len + 2));
    strcpy (tmp, str);
    str = (char *) realloc (str, sizeof (char) *(len +2));
    sprintf (str, "%c%s", prefix, tmp);
    str[len+1] = '\0';
    free (tmp);

    return str;
}




/*
    Memory allocation
*/
int scienceMMalloc (mmachine m, void* object)
{
    int l = (sizeof (object) + 3) >> 2;
	return MMmalloc (m, l, TYPETAB);
}






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

/* function by Allan Darling */
int nisalnum(char *s, int n) {

   for (; *s && n; s++, n--)

      if (!isalnum(*s))
         return (0);

   return (1);

} /* end nisalnum */

/* function by Allan Darling */
int nisalpha(char *s, int n) {

   for (; *s && n; s++, n--)

      if (!isalpha(*s))
         return (0);

   return (1);

} /* end nisalpha */

/* function by Allan Darling */
int nisdigit(char *s, int n) {

   for (; *s && n; s++, n--)

      if (!isdigit(*s))
         return (0);

   return (1);

} /* end nisdigit */







