#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

#include "Ssrmain.h"

#define NBNBLOQ 35

short index(char s1[],char s2[],int nb)
{
 int i,j,k,l;
 l=0;
 for(i=0;s1[i]!='\0';i++)
  {
   for(j=i,k=0;s2[k]!='\0' && s1[j]==s2[k];j++,k++);
   if(s2[k]=='\0')
    {
     l++;
     if(l==nb)
         return(i);
    }
  }
  return(-1);
}


int main(int argc, char *argv[])
{
    //CD 05/01/07
    //short i,j,ok_gr;
    int  i,j,ok_gr;
    FILE *ptfin,*ptfout,*ptferr;
    char defsuf[_MAX_FNAME];
    char nf[_MAX_PATH];
    char indac[3];
    short errf;
    char buf[500];
    char dirtab[_MAX_PATH];
    long nbris;
    short ghj,cmc,retc;
    char version[3];
    char ver;
    short vecerr[MAXVEC];
    int nberr_b,nberr_nb,nberr_f,fbloc,fnbloc;
    short nonbloq[NBNBLOQ]={1,4,5,6,7,17,18,21,22,23,24,25,26,27,28,29,30,31,33,34,35,36,37,38,39,40,41,44,64,69,70,71,72,73,82};

    char ficini[_MAX_PATH],un[_MAX_DRIVE],nom[_MAX_FNAME],drep[_MAX_DIR],ext[_MAX_EXT];
    char ficent[_MAX_PATH],repsor[_MAX_PATH];
    char ch_errbloq[300],ch_errnbloq[300];
    //CD 03/01/07
    //int lgris,lgacte;
    unsigned int lgris,lgacte;
    int x;
    char zone[15];

    //CD 04/01/07
    memset(zone,0,sizeof(zone));

    _splitpath(argv[0],un,drep,nom,ext);
    _makepath(ficini,un,drep,"ssrfg","ini");

    x=GetPrivateProfileString("Général","RepTab","",dirtab,_MAX_PATH,ficini);
    dirtab[x]=0;

    x=GetPrivateProfileString("Général","FicEnt","",ficent,_MAX_PATH,ficini);
    ficent[x]=0;
    _splitpath(ficent,un,drep,defsuf,ext);

    x=GetPrivateProfileString("Général","RepSor","",repsor,_MAX_PATH,ficini);
    repsor[x]=0;


    if((ptfin=fopen(ficent,"r"))==NULL){return(1);}

    sprintf(nf,"%s\\%s.err",repsor,defsuf);
    if ((ptferr=fopen(nf,"w"))==NULL){return(1);}

    sprintf(nf,"%s\\%s.grp",repsor,defsuf);
    if ((ptfout=fopen(nf,"w"))==NULL){
        //fprintf(ptferr,"Pb d'ouverture fichier en sortie : %s.grp\n",argv[1]);
        fprintf(ptferr,"Pb d'ouverture fichier en sortie : %s.grp\n",nf);
        return(1);
    }


    nbris=0;
    nberr_nb=0;
    nberr_b=0;
    nberr_f=0;
    while(fgets(buf,500,ptfin)!=0)
    {
        buf[strlen(buf)-1]=0;
        nbris++;
        //CD 04/01/07 erreur initialisation de errf
        errf=0;
        //CD 04/01/07 initialisation
        lgris=0;lgacte=0;ver=' ';
        //CD 04/01/07 cas ou l'on ne peut même pas lire le format
        if (strlen(buf)<12)
            errf=3;
       
        /*CD 02/01/07 format M04*/
        else
        if ((strncmp(buf+9,"M04",3)==0) || (strncmp(buf+9,"MA4",3)==0))
        {
            lgris=343;
            lgacte=8;
            ver='4';
        }
        else
            errf=2;
        //CD 02/01/07 erreur initialisation de errf
        //errf=0;
        if(errf==0)
            if (strlen(buf)<lgris)
                errf=1;
        if (errf==0)
        {
            strncpy(indac,buf+lgris-2,2);indac[2]=0;
            if (strlen(buf) != lgris+(atoi(indac)*lgacte))
                errf=1;
        }
        if (errf)
        {
            nberr_f++;
            if (strlen(buf)>87)
            {
                fprintf(ptferr,"====================================Enregistrement n° %d\n",nbris);
                fprintf(ptferr,"N° de SSR / N° de semaine / N° UM : %7.7s / %6.6s / %4.4s\n",buf+12,buf+74,buf+87);
                if(errf==1)fprintf(ptferr,"   -> Format incorrect : Taille incompatible : %d \n",strlen(buf));
                if(errf==2)fprintf(ptferr,"   -> Format incorrect : N° de format inconnu : %3.3s \n",buf+9);
            }
            else
            {
                fprintf(ptferr,"====================================Enregistrement n° %d\n",nbris);
                if(errf==1)fprintf(ptferr,"   -> Format incorrect : Taille incompatible : %d \n",strlen(buf));
                if(errf==2)fprintf(ptferr,"   -> Format incorrect : N° de format inconnu : %3.3s \n",buf+9);
                //CD 04/01/07 cas ou l'on ne peut même pas lire le format
                if(errf==3)fprintf(ptferr,"   -> Impossible de lire le format\n");
            }
            continue;
        }
        //CD 02/01/07
        version[0]=0;
        cmc=ghj=retc=0;
        ok_gr=grpssr(buf,dirtab,&cmc,&ghj,&retc,vecerr,ptferr,version);
        fnbloc=0;
        fbloc=0;
        ch_errbloq[0]=0;ch_errnbloq[0]=0;
        for(i=0;i<MAXVEC;i++)
        {
            if (vecerr[i]!=0 )
            {
                j=0;
                while((j<NBNBLOQ) && (vecerr[i]!=nonbloq[j]))
                    j++;
                if (j==NBNBLOQ)
                {
                    sprintf(ch_errbloq,"%s%d,",ch_errbloq,vecerr[i]);
                    fbloc=1;
                }
                else
                {
                    sprintf(ch_errnbloq,"%s%d,",ch_errnbloq,vecerr[i]);
                    fnbloc=1;
                }
            }
        }
        if (fbloc==1)
            nberr_b++;
        else
            if(fnbloc==1)
                nberr_nb++;
        if (fnbloc!=0 || fbloc!=0)
        {
            fprintf(ptferr,"====================================Enregistrement n° %d\n",nbris);
            fprintf(ptferr,"N° de SSR / N° de semaine / N° UM : %7.7s / %6.6s / %4.4s\n",buf+12,buf+74,buf+87);
            fprintf(ptferr,"	->  Liste des erreurs bloquantes: %s\n",ch_errbloq);
            fprintf(ptferr,"	->  Liste des erreurs non bloquantes: %s\n",ch_errnbloq);
        }
        if(fbloc==0)
        {
            fprintf(ptfout,"%2.2s%02d%03d%02dM1%c%s\n",version,cmc,ghj,retc,ver,buf);
        }
    }
    sprintf(zone,"%d",nbris);
    x=WritePrivateProfileString("Résultats","NbLus",zone,ficini);
    sprintf(zone,"%d",nberr_b);
    x=WritePrivateProfileString("Résultats","NbErrBloq",zone,ficini);
    sprintf(zone,"%d",nberr_nb);
    x=WritePrivateProfileString("Résultats","NbErrNonBloq",zone,ficini);
    sprintf(zone,"%d",nberr_f);
    x=WritePrivateProfileString("Résultats","NbErrForm",zone,ficini);

    _fcloseall();
    return(EXIT_SUCCESS);


 }