Skip to content

Instantly share code, notes, and snippets.

@benallamar
Last active October 23, 2016 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benallamar/b0ca481fec1f561c67584c705e08cf51 to your computer and use it in GitHub Desktop.
Save benallamar/b0ca481fec1f561c67584c705e08cf51 to your computer and use it in GitHub Desktop.
//
// Created by Marouane BENALLA on 07/10/2016.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* Main function
*/
//Define the main modes of the excution
static int MODE_SHOW_IN = 0;
static int MODE_SHOW_OUT = 0;
static int MODE_ERASE = 0;
//To activate the interactive mode, just switch the value to 1 or add in your command line the params -I
static int MODE_INTERACTIVE = 0;
//We initiate the orgPath and the desPath
static char orgPath[50];
static char desPath[50];
//Define the new type StringArray
struct StringArray {
char text[100];
struct StringArray *next;
};
//Look if the file with the name 'file_name' exist
int fileExist(char file_name[]) {
return !access(file_name, F_OK);
}
//This function take a file as an argument, and return a 'StringArrayList' of all the file's lines.
struct StringArray *extractString(FILE *file) {
struct StringArray *aOne = (struct StringArray *) malloc(sizeof(struct StringArray));
struct StringArray *first = aOne;
if (aOne != NULL) {
char str[100];
while (fgets(str, 100, file) != NULL) {
if (MODE_SHOW_IN) {
puts(str);
}
strcpy(aOne->text, str);
struct StringArray *aTwo = (struct StringArray *) malloc(sizeof(struct StringArray));
if (aTwo != NULL) {
aOne->next = aTwo;
aOne = aTwo;
}
}
}
return first;
}
//Switch the core values 'text' of two given Node(StringArray)
void switchNode(struct StringArray *nodeOne, struct StringArray *nodeTwo) {
char aux[100];
strcpy(aux, nodeOne->text);
strcpy(nodeOne->text, nodeTwo->text);
strcpy(nodeTwo->text, aux);
}
//Order the StringArray list, using the 'Selection Sort' Algorithm
void orderStringArray(struct StringArray *stringArray) {
struct StringArray *aux = stringArray;
while (aux != NULL && aux->next != NULL) {
puts("***********");
if (strcmp(aux->text, aux->next->text) > 0) {
switchNode(aux, aux->next);
}
aux = aux->next;
}
if (stringArray->next != NULL) {
orderStringArray(stringArray->next);
}
}
//Create a file from the StringArrayList
void writeFile(struct StringArray *stringArray, FILE *destFile) {
fputs(stringArray->text, destFile);
while (stringArray->next != NULL) {
stringArray = stringArray->next;
if (MODE_SHOW_OUT) {
puts(stringArray->text);
}
fputs(stringArray->text, destFile);
}
}
// Function regrouping the main features: Opening the files, extract information, sort and create the final file.
void trifich(char originFile[], char destinFile[]) {
// Open the files
FILE *ori = fopen(orgPath, "r");
if (ori == NULL) {
printf("No file has been found");
} else {
//Extract the list
struct StringArray *stringArray = extractString(ori);
fclose(ori);
FILE *des = fopen(desPath, "w");
// Order the list
orderStringArray(stringArray);
//Create the file
writeFile(stringArray, des);
fclose(des);
}
}
//Handles the arguments, and send the result to the trifich
int handleArgs(int args, char *argv[]) {
int fileCount = 0;
int index = 1;
while (index < args) {
if (strlen(argv[index]) == 2) {
if (strcmp(&argv[index][0], "-i") == 0)
MODE_SHOW_IN = 1;
else if (strcmp(&argv[index][0], "-o") == 0)
MODE_SHOW_OUT = 1;
else if (strcmp(&argv[index][0], "-I") == 0)
MODE_INTERACTIVE = 1;
} else {
if (fileCount == 0) {
strcpy(orgPath, argv[index]);
printf("orgPath: %s \n", argv[index]);
fileCount++;
} else {
strcpy(desPath, argv[index]);
printf("desPath: %s \n", desPath);
fileCount++;
}
}
index++;
}
//Here to handle the interactive mode.
if (MODE_INTERACTIVE) {
while (!fileCount) {
printf("Please tape the name of your source file:\n");
gets(orgPath);
if (fileExist(orgPath)) {
fileCount++;
} else {
puts("Error: The given file doesn't exist, please try again,\r");
}
}
if (fileCount == 1) {
printf("Please tape the name of your destination file:\n");
gets(desPath);
if (strlen(desPath) == 0) {
puts("Error: No destination file has been given, abort");
return 0;
}
while (fileExist(desPath) && MODE_ERASE) {
printf("Please tape the name of your destination file:\n");
gets(desPath);
}
}
} else {
if (fileCount > 0) {
if (fileCount == 1) {
if (fileExist(orgPath) && MODE_ERASE)
strcpy(desPath, orgPath);
else
puts("Error: Either your file doesn't exist or you haven't activated the erase mode");
} else {
if (fileExist(desPath) && !MODE_ERASE) {
puts("Error: the destination file already exist, but the mode eras hasn't been activated");
return 0;
}
}
} else {
puts("Error: No file source has been given");
return 0;
}
}
trifich(orgPath, desPath);
}
//Finally the function main
int main(int args, char *argv[]) {
// Function tp execute the finale code.
handleArgs(args, argv);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment