Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.38 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 SPLIT_TO_GIVEN_SIZE     0x01    // split into SPLIT_TO_GIVEN_SIZE with given size -s
  8. #define SPLIT_TO_GIVEN_NUMBER   0x02    // split into given number of parts -n
  9. #define ERROR                   0xFF    // error
  10.  
  11. typedef struct
  12. {
  13.     char* inputFileName;
  14.     char* outputFileTemplate;
  15.     int commandCode;
  16.     int parameter;
  17. } inputStruct;
  18.  
  19. inputStruct parseInput(int argc, char *argv[]);
  20. int stringToInt(char* str);
  21. int getCountsOfDigits(int number);
  22. long int sizeOfFile(FILE* fp);
  23. char* itos(int num);
  24. char* createFileName(char* base, int fileNumber);
  25. int spliToGivenSize(inputStruct input, FILE* fp);
  26.  
  27. int main (int argc, char *argv[])
  28. {
  29.     argc = 6;
  30.     argv[1] = "hey.txt";
  31.     argv[2] = "-b";
  32.     argv[3] = "bubuka";
  33.     argv[4] = "-n";
  34.     argv[5] = "5";
  35.  
  36.     inputStruct input = parseInput(argc, argv);
  37.     FILE* fp;
  38.  
  39.     if(input.commandCode == ERROR)
  40.     {
  41.         printf("Input error\n");
  42.         return -99;
  43.     }
  44.     else if(input.commandCode == SPLIT_TO_GIVEN_SIZE)
  45.     {
  46.         printf("given size of part: %i\n", input.parameter);
  47.  
  48.         fp = fopen(input.inputFileName, "rb");
  49.         if(fp == NULL)
  50.         {
  51.             printf("No such file: %s\n", input.inputFileName);
  52.             return -97;
  53.         }
  54.  
  55.         spliToGivenSize(input, fp);
  56.     }
  57.     else if(input.commandCode == SPLIT_TO_GIVEN_NUMBER)
  58.     {
  59.         printf("given number of parts: %i\n", input.parameter);
  60.  
  61.         fp = fopen(input.inputFileName, "rb");
  62.         if(fp == NULL)
  63.         {
  64.             printf("No such file: %s\n", input.inputFileName);
  65.             return -97;
  66.         }
  67.         long int fileSize = sizeOfFile(fp);
  68.         unsigned int numOfParts = input.parameter;
  69.         int indexOfLastFile = 0;
  70.  
  71.         input.commandCode = SPLIT_TO_GIVEN_SIZE;
  72.         if(fileSize >= numOfParts)
  73.             input.parameter = fileSize / numOfParts;
  74.         else
  75.             input.parameter = 1;
  76.  
  77.         indexOfLastFile = spliToGivenSize(input, fp);
  78.  
  79.         // Let's create empty files if number of parts more than fileSize
  80.         if(fileSize < numOfParts)
  81.         {
  82.             int i;
  83.             char* name;
  84.             FILE* nano;
  85.             for(i = indexOfLastFile + 1; i <= numOfParts; i++)
  86.             {
  87.                 name = createFileName(input.outputFileTemplate, i);
  88.                 nano = fopen(name, "wb");
  89.                 putc('\n', nano);
  90.                 fclose(nano);
  91.             }
  92.             free(name);
  93.  
  94.             printf("%i empty files was added\n", numOfParts - indexOfLastFile);
  95.         }
  96.         // Let's pull everything that is not divided evenly to the last file
  97.         else
  98.         {
  99.  
  100.         }
  101.     }
  102.     else
  103.     {
  104.         printf("Unknown parse error\n");
  105.         return -98;
  106.     }
  107.  
  108.     fclose(fp);
  109.  
  110.     return 0;
  111. }
  112.  
  113. inputStruct parseInput(int argc, char *argv[])
  114. {
  115.     inputStruct input;
  116.  
  117.     input.commandCode = 0;
  118.     input.inputFileName = '\0';
  119.     input.outputFileTemplate = '\0';
  120.     input.parameter = 0;
  121.  
  122.     if(argc != 6)
  123.     {
  124.         input.commandCode = ERROR;
  125.         return input;
  126.     }
  127.  
  128.     // first argument
  129.     input.inputFileName = argv[1];
  130.  
  131.     // second argument
  132.     char* arg2 = argv[2];
  133.     if(strlen(arg2) != 2 || arg2[0] != '-')
  134.     {
  135.         input.commandCode = ERROR;
  136.         return input;
  137.     }
  138.     else if(arg2[1] == 'b')
  139.     {
  140.         input.outputFileTemplate = argv[3];
  141.     }
  142.     else if(arg2[1] == 'n')
  143.     {
  144.         int parametr = stringToInt(argv[3]);
  145.  
  146.         if((bool)parametr)
  147.             input.commandCode = SPLIT_TO_GIVEN_NUMBER;
  148.         else
  149.             input.commandCode = ERROR;
  150.  
  151.         input.parameter = parametr;
  152.     }
  153.     else if(arg2[1] == 's')
  154.     {
  155.         int parametr = stringToInt(argv[3]);
  156.  
  157.         if((bool)parametr)
  158.             input.commandCode = SPLIT_TO_GIVEN_SIZE;
  159.         else
  160.             input.commandCode = ERROR;
  161.  
  162.         input.parameter = parametr;
  163.     }
  164.     else
  165.     {
  166.         input.commandCode = ERROR;
  167.         return input;
  168.     }
  169.  
  170.     // fourth argument
  171.  
  172.     char* arg4 = argv[4];
  173.     if(strlen(arg4) != 2 || arg2[0] != '-')
  174.     {
  175.         input.commandCode = ERROR;
  176.         return input;
  177.     }
  178.     else if(arg4[1] == 'b')
  179.     {
  180.         if(input.outputFileTemplate == '\0')
  181.             input.outputFileTemplate = argv[5];
  182.         else
  183.             input.commandCode = ERROR;
  184.     }
  185.     else if(arg4[1] == 'n')
  186.     {
  187.         int parametr = stringToInt(argv[5]);
  188.  
  189.         if((bool)parametr == true)
  190.         {
  191.             if(input.commandCode == 0)
  192.                 input.commandCode = SPLIT_TO_GIVEN_NUMBER;
  193.         }
  194.         else
  195.             input.commandCode = ERROR;
  196.  
  197.         input.parameter = parametr;
  198.     }
  199.     else if(arg4[1] == 's')
  200.     {
  201.         int parametr = stringToInt(argv[5]);
  202.  
  203.         if((bool)parametr == true)
  204.         {
  205.             if(input.commandCode == 0)
  206.                 input.commandCode = SPLIT_TO_GIVEN_SIZE;
  207.         }
  208.         else
  209.             input.commandCode = ERROR;
  210.  
  211.         input.parameter = parametr;
  212.     }
  213.     else
  214.     {
  215.         input.commandCode = ERROR;
  216.         return input;
  217.     }
  218.  
  219.     return input;
  220. }
  221.  
  222. int stringToInt(char* str)
  223. {
  224.     bool valid = true;
  225.  
  226.     unsigned int i;
  227.     for(i = 0; i < strlen(str); i++)
  228.         valid &= (bool)isdigit(str[i]);
  229.  
  230.     if(valid)
  231.         return atoi(str);
  232.  
  233.     return 0;
  234. }
  235.  
  236. int getCountsOfDigits(int number)
  237. {
  238.     //int count = (number == 0) ? 1 : 0;
  239.     int count;
  240.     if (number==0)
  241.         count = 1;
  242.     else
  243.         count = 0;
  244.     while (number != 0) {
  245.         count++;
  246.         number /= 10;
  247.     }
  248.     return count;
  249. }
  250.  
  251. long int sizeOfFile(FILE* fp)
  252. {
  253.     long int size;
  254.     fseek(fp, 0, SEEK_END);
  255.     size = ftell(fp);
  256.     rewind(fp);
  257.  
  258.     return size;
  259. }
  260.  
  261. char* itos(int num)
  262. {
  263.     int digits = getCountsOfDigits(num);
  264.     char* result = malloc((digits + 1) * sizeof(char));
  265.  
  266.     if(result == NULL)
  267.         return result;
  268.  
  269.     sprintf(result, "%d", num);
  270.     return result;
  271. }
  272.  
  273. char* createFileName(char *base, int fileNumber)
  274. {
  275.     char* name = (char*)malloc((strlen(base) + strlen("_") + strlen(itos(fileNumber)) + strlen(".txt") + 1) * sizeof(char));
  276.  
  277.     strcpy(name, base);
  278.     strcat(name,"_");
  279.     strcat(name, itos(fileNumber));
  280.     strcat(name,".txt");
  281.  
  282.     return name;
  283. }
  284.  
  285. int spliToGivenSize(inputStruct input, FILE* fp)
  286. {
  287.     int outFileIndex = 1, n = 0;
  288.     char box;
  289.  
  290.     char* name = createFileName(input.outputFileTemplate, outFileIndex);
  291.     FILE* nano = fopen(name, "wb"); // a bit strange name for output file 0_0
  292.  
  293.     box = getc(fp);
  294.     while(box != EOF)
  295.     {
  296.         putc(box, nano);
  297.         n++;
  298.         if(n == input.parameter)
  299.         {
  300.             // end work with priviose file
  301.             if(box != '\n')
  302.                 putc('\n', nano);
  303.             fclose(nano);
  304.             free(name);
  305.  
  306.             // update variables
  307.             outFileIndex++;
  308.             n = 0;
  309.             name = createFileName(input.outputFileTemplate, outFileIndex);
  310.         }
  311.  
  312.         box = getc(fp);
  313.         if(box != EOF && n == 0)
  314.             // begin work with new file
  315.             nano = fopen(name, "wb");
  316.     }
  317.     if(n != 0)
  318.         fclose(nano);
  319.  
  320.     printf("%i files was created!\n", outFileIndex);
  321.  
  322.     free(name);
  323.  
  324.     return outFileIndex;
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement