Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #define SPLIT_TO_GIVEN_SIZE 0x01 // split into SPLIT_TO_GIVEN_SIZE with given size -s
- #define SPLIT_TO_GIVEN_NUMBER 0x02 // split into given number of parts -n
- #define ERROR 0xFF // error
- typedef struct
- {
- char* inputFileName;
- char* outputFileTemplate;
- int commandCode;
- int parameter;
- } inputStruct;
- inputStruct parseInput(int argc, char *argv[]);
- int stringToInt(char* str);
- int getCountsOfDigits(int number);
- long int sizeOfFile(FILE* fp);
- char* itos(int num);
- void createFileName(char* base, int fileNumber, char **pname);
- int spliToGivenSize(inputStruct input, FILE* fp);
- int main (int argc, char *argv[])
- {
- argc = 6;
- argv[1] = "hey.txt";
- argv[2] = "-b";
- argv[3] = "bubuka";
- argv[4] = "-s";
- argv[5] = "5";
- inputStruct input = parseInput(argc, argv);
- FILE* fp;
- if(input.commandCode == ERROR)
- {
- printf("Input error\n");
- return -99;
- }
- else if(input.commandCode == SPLIT_TO_GIVEN_SIZE)
- {
- printf("given size of part: %i\n", input.parameter);
- fp = fopen(input.inputFileName, "rb");
- if(fp == NULL)
- {
- printf("No such file: %s\n", input.inputFileName);
- return -97;
- }
- spliToGivenSize(input, fp);
- }
- else if(input.commandCode == SPLIT_TO_GIVEN_NUMBER)
- {
- printf("given number of parts: %i\n", input.parameter);
- fp = fopen(input.inputFileName, "rb");
- if(fp == NULL)
- {
- printf("No such file: %s\n", input.inputFileName);
- return -97;
- }
- // long int fileSize = sizeOfFile(fp);
- // unsigned int numOfParts = input.parameter;
- // int indexOfLastFile = 0;
- // input.commandCode = SPLIT_TO_GIVEN_SIZE;
- // if(fileSize >= numOfParts)
- // input.parameter = fileSize / numOfParts;
- // else
- // input.parameter = 1;
- // indexOfLastFile = spliToGivenSize(input, fp);
- // // Let's create empty files if number of parts more than fileSize
- // if(fileSize < numOfParts)
- // {
- // unsigned int i;
- // char* name;
- // FILE* nano;
- // for(i = indexOfLastFile + 1; i <= numOfParts; i++)
- // {
- // createFileName(input.outputFileTemplate, i, name);
- // nano = fopen(name, "wb");
- // putc('\n', nano);
- // fclose(nano);
- // }
- // free(name);
- // printf("%i empty files was added\n", numOfParts - indexOfLastFile);
- // }
- // // Let's pull everything that is not divided evenly to the last file
- // else
- // {
- // }
- }
- else
- {
- printf("Unknown parse error\n");
- return -98;
- }
- fclose(fp);
- return 0;
- }
- inputStruct parseInput(int argc, char *argv[])
- {
- inputStruct input;
- input.commandCode = 0;
- input.inputFileName = '\0';
- input.outputFileTemplate = '\0';
- input.parameter = 0;
- if(argc != 6)
- {
- input.commandCode = ERROR;
- return input;
- }
- // first argument
- input.inputFileName = argv[1];
- // second argument
- char* arg2 = argv[2];
- if(strlen(arg2) != 2 || arg2[0] != '-')
- {
- input.commandCode = ERROR;
- return input;
- }
- else if(arg2[1] == 'b')
- {
- input.outputFileTemplate = argv[3];
- }
- else if(arg2[1] == 'n')
- {
- int parametr = stringToInt(argv[3]);
- if((bool)parametr)
- input.commandCode = SPLIT_TO_GIVEN_NUMBER;
- else
- input.commandCode = ERROR;
- input.parameter = parametr;
- }
- else if(arg2[1] == 's')
- {
- int parametr = stringToInt(argv[3]);
- if((bool)parametr)
- input.commandCode = SPLIT_TO_GIVEN_SIZE;
- else
- input.commandCode = ERROR;
- input.parameter = parametr;
- }
- else
- {
- input.commandCode = ERROR;
- return input;
- }
- // fourth argument
- char* arg4 = argv[4];
- if(strlen(arg4) != 2 || arg2[0] != '-')
- {
- input.commandCode = ERROR;
- return input;
- }
- else if(arg4[1] == 'b')
- {
- if(input.outputFileTemplate == '\0')
- input.outputFileTemplate = argv[5];
- else
- input.commandCode = ERROR;
- }
- else if(arg4[1] == 'n')
- {
- int parametr = stringToInt(argv[5]);
- if((bool)parametr == true)
- {
- if(input.commandCode == 0)
- input.commandCode = SPLIT_TO_GIVEN_NUMBER;
- }
- else
- input.commandCode = ERROR;
- input.parameter = parametr;
- }
- else if(arg4[1] == 's')
- {
- int parametr = stringToInt(argv[5]);
- if((bool)parametr == true)
- {
- if(input.commandCode == 0)
- input.commandCode = SPLIT_TO_GIVEN_SIZE;
- }
- else
- input.commandCode = ERROR;
- input.parameter = parametr;
- }
- else
- {
- input.commandCode = ERROR;
- return input;
- }
- return input;
- }
- int stringToInt(char* str)
- {
- bool valid = true;
- unsigned int i;
- for(i = 0; i < strlen(str); i++)
- valid &= (bool)isdigit(str[i]);
- if(valid)
- return atoi(str);
- return 0;
- }
- int getCountsOfDigits(int number)
- {
- //int count = (number == 0) ? 1 : 0;
- int count;
- if (number==0)
- count = 1;
- else
- count = 0;
- while (number != 0) {
- count++;
- number /= 10;
- }
- return count;
- }
- long int sizeOfFile(FILE* fp)
- {
- long int size;
- fseek(fp, 0, SEEK_END);
- size = ftell(fp);
- rewind(fp);
- return size;
- }
- char* itos(int num)
- {
- int digits = getCountsOfDigits(num);
- char* result = malloc((digits + 1) * sizeof(char));
- if(result == NULL)
- return result;
- sprintf(result, "%d", num);
- return result;
- }
- void createFileName(char* base, int fileNumber, char **pname)
- {
- *pname = (char*)malloc((strlen(base) + strlen("_") + strlen(itos(fileNumber)) + strlen(".txt") + 1) * sizeof(char));
- strcpy(*pname, base);
- strcat(*pname,"_");
- strcat(*pname, itos(fileNumber));
- strcat(*pname,".txt");
- }
- int spliToGivenSize(inputStruct input, FILE* fp)
- {
- int outFileIndex = 1, n = 0;
- char box;
- char* name;
- createFileName(input.outputFileTemplate, outFileIndex, &name);
- FILE* nano = fopen(name, "wb"); // a bit strange name for output file 0_0
- box = getc(fp);
- while(box != EOF)
- {
- putc(box, nano);
- n++;
- if(n == input.parameter)
- {
- // end work with priviose file
- if(box != '\n')
- putc('\n', nano);
- fclose(nano);
- free(name);
- // update variables
- outFileIndex++;
- n = 0;
- createFileName(input.outputFileTemplate, outFileIndex, &name);
- }
- box = getc(fp);
- if(box != EOF && n == 0)
- // begin work with new file
- nano = fopen(name, "wb");
- }
- if(n != 0)
- fclose(nano);
- printf("%i files was created!\n", outFileIndex);
- free(name);
- return outFileIndex;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement