/*
 * $Id: strings.c,v 1.7 89/11/11 19:13:40 tynor Exp $
 *----------------------------------------------------------------------------
 *	FPLAN - Flight Planner
 *	Steve Tynor
 *	tynor@prism.gatech.edu
 *
 *	This program is in the public domain. Permission to copy,
 * distribute, modify this program is hearby given as long as this header
 * remains. If you redistribute this program after modifying it, please
 * document your changes so that I do not take the blame (or credit) for
 * those changes.  If you fix bugs or add features, please send me a
 * patch so that I can keep the 'official' version up-to-date.
 *
 *	Bug reports are welcome and I'll make an attempt to fix those
 * that are reported.
 *
 *	USE AT YOUR OWN RISK! I assume no responsibility for any
 * errors in this program, its database or documentation. I will make an
 * effort to fix bugs, but if you crash and burn because, for example,
 * fuel estimates in this program were inaccurate, it's your own fault
 * for trusting somebody else's code! Remember, as PIC, it's _your_
 * responsibility to do complete preflight planning. Use this program as
 * a flight planning aid, but verify its results before using them.
 *----------------------------------------------------------------------------
 */

static char rcsid[] = "$Id: strings.c,v 1.7 89/11/11 19:13:40 tynor Exp $";

#include "mystring.h"
#include "wp_info.h"
#include "math.h"

extern char* malloc();

/*
 * define a couple of functions that are in SunOS string(3), but apparently
 * aren't 'standard' - so much for the wonder of the portability of the 
 * standard C library...
 *
 * The only thing we count on is strlen().
 */

/*---------------------------------------------------------------------------*/
char* index (s, c)
     char *s, c;
{
   char *p;
   for (p = s; *p; p++)
      if (*p == c)
	 return p;
   if (!c)
      return p;
   else
      return (char*) 0;
}

/*---------------------------------------------------------------------------*/
char* strdup (s)
     char *s;
{
   int len = strlen (s);
   char *new = (char*) malloc (len + 1);

   if (new)
      strcpy (new, s);
   return (new);
}

static char *buffer;
static int  start;
static int  buf_len;

/*---------------------------------------------------------------------------*/
char *strtok (str, separators)
     char *str;
     char *separators;
{
   int i, k;
   int len = strlen (separators);
   char *ptr;

   if (str) {
      buf_len = strlen (str);
      buffer = str;
      start = 0;
   }
   if (start > buf_len) {
      return (char*) 0;
   }
   for (i = 0; i <= len; i++) {
      /*
       * notice we cheat and use the '\0' terminator in the separators string 
       * to always recognize '\0' as a separator.
       */
      if (ptr = index (&buffer[start], separators[i])) {
	 *ptr = '\0';
	 k = start;
	 start = (int) (ptr - buffer + 1);
	 return (&buffer[k]);
      }
   }
   return (char*) 0;
}

