File:  [Local Repository] / rleda / rleda.c
Revision 1.4: download - view: text, annotated - select for diffs
Mon Feb 14 12:15:06 2005 UTC (19 years, 8 months ago) by brouard
Branches: MAIN
CVS tags: HEAD
Garde une variable intitulée numero qui relit les niveaux 2 et 3, tant
en SAS qu'en STATA.

/* $Id: rleda.c,v 1.4 2005/02/14 12:15:06 brouard Exp $
  $State: Exp $
  $Log: rleda.c,v $
  Revision 1.4  2005/02/14 12:15:06  brouard
  Garde une variable intitulée numero qui relit les niveaux 2 et 3, tant
  en SAS qu'en STATA.

  Revision 1.3  2005/02/08 17:10:08  brouard
  Lit le fichier index deux fois pour n'avoir qu'un passage en SAS.
  AJoute le proc contents des fichiers créés en fin de run;

  Revision 1.2  2003/12/12 19:08:17  brouard
  Les variables ne sont plus toutes écrites en ligne mais avec un retour
  à la ligne toutes les 5 variables.

  Revision 1.1.1.1  2003/12/12 18:53:44  brouard
  Import de rleda


 rleda.c
 Programme pour lire une fichier d'index de LEDA qui a t  converti de l'EBCDIC vers l'ascii
 Nicolas Brouard (Institut national d'tudes d mographiques)
 Usage : rleda nom_d_index 
 Il y a plusieurs sorties, un log (pour le debug) un .txt pour le format et
 un .sas pour le sas.
 Je me suis inspir de la version sas d'Andr  Wielki (INED).
 Mardi 25 novembre 2003
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define MAXLINE 300
#define FILENAMELENGTH 300
#define FILENAMEEXTLEN 10
#define	GLOCK_ERROR_NOPATH		-1	/* empty path */
#define	GLOCK_ERROR_GETCWD		-2	/* cannot get cwd */

#ifdef unix
#define DIRSEPARATOR '/'
#define ODIRSEPARATOR '\\'
#else
#define DIRSEPARATOR '\\'
#define ODIRSEPARATOR '/'
#endif

char version[]="rleda-0.9.5 Programme de lecture de fichiers d'index au format LEDA convertis de l'EBCDIC en ASCII ";
char fullversion[]="$Revision: 1.4 $ $Date: 2005/02/14 12:15:06 $"; 
char line[MAXLINE];
char title[MAXLINE],namevar[MAXLINE], namevara[MAXLINE];

char fileres[FILENAMELENGTH], filereso[FILENAMELENGTH];
char filesas[FILENAMELENGTH]; /* SAS  file */
char filestata[FILENAMELENGTH]; /* STATA  file */
char filestatal[10][FILENAMELENGTH]; /* STATA dictionary  files */
char filelog[FILENAMELENGTH]; /* Log file */

FILE *ficlog, *ficres, *ficdat, *ficreso, *ficsas, *ficstata, *ficstatal[10];


static unsigned char const ascii_to_ebcdic[] =
{
  '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
  '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
  '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
  '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
  '\100', '\117', '\177', '\173', '\133', '\154', '\120', '\175',
  '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
  '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
  '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
  '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
  '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
  '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
  '\347', '\350', '\351', '\112', '\340', '\132', '\137', '\155',
  '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
  '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
  '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
  '\247', '\250', '\251', '\300', '\152', '\320', '\241', '\007',
  '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
  '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
  '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
  '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
  '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
  '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
  '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
  '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
  '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
  '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
  '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
  '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
  '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
  '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
  '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
  '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
};

static unsigned char const ascii_to_ibm[] =
{
  '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
  '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
  '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
  '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
  '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',
  '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
  '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
  '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
  '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
  '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
  '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
  '\347', '\350', '\351', '\255', '\340', '\275', '\137', '\155',
  '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
  '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
  '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
  '\247', '\250', '\251', '\300', '\117', '\320', '\241', '\007',
  '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
  '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
  '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
  '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
  '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
  '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
  '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
  '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
  '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
  '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
  '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
  '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
  '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
  '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
  '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
  '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
};

