/*
 * String functions...they were just to messy in level.c
 */
#include <windows.h>
#include <stdio.h>
#include <stdarg.h>
#include "main.h"

/*
   Original Code from SW:FotE 1.1
   Reworked strrep function.
   Fixed a few glaring errors. It also will not overrun the bounds of a string.
   -- Xorith
   
   Modified to run once for SGE
   -- Odis
*/
char *strrep( const char *src, const char *sch, const char *rep, char *die )
{
   int lensrc = strlen( src ), lensch = strlen( sch ), lenrep = strlen( rep ), x, y, in_p;
   static char newsrc[MSL];
   bool searching = FALSE;
   bool once = FALSE;

   newsrc[0] = '\0';
   for( x = 0, in_p = 0; x < lensrc; x++, in_p++ )
   {
    if ( once == FALSE )
      if( src[x] == sch[0] )
      {
         searching = TRUE;
         for( y = 0; y < lensch; y++ )
            if( src[x+y] != sch[y] )
               searching = FALSE;

         if( searching )
         {
            for( y = 0; y < lenrep; y++, in_p++ )
            {
               if( in_p == ( MSL - 1 ) )
               {
                  newsrc[in_p] = '\0';
                  return newsrc;
               }
               newsrc[in_p] = rep[y];
            }
            x += lensch - 1;
            in_p--;
            searching = FALSE;
            once = TRUE;
            continue;
         }
      }
      if( in_p == ( MSL - 1 ) )
      {
         newsrc[in_p] = '\0';
         return newsrc;
      }
      newsrc[in_p] = src[x];
   }
   newsrc[in_p] = '\0';
   return newsrc;
}

/*
 * Get one word/string from a string, seperated by spaces. Skips preceeding spaces.
 */
char *read_word( char *string )
{
    static char word[MSL];
    char *pword;
    char cEnd;
    int i = 0, b;

    cEnd = string[i];
    
    while( isspace( cEnd ) )
    {
      if ( cEnd == '\0' )
       return (char *)strdup(" ");

      i++;
      cEnd = string[i];
    }


    if ( cEnd == '\'' || cEnd == '"' )
   	   pword   = word;
    else
    {
	   word[0] = cEnd;
	   pword   = word;
	   cEnd    = ' ';
    }

    for ( ; pword < word + MSL; pword++, i++ )
    {
       *pword = string[i];
    	if ( cEnd == ' ' ? isspace(*pword) : *pword == cEnd )
	    {
     	    *pword = '\0';
	        return word;
    	}
    }

    return NULL;
}

/* strip \'s, and \n's */
char *strip_spaces( char *buf )
{
   static char arg[MSL];
   char *newarg;

   newarg = arg;

   for ( ; *buf != '\0'; buf++ )
   {
     if (*buf == 13 )
      buf++;
     else if ( *buf == '\'' )
      buf++;
     else
     {
       *newarg = *buf;
        newarg++;
     }
   }

   *newarg = '\0';
    return arg;
}

/* Get rid of spaces before words */
char *strip_leading_spaces( char *buf )
{
  static char arg[MSL];
  char *newarg;
  char c;
  bool done = FALSE;
  
  newarg = arg;
  
  for ( ; *buf != '\0'; buf++ )
  {
    buf++;
    c = *buf;
    buf--;
    
    if ( (isspace(*buf) && isspace(c)) && done == FALSE )
     buf++;
    else if ( (isspace(*buf) && !isspace(c)) && done == FALSE )
      done = TRUE;
    else
    {
       *newarg = *buf;
        newarg++;
    }
  }

   *newarg = '\0';
    return arg;
}

/*
 * Is this only numeric digits?
 */
bool is_number( char *num )
{
   if ( *num == '\0' )
     return FALSE;

   for ( ; *num != '\0'; num++ )
   {
      if ( !isdigit(*num) )
        return FALSE;
   }

   return TRUE;
}