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


/*
    extraie une sous-chaîne d'une longueur 'len'

*/
void nsubstr2 (char * r, char * string, int len)
{
    int i;

    for (i = 0; i < len; i++)
       r[i] = string[i];

    r[len] = '\0';
    return;
}


char * nsubstr (char * string, int len)
{
    char * tmpstr, * returnedvalue ;
    int i;

    tmpstr = malloc ((len+1) * sizeof (tmpstr));

    if (tmpstr == NULL)
    {
        returnedvalue = "";
        return returnedvalue;
    }

    for (i = 0; i < len; i++)
       tmpstr[i] = string[i];

    tmpstr[len] = '\0';
    /*strncpy (returnedvalue, tmpstr, len);*/
    /*returnedvalue[len] = '\0';*/
    /*returnedvalue = tmpstr;
    free (tmpstr);*/
    /*return returnedvalue;printf ("____________________>>>OOO>>>%s<<<<\n", tmpstr);*/
    return tmpstr;
}


void copyValue (char * om, char * word, int len)
{
    strncpy (om, word, len);
    om [len-1] = '\0';
    return;
}

void copyNegativeValue (char * om, char * word, int len)
{
    strcat (om, "-");
    strncat (om, word, len);
}

void copyPositiveValue (char * om, char * word, int len)
{
    strcat (om, "+");
    strncat (om, word, len);
}

void addWeather (char* om, const char * word)
{
    strncat (om, word, 2);
    strcat (om, " ");
    return;
}

int searchChar (char * word, char c)
{
	int i;
	for (i = 0; word[i] != '\0'; i++)
		if (word[i] == c)
			return i;
	return (int) NULL;
}


void emptyBuffer ()
{
    int c = 0;
    while (c!= '\n' && c!= EOF)
        c = getchar ();
}

int myfgets (char * s, int len)
{
    char * posEnter = NULL;

    if (fgets (s, len, stdin) != NULL)
    {
        posEnter = strchr (s, '\n');
        if (posEnter != NULL)
            *posEnter = '\0';
        else
            emptyBuffer;
        return 1;
    }
    emptyBuffer;
    return 0;
}



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

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


