Advertisement
Guest User

Untitled

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