Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Author: Leandro Soares
- School: INETE
- Date: 19-03-2012
- Time: 20:19 */
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <ctype.h>
- #include <string.h>
- #define maxRuns 13
- #define ENC "Encode"
- #define DEC "Decode"
- char Encode_Letter(char letter, char type[]);
- void code_file(char *fname, char type[]);
- void Add_ext(char *sname);
- void code_text(char *fnameO);
- int main(int argc, char *argv[]) {
- char snameI[20], snameO[20];
- snameI[0] = snameO[0] = '\0';
- if( !strcmp(argv[1], "textencode") && argc == 3) {
- strcpy(snameO, argv[2]);
- code_text(snameO);
- }
- else {
- if( ( strlen(argv[2]) < 20 && strlen(argv[3]) < 20 ) && argc == 4 ) {
- strcpy(snameI, argv[2]);
- strcpy(snameO, argv[3]);
- if( !strcmp(argv[1], "encode") )
- code_file(snameI, snameO, ENC);
- else if( !strcmp(argv[1], "decode") )
- code_file(snameI, snameO, DEC);
- }
- }
- return 0;
- }
- char Encode_Letter(char letter, char type[]) {
- int idxa, idxb;
- char min, max;
- if( ( letter >= 'a' && letter <= 'z' ) || ( letter >= 'A' && letter <= 'Z' ) ) {
- if( islower(letter) ) {
- min = 'a';
- max = 'z';
- }
- else if ( isupper(letter) ){
- min = 'A';
- max = 'Z';
- }
- for(idxb = maxRuns, idxa = letter; idxb > 0 ; idxb--, idxa++) {
- if( idxa == max )
- idxa = min-1;
- }
- letter = idxa;
- }
- else
- letter = '\0';
- return letter;
- }
- void code_file(char *fname, char *Ofname, char type[]) {
- FILE * pFileE, *pFileOutput;
- char strbuffer[5000], strbufferOUT[5000];
- int idx = 0;
- strbufferOUT[0] = strbuffer[0] = '\0';
- Add_ext(fname);
- Add_ext(Ofname);
- pFileE = fopen(fname,"r");
- if( !strcmp(type, DEC) )
- pFileOutput = fopen(Ofname, "w");
- if( !strcmp(type, ENC) )
- pFileOutput = fopen(Ofname, "w");
- while( fgets(strbuffer,4997,pFileE) != NULL ){
- while( strbuffer[idx] != '\0' ) {
- if( ( strbuffer[idx] >= 'a' && strbuffer[idx] <= 'z' ) || ( strbuffer[idx] >= 'A' && strbuffer[idx] <= 'Z' ) )
- strbufferOUT[idx] = Encode_Letter(strbuffer[idx], type);
- else
- strbufferOUT[idx] = strbuffer[idx];
- idx++;
- }
- strbufferOUT[idx] = '\0';
- fputs(strbufferOUT, pFileOutput);
- idx = 0;
- }
- if( !strcmp(type, DEC) )
- printf("\nDescodificacao completa!!\n\tFicheiro de Input: %s \n\tFicheiro de Output: %s \n\n", Ofname, fname);
- if( !strcmp(type, ENC) )
- printf("\n\nCodificacao completa!!\n\tFicheiro de Input: %s \n\tFicheiro de Output: %s \n\n", fname, Ofname);
- fclose(pFileOutput);
- fclose(pFileE);
- }
- void code_text(char *fnameO) {
- char strbuffer[5000], strbufferOUT[5000];
- int idx = 0;
- FILE *pFileOutput;
- Add_ext(fnameO);
- pFileOutput = fopen(fnameO, "w");
- printf("\n\n>");
- scanf("%[^\n]s", strbuffer);
- while( strbuffer[idx] != '\0' ) {
- if( ( strbuffer[idx] >= 'a' && strbuffer[idx] <= 'z' ) || ( strbuffer[idx] >= 'A' && strbuffer[idx] <= 'Z' ) )
- strbufferOUT[idx] = Encode_Letter(strbuffer[idx], ENC);
- else
- strbufferOUT[idx] = strbuffer[idx];
- idx++;
- }
- strbufferOUT[idx] = '\0';
- fputs(strbufferOUT, pFileOutput);
- idx = 0;
- printf("\nCodificacao completa!!\n\tFicheiro de Output: %s \n\n", fnameO);
- fclose(pFileOutput);
- }
- void Add_ext(char *sname) {
- while( *sname != '\0' ) sname++;
- *(sname) = '.';
- *(sname+1) = 't';
- *(sname+2) = 'x';
- *(sname+3) = 't';
- *(sname+4) = '\0';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement