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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include "Structure.h"
//Aide mémoire to myself: -to stock[marchandise]
// -to store[computer science]
int main(int argc,char *argv[])
{
Picture Im;
int i=0,j=0;
int count;
char **tab;
// char temp[6][12];
/**************************************************/
/*********Appel du programme sans argument*********/
if(argc == 1)
{
printf("example of command:\n \t -i picture.ppm\n ");
printf("It copies and paste the input file to the one in the output\n");
printf("how many words would like to write ? ex:-i = one word\n");
scanf("%i",&count);
tab=createTable(count, 20);
for(i=0;i<count;i++)//on rentre autant de variable qu'on veut
{
printf("please enter new variable:\n");
scanf("%s",tab[i]);
}
}//Nb: il faut un tableau dynamique a deux dimmension qui permet de rentrer autant de mots qu'on veut
/**************************************************/
/*********Appel du programme avec argument********/
if(argc >1)//l'utilisateur a écrit dans argv (à terminer)
{
for(i=0;i<argc;i++)
{
//tab[i]=argv[i+1];//argv[0] adresse réservé
}
}
for(;;)
{
if(tab[0][0]=='-')
{
if(strcmp(tab[0],"-?")==0)
{
printf("example of command:\n \t -i picture.ppm pak.ppm\n ");
printf("It copies and paste the input file to the one in the output\n");
}
else if(strcmp(tab[0],"--help")==0)
printf("need mega help ?\n");
/*
else if(strcmp(argv[1],"-g")==0)
printf("work in progress ...");
else if(strcmp(argv[1],"-h")==0)
printf("work in progress ...");
else if(strcmp(argv[1],"-e")==0)
printf("work in progress ...");
else if(strcmp(argv[1],"-d")==0)
printf("work in progress ...");
else if(strcmp(argv[1],"-b")==0)
printf("work in progress ...");
else if(strcmp(argv[1],"-c")==0)
printf("work in progress ...");
else if(strcmp(argv[1],"-p")==0)
printf("work in progress ...");
*/
else if(strcmp(tab[0],"-i")==0)
{
InpFile(&Im,tab);
cptChar(&Im);
}
else if(strcmp(tab[0],"-o")==0)
{
OutFile(&Im,tab);
StoreMemory(&Im);
CopyFile(&Im);
printf("copy succeeded\n\n");
}
/*
else if(strcmp(argv[1],"-li")==0)
printf("work in progress ...");
else if(strcmp(argv[1],"-lo")==0)
printf("work in progress ...");
*/
fflush(stdin);
freeTable(tab);
printf("how many words would like to write ? ex:-i = one word\n");
scanf("%i",&count);
tab=createTable(count, 12);
for(i=0;i<count;i++)//on rentre autant de variable qu'on veut
{
printf("please enter new variable:\n");
scanf("%s",tab[i]);
}
}//fin du if tab[0]
else {
fflush(stdin);
freeTable(tab);
printf("how many words would like to write ? ex:-i = one word\n");
scanf("%i",&count);
tab=createTable(count, 12);
for(i=0;i<count;i++)//on rentre autant de variable qu'on veut
{
printf("please enter new variable:\n");
scanf("%s",tab[i]);
}
}
}
return 0;//The programm quit normally
}
/******Function***************/
void InpFile(Picture *Im1,char **arguments)
//That function catch up the input file name and move it to the structure
{
FILE *pictureI=NULL;
int i;
for(i=0;arguments[1][i]!='\0';i++)
{
Im1->InpName[i]=arguments[1][i];
printf("%c",arguments[1][i]);
//tab[1]=inputFile
}
pictureI= fopen(Im1->InpName,"rb");
if(pictureI== NULL)
perror(Im1->InpName);
//If the inputPicture is null,it displays an error
else {printf("ok\n");}
//However, if it is ok, it displays "ok"
close(pictureI);
}
void OutFile(Picture *Im1,char **arguments1)
//That function catch up the input file name and move it to the structure
{
FILE *pictureO=NULL;
int i;
for(i=0;arguments1[1][i]!='\0';i++)
{
Im1->OutName[i]=arguments1[1][i];
//argv[3]=Output File
}
pictureO= fopen(Im1->OutName,"rb");
close(pictureO);
}
char *cptChar(Picture *Im2)
//CptChar is used as to count the number of character in
//the input file
{
FILE *pictureI=NULL;
int temp;
pictureI= fopen(Im2->InpName,"rb");
if(pictureI== NULL)
perror(Im2->InpName);
while(fgetc(pictureI) != EOF)
Im2->cpt++;
//Cpt is incremented while the file is not at the end
close(pictureI);
}
void StoreMemory(Picture *Im3)
//StoreMemory is used for putting in memory the input File
{
FILE *pictureI=NULL;
char tab[Im3->cpt+1];
int i=0;
pictureI= fopen(Im3->InpName,"rb");
if(pictureI== NULL)
perror(Im3->InpName);
Im3->tabPicture = malloc( Im3->cpt * sizeof(Im3->cpt) );
while( fgets(tab,Im3->cpt,pictureI) != NULL)
{
strcat(Im3->tabPicture,tab);
//Each line of the input file is store on "tab", then concenatancé
//with Im3->tabPicture
}
close(pictureI);
}
void CopyFile(Picture *Im3)
//That function copy from the memory to the Output file
{
FILE *pictureI=NULL;
FILE *pictureO=NULL;
char tab[Im3->cpt+1];
int i=0;
pictureI= fopen(Im3->InpName,"rb");
if(pictureI== NULL)
perror(Im3->InpName);
pictureO = fopen(Im3->OutName,"wb");
if (pictureO == NULL)
perror(Im3->OutName);
for(i=0;i<Im3->cpt;i++)
{
fputc(Im3->tabPicture[i],pictureO);
//Paste the "memory tab" to the output file
}
free(Im3->tabPicture);
//system("cd Projet/livrable_1/%s gqview ", Im3->OutName );
close(pictureI);
close(pictureO);
}
char **createTable(int nbLin, int nbCol){
int i=0;
char **tableau = (int **)malloc(sizeof(int*)*nbLin);
char *tableau2 = (int *)malloc(sizeof(int)*nbCol*nbLin);
for(i = 0 ; i < nbLin ; i++){
tableau[i] = &tableau2[i*nbCol];
}
return tableau;
}
void freeTable(char **tableau){
free(tableau[0]);
free(tableau);
} |
Partager