/**************************************************************************
*                                                                         *
* mcstring.c                                                              *
*                                                                         *
* userful string handling functions                                       *
*                                                                         *
**************************************************************************/



#ifndef MCSTRING_C
#define MCSTRING_C

#define MCSTRING_C_VERSION "$VER: mcstring_c 1.00 (34.4.1995)\0"


/* essential include stuff */
/* to be honest, I don't know if all the includes are necessary */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "mcstd.h"  /* necessary - it defines BOOL */


/* prototypes */

void  multicharfprintf(FILE *, char, int) ; /* fprintf several copies   */
void  multicharprint(char, int )  ; /* printf  several copies of char   */
char* striplead(char *, char * ) ; /* remove leading spaces from a str  */
char* striptail(char *, char * ) ; /* remove trailing spaces from a str */
BOOL  strpadleft(char *,char *, int) ; /* pad string with trail spaces  */
BOOL  strpadright(char*,char *, int) ; /* pad string with lead spaces   */
char* strtolower(char *,char *) ; /* return the lower case of a string  */




/*************************************************************************
*                                                                        *
* the functions proper                                                   *
*                                                                        *
*************************************************************************/




/* *** multicharfprintf ****************************************/

void multicharfprintf(FILE *out_fp, char c, int i)
{
   int j;

   for(j=1;j<=i;j++) fprintf(out_fp,"%c",c);
}



/* *** multicharprint *******************************************/


void multicharprint(char c, int i)
{
   int j;
   
   for (j=1;j<=i;j++) printf("%c",c);
}


/* *** striplead ***********************************************/

char* striplead(char *source, char *dest)
{
   int i=0;
   int j=0;
   BOOL record_accross=FALSE;

   while(source[i]!='\0')
   {
      if(source[i]!=' ') record_accross=TRUE;
      
      if(record_accross)
      {
         dest[j]=source[i];
         j++;
      }

      i++;
   }

   dest[j]='\0';

   return dest;
}



/* *** striptail ***********************************************/

char* striptail(char *source, char *dest)
{
   int i=0;
   int last_non_space=0;

   /* determine where the last non-space char is */
   while(source[i]!='\0')
   {
      if(source[i]!=' ') last_non_space=i;
      i++;
   }

   /* transfer up to last non space */
   for(i=0;i<=last_non_space;i++) dest[i]=source[i];

   /* tack on the terminator */
   dest[last_non_space+1]='\0';

   return dest;
}



/* *** strpadleft ****************************************************/


BOOL strpadleft(char *source, char *dest,int size)
{
   /* the layout will look something like this


	source	x x x x x x \0
	dest	x x x x x x x  x \0
	size	1 2 3 4 5 6 7  8
	i	0 1 2 3 4 5 6  7 8
			    ^
			    |
		      term (of i)
   */

   int i=0; /* count over the destination characters */
   int term=-1; /* locate '\0' in the source         */

   /* do a simple-minded copy from source to dest */
   while (i<size)
   {
      dest[i]=source[i];
      if ((source[i] == '\0') && (term==-1)) term=i;
      i++;
   };

   dest[i]='\0';
   if ((source[i]=='\0') && (term==-1)) term=i;

   /* insert trailing spaces in dest if necessary */
   if ((term>=0) && (term<i))
   {
      int j;
      for (j=term;j<i;j++) dest[j]=' ';
   };

   /* do some diagnostics */
   #ifdef DEBUG
      if (term>0) {assert(source[term]=='\0')};
      assert(dest[i]=='\0');
      if (strlen(dest) != size) printf("*%s*\n",dest);
      assert(strlen(dest)==size);
      assert(i==size);
   #endif

   /* figure out what to return */
   if (term==-1)
      return FALSE; /* source terminator never reached */
   else
      return TRUE;
};





/* *** strpadright *****************************************************/


BOOL strpadright(char *source,char *dest,int size)
{
   char blank[]="\0";  /* a dummy, blank string */
   BOOL a;
   int start,j;
   int i=0;

   /* fill dest[] with spaces */
   a=strpadleft(blank,dest,size);
   #ifdef DEBUG
      assert(strlen(dest)==size);
   #endif

   /* at what index number should we fill dest[] at ? */
   start=size-strlen(source);
   if (start<0) start=0;

   /* copy source[] to dest[] */
   j=start;
   while ((source[i]!='\0') && (dest[j]!='\0'))
   {
      dest[j]=source[i];
      j++;
      i++;
   };

   /* do some diagnostics */
   #ifdef DEBUG
      assert(strlen(dest)==size);
   #endif

   /* figure out what to return */
   if (strlen(source)>strlen(dest))
      return FALSE;
   else
      return TRUE;
}


/* *** strtolower **********************************************/

char* strtolower(char *source, char *dest)
{
   int i=0;

   while(source[i]!='\0')
   {
      dest[i]=tolower(source[i]);
      i++;
   }

   dest[i]='\0';

   return dest;
}

#endif /* MCSTRING_C */
