Informations sur: duplic

Publié par Freem le 11/01/2008

Description

Ce code devrait normallement servir à dupliquer des lignes selon un tableau (séparateur: tabulation) mais ca ne marche pas tout a fait...

Code source (langage non précisé)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct
{
	char nom[30];
	char nbVitr[3];
	char v0,v4,v6,v7,v8,v8p;
}TVitrauxDeco ;

void quit(void);
void ecrLigne2(char car,int offset, char*ligne2 ,char* substitut, FILE *fic);

char *ligne, *ligne2;
TVitrauxDeco *tableau;

int main(/*int nbArg, char **pArg*/)
{
	int var=0;
	FILE *input=0, *output=0, *tableauImprime=0;
	int tllLigne, nbLigTab=0, i,j,y=-2, x;
	char code[6][5]={{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}};
	char nom[30], *ptrNom=0;
	char nom2[30], *ptrNom2=0;


	ligne=0;ligne2=0;tableau=0;

	atexit(quit);
	input=fopen("data2.txt"/*pArg[1]*/,"r");
	tableauImprime=fopen("tableau.txt"/*pArg[2]*/,"r");

	if(!input)
		printf("Erreur: le fichier d'entree %d n'a pas ete ouvert\\n","data.txt"/*pArg[1]*/);
	if(!tableauImprime)
		printf("Erreur: le fichier tableau %d n'a pas ete ouvert\\n","tableau.txt"/*pArg[2]*/);
	if(!input || !tableauImprime)
	{
		printf("Usage:\\targ1: fichier contenant les données à dupliquer\\n\\targ2: fichier contenant le tableau de correspondances\\n\\targ3: fichier de sortie qui est cree si non existant et ecrase sinon\\n");
		exit(1);
	}

	//compte les lignes et alloue tableau
	do
	{
		nbLigTab++;
		if(!lireLigne(0,tableauImprime))
			nbLigTab--;
	}while(!feof(tableauImprime));
	if(tableau)
		free(tableau);
	tableau=calloc(nbLigTab,sizeof (TVitrauxDeco));

	//lit la 1ere ligne du tableau et la parse dans code
	if(ligne)
		free(ligne);
	ligne=malloc(100);
	if(ligne2)
		free(ligne2);
	ligne2=malloc(100);
	fseek(tableauImprime, 0, SEEK_SET);
	lireLigne(ligne, tableauImprime);
	for(i=0; i<strlen(ligne); i++)
	{
		while(ligne[i]=='\\t')
		{
			x=0;
			y++;
			i++;
		}
		code[y][x]=ligne[i];
		x++;
	}

	//enregistre les lignes du tableau
	for(y=0;y<nbLigTab;y++)
	{
		lireLigne(ligne, tableauImprime);
		x=0;
		i=0;
		j=0;
		while(ligne[i] && ligne[i]!='\\n' && ligne[i]!='\\r')
		{
			while(ligne[i]=='\\t')
			{
				j=0;
				i++;
				x++;
			}
			switch(x)
			{
				case 0:
					tableau[y].nom[j]=ligne[i];
					break;
				case 1:
					tableau[y].nbVitr[j]=ligne[i];
					break;
				case 2:
					tableau[y].v0=ligne[i];
					break;
				case 3:
					tableau[y].v4=ligne[i];
					break;
				case 4:
					tableau[y].v6=ligne[i];
					break;
				case 5:
					tableau[y].v7=ligne[i];
					break;
				case 6:
					tableau[y].v8=ligne[i];
					break;
				case 7:
					tableau[y].v8p=ligne[i];
					break;
			}
			j++;
			i++;
			if(x>1)
				while(ligne[i]!='\\t' && ligne[i]!=0)
					i++;
		}
	}


	output=fopen("test.txt"/*pArg[3]*/,"w");
	fgetc(input);
	while(!feof(input))
	{
		fseek(input,-1,SEEK_CUR);
		//lecture d'une ligne du fichier d'entree
		tllLigne=lireLigne(0,input);
		if(ligne)
			free(ligne);
		ligne=calloc(tllLigne+1,1);
		if(ligne2)
			free(ligne2);
		ligne2=calloc(tllLigne+1,1);
		fseek(input, -tllLigne-2, SEEK_CUR);
		lireLigne(ligne, input);
		strcpy(ligne2, ligne);

		//lecture du champ de nom et recherche dans le tableau

		x=chCaseN(ligne,7);
		i=chCaseN(ligne,8);
		ptrNom2=strcpy(nom2, &(ligne[x]));
		if(code[0][0]==0)
		{
			// ERREUR!!! ERREUR!!! ERREUR!!!
			printf("La, y'a une ...!!\\n");

		}
		ptrNom=strcpy(nom, &(ligne[x]));
		if(code[0][0]==0)
		{
			// ERREUR!!! ERREUR!!! ERREUR!!!
			printf("La, y'a une ...!!\\n");
		}
		nom[i-x-1]=0;
		x=chCaseN(ligne,14);
		x=ligne[x]-'0';
		if(x)
		{
			for(i=0;i<nbLigTab;i++)
			{
				if(!strcmp(nom, tableau[i].nom) && x==atoi(tableau[i].nbVitr))
				{
					y=chCaseN(ligne2,10);//localise la valeur à remplacer
					if(strcmp("0000",&(ligne[y]))) // si la valeur ne vaut PAS "0000"
					{
						fputs(ligne,output); //recopier l'original d'abord
						fputc('\\n',output);
					}
					ecrLigne2(tableau[i].v0,y,ligne2,code[0],output);
					ecrLigne2(tableau[i].v4,y,ligne2,code[1],output);
					ecrLigne2(tableau[i].v6,y,ligne2,code[2],output);
					ecrLigne2(tableau[i].v7,y,ligne2,code[3],output);
					ecrLigne2(tableau[i].v8,y,ligne2,code[4],output);
					ecrLigne2(tableau[i].v8p,y,ligne2,code[5],output);
					break;
				}
			}
		}
		else
		{
			fputs(ligne,output);
			fputc('\\n',output);
		}
		fgetc(input);
	}

	fclose(input);
	fclose(output);
	exit(0);
}

/*
//concatene les donnees d'exportation de prodevis4
//prend 2 arg.: le fichier d'entree et le fichier de sortie
main(int nbArg, char **pArg)
{
	FILE *input1, *input2, *output, *erreur;
	int cpt, c=0,i=0;
	char chaine[3000];
	char nomFic[50];
	unsigned int truc=0;

	erreur=fopen("erreur.txt","w");
	output=fopen(pArg[2],"w");
	input1=fopen(pArg[1],"r");
	while(c!=EOF)
	{
		memset(chaine,0,3000);
		truc++;
		cpt=0;
		do
		{
			c=fgetc(input1);
			chaine[cpt]=c;
			cpt++;
		}while(c!='|' && c!=EOF);

		strncpy(nomFic,chaine,cpt);
		nomFic[cpt-1]=0;
		input2=fopen(nomFic,"r");
		if(!input2)
		{
			printf("Erreur de fichier à la %dième opération.\\n",truc);
			fprintf(erreur,"Le fichier <%s> n'a pu etre ouvert.\\n",nomFic);
		}
		else
		{
			cpt+=lireLigne(&(chaine[cpt]),input1);
			i=0;
			do
			{
				cpt+=i;
				chaine[cpt]='|';
				cpt++;
			}while((i=lireLigne(&(chaine[cpt]),input2))>0);
			fputs(chaine, output);
			fputs("\\n",output);
			fclose(input2);
		}
	}
	fclose(input1);
	fclose(output);
	fclose(erreur);
}
*/

//Cette fonction renvoie 1 si toutes les cases ont été lues, 0 sinon.
//Cette fonction ne controle pas la taille des cases avant d'écrire
//attention donc aux dépassements!!!
int scanLigne(char *iLigne, int *nbCases, void **pCase)
{
	int i=0, j=0, k=0;

	while(iLigne[i] && iLigne[i]!='\\r' && iLigne[i]!='\\n')
	{
		while(iLigne[i]=='\\t')
		{
			((char**)pCase)[j][k]=0;
			k=0;
			i++;
			j++;
			if(j>*nbCases)
			{
				*nbCases=j;
				return 0;
			}
		}
		((char**)pCase)[j][k]=iLigne[i];
		i++;
		k++;
	}
	return 1;
}

//lit une ligne et retourne le nombre de car. lus (comme strlen).
//Le \\n n'est pas intégré dans la chaine.
//si chaine vaut 0, les caracteres ne sont simplement pas stockes
int lireLigne(char *oLigne, FILE *iFic)
{
	int cpt=0;
	int c=0;

	while(c!='\\n' && !feof(iFic))
	{
		c=fgetc(iFic);
		if(oLigne)
			oLigne[cpt]=c;
		cpt++;
	}
	cpt--;
	if(oLigne)
		oLigne[cpt]=0;
	return cpt;
}

int chCaseN(char *ligne, int numCase)
{
	int x=0, i;

	for(i=0;i<strlen(ligne);i++)
	{
		if(ligne[i]=='\\t')
		{
			x++;
			if(x==numCase)
				return i+1;
		}
	}
	printf("Erreur: aucune tabulation trouvée dans la ligne:\\n%s\\n");
	return 0;
}

void quit(void)
{
	if(ligne)
		free(ligne);
	if(ligne2)
		free(ligne2);
}

void ecrLigne2(char car,int offset, char*ligne2 ,char* substitut, FILE *fic)
{
	if(car=='O')
	{
		strncpy(&(ligne2[offset]),substitut,4);
		fputs(ligne2,fic);
		fputc('\\n',fic);
	}
}
v6 © Computaid SPRL 2005-2008 - Tous droits réservés - Hébergé par eTigris - Page générée en 0,094 s - Crédits - Stats
1 connecté