Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int get_line_count(int block_index);
- void merge(char *file_path1, char *file_path2);
- void print_blocks(char ***blocks);
- void init_blocks(int size);
- void fill_blocks_order(char *files);
- void delete_line(char ***blocks, int block_index, int line_index);
- void delete_block(char ***blocks, int block_index);
- void insert_block();
- void print_all();
- char ***blocks;
- int blocks_amount = 10;
- int max_lines = 100;
- int buffer_length = 100;
- int filled_up_to = 0;
- int main(int argc, char const *argv[]){
- char buffer[buffer_length];
- init_blocks(10);
- fill_blocks_order("ex1.txt ex3.txt ex2.txt ex4.txt"); //ex1.txt ex2.txt ex3.txt ex4.txt
- printf("%s", blocks[0][5]);
- printf("%i", get_line_count(0));
- delete_line(blocks, 0, 5);
- printf("%s", blocks[0][5]);
- printf("%i", get_line_count(0));
- return 0;
- }
- void init_blocks(int size){
- blocks = (char ***)calloc(size, sizeof(char **));
- for(int i = 0; i < size; i++){
- blocks[i] = NULL;
- }
- }
- void fill_blocks_order(char *files){
- int files_amount = 100;
- int file_name_max_len = 100;
- char **file_names = (char **)calloc(files_amount, sizeof(char *));
- for(int i = 0; i < files_amount; i++){
- file_names[i] = (char *)calloc(file_name_max_len, sizeof(char));
- }
- char *temptext = (char *)calloc(100, sizeof(char));
- strcpy(temptext, files);
- char *single;
- single = strtok(temptext, " ");
- int index = 0;
- while(single != NULL){
- strcpy(file_names[index], single);
- single = strtok(NULL, " ");
- index++;
- }
- for(int i = 0; i < index; i+=2){
- merge(file_names[i], file_names[i+1]);
- }
- }
- int get_line_count(int block_index){
- int full_lines = 0;
- for(int i = 0; i < max_lines; i++){
- if(strstr(blocks[block_index][i], "\n") != NULL){
- full_lines++;
- }
- }
- return full_lines;
- }
- void delete_line(char ***blocks, int block_index, int line_index){
- free(blocks[block_index][line_index]);
- }
- void delete_block(char ***blocks, int block_index){
- for(int i = 0; i < max_lines; i++){
- free(blocks[block_index][i]);
- }
- free(blocks[block_index]);
- }
- void merge(char *file_path1, char *file_path2){
- FILE *file1 = fopen(file_path1, "r");
- FILE *file2 = fopen(file_path2, "r");
- int max_lines = 100;
- int buffer_length = 100;
- char buffer[buffer_length];
- char **block = (char **)calloc(max_lines, sizeof(char *));
- for(int i = 0; i < max_lines; i++){
- block[i] = (char *)calloc(buffer_length, sizeof(char *));
- }
- int index = 0;
- int full_files = 2;
- while(full_files != 0){
- full_files = 0;
- if(fgets(buffer, buffer_length, file1)){
- strcpy(block[index], buffer);
- index++;
- full_files++;
- }
- if(fgets(buffer, buffer_length, file2)){
- strcpy(block[index], buffer);
- index++;
- full_files++;
- }
- }
- blocks[filled_up_to] = block;
- filled_up_to++;
- }
- void print_all(){
- for(int i = 0; i < filled_up_to; i++){
- for(int j = 0; j < max_lines; j++){
- printf("%s", blocks[i][j]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement