Advertisement
FoxTuGa

Encode_DecodeV2 [Complete]

Mar 19th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.30 KB | None | 0 0
  1. /*  Author: Leandro Soares
  2. School: INETE
  3. Date:   19-03-2012
  4. Time:   20:19   */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <conio.h>
  9. #include <ctype.h>
  10. #include <string.h>
  11.  
  12. #define maxRuns 13
  13. #define ENC "Encode"
  14. #define DEC "Decode"
  15.  
  16. char Encode_Letter(char letter, char type[]);
  17. void code_file(char *fname, char type[]);
  18. void Add_ext(char *sname);
  19. void code_text(char *fnameO);
  20.  
  21. int main(int argc, char *argv[]) {
  22.     char snameI[20], snameO[20];
  23.     snameI[0] = snameO[0] = '\0';
  24.  
  25.     if( !strcmp(argv[1], "textencode") && argc == 3) {
  26.         strcpy(snameO, argv[2]);
  27.         code_text(snameO);
  28.     }
  29.     else {
  30.         if( ( strlen(argv[2]) < 20 && strlen(argv[3]) < 20 ) && argc == 4 ) {
  31.             strcpy(snameI, argv[2]);
  32.             strcpy(snameO, argv[3]);
  33.  
  34.             if( !strcmp(argv[1], "encode")  )
  35.                 code_file(snameI, snameO, ENC);
  36.             else    if( !strcmp(argv[1], "decode") )
  37.                 code_file(snameI, snameO, DEC);
  38.         }
  39.     }
  40.     return 0;
  41. }
  42.  
  43. char Encode_Letter(char letter, char type[]) {
  44.     int idxa, idxb;
  45.     char min, max;
  46.  
  47.     if( ( letter >= 'a' && letter <= 'z' ) || ( letter >= 'A' && letter <= 'Z' ) ) {
  48.         if( islower(letter) ) {
  49.             min = 'a';
  50.             max = 'z';
  51.         }
  52.         else    if ( isupper(letter) ){
  53.             min = 'A';
  54.             max = 'Z';
  55.         }
  56.  
  57.         for(idxb = maxRuns, idxa = letter; idxb > 0 ; idxb--, idxa++) {
  58.             if( idxa == max )
  59.                 idxa = min-1;
  60.         }
  61.  
  62.         letter = idxa;
  63.     }
  64.     else
  65.         letter = '\0';
  66.  
  67.     return letter;
  68. }
  69. void code_file(char *fname, char *Ofname, char type[]) {
  70.     FILE * pFileE, *pFileOutput;
  71.     char strbuffer[5000], strbufferOUT[5000];
  72.     int idx = 0;
  73.  
  74.     strbufferOUT[0] = strbuffer[0] = '\0';
  75.  
  76.     Add_ext(fname);
  77.     Add_ext(Ofname);
  78.     pFileE = fopen(fname,"r");
  79.     if( !strcmp(type, DEC) )
  80.         pFileOutput = fopen(Ofname, "w");
  81.     if( !strcmp(type, ENC) )
  82.         pFileOutput = fopen(Ofname, "w");
  83.  
  84.     while( fgets(strbuffer,4997,pFileE) != NULL ){
  85.         while( strbuffer[idx] != '\0' ) {
  86.             if( ( strbuffer[idx] >= 'a' && strbuffer[idx] <= 'z' ) || ( strbuffer[idx] >= 'A' && strbuffer[idx] <= 'Z' ) )
  87.                 strbufferOUT[idx] = Encode_Letter(strbuffer[idx], type);
  88.             else
  89.                 strbufferOUT[idx] = strbuffer[idx];
  90.             idx++;
  91.         }
  92.         strbufferOUT[idx] = '\0';
  93.         fputs(strbufferOUT, pFileOutput);
  94.         idx = 0;
  95.     }
  96.  
  97.     if( !strcmp(type, DEC) )
  98.         printf("\nDescodificacao completa!!\n\tFicheiro de Input: %s \n\tFicheiro de Output: %s \n\n", Ofname, fname);
  99.     if( !strcmp(type, ENC) )
  100.         printf("\n\nCodificacao completa!!\n\tFicheiro de Input: %s \n\tFicheiro de Output: %s \n\n", fname, Ofname);
  101.  
  102.     fclose(pFileOutput);
  103.     fclose(pFileE);
  104. }
  105. void code_text(char *fnameO) {
  106.     char strbuffer[5000], strbufferOUT[5000];
  107.     int idx = 0;
  108.     FILE *pFileOutput;
  109.    
  110.     Add_ext(fnameO);
  111.     pFileOutput = fopen(fnameO, "w");
  112.  
  113.     printf("\n\n>");
  114.     scanf("%[^\n]s", strbuffer);
  115.  
  116.     while( strbuffer[idx] != '\0' ) {
  117.         if( ( strbuffer[idx] >= 'a' && strbuffer[idx] <= 'z' ) || ( strbuffer[idx] >= 'A' && strbuffer[idx] <= 'Z' ) )
  118.             strbufferOUT[idx] = Encode_Letter(strbuffer[idx], ENC);
  119.         else
  120.             strbufferOUT[idx] = strbuffer[idx];
  121.         idx++;
  122.     }
  123.     strbufferOUT[idx] = '\0';
  124.     fputs(strbufferOUT, pFileOutput);
  125.     idx = 0;
  126.  
  127.     printf("\nCodificacao completa!!\n\tFicheiro de Output: %s \n\n", fnameO);
  128.     fclose(pFileOutput);
  129. }
  130. void Add_ext(char *sname) {
  131.     while( *sname != '\0' ) sname++;
  132.     *(sname) = '.';
  133.     *(sname+1) = 't';
  134.     *(sname+2) = 'x';
  135.     *(sname+3) = 't';
  136.     *(sname+4) = '\0';
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement