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 truel true
- #define fragments 0x01 // split into fragments with given size -s
- #define parts 0x02 // split into given number of parts -n
- #define error 0x00 // error
- typedef struct
- {
- char* inputFileName;
- char* outputFileTemplate;
- int commandCode;
- int parameter;
- } inputStruct;
- inputStruct parseInput(int argc, char *argv[]);
- int stringToInt(char* str);
- int main (int argc, char *argv[])
- {
- argc = 6;
- argv[1] = "hey.txt";
- argv[2] = "-s";
- argv[3] = "1";
- argv[4] = "-b";
- argv[5] = "bubuka";
- FILE* fp;
- inputStruct input = parseInput(argc, argv);
- if(input.commandCode == error)
- {
- printf("Input error\n");
- return -99;
- }
- else if(input.commandCode == fragments)
- {
- fp = fopen(input.inputFileName, "rb");
- char box;
- int n=0,m=1;
- char* name = (char*)malloc(strlen(input.outputFileTemplate)+100);
- strcpy(name, input.outputFileTemplate);
- printf("%s", name);
- strcat(name, "_");
- strcat(name, (char*)m);
- strcat(name,".txt");
- FILE* nano = fopen(name,"wb");
- while((box=getc(fp))!=EOF)
- {
- putc(box,nano);
- n++;
- if (n == input.parameter)
- {
- fclose(nano);
- m++;
- n=0;
- //name = input.outputFileTemplate;
- strcpy(name, input.outputFileTemplate);
- strcat(name,"_");
- strcat(name, (char*)m);
- strcat(name,".txt");
- //name = name + '_'+ m + '.txt';
- nano = fopen(name,"wb");
- }
- }
- if (n!=0)
- {
- fclose(nano);
- }
- }
- else if(input.commandCode == parts)
- {
- }
- 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(parametr)
- input.commandCode = parts;
- else
- input.commandCode = error;
- input.parameter = parametr;
- }
- else if(arg2[1] == 's')
- {
- int parametr = stringToInt(argv[3]);
- if(parametr)
- input.commandCode = fragments;
- 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(parametr == true)
- {
- if(input.commandCode == 0)
- input.commandCode = parts;
- }
- else
- input.commandCode = error;
- input.parameter = parametr;
- }
- else if(arg4[1] == 's')
- {
- int parametr = stringToInt(argv[5]);
- if(parametr == truel)
- {
- if(input.commandCode == 0)
- input.commandCode = fragments;
- }
- else
- input.commandCode = error;
- input.parameter = parametr;
- }
- else
- {
- input.commandCode = error;
- return input;
- }
- pastebin
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement