/*

File: string.c
Author: Neil Cafferkey
Copyright (C) 1999-2001 Neil Cafferkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU 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 General Public License for more details.

You should have received a copy of the GNU 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.

*/


#include "string_protos.h"
#include "memory_protos.h"
#include <stdio.h>


/* Function: CreateString
 * ======================
 */

String CreateString(ULONG length)
{
   String string=Malloc((length+1)*sizeof(TEXT));
   *string='\0';

   return string;
}


/* Function: ReadString
 * ====================
 * Makes a string from the contents of a file.
 */

String ReadString(TEXT *file_name)
{
   String string;
   FILE *file;
   ULONG length;

   if((file=fopen(file_name,"r"))==NULL)
   {
      printf("Couldn't open \"%s\" for reading. Exiting...\n",file_name);
      NoMoreMem();
      exit(20);
   }

   fseek(file,0,SEEK_END);
   length=ftell(file);
   rewind(file);

   string=CreateString(length);

   fread(string,sizeof(UBYTE),length,file);
   fclose(file);

   string[length]='\0';

   return string;
}


/* Function: StringToULong
 * =======================
 */

ULONG StringToULong(String string)
{
   ULONG n;
   UBYTE base;
   TEXT ch;

   switch(*string)
   {
   case '$':
      base=16;
      string++;
      break;
   case '%':
      base=2;
      string++;
      break;
   default:
      base=10;
   }

   for(n=0;(ch=*(string++))!='\0';)
   {
      if(ch>='a')
         n=base*n+ch-'a'+10;
      else if(ch>='A')
         n=base*n+ch-'A'+10;
      else
         n=base*n+ch-'0';
   }

   return n;
}


/* Function: GetLine
 * =================
 */

String GetLine(String *string_p)
{
   String string;
   ULONG length,i;

   length=(TEXT *)strchr(*string_p,'\n')-(*string_p);

   if(length<0)
   {
      fprintf(stderr,"No more lines!\n");
      exit(20);
      return NULL;
   }

   string=CreateString(length);

   /*strncpy(string,*string_p,length);*/  /* is there a bug in DICE? */

   for(i=0;i<length;i++)
      string[i]=(*string_p)[i];
   string[length]='\0';


   (*string_p)+=length+1;

   /*printf("GetLine: string=\"%s\"\n",string);*/

   return string;
}


/* Function: KillString
 * ====================
 */

VOID KillString(String string)
{
   Free(string,strlen(string)+1);

   return;
}


#ifdef TEST


#include <assert.h>


LONG main()
{
   String string=CreateString(80),s,t;

   strcpy(string,"Hello!\nGoodbye.\n");

   s=string;

   assert(strcmp(t=GetLine(&s),"Hello!")==0);
   KillString(t);
   assert(strcmp(t=GetLine(&s),"Goodbye.")==0);
   KillString(t);

   assert(StringToULong("$42")==0x42);
   assert(StringToULong("42")==42);
   assert(StringToULong("%101")==5);

   KillString(string);

   return 0;
}


#endif