static unsigned char const ebcdic_to_ascii[] =
{
  '\000', '\001', '\002', '\003', '\234', '\011', '\206', '\177',
  '\227', '\215', '\216', '\013', '\014', '\015', '\016', '\017',
  '\020', '\021', '\022', '\023', '\235', '\205', '\010', '\207',
  '\030', '\031', '\222', '\217', '\034', '\035', '\036', '\037',
  '\200', '\201', '\202', '\203', '\204', '\012', '\027', '\033',
  '\210', '\211', '\212', '\213', '\214', '\005', '\006', '\007',
  '\220', '\221', '\026', '\223', '\224', '\225', '\226', '\004',
  '\230', '\231', '\232', '\233', '\024', '\025', '\236', '\032',
  '\040', '\240', '\241', '\242', '\243', '\244', '\245', '\246',
  '\247', '\250', '\133', '\056', '\074', '\050', '\053', '\041',
  '\046', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
  '\260', '\261', '\135', '\044', '\052', '\051', '\073', '\136',
  '\055', '\057', '\262', '\263', '\264', '\265', '\266', '\267',
  '\270', '\271', '\174', '\054', '\045', '\137', '\076', '\077',
  '\272', '\273', '\274', '\275', '\276', '\277', '\300', '\301',
  '\302', '\140', '\072', '\043', '\100', '\047', '\075', '\042',
  '\303', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
  '\150', '\151', '\304', '\305', '\306', '\307', '\310', '\311',
  '\312', '\152', '\153', '\154', '\155', '\156', '\157', '\160',
  '\161', '\162', '\313', '\314', '\315', '\316', '\317', '\320',
  '\321', '\176', '\163', '\164', '\165', '\166', '\167', '\170',
  '\171', '\172', '\322', '\323', '\324', '\325', '\326', '\327',
  '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
  '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
  '\173', '\101', '\102', '\103', '\104', '\105', '\106', '\107',
  '\110', '\111', '\350', '\351', '\352', '\353', '\354', '\355',
  '\175', '\112', '\113', '\114', '\115', '\116', '\117', '\120',
  '\121', '\122', '\356', '\357', '\360', '\361', '\362', '\363',
  '\134', '\237', '\123', '\124', '\125', '\126', '\127', '\130',
  '\131', '\132', '\364', '\365', '\366', '\367', '\370', '\371',
  '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
  '\070', '\071', '\372', '\373', '\374', '\375', '\376', '\377'
};

/**************** split *************************/
static	int split( char *path, char *dirc, char *name, char *ext, char *finame )
{
  char	*ss;				/* pointer */
  int	l1, l2;				/* length counters */

  l1 = strlen(path );			/* length of path */
  if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
  ss= strrchr( path, DIRSEPARATOR );		/* find last / */
  if ( ss == NULL ) {			/* no directory, so use current */
    /*if(strrchr(path, ODIRSEPARATOR )==NULL)
      printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
    /* get current working directory */
    /*    extern  char* getcwd ( char *buf , int len);*/
    if ( getcwd( dirc, FILENAME_MAX ) == NULL ) {
      return( GLOCK_ERROR_GETCWD );
    }
    strcpy( name, path );		/* we've got it */
  } else {				/* strip direcotry from path */
    ss++;				/* after this, the filename */
    l2 = strlen( ss );			/* length of filename */
    if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
    strcpy( name, ss );		/* save file name */
    strncpy( dirc, path, l1 - l2 );	/* now the directory */
    dirc[l1-l2] = 0;			/* add zero */
  }
  l1 = strlen( dirc );			/* length of directory */
  /*#ifdef windows
  if ( dirc[l1-1] != '\\' ) { dirc[l1] = '\\'; dirc[l1+1] = 0; }
#else
  if ( dirc[l1-1] != '/' ) { dirc[l1] = '/'; dirc[l1+1] = 0; }
#endif
  */
  l1= strlen( name);
  ss = strrchr( name, '.' );		/* find last / */
  if (ss != '\0'){
    ss++;
    strcpy(ext,ss);			/* save extension */
    l2= strlen(ss)+1;
  }else{
    ext[0]='\0';
    l2 = 0;
  }
  strncpy( finame, name, l1-l2);
  finame[l1-l2]= 0;
  return( 0 );				/* we're done */
}

void trzb(char *z, char *in)
{
  /* Enlve les blancs en fin de cha ne et le caractre 0 qui se trouve
     au d but s'il y est */
  int i=0, j=0, k=0;
  int n;

  n =strlen(in);
/*   for(i=n;(i>=0) || (in[i]==' ');i--){ */
/*     printf ("i=%d in[i]=%s\n",i,in[i]); */
/*   } */
  k=0;
  if(in[0]== '0')
    k=1;
  for(j=k; j<=n;j++){
    if(in[j]==' '){
      z[j-k]='\0';
      break;
    }
    else
      z[j-k]=in[j];
  }
  z[j-k]='\0';
}

