/* Auteur : nicolas Janey        */
/* nicolas.janey@univ-fcomte.fr  */

#include <stdio.h>
#include <string.h>
#include <direct.h>
#include <io.h>
#include <stdlib.h>

char rc[3] = { 0x0D,0x0A,0x00 };

int longueur(char *s) {
  int cpt = 0;
  while ( s[cpt] != 0x00 )
    cpt++;
  return(cpt);
}

void substitution(char *buf,char *src,char *dst,int p) {
  int l = longueur(buf);
  int lsrc = longueur(src);
  int ldst = longueur(dst);
  printf("*");
//  printf("%d %d %d %d\n",l,p,lsrc,ldst);
  int i;
  for ( i = 0 ; i < ldst ; i++ ) {
    buf[p+i] = dst[i]; }
  for ( int j = 0 ; j < l-p-lsrc+1 ; j++ ) {
    buf[p+i+j] = buf[p+lsrc+j]; }
}

void substitution2(char *buf,char *src,char *dst,int p) {
  int l = longueur(buf);
  int lsrc = longueur(src);
  int ldst = longueur(dst);
  printf("*");
//  printf("%d %d %d %d\n",l,p,lsrc,ldst);
  if ( ldst >= lsrc ) {
    int nbo = l-p-lsrc+1;
    for ( int j = 0 ; j < nbo ; j++ )
      buf[l+ldst-lsrc-j] = buf[l-j];
    for ( int i = 0 ; i < ldst ; i++ ) {
      buf[p+i] = dst[i]; } }
    else {
    int i;
    for ( i = 0 ; i < ldst ; i++ )
      buf[p+i] = dst[i];
    for ( i = p+i ; i <= l+1-lsrc+ldst ; i++ )
      buf[i] = buf[i+lsrc-ldst]; }

}

int firstIndexOf(char *s,char *c) {
  int ns = longueur(s);
  int nc = longueur(c);
  int p = 0;
  int stop;
  do {
    stop = 1;
    for ( int i = 0 ; i < nc ; i++ )
      if ( s[p+i] != c[i] ) {
        stop = 0;
        i = nc ; }
    p++; }
  while ( ( !stop ) && ( p <= ns-nc ) );
  p--;
  return(( p > ns-nc-1 ) ? -1 : p);
}

int nextIndexOf(char *s,char *c,int p) {
  int ns = longueur(s);
  int nc = longueur(c);
  int stop;
  do {
    stop = 1;
    for ( int i = 0 ; i < nc ; i++ )
      if ( s[p+i] != c[i] ) {
        stop = 0;
        i = nc ; }
    p++; }
  while ( ( !stop ) && ( p <= ns-nc ) );
  p--;
  return(( p > ns-nc-1 ) ? -1 : p);
}

char *filename(char *s) {
  int p = strlen(s)-1;
  while ( ( p != -1 ) && ( s[p] != '/' ) && ( s[p] != '\\' ) )
    p-- ;
  p++;
  return(&s[p]);
}

void regenerationFichierJava(char *javaFile,
                             char *item,
                             char *newValue) {
  printf("Fichier java source      : %s\n",javaFile);
  printf("Item modifie             : %s\n",item);
  printf("Nouvelle valeur          : %s\n",newValue);
  int size;
  { struct _finddata_t fileinfo ;
    long hld = _findfirst(javaFile,&fileinfo) ;
    size = fileinfo.size;
    _findclose(hld) ; }
  FILE *src = fopen(javaFile,"rb");
  if ( src != NULL ) {
    char *buf =(char *) calloc(1,size+10000);
    fread(buf,1,size,src);
    fclose(src);
    { char chaine[4] = { 0x20,0x0D,0x0A,0x00 } ;
      int p;
      do {
        p = firstIndexOf(buf,chaine);
        if ( p != -1 )
          substitution(buf,chaine,rc,p); }
      while ( p != -1 ) ; }
    { int p;
      p = firstIndexOf(buf,item);
      if ( p != -1 ) {
        while ( buf[p] != '=' )
          p++;
        p++;
        while ( buf[p] == ' ' )
          p++;
        int pf = p;
        while ( buf[pf] != ';' )
          pf++;
        pf--;
        char chaine[100];
        int i;
        for ( i = 0 ; i <= pf-p ; i++ )
          chaine[i] = buf[p+i];
        chaine[i] = 0x00;
//        printf("%d %d %s\n",p,pf,chaine);
        substitution2(buf,chaine,newValue,p);
        } }
    FILE *dst = fopen(javaFile,"wb");
    fwrite(buf,1,longueur(buf),dst);
    fclose(dst);
    printf("\n");
    free(buf); }
    else {
    printf("Probleme!!!\n"); }
}

int main(int argc,char* argv[]) {
  char javaFile[4096];
  strcpy(javaFile,argv[1]);
  char item[4096];
  strcpy(item,argv[2]);
  char newValue[4096];
  strcpy(newValue,argv[3]);
  regenerationFichierJava(javaFile,item,newValue);
	return(0);
}

