Annotation of rleda/rleda.c, revision 1.4
1.4 ! brouard 1: /* $Id: rleda.c,v 1.3 2005/02/08 17:10:08 brouard Exp $
1.2 brouard 2: $State: Exp $
3: $Log: rleda.c,v $
1.4 ! brouard 4: Revision 1.3 2005/02/08 17:10:08 brouard
! 5: Lit le fichier index deux fois pour n'avoir qu'un passage en SAS.
! 6: AJoute le proc contents des fichiers créés en fin de run;
! 7:
1.3 brouard 8: Revision 1.2 2003/12/12 19:08:17 brouard
9: Les variables ne sont plus toutes écrites en ligne mais avec un retour
10: à la ligne toutes les 5 variables.
11:
1.2 brouard 12: Revision 1.1.1.1 2003/12/12 18:53:44 brouard
13: Import de rleda
14:
1.1 brouard 15:
16: rleda.c
17: Programme pour lire une fichier d'index de LEDA qui a t converti de l'EBCDIC vers l'ascii
18: Nicolas Brouard (Institut national d'tudes d mographiques)
19: Usage : rleda nom_d_index
20: Il y a plusieurs sorties, un log (pour le debug) un .txt pour le format et
21: un .sas pour le sas.
22: Je me suis inspir de la version sas d'Andr Wielki (INED).
23: Mardi 25 novembre 2003
24: */
25: #include <stdio.h>
26: #include <string.h>
27: #include <unistd.h>
28:
29: #define MAXLINE 300
30: #define FILENAMELENGTH 300
31: #define FILENAMEEXTLEN 10
32: #define GLOCK_ERROR_NOPATH -1 /* empty path */
33: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
34:
35: #ifdef unix
36: #define DIRSEPARATOR '/'
37: #define ODIRSEPARATOR '\\'
38: #else
39: #define DIRSEPARATOR '\\'
40: #define ODIRSEPARATOR '/'
41: #endif
42:
1.4 ! brouard 43: char version[]="rleda-0.9.5 Programme de lecture de fichiers d'index au format LEDA convertis de l'EBCDIC en ASCII ";
! 44: char fullversion[]="$Revision: 1.3 $ $Date: 2005/02/08 17:10:08 $";
1.1 brouard 45: char line[MAXLINE];
46: char title[MAXLINE],namevar[MAXLINE], namevara[MAXLINE];
47:
48: char fileres[FILENAMELENGTH], filereso[FILENAMELENGTH];
49: char filesas[FILENAMELENGTH]; /* SAS file */
1.4 ! brouard 50: char filestata[FILENAMELENGTH]; /* STATA file */
! 51: char filestatal[10][FILENAMELENGTH]; /* STATA dictionary files */
1.1 brouard 52: char filelog[FILENAMELENGTH]; /* Log file */
53:
1.4 ! brouard 54: FILE *ficlog, *ficres, *ficdat, *ficreso, *ficsas, *ficstata, *ficstatal[10];
1.1 brouard 55:
56:
57: static unsigned char const ascii_to_ebcdic[] =
58: {
59: '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
60: '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
61: '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
62: '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
63: '\100', '\117', '\177', '\173', '\133', '\154', '\120', '\175',
64: '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
65: '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
66: '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
67: '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
68: '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
69: '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
70: '\347', '\350', '\351', '\112', '\340', '\132', '\137', '\155',
71: '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
72: '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
73: '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
74: '\247', '\250', '\251', '\300', '\152', '\320', '\241', '\007',
75: '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
76: '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
77: '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
78: '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
79: '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
80: '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
81: '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
82: '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
83: '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
84: '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
85: '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
86: '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
87: '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
88: '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
89: '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
90: '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
91: };
92:
93: static unsigned char const ascii_to_ibm[] =
94: {
95: '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
96: '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
97: '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
98: '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
99: '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',
100: '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
101: '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
102: '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
103: '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
104: '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
105: '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
106: '\347', '\350', '\351', '\255', '\340', '\275', '\137', '\155',
107: '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
108: '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
109: '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
110: '\247', '\250', '\251', '\300', '\117', '\320', '\241', '\007',
111: '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
112: '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
113: '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
114: '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
115: '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
116: '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
117: '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
118: '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
119: '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
120: '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
121: '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
122: '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
123: '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
124: '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
125: '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
126: '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
127: };
128:
129: static unsigned char const ebcdic_to_ascii[] =
130: {
131: '\000', '\001', '\002', '\003', '\234', '\011', '\206', '\177',
132: '\227', '\215', '\216', '\013', '\014', '\015', '\016', '\017',
133: '\020', '\021', '\022', '\023', '\235', '\205', '\010', '\207',
134: '\030', '\031', '\222', '\217', '\034', '\035', '\036', '\037',
135: '\200', '\201', '\202', '\203', '\204', '\012', '\027', '\033',
136: '\210', '\211', '\212', '\213', '\214', '\005', '\006', '\007',
137: '\220', '\221', '\026', '\223', '\224', '\225', '\226', '\004',
138: '\230', '\231', '\232', '\233', '\024', '\025', '\236', '\032',
139: '\040', '\240', '\241', '\242', '\243', '\244', '\245', '\246',
140: '\247', '\250', '\133', '\056', '\074', '\050', '\053', '\041',
141: '\046', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
142: '\260', '\261', '\135', '\044', '\052', '\051', '\073', '\136',
143: '\055', '\057', '\262', '\263', '\264', '\265', '\266', '\267',
144: '\270', '\271', '\174', '\054', '\045', '\137', '\076', '\077',
145: '\272', '\273', '\274', '\275', '\276', '\277', '\300', '\301',
146: '\302', '\140', '\072', '\043', '\100', '\047', '\075', '\042',
147: '\303', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
148: '\150', '\151', '\304', '\305', '\306', '\307', '\310', '\311',
149: '\312', '\152', '\153', '\154', '\155', '\156', '\157', '\160',
150: '\161', '\162', '\313', '\314', '\315', '\316', '\317', '\320',
151: '\321', '\176', '\163', '\164', '\165', '\166', '\167', '\170',
152: '\171', '\172', '\322', '\323', '\324', '\325', '\326', '\327',
153: '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
154: '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
155: '\173', '\101', '\102', '\103', '\104', '\105', '\106', '\107',
156: '\110', '\111', '\350', '\351', '\352', '\353', '\354', '\355',
157: '\175', '\112', '\113', '\114', '\115', '\116', '\117', '\120',
158: '\121', '\122', '\356', '\357', '\360', '\361', '\362', '\363',
159: '\134', '\237', '\123', '\124', '\125', '\126', '\127', '\130',
160: '\131', '\132', '\364', '\365', '\366', '\367', '\370', '\371',
161: '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
162: '\070', '\071', '\372', '\373', '\374', '\375', '\376', '\377'
163: };
164:
165: /**************** split *************************/
166: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
167: {
168: char *ss; /* pointer */
169: int l1, l2; /* length counters */
170:
171: l1 = strlen(path ); /* length of path */
172: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
173: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
174: if ( ss == NULL ) { /* no directory, so use current */
175: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
176: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
177: /* get current working directory */
178: /* extern char* getcwd ( char *buf , int len);*/
179: if ( getcwd( dirc, FILENAME_MAX ) == NULL ) {
180: return( GLOCK_ERROR_GETCWD );
181: }
182: strcpy( name, path ); /* we've got it */
183: } else { /* strip direcotry from path */
184: ss++; /* after this, the filename */
185: l2 = strlen( ss ); /* length of filename */
186: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
187: strcpy( name, ss ); /* save file name */
188: strncpy( dirc, path, l1 - l2 ); /* now the directory */
189: dirc[l1-l2] = 0; /* add zero */
190: }
191: l1 = strlen( dirc ); /* length of directory */
192: /*#ifdef windows
193: if ( dirc[l1-1] != '\\' ) { dirc[l1] = '\\'; dirc[l1+1] = 0; }
194: #else
195: if ( dirc[l1-1] != '/' ) { dirc[l1] = '/'; dirc[l1+1] = 0; }
196: #endif
197: */
198: l1= strlen( name);
199: ss = strrchr( name, '.' ); /* find last / */
200: if (ss != '\0'){
201: ss++;
202: strcpy(ext,ss); /* save extension */
203: l2= strlen(ss)+1;
204: }else{
205: ext[0]='\0';
206: l2 = 0;
207: }
208: strncpy( finame, name, l1-l2);
209: finame[l1-l2]= 0;
210: return( 0 ); /* we're done */
211: }
212:
213: void trzb(char *z, char *in)
214: {
215: /* Enlve les blancs en fin de cha ne et le caractre 0 qui se trouve
216: au d but s'il y est */
217: int i=0, j=0, k=0;
218: int n;
219:
220: n =strlen(in);
221: /* for(i=n;(i>=0) || (in[i]==' ');i--){ */
222: /* printf ("i=%d in[i]=%s\n",i,in[i]); */
223: /* } */
224: k=0;
225: if(in[0]== '0')
226: k=1;
227: for(j=k; j<=n;j++){
228: if(in[j]==' '){
229: z[j-k]='\0';
230: break;
231: }
232: else
233: z[j-k]=in[j];
234: }
235: z[j-k]='\0';
236: }
237:
238: main(int argc, char *argv[])
239: {
1.4 ! brouard 240: int numlinepar=0, ilect=0, firstpass=0, idic[10]={0,0,0,0,0,0,0,0,0,0};
1.3 brouard 241: long nobs=0, ncar=0,ncara=0, ncartes=0, nn=0, nvaris=0;
1.1 brouard 242: long c;
243: long d;
244: long numlis, ntype;
245: long n, pos;
1.3 brouard 246: int level, olevel, levelmax;
1.1 brouard 247: char scar;
248: char s;
1.3 brouard 249: char var1[4][10], varn[4][10];
1.1 brouard 250:
1.3 brouard 251: char pathtot[MAXLINE], path[MAXLINE], indexfile[FILENAMELENGTH], indexfilext[FILENAMEEXTLEN];
252: char indexfilefiname[FILENAMELENGTH];
1.1 brouard 253:
254: numlinepar=0;
255: if(argc <=1){
1.3 brouard 256: printf("\nEntrez le nom du fichier index binaire (dtrirdex) : ");
1.1 brouard 257: scanf("%s",pathtot);
258: }
259: else{
260: strcpy(pathtot,argv[1]);
261: }
1.3 brouard 262: split(pathtot,path,indexfile,indexfilext,indexfilefiname);
263: printf("pathtot=%s,\npath=%s,\nindexfile=%s \nindexfilext=%s \nindexfilefiname=%s\n",pathtot,path,indexfile,indexfilext,indexfilefiname);
1.1 brouard 264: chdir(path);
265:
266:
267: /* Log file */
1.3 brouard 268: strcat(filelog, indexfilefiname);
1.1 brouard 269: strcat(filelog,".log"); /* */
270: if((ficlog=fopen(filelog,"w"))==NULL) {
271: printf("Problem with logfile %s\n",filelog);
272: goto end;
273: }
274: fprintf(ficlog,"Log filename:%s\n",filelog);
275: fprintf(ficlog,"\n%s\n%s",version,fullversion);
276: fprintf(ficlog,"\nEntrez le nom du fichier d'index: ");
277: fprintf(ficlog,"pathtot=%s\n\
278: path=%s \n\
1.3 brouard 279: indexfile=%s\n\
280: indexfilext=%s\n\
281: indexfilefiname=%s\n",pathtot,path,indexfile,indexfilext,indexfilefiname);
1.1 brouard 282: fflush(ficlog);
283:
284:
285:
286: strcpy(filereso,"o");
1.3 brouard 287: strcat(filereso,indexfilefiname);
1.1 brouard 288: strcat(filereso,".txt");
1.3 brouard 289: if((ficreso=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
1.1 brouard 290: printf("Problem with Output resultfile: %s\n", filereso);
291: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
292: fflush(ficlog);
293: goto end;
294: }
1.3 brouard 295: fprintf(ficreso,"Log filename:%s\n",filelog);
296: fprintf(ficreso,"\n%s\n%s",version,fullversion);
297: fprintf(ficreso,"\nEntrez le nom du fichier d'index: ");
298: fprintf(ficreso,"pathtot=%s\n\
1.1 brouard 299: path=%s \n\
1.3 brouard 300: indexfile=%s\n\
301: indexfilext=%s\n\
302: indexfilefiname=%s\n",pathtot,path,indexfile,indexfilext,indexfilefiname);
303: fflush(ficreso);
1.1 brouard 304:
305: strcpy(filesas,"s");
1.3 brouard 306: strcat(filesas,indexfilefiname);
1.1 brouard 307: strcat(filesas,".sas");
308:
1.4 ! brouard 309: strcpy(filestata,"s");
! 310: strcat(filestata,indexfilefiname);
! 311: strcat(filestata,".do");
1.1 brouard 312: printf("Le nom du fichier de sortie est %s\n",filereso);
313: printf("Le nom du fichier SAS de sortie est %s\n",filesas);
1.4 ! brouard 314: printf("Le nom du fichier STATA de sortie est %s\n",filestata);
1.1 brouard 315:
1.3 brouard 316: for(ilect=1;ilect<=2;ilect++){
317: pos=9;nvaris=0 ;
318: level=0;
319: printf("Lecture du fichier d index pour la %d fois\n",ilect);
320: if((ficdat=fopen(indexfile,"rb"))==NULL) {
321: printf("Problem with binary file %s\n",indexfile);
322: fprintf(ficlog,"Problem with binary file %s\n",indexfile);
323: fflush(ficlog);
324: goto end;
325: }
326: if(ilect==2){
327: if((ficsas=fopen(filesas,"w"))==NULL) { /* opened on subdirectory */
1.4 ! brouard 328: printf("Problem with Output SAS file: %s\n", filesas);
! 329: fprintf(ficlog,"Problem with Output SAS file: %s\n", filesas);
1.3 brouard 330: fflush(ficlog);
331: goto end;
332: }
333: fprintf(ficsas,"*Sortie de rleda version %s *;\n* %s *;\n",version,fullversion);
334: fprintf(ficsas,"libname %s \"%s\";\n",indexfilefiname,path);
335: fprintf(ficsas,"filename a \"%s.dat\";\n",indexfilefiname );
336: fprintf(ficsas,"* Ci dessous a modifier eventuellement *;\n");
337: fprintf(ficsas,"data %s.%s0 (keep=ID)\n",indexfilefiname,indexfilefiname);
1.4 ! brouard 338: fprintf(ficsas," %s.%s%d (keep=%s--%s)\n",indexfilefiname,indexfilefiname,1,var1[1],varn[1]);
! 339: idic[1]=0;
! 340: for(level=2; level <=levelmax; level++){
! 341: fprintf(ficsas," %s.%s%d (keep=numero %s--%s)\n",indexfilefiname,indexfilefiname,level,var1[level],varn[level]);
! 342: idic[level]=0;
1.1 brouard 343: }
1.3 brouard 344: fprintf(ficsas," ;\n");
345: fprintf(ficsas," infile a lrecl=500 ;\n");
1.4 ! brouard 346: fprintf(ficsas," input id 1. @; retain numero 0; \n");
1.3 brouard 347: fprintf(ficsas,"");
348: fprintf(ficsas,"");
1.4 ! brouard 349: if((ficstata=fopen(filestata,"w"))==NULL) { /* opened on subdirectory */
! 350: printf("Problem with Output Stata file: %s\n", filestata);
! 351: fprintf(ficlog,"Problem with Output Stata: %s\n", filestata);
! 352: fflush(ficlog);
! 353: goto end;
! 354: }
! 355: fprintf(ficstata,"*Sortie de rleda version %s *;\n* %s *;\n",version,fullversion);
! 356: fprintf(ficstata,"*repertoire %s \"%s\";\n",indexfilefiname,path);
! 357: fprintf(ficstata,"* Ci dessous a modifier eventuellement *;\n");
! 358: fprintf(ficstata,"* data %s.%s0 (keep=ID)\n",indexfilefiname,indexfilefiname);
! 359: for(level=1; level <=levelmax; level++){
! 360: fprintf(ficstata,"* %s.%s%d (keep=%s--%s)\n",indexfilefiname,indexfilefiname,level,var1[level],varn[level]);
! 361: }
! 362: fprintf(ficstata,"* ;\n");
1.3 brouard 363: }
364: while((c=getc(ficdat)) != EOF){
365: ungetc(c,ficdat);
366: ntype=0;for(n=0;n<2;n++){ c=getc(ficdat); ntype+=(n+(1-n)*256)*ascii_to_ebcdic[c];}
367: fprintf(ficlog,"ntype=%d ",ntype);
368: /* printf("ntype=%d ",ntype); */
369: switch(ntype){
370: case 11 : /* 11*/
371: nobs=0;
372: for(n=0;n<2;n++){
373: c=getc(ficdat);
374: d=(n+(1-n)*256)*ascii_to_ebcdic[c];
375: nobs+=d;
1.1 brouard 376: }
1.3 brouard 377: fprintf(ficlog,"nobs=%d ",nobs);
378: 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);
379: for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"\"%s\" ",title);
380: printf(" %s\n", title);
381: 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);
382: 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);
383: for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"\"%s\" ",title);
384: 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);
385: for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"\"%s\" ",title);
386: 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);
387: for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"\"%s\" ",title);
388: 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);
389: for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"\"%s\" ",title);
390: 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);
391: for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"\"%s\" ",title);
392: break;
393: case 12: /* 12 */
394: 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);
395: for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"\"%s\" ",title);
396: printf(" %s\n", title);
397: 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);
398: 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);
399: for(n=0;n<8;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"\"%s\" ",title);
400: olevel=level;
401: level=title[0]-48;
402: printf(" Niveau = %d Colonnes 1 8 = %s\n",level,title);
403: fprintf(ficlog," Niveau = %d Colonnes 1 8 = %s\n",level,title);
404: fprintf(ficreso," Niveau = %d Colonnes 1 8 = %s\n",level,title);
405: if(level ==0){
406: if(ilect==2){
407: fprintf(ficsas," if id=%d then do;\n",level);
1.4 ! brouard 408: /* fprintf(ficstata,"* if id=%d then do;\n",level); */
1.3 brouard 409: /* fprintf(ficsas," input @9 "); */
410: }
1.1 brouard 411: }
1.3 brouard 412: 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);
413: 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);
414: 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);
415: for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"\"%s\" ",title);
416: 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);
417: for(n=0;n<2;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"\"%s\" ",title);
418: 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);
419: break;
420: case 10: /* 10 = 142 ascii*/
421: 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);
422: for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"%s ",title);
423: printf(" %s\n", title);
424: trzb(namevar,title);
425: 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);
426: 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);
427: 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);
428: 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);
429: printf(" %s %d\n",namevar,ncara);
430: fprintf(ficreso," %s %d\n",namevar,ncara);
431: 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);
432: break;
433: case 14: /* 14 */
434: 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);
435: for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"%s ",title);
436: trzb(namevar,title);
437: printf("%d %20s ", level, namevar);
438: fprintf(ficreso,"%d %20s ",level, namevar);
439: fprintf(ficlog,"Variable \"%s\" \n",namevar);
440: 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);
441: 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);
442: 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);
443: printf(" %6d",ncara); /* Nombre de caractres */
444: fprintf(ficreso," %6d",ncara); /* Nombre de caract res */
445: 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);
446: for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"%s ",title);
447: trzb(namevara,title);
448: printf("%10s ", namevara); /* Association */
449: fprintf(ficreso,"%10s ", namevara); /* Association */
450: 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);
451: 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);
452: for(n=0;n<2;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0';fprintf(ficlog,"\"%s\"",title);
453: printf("%3s", title); /* Brute C ou Calcule R */
454: fprintf(ficreso,"%3s", title); /* Brute C ou Calcul e R */
455: if(strrchr(title,'C')!= 0 || strrchr(title,'G')!=0){
456: if(level!=olevel){
457: pos=9;
458: firstpass=0;
459: if(ilect==2){
1.4 ! brouard 460: if(olevel==1){
! 461: fprintf(ficsas," ;\n output %s.%s%d;\n",indexfilefiname,indexfilefiname,olevel);
! 462: }else if (olevel==2){
! 463: fprintf(ficsas," ;\n numero +1;\n output %s.%s%d;\n",indexfilefiname,indexfilefiname,olevel);
! 464: }
! 465: fflush(ficsas);
! 466: if(idic[olevel]!=0)
! 467: fprintf(ficstatal[olevel],"\n}\n");
1.3 brouard 468: fprintf(ficsas," end\n");
469: }
1.4 ! brouard 470: if(level !=0){
1.3 brouard 471: if(ilect==2){
472: fprintf(ficsas," ;\n else if id=%d then do;\n",level);
1.4 ! brouard 473: if(idic[level]==0){
! 474: sprintf(filestatal[level],"s%s-dict%d.dct",indexfilefiname,level);
! 475: fprintf(ficlog,"\nOutput Stata dictionary: %s\n", filestatal[level]);
! 476: if((ficstatal[level]=fopen(filestatal[level],"w"))==NULL) { /* opened on subdirectory */
! 477: printf("Problem with Output Stata dictionary file: %s\n", filestatal[level]);
! 478: fprintf(ficlog,"Problem with Output Stata dictionary: %s\n", filestatal[level]);
! 479: fflush(ficlog);
! 480: goto end;
! 481: }
! 482: idic[level]=1;
! 483: /* fprintf(ficstatal[level],"* Sortie de rleda version %s, %s\n",version,fullversion ); */
! 484: /* fprintf(ficstatal[level],"* Nom de fichier %s et chemin \"%s\";\n* Stata dictionary\n",indexfilefiname,path); */
! 485: fprintf(ficstatal[level],"dictionary using \"%s.dat\"{\n",indexfilefiname );
! 486: }
1.3 brouard 487: }
1.4 ! brouard 488: }
1.3 brouard 489: if(ilect==2){
490: fprintf(ficsas," input @9 ");
1.4 ! brouard 491: if(idic[level]!=0)
! 492: fprintf(ficstatal[level]," str1 id _column(9) ");
1.3 brouard 493: printf(" input @9 ");
494: }
495: if(ilect==1){
496: levelmax=level;
497: }
498: olevel=level;
499: }
500: printf(" %d-%d", pos, pos+(ncara-1));
501: fprintf(ficreso," %d-%d", pos, pos+(ncara-1));
502: for(n=0;n<strlen(namevar);n++){
503: if(namevar[n]=='-')
504: break;
505: }
506: namevar[n]='\0';
507: if(ilect==1){
508: if(firstpass==0){
509: strcpy(var1[level],namevar);
510: printf(" Variable premiere %s, niveau %d namevar=%s\n",var1[level],level, namevar);
511: firstpass=1;
512: }
513: else{
514: strcpy(varn[level],namevar);
515: printf(" Variable N %s, niveau %d namevar=%s\n",varn[level],level, namevar);
516: }
517: }
518: if(ilect==2){
519: fprintf(ficsas, " %s $%d.", namevar, ncara);
1.4 ! brouard 520: fprintf(ficstatal[level], " str%d %s %%%ds", ncara, namevar, ncara);
1.3 brouard 521: }
522: pos+=ncara;nvaris++ ;
523: if (nvaris>=5) {
524: if(ilect==2){
525: fprintf(ficsas,"\n ") ;
1.4 ! brouard 526: fprintf(ficstatal[level],"\n ") ;
1.3 brouard 527: }
528: nvaris=0 ;
529: }
1.2 brouard 530: }
1.3 brouard 531: 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);
532: 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);
533: 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);
534: 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);
535: for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0';fprintf(ficlog,"%s ",title);
536: trzb(namevar,title);
537: printf(" %10s\n", namevar); /* format */
538: fprintf(ficreso," %10s", namevar); /* format */
539: 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);
540: for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0';fprintf(ficlog,"%s ",title);
541: 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);
542: break;
543: default: fprintf(ficlog,"Warning ntype=%d cas non prvu, voir les fichiers sources",ntype);
544: 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);
545: for(n=0;n<30;n++){ c=getc(ficdat); title[n]=c;} title[n]='\0'; fprintf(ficlog,"%s ",title);
546: printf(" %s\n", title);
1.1 brouard 547: }
1.3 brouard 548: n=0;
549: while((c=getc(ficdat)) !='\r'){
550: if(ncara != 0)
551: nn= n % ncara;
552: else
553: nn=0;
554: title[nn]=c;
555: if(nn==ncara-1){
556: title[nn+1]='\0';
557: fprintf(ficlog," \"%s\"",title);
558: printf(" \"%s\"",title);
559: fprintf(ficreso," \"%s\"",title);
560: n=n+1;
561: }else{
562: n=n+1;
563: }
1.1 brouard 564: }
1.3 brouard 565: if(c=='\r')
566: if((c=getc(ficdat))=='\n');
567: else ungetc(c,ficdat);
568: fprintf(ficlog,"\n");
569: fprintf(ficreso,"\n");
570: } /* End of read loop */
571: fclose(ficdat);
572: } /* End loop ilect */
573:
574: fprintf(ficsas," ;\n output %s.%s%d;\n end;\n else input;\nrun;\n",indexfilefiname,indexfilefiname,level);
1.4 ! brouard 575: fprintf(ficstatal[level],"\n}\n");
1.3 brouard 576: for(level=0; level <=levelmax; level++){
577: fprintf(ficsas,"proc contents data=%s.%s%d;run;\n",indexfilefiname,indexfilefiname,level);
1.4 ! brouard 578: /* fprintf(ficstata,"*proc contents data=%s.%s%d;run;\n",indexfilefiname,indexfilefiname,level); */
! 579: if(level==2){
! 580: 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);
! 581: }
! 582: else if(level==3){
! 583: 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);
! 584: fprintf(ficstata,"* merge numero using %s2\n* drop numero\n drop if id != \"3\"\nsave %s%d,replace\n",indexfilefiname,indexfilefiname,level);
! 585: }
1.3 brouard 586: }
587: fclose(ficsas);
1.4 ! brouard 588: fclose(ficstata);
1.1 brouard 589: printf("Le nom du fichier de debug est %s\n",filelog);
590: printf("Le nom du fichier du format de sortie est %s\n",filereso);
591: printf("Le nom du fichier SAS de sortie est %s\n",filesas);
1.4 ! brouard 592: printf("Le nom du fichier STATA de sortie est %s\n",filestata);
! 593: for(level=1;level<=levelmax;level++){
! 594: fclose(ficstatal[level]);
! 595: printf("Le nom du fichier STATA dictionnaire niveau %d de sortie est %s\n",level,filestatal[level]);
! 596: }
1.3 brouard 597: fclose(ficreso);
1.1 brouard 598: fclose(ficlog);
599: exit(0);
600: end:
601: fclose(ficlog);
602: exit(1);
603:
604: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>