Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define UTF_SIZE 2
  5. #define UNIX "-unix"
  6. #define MAC "-mac"
  7. #define WIN "-win"
  8. #define BSLASH_N 0x004f
  9. #define BSLASH_R 0x006f
  10.  
  11. #define SWAP_END "-swap"
  12. #define KEEP_END "-keep"
  13.  
  14. int plainCopy(const char* srcName, const char* destName);
  15.  
  16. int opSystemChangeCopy(const char* srcName, const char* destName, const char* srcOpSysFlg,
  17.                        const char* destOpSysFlg);
  18.  
  19. int endSwapCopy(const char* srcName, const char* destName, const char* srcOpSysFlg,
  20.                 const char* destOpSysFlg, const char* endSwapFlg);
  21.  
  22. void swap(FILE* dest, char buffer[], const char* endSwapFlg);
  23.  
  24. char getNewLine(const char* opSysFlg);
  25.  
  26. int main(int argc, char *argv[]) {
  27.   // the output file would be the first arg.
  28.   // option 1: srcName + destName
  29.   // option 2: srcName + destName + srcOpSystemFlag + destOpSystemFlag
  30.   // option 3: same as option 2 but with endianChangeFlag
  31.  
  32.   // reading and writing files as binary files ----
  33.   // every char is in 2 bytes. UTF-16
  34.   // override same name file
  35.  
  36.   // try to open the source file
  37.   printf("got here \n");
  38.   switch (argc) {
  39.     case 3:
  40.     // option 1: srcName + destName
  41.     printf("case 1 \n");
  42.     return plainCopy(argv[1], argv[2]);
  43.  
  44.     /*case 5:
  45.     // option 2
  46.     fourArgs(argv[1], argv[2], argv[3], argv[4]);
  47.     break; */
  48.  
  49.     case 6:
  50.     // option 2
  51.     return endSwapCopy(argv[1], argv[2], argv[3], argv[4], argv[5]);
  52.  
  53.     default:
  54.     return 1;
  55.   }
  56.   return 0;
  57. }
  58.  
  59. int plainCopy(const char* srcName, const char* destName) {
  60.   return opSystemChangeCopy(srcName, destName, UNIX, UNIX);  
  61. }
  62.  
  63. int opSystemChangeCopy(const char* srcName, const char* destName, const char* srcOpSysFlg,
  64.                        const char* destOpSysFlg) {
  65.   return endSwapCopy(srcName, destName, srcOpSysFlg,
  66.               destOpSysFlg, KEEP_END);
  67. }
  68.  
  69. int endSwapCopy(const char* srcName, const char* destName, const char* srcOpSysFlg,
  70.                 const char* destOpSysFlg, const char* endSwapFlg) {
  71.  
  72.   // if the two files share the same name, we can't convert
  73.   if (!strcmp(srcName, destName)) {
  74.     return 1;
  75.   }
  76.  
  77.   FILE* src = fopen(srcName, "rb");
  78.   FILE* dest = fopen(destName, "wb");
  79.  
  80.   // if any File failed to open, return failure
  81.   if (src == NULL || dest == NULL) {
  82.     printf("files not open");
  83.     return 1;
  84.   }
  85.  
  86.   // if both opened successfully, continue
  87.   char* srcNewLineChar, destNewLineChar;
  88.   char buffer[2];
  89.  
  90.   // get the system's newLine character
  91.   char srcNewLine = getNewLine(srcOpSysFlg);
  92.   char destNewLine = getNewLine(destOpSysFlg);
  93.   char newLineBuff[2] = {destNewLine, 0};
  94.   printf("%c \n", srcNewLine);
  95.   // go over src, taking 2 bytes at a time (a single character in UTF-16)
  96.   while (fread(buffer, 1, 2, src) == 2) {
  97.      
  98.     // if the character is a newLine characer and the two systems are different
  99.     // change the char to the destination file's operating system's format
  100.     if ((buffer[0] == srcNewLine && buffer[1] == 0) && strcmp(srcOpSysFlg, destOpSysFlg)) {
  101.       printf("found a tiny o\n");
  102.       // if our source is Windows
  103.       if (!strcmp(srcOpSysFlg, WIN)) {
  104.         char temp[2];
  105.  
  106.         // keep reading characters to search for \r\n
  107.         while (fread(temp, 1, 2, src) == 2) {
  108.           if (temp[0] == BSLASH_R && temp[1] == 0) {
  109.  
  110.             // it's just a hanging \r, but we found another \r. keep looking.
  111.             swap(dest, temp, endSwapFlg);
  112.           } else {
  113.  
  114.             // we found a '\r\n' !! we can stop the loop that was looking for it.
  115.             if (temp[0] == BSLASH_N && temp[1] == 0) {
  116.               swap(dest, newLineBuff, endSwapFlg);
  117.             } else {
  118.               // the sequence is done
  119.               swap(dest, buffer, endSwapFlg);
  120.               swap(dest, temp, endSwapFlg);
  121.             }
  122.             continue;
  123.           }
  124.         } // looking for \n loop
  125.       } else {
  126.  
  127.         // if we're not on windows, first put the new line char
  128.         swap(dest, newLineBuff, endSwapFlg);
  129.  
  130.         // if we are transferring to windows, just add another \n
  131.         if (!strcmp(destOpSysFlg, WIN)) {
  132.           char bSlashNBuff[2] = {BSLASH_N, 0};
  133.           swap(dest, bSlashNBuff, endSwapFlg);
  134.         }
  135.       }
  136.     } else {
  137.  
  138.       // nothing special is needes for a random character
  139.       swap(dest, buffer, endSwapFlg);
  140.     }
  141.   }
  142.   fclose(src);
  143.   fclose(dest);
  144.   printf("end func");
  145.   return 0;
  146. }
  147.  
  148. void swap(FILE* dest, char buffer[], const char* endSwapFlg) {
  149.  
  150.   // if we have to swap endian-ness, swap the two bytes of every character
  151.   if (!strcmp(endSwapFlg, SWAP_END)) {
  152.     // takes only a single byte since we're in C
  153.     char firstByte = buffer[0];
  154.     char secondByte = buffer[1];
  155.  
  156.     // write the two bytes in reverse order
  157.     fwrite(&secondByte, 1, 1, dest);
  158.     fwrite(&firstByte, 1, 1, dest);
  159.   } else {
  160.  
  161.       // write the buffer to the file without swapping endians
  162.       fwrite(buffer, 1, 2, dest);
  163.   }
  164. }
  165.  
  166. char getNewLine(const char* opSysFlg) {
  167.   if (!strcmp(opSysFlg, UNIX)) {
  168.     return BSLASH_R;
  169.   }
  170.   return BSLASH_N;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement