Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6.  
  7. #define truel true
  8. #define fragments   0x01    // split into fragments with given size -s
  9. #define parts       0x02    // split into given number of parts -n
  10. #define error       0x00    // error
  11.  
  12. typedef struct
  13. {
  14.     char* inputFileName;
  15.     char* outputFileTemplate;
  16.     int commandCode;
  17.     int parameter;
  18. } inputStruct;
  19.  
  20. inputStruct parseInput(int argc, char *argv[]);
  21. int stringToInt(char* str);
  22.  
  23. int main (int argc, char *argv[])
  24. {
  25.     argc = 6;
  26.     argv[1] = "hey.txt";
  27.     argv[2] = "-s";
  28.     argv[3] = "1";
  29.     argv[4] = "-b";
  30.     argv[5] = "bubuka";
  31.  
  32.     FILE* fp;
  33.     inputStruct input = parseInput(argc, argv);
  34.  
  35.     if(input.commandCode == error)
  36.     {
  37.         printf("Input error\n");
  38.         return -99;
  39.     }
  40.     else if(input.commandCode == fragments)
  41.     {
  42.         fp = fopen(input.inputFileName, "rb");
  43.         char box;
  44.         int n=0,m=1;
  45.         char* name = (char*)malloc(strlen(input.outputFileTemplate)+100);
  46.         strcpy(name, input.outputFileTemplate);
  47.         printf("%s", name);
  48.         strcat(name, "_");
  49.         strcat(name, (char*)m);
  50.         strcat(name,".txt");
  51.         FILE* nano = fopen(name,"wb");
  52.         while((box=getc(fp))!=EOF)
  53.         {
  54.             putc(box,nano);
  55.             n++;
  56.             if (n == input.parameter)
  57.             {
  58.                 fclose(nano);
  59.                 m++;
  60.                 n=0;
  61.                 //name = input.outputFileTemplate;
  62.                 strcpy(name, input.outputFileTemplate);
  63.                 strcat(name,"_");
  64.                 strcat(name, (char*)m);
  65.                 strcat(name,".txt");
  66.                 //name = name + '_'+ m + '.txt';
  67.                 nano = fopen(name,"wb");
  68.             }
  69.         }
  70.         if (n!=0)
  71.         {
  72.             fclose(nano);
  73.         }
  74.     }
  75.     else if(input.commandCode == parts)
  76.     {
  77.  
  78.     }
  79.     else
  80.     {
  81.         printf("Unknown parse error\n");
  82.         return -98;
  83.     }
  84.  
  85.     fclose(fp);
  86.  
  87.     return 0;
  88. }
  89.  
  90. inputStruct parseInput(int argc, char *argv[])
  91. {
  92.     inputStruct input;
  93.  
  94.     input.commandCode = 0;
  95.     input.inputFileName = '\0';
  96.     input.outputFileTemplate = '\0';
  97.     input.parameter = 0;
  98.  
  99.     if(argc != 6)
  100.     {
  101.         input.commandCode = error;
  102.         return input;
  103.     }
  104.  
  105.     // first argument
  106.     input.inputFileName = argv[1];
  107.  
  108.     // second argument
  109.     char* arg2 = argv[2];
  110.     if(strlen(arg2) != 2 || arg2[0] != '-')
  111.     {
  112.         input.commandCode = error;
  113.         return input;
  114.     }
  115.     else if(arg2[1] == 'b')
  116.     {
  117.         input.outputFileTemplate = argv[3];
  118.     }
  119.     else if(arg2[1] == 'n')
  120.     {
  121.         int parametr = stringToInt(argv[3]);
  122.  
  123.         if(parametr)
  124.             input.commandCode = parts;
  125.         else
  126.             input.commandCode = error;
  127.  
  128.         input.parameter = parametr;
  129.     }
  130.     else if(arg2[1] == 's')
  131.     {
  132.         int parametr = stringToInt(argv[3]);
  133.  
  134.         if(parametr)
  135.             input.commandCode = fragments;
  136.         else
  137.             input.commandCode = error;
  138.  
  139.         input.parameter = parametr;
  140.     }
  141.     else
  142.     {
  143.         input.commandCode = error;
  144.         return input;
  145.     }
  146.  
  147.     // fourth argument
  148.  
  149.     char* arg4 = argv[4];
  150.     if(strlen(arg4) != 2 || arg2[0] != '-')
  151.     {
  152.         input.commandCode = error;
  153.         return input;
  154.     }
  155.     else if(arg4[1] == 'b')
  156.     {
  157.         if(input.outputFileTemplate == '\0')
  158.             input.outputFileTemplate = argv[5];
  159.         else
  160.             input.commandCode = error;
  161.     }
  162.     else if(arg4[1] == 'n')
  163.     {
  164.         int parametr = stringToInt(argv[5]);
  165.  
  166.         if(parametr == true)
  167.         {
  168.             if(input.commandCode == 0)
  169.                 input.commandCode = parts;
  170.         }
  171.         else
  172.             input.commandCode = error;
  173.  
  174.         input.parameter = parametr;
  175.     }
  176.     else if(arg4[1] == 's')
  177.     {
  178.         int parametr = stringToInt(argv[5]);
  179.  
  180.         if(parametr == truel)
  181.         {
  182.             if(input.commandCode == 0)
  183.                 input.commandCode = fragments;
  184.         }
  185.         else
  186.             input.commandCode = error;
  187.  
  188.         input.parameter = parametr;
  189.     }
  190.     else
  191.     {
  192.         input.commandCode = error;
  193.         return input;
  194.     }
  195.  
  196. pastebin
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement