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

Copyright (c) 2011 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 "main.h"

int net_nisdigit (char *s, int n)
{
   for (; *s && n; s++, n--)
      if (!isdigit (*s))
         return 0;
   return 1;
}

int net_nisxdigit (char *s, int n)
{
   for (; *s && n; s++, n--)
      if (!isxdigit (*s))
         return 0;
   return 1;
}

void replaceChar (char *s, char cin, char cout)
{
    unsigned 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 searchCharMem (const char * word, const char c)
{
    char * s;
    s = (char*) memchr (word, c, strlen (word));
    if (s != NULL)
        return s-word+1;
    return -1;
}

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

/* based on glib code */ /* not used ! not finished ! */
int net_string_is_ip (const char *ip)
{
    char *s, *end;
    int bloc, octet = 0;

    s = (char *) ip;

    if (strchr (ip, '.'))   /* ipv4 */
    {
        for (bloc = 0; bloc < 4; bloc++)  /* 8 blocs per ip */
        {
            if (bloc != 0)  /* except the first bloc (0), each bloc must begin by .*/
            {
                if (*s != '.')
                    return 0;
                s++;
            }
            octet = 0;
            if (*s == '0')
                end = s + 1;
            else
            {
                for (end = s; isdigit (*end); end++)
                    octet = 10 * octet + (*end - '0');
            }
            if (end == s || end > s + 3 || octet > 255)
                return FALSE;

            s = end;
        }
        return !*s;
    }

    /* ipv6 */
    return 0; /* to do */
}

