#include "functions.h"

void usage() {
	printf("Usage: [options] [crypt|decrypt] [file]\n");
	printf("Options:\n");
	printf("\t-c: chiffrement de Cesar\n");
	printf("\t-v: chiffrement de Vigenere\n");
	printf("\t-t: chiffrement par transposition\n");
}
int fileExist(const char *path) {
	FILE *f=NULL;
	f=fopen(path,"r");

	if(f==NULL){
		return -1;
	}
	fclose(f);
	return 0;
}
/*
	path -> path source
	This function copy all characters of the file in the buffer
*/
int copyFile(const char *path, char *buffer) {
	FILE *f = fopen(path, "r+");
	int caract = 0;
	int i = 0;

	if(f == NULL) return -1;

	/* Copy the characters */
	do {
		caract = fgetc(f);
		buffer[i] = caract;
		i++;
	}while(caract != EOF);

	/* add '\0' */
	buffer[i-1] = '\0';

	fclose(f);

	return 0;
}
/*
	s -> path source
	This function count the number of characters in the file
	return -1 -> error
*/
int fileNumberCaract(const char *s) {
	FILE *f = NULL;
	int count = 0;

	f = fopen(s, "r+");
	if(f == NULL) return -1;

	/* Read character by character */
	while(fgetc(f) != EOF)
		count++;

	fclose(f);

	return count;
}
/*
	data -> data add in the file
	path ->path of the file
	This function add the data to file
	error : -2 -> impossible to open the file dest
*/
int addDataToFile(const char *data, const char *path) {
	FILE *f = fopen(path, "w+");

	if(f == NULL) return -2;

	fputs(data, f);

	fclose(f);

	return 0;
}
/*
	This function display the error 
	Error :
	0 -> no error for encryption
	1 -> no error for decryption
	-1 -> impossible to open the file source
	-2 -> impossible to open the file dest
*/
void err(const int error, const char *path) {
	switch(error) {
	case 0 :
		printf("Vos donnees ont bien etait crypter ou decrypter dans le fichier %s.\n", path);
		break;
	case -1 :
		printf("Une erreur s'est produite durant l'ouverture du fichier %s\n", path);
		break;
	case -2 :
		printf("Une erreur s'est produite durant l'ouverture du fichier %s\n", path);
	default:
		break;
	}
}