/* -------------------------------------------- */
/* Some functions to manipulate strings of char */
/* By MaVaTi, for ClipWatch                     */
/* -------------------------------------------- */

/* Copy 2 strings with length specified */
void strcpy_limited(char *StringDest,char *StringSrc,int Lgt)
{
 int i;
 for(i=0;i<Lgt;i++)
 {
  StringDest[i]=StringSrc[i];
 }
}

/* Compare 2 strings with length specified */
int strcmp_limited(char *String1,char *String2,int Lgt)
{
 int i=0;
 int idem=True;
 do
 {
  if(String1[i]!=String2[i])
  {
   idem=False;
  }
  i++;
 }
 while((i<Lgt)&&(idem==True));
 if (idem==True)
  return 0;
 else
  return 1;
}

/* Copy 2 strings terminated with specials caracters (other than traditionnal '\0') */
/* We can have 2 types of special caracters (ex:'\t' and '\n') */
/* Put 0 if you have only 1 type of special caracter */
/* End the new string with a '\0' caracter instead of the special caracter */
void strcpy_termnotnull(char *StringDest,char *StringSrc,char SpecialTerm,char SpecialTerm2)
{
 int i=0;
 char car;
 char end=False;
 do
 {
  car=StringSrc[i];
  StringDest[i]=car;
  if ((car==SpecialTerm)||((SpecialTerm2!=0)&&(car==SpecialTerm2)))
  {
   StringDest[i]='\0';
   end=True;
  }
  i++;
 }
 while(end!=True);
}

/* Compare 2 strings terminated with specials caracters (other than traditionnal '\0') */
/* We can have 2 types of special caracters (ex:'\t' and '\n') */
/* Put 0 if you have only 1 type of special caracter */
int strcmp_termnotnull(char *String1,char *String2,char SpecialTerm,char SpecialTerm2)
{
 int i=0;
 char car;
 char end=False;
 int idem=True;
 do
 {
  car=String1[i];
  if ((car==SpecialTerm)||((SpecialTerm2!=0)&&(car==SpecialTerm2)))
  {
   end=True;
  }
  if((car!=String2[i])&&(end==False))
  {
   idem=False;
  }
  i++;
 }
 while((end==False)&&(idem==True));
 if (idem==True)
  return 0;
 else
  return 1;
}

/* Search a string into another one
   Back:pointer on the string found if found
   -----------------------------------------------------------
   String:pointer on the string to explore
   nbrcar:nbr of caracters of the string to explore
   StringKey:pointer on the string to found
   nbrcarkey:nbr of caracters of the string to found
*/
char * str_pos(char *String,int nbrcar,char *StringKey,int nbrcarkey)
{
 int i=0;
 int i_key;
 char *idem=NULL;
 char degage;
 do
 {
  /* Found same start... */
  if (String[i]==StringKey[0])
  {
   degage=False;
   i_key=0;
   do
   {
    if (String[i+i_key]!=StringKey[i_key])
    {
     degage=True;
    }
    i_key++;
   }
   while((i_key<nbrcarkey)&&(degage==False));
   if (degage==False)
   {
    idem=&String[i];
   }
  }
  i++;
 }
 while((i<nbrcar)&&(idem==NULL));
 return idem;
}

/* Extracting a string from a frame with many fields separated
   with a special caracter
   If found caracters ',' they're replaced with '.' ( for atof() )
   Back:give a pointer on the string searched
   -----------------------------------------------------------
   String:pointer on the string to explore
   nbrcar:nbr of caracters of the string to explore
   Champ:which field we're interested in
   Separateur:special caracter delimiting the differents fields   
*/
char * str_field(char *String,int nbrcar,int Champ,char Separateur)
{
 int i=0;
 char car;
 int CptChamp=1;
 char *adrstart=NULL;
 /* Premier Champ sélectionné ? */
 if (Champ==1)
 {
  adrstart=String;
 }
 else
 {
  /* Recherche Séparateur de champ */
  do
  {
   car=String[i];
   if (car==Separateur)
   {
    CptChamp++;
    /*C'est le champ qui nous intéresse ?*/
    if (CptChamp==Champ)
    {
     adrstart=String+i+1;
    }
   }
   i++;
  }
  while((i<nbrcar)&&(CptChamp!=Champ));
 }
 /* Remplacement caractère ',' par le '.' (fonction atof!) */
 if (adrstart!=NULL)
 {
  do
  {
   car=String[i];
   if (car==',')
   {
    String[i]='.';
   }
   i++;
  }
  while(i<nbrcar);
 }
 return adrstart;
}

/* Generate a string from a number with a number of zeros in front required */ 
void RealToStrWithZeros(char * strfmt,int nbrcar,unsigned long nbr)
{
 int lackcar;
 int lgt;
 int i;
 sprintf(strfmt,"%d",nbr);
 lgt=strlen(strfmt);
 if (lgt<nbrcar)
 {
  lackcar=nbrcar-lgt;
  for(i=0;i<lackcar;i++)
  {
   strfmt[i]='0';
  }
  sprintf(strfmt+i,"%d",nbr);
 }
}