main(int argc, char *argv[])
{
  int numlinepar=0, ilect=0, firstpass=0, idic[10]={0,0,0,0,0,0,0,0,0,0};
  long nobs=0, ncar=0,ncara=0, ncartes=0, nn=0, nvaris=0;
  long c;
  long d;
  long numlis, ntype;
  long n, pos;
  int level, olevel, levelmax;
  char scar;
  char s;
  char var1[4][10], varn[4][10]; 

  char pathtot[MAXLINE], path[MAXLINE], indexfile[FILENAMELENGTH], indexfilext[FILENAMEEXTLEN];
  char indexfilefiname[FILENAMELENGTH];

  numlinepar=0;
  if(argc <=1){
    printf("\nEntrez le nom du fichier index binaire (dtrirdex) : ");
    scanf("%s",pathtot);
  }
  else{
    strcpy(pathtot,argv[1]);
  }
  split(pathtot,path,indexfile,indexfilext,indexfilefiname);
  printf("pathtot=%s,\npath=%s,\nindexfile=%s \nindexfilext=%s \nindexfilefiname=%s\n",pathtot,path,indexfile,indexfilext,indexfilefiname);
  chdir(path);


  /* Log file */
  strcat(filelog, indexfilefiname);
  strcat(filelog,".log");    /* */
  if((ficlog=fopen(filelog,"w"))==NULL)    {
    printf("Problem with logfile %s\n",filelog);
    goto end;
  }
  fprintf(ficlog,"Log filename:%s\n",filelog);
  fprintf(ficlog,"\n%s\n%s",version,fullversion);
  fprintf(ficlog,"\nEntrez le nom du fichier d'index: ");
  fprintf(ficlog,"pathtot=%s\n\
 path=%s \n\
 indexfile=%s\n\
 indexfilext=%s\n\
 indexfilefiname=%s\n",pathtot,path,indexfile,indexfilext,indexfilefiname);
  fflush(ficlog);



  strcpy(filereso,"o");
  strcat(filereso,indexfilefiname);
  strcat(filereso,".txt");
  if((ficreso=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
    printf("Problem with Output resultfile: %s\n", filereso);
    fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
    fflush(ficlog);
    goto end;
  }
  fprintf(ficreso,"Log filename:%s\n",filelog);
  fprintf(ficreso,"\n%s\n%s",version,fullversion);
  fprintf(ficreso,"\nEntrez le nom du fichier d'index: ");
  fprintf(ficreso,"pathtot=%s\n\
 path=%s \n\
 indexfile=%s\n\
 indexfilext=%s\n\
 indexfilefiname=%s\n",pathtot,path,indexfile,indexfilext,indexfilefiname);
  fflush(ficreso);

  strcpy(filesas,"s");
  strcat(filesas,indexfilefiname);
  strcat(filesas,".sas");

  strcpy(filestata,"s");
  strcat(filestata,indexfilefiname);
  strcat(filestata,".do");
  printf("Le nom du fichier de sortie est %s\n",filereso);
  printf("Le nom du fichier SAS de sortie est %s\n",filesas);
  printf("Le nom du fichier STATA de sortie est %s\n",filestata);

  for(ilect=1;ilect<=2;ilect++){
    pos=9;nvaris=0 ; 
    level=0;
    printf("Lecture du fichier d index pour la %d fois\n",ilect);
    if((ficdat=fopen(indexfile,"rb"))==NULL)    {
      printf("Problem with binary file %s\n",indexfile);
      fprintf(ficlog,"Problem with binary file %s\n",indexfile);
      fflush(ficlog);
      goto end;
    }
    if(ilect==2){
      if((ficsas=fopen(filesas,"w"))==NULL) { /* opened on subdirectory */
	printf("Problem with Output SAS file: %s\n", filesas);
	fprintf(ficlog,"Problem with Output SAS file: %s\n", filesas);
	fflush(ficlog);
	goto end;
      }
      fprintf(ficsas,"*Sortie de rleda version %s *;\n* %s *;\n",version,fullversion);
      fprintf(ficsas,"libname %s \"%s\";\n",indexfilefiname,path);
      fprintf(ficsas,"filename a \"%s.dat\";\n",indexfilefiname );
      fprintf(ficsas,"* Ci dessous a modifier eventuellement *;\n");
      fprintf(ficsas,"data %s.%s0 (keep=ID)\n",indexfilefiname,indexfilefiname);
	fprintf(ficsas,"     %s.%s%d (keep=%s--%s)\n",indexfilefiname,indexfilefiname,1,var1[1],varn[1]);
	idic[1]=0;
      for(level=2; level <=levelmax; level++){
	fprintf(ficsas,"     %s.%s%d (keep=numero %s--%s)\n",indexfilefiname,indexfilefiname,level,var1[level],varn[level]);
	idic[level]=0;
      }
      fprintf(ficsas,"     ;\n");
      fprintf(ficsas,"  infile a lrecl=500 ;\n");
      fprintf(ficsas,"  input id 1. @; retain numero 0; \n");
      fprintf(ficsas,"");
      fprintf(ficsas,"");
      if((ficstata=fopen(filestata,"w"))==NULL) { /* opened on subdirectory */
	printf("Problem with Output Stata file: %s\n", filestata);
	fprintf(ficlog,"Problem with Output Stata: %s\n", filestata);
	fflush(ficlog);
	goto end;
      }
      fprintf(ficstata,"*Sortie de rleda version %s *;\n* %s *;\n",version,fullversion);
      fprintf(ficstata,"*repertoire %s \"%s\";\n",indexfilefiname,path);
      fprintf(ficstata,"* Ci dessous a modifier eventuellement *;\n");
      fprintf(ficstata,"* data %s.%s0 (keep=ID)\n",indexfilefiname,indexfilefiname);
      for(level=1; level <=levelmax; level++){
	fprintf(ficstata,"*     %s.%s%d (keep=%s--%s)\n",indexfilefiname,indexfilefiname,level,var1[level],varn[level]);
      }
      fprintf(ficstata,"*     ;\n");
    }
    while((c=getc(ficdat)) != EOF){  
      ungetc(c,ficdat); 
      ntype=0;for(n=0;n<2;n++){ c=getc(ficdat); ntype+=(n+(1-n)*256)*ascii_to_ebcdic[c];} 
      fprintf(ficlog,"ntype=%d ",ntype); 
      /*     printf("ntype=%d ",ntype); */
      switch(ntype){
      case 11 : /* 11*/
	nobs=0;
	for(n=0;n<2;n++){
	  c=getc(ficdat);
	  d=(n+(1-n)*256)*ascii_to_ebcdic[c];
	  nobs+=d;
	}
	fprintf(ficlog,"nobs=%d ",nobs);
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"\"%s\" ",title);
	printf(" %s\n", title);
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"\"%s\" ",title);
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"\"%s\" ",title);
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"\"%s\" ",title);
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"\"%s\" ",title);
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"\"%s\" ",title);
	break;
      case 12: /* 12 */
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"\"%s\" ",title);
	printf(" %s\n", title);
	ncartes=0;for(n=0;n<2;n++){ c=getc(ficdat); ncartes+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"ncartes=%d ",ncartes);
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	for(n=0;n<8;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"\"%s\" ",title);
	olevel=level;
	level=title[0]-48;
	printf(" Niveau = %d Colonnes 1  8 = %s\n",level,title);
	fprintf(ficlog," Niveau = %d Colonnes 1  8 = %s\n",level,title);
	fprintf(ficreso," Niveau = %d Colonnes 1   8 = %s\n",level,title);
	if(level ==0){
	  if(ilect==2){
	    fprintf(ficsas,"  if id=%d then do;\n",level); 
	    /* 	    fprintf(ficstata,"*  if id=%d then do;\n",level);  */
	  /*       fprintf(ficsas,"  input @9 "); */
	  }
	}
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	ncara=0;for(n=0;n<2;n++){ c=getc(ficdat); ncara+=(n+(1-n)*256)*ascii_to_ebcdic[c]; } fprintf(ficlog,"ncara=%d ",ncara);
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"\"%s\" ",title);
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	for(n=0;n<2;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"\"%s\" ",title);
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	break;
      case 10:  /* 10 = 142 ascii*/
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"%s ",title);
	printf(" %s\n", title);
	trzb(namevar,title); 
	ncartes=0;for(n=0;n<2;n++){ c=getc(ficdat); ncartes+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"ncartes=%d ",ncartes);
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	ncara=0;for(n=0;n<2;n++){ c=getc(ficdat); ncara+=(n+(1-n)*256)*ascii_to_ebcdic[c]; } fprintf(ficlog,"ncara=%d ",ncara);
	ncar=0;for(n=0;n<2;n++){ c=getc(ficdat); ncar+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"ncar=%d ",ncar);
	printf(" %s %d\n",namevar,ncara);
	fprintf(ficreso," %s %d\n",namevar,ncara);
	ncar=0;for(n=0;n<2;n++){ c=getc(ficdat); ncar+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"ncar=%d ",ncar);
	break;
      case 14: /* 14 */
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"%s ",title);
	trzb(namevar,title); 
	printf("%d %20s ", level, namevar);
	fprintf(ficreso,"%d %20s ",level, namevar);
	fprintf(ficlog,"Variable \"%s\" \n",namevar);
	ncartes=0;for(n=0;n<2;n++){ c=getc(ficdat); ncartes+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"ncartes=%d ",ncartes);
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	ncara=0;for(n=0;n<2;n++){ c=getc(ficdat); ncara+=(n+(1-n)*256)*ascii_to_ebcdic[c]; } fprintf(ficlog,"ncara=%d ",ncara);
	printf(" %6d",ncara); /* Nombre de caractres */
	fprintf(ficreso," %6d",ncara); /* Nombre de caract res */
	ncar=0;for(n=0;n<2;n++){ c=getc(ficdat); ncar+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"ncar=%d ",ncar);
	for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0';  fprintf(ficlog,"%s ",title);
	trzb(namevara,title); 
	printf("%10s ", namevara); /* Association */
	fprintf(ficreso,"%10s ", namevara); /* Association */
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	for(n=0;n<2;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0';fprintf(ficlog,"\"%s\"",title);
	printf("%3s", title); /* Brute C ou Calcule R */
	fprintf(ficreso,"%3s", title); /* Brute C ou Calcul e R */
	if(strrchr(title,'C')!= 0 || strrchr(title,'G')!=0){
	  if(level!=olevel){
	    pos=9;
	    firstpass=0;
	    if(ilect==2){
	      if(olevel==1){
		fprintf(ficsas,"    ;\n   output %s.%s%d;\n",indexfilefiname,indexfilefiname,olevel);
	      }else if (olevel==2){
		fprintf(ficsas,"    ;\n      numero +1;\n   output %s.%s%d;\n",indexfilefiname,indexfilefiname,olevel);
	      }
	      fflush(ficsas);
	      if(idic[olevel]!=0)
		fprintf(ficstatal[olevel],"\n}\n");
	      fprintf(ficsas,"  end\n");
	    }
	    if(level !=0){
	      if(ilect==2){
		fprintf(ficsas,"  ;\n  else if id=%d then do;\n",level);
		if(idic[level]==0){
		  sprintf(filestatal[level],"s%s-dict%d.dct",indexfilefiname,level);
		  fprintf(ficlog,"\nOutput Stata dictionary: %s\n", filestatal[level]);
		  if((ficstatal[level]=fopen(filestatal[level],"w"))==NULL) { /* opened on subdirectory */
		    printf("Problem with Output Stata dictionary file: %s\n", filestatal[level]);
		    fprintf(ficlog,"Problem with Output Stata dictionary: %s\n", filestatal[level]);
		    fflush(ficlog);
		    goto end;
		  }
		  idic[level]=1;
/* 		  fprintf(ficstatal[level],"* Sortie de rleda version %s, %s\n",version,fullversion ); */
/* 		  fprintf(ficstatal[level],"* Nom de fichier  %s et chemin \"%s\";\n* Stata dictionary\n",indexfilefiname,path); */
		  fprintf(ficstatal[level],"dictionary using \"%s.dat\"{\n",indexfilefiname );
		}
	      }
	    }
	    if(ilect==2){
	      fprintf(ficsas,"    input @9 ");
	      if(idic[level]!=0)
		fprintf(ficstatal[level],"    str1 id _column(9) ");
	      printf("  input @9 ");
	    }
	    if(ilect==1){
	      levelmax=level;
	    }
	    olevel=level;
	  }
	  printf(" %d-%d", pos, pos+(ncara-1));
	  fprintf(ficreso," %d-%d", pos, pos+(ncara-1));
	  for(n=0;n<strlen(namevar);n++){
	    if(namevar[n]=='-')
	      break;
	  }
	  namevar[n]='\0';
	  if(ilect==1){
	    if(firstpass==0){
	      strcpy(var1[level],namevar);
	      printf(" Variable premiere %s, niveau %d  namevar=%s\n",var1[level],level, namevar);
	      firstpass=1;
	    }
	    else{
	      strcpy(varn[level],namevar);
	      printf(" Variable N %s, niveau %d  namevar=%s\n",varn[level],level, namevar);
	    }
	  }
	  if(ilect==2){
	    fprintf(ficsas, " %s $%d.", namevar, ncara);
	    fprintf(ficstatal[level], " str%d %s %%%ds", ncara, namevar, ncara);
	  }
	  pos+=ncara;nvaris++ ; 
	  if (nvaris>=5) {
	    if(ilect==2){
	      fprintf(ficsas,"\n     ") ; 
	      fprintf(ficstatal[level],"\n     ") ; 
	    }
	    nvaris=0 ; 
	  }
	}
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0';fprintf(ficlog,"%s ",title);
	trzb(namevar,title); 
	printf(" %10s\n", namevar); /* format */
	fprintf(ficreso," %10s", namevar); /* format */
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0';fprintf(ficlog,"%s ",title);
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	break;
      default: fprintf(ficlog,"Warning ntype=%d cas non prvu, voir les fichiers sources",ntype);
	nobs=0;for(n=0;n<2;n++){ c=getc(ficdat); nobs+=(n+(1-n)*256)*ascii_to_ebcdic[c];} fprintf(ficlog,"nobs=%d ",nobs);
	for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"%s ",title);
	printf(" %s\n", title);
      }
      n=0;
      while((c=getc(ficdat)) !='\r'){
	if(ncara != 0) 
	  nn= n % ncara;
	else
	  nn=0;
	title[nn]=c; 
	if(nn==ncara-1){
	  title[nn+1]='\0';
	  fprintf(ficlog," \"%s\"",title);
	  printf(" \"%s\"",title);
	  fprintf(ficreso," \"%s\"",title);
	  n=n+1;
	}else{
	  n=n+1;
	}
      }
      if(c=='\r')
	if((c=getc(ficdat))=='\n');
	else ungetc(c,ficdat);
      fprintf(ficlog,"\n");
      fprintf(ficreso,"\n");
    } /* End of read loop */
    fclose(ficdat);
  } /* End loop ilect */
  
  fprintf(ficsas,"   ;\n    output %s.%s%d;\n  end;\n  else input;\nrun;\n",indexfilefiname,indexfilefiname,level);
  fprintf(ficstatal[level],"\n}\n");
  for(level=0; level <=levelmax; level++){
    fprintf(ficsas,"proc contents data=%s.%s%d;run;\n",indexfilefiname,indexfilefiname,level);
    /*     fprintf(ficstata,"*proc contents data=%s.%s%d;run;\n",indexfilefiname,indexfilefiname,level); */
    if(level==2){
      fprintf(ficstata,"clear\n  quiet infile using s%s-dict%d if id==\"%d\"\n  gen long numero=_n\n  sort numero\nsave %s%d,replace\n",indexfilefiname,level,level,indexfilefiname,level);
    }
    else if(level==3){
      fprintf(ficstata,"clear\n  quiet infile using s%s-dict%d if id != \"1\"\n  gen long numero=1  if id==\"2\"\n  replace numero=sum(numero)\n  sort numero\n",indexfilefiname,level);
      fprintf(ficstata,"* merge numero using %s2\n* drop numero\n drop if id != \"3\"\nsave %s%d,replace\n",indexfilefiname,indexfilefiname,level);
    }
  }
  fclose(ficsas);
  fclose(ficstata);
  printf("Le nom du fichier de debug est %s\n",filelog);
  printf("Le nom du fichier du format de sortie est %s\n",filereso);
  printf("Le nom du fichier SAS de sortie est %s\n",filesas);
  printf("Le nom du fichier STATA de sortie est %s\n",filestata);
  for(level=1;level<=levelmax;level++){
    fclose(ficstatal[level]);
	   printf("Le nom du fichier STATA dictionnaire niveau %d de sortie est %s\n",level,filestatal[level]);
  }
  fclose(ficreso);
  fclose(ficlog);
  exit(0);
 end: 
    fclose(ficlog);
    exit(1);

}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>