Advertisement
matbiz01

Untitled

Mar 15th, 2021
938
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. int get_line_count(int block_index);
  6. void merge(char *file_path1, char *file_path2);
  7. void print_blocks(char ***blocks);
  8. void init_blocks(int size);
  9. void fill_blocks_order(char *files);
  10. void delete_line(char ***blocks, int block_index, int line_index);
  11. void delete_block(char ***blocks, int block_index);
  12. void insert_block();
  13. void print_all();
  14.  
  15. char ***blocks;
  16. int blocks_amount = 10;
  17. int max_lines = 100;
  18. int buffer_length = 100;
  19. int filled_up_to = 0;
  20.  
  21. int main(int argc, char const *argv[]){
  22.  
  23.     char buffer[buffer_length];
  24.     init_blocks(10);
  25.     fill_blocks_order("ex1.txt ex3.txt ex2.txt ex4.txt"); //ex1.txt ex2.txt ex3.txt ex4.txt
  26.     printf("%s", blocks[0][5]);
  27.     printf("%i", get_line_count(0));
  28.     delete_line(blocks, 0, 5);
  29.     printf("%s", blocks[0][5]);
  30.     printf("%i", get_line_count(0));
  31.     return 0;
  32. }
  33.  
  34. void init_blocks(int size){
  35.     blocks = (char ***)calloc(size, sizeof(char **));
  36.     for(int i = 0; i < size; i++){
  37.         blocks[i] = NULL;
  38.     }
  39. }
  40.  
  41. void fill_blocks_order(char *files){
  42.     int files_amount = 100;
  43.     int file_name_max_len = 100;
  44.  
  45.     char **file_names = (char **)calloc(files_amount, sizeof(char *));
  46.     for(int i = 0; i < files_amount; i++){
  47.         file_names[i] = (char *)calloc(file_name_max_len, sizeof(char));
  48.     }
  49.  
  50.     char *temptext = (char *)calloc(100, sizeof(char));
  51.     strcpy(temptext, files);
  52.     char *single;
  53.    
  54.     single = strtok(temptext, " ");
  55.     int index = 0;
  56.     while(single != NULL){
  57.         strcpy(file_names[index], single);
  58.         single = strtok(NULL, " ");
  59.         index++;
  60.     }
  61.    
  62.     for(int i = 0; i < index; i+=2){
  63.         merge(file_names[i], file_names[i+1]);
  64.     }
  65. }
  66.  
  67. int get_line_count(int block_index){
  68.     int full_lines = 0;
  69.     for(int i = 0; i < max_lines; i++){
  70.         if(strstr(blocks[block_index][i], "\n") != NULL){
  71.             full_lines++;
  72.         }
  73.     }
  74.     return full_lines;
  75. }
  76.  
  77. void delete_line(char ***blocks, int block_index, int line_index){
  78.     free(blocks[block_index][line_index]);
  79. }
  80.  
  81. void delete_block(char ***blocks, int block_index){
  82.     for(int i = 0; i < max_lines; i++){
  83.         free(blocks[block_index][i]);        
  84.     }
  85.     free(blocks[block_index]);
  86. }
  87.  
  88. void merge(char *file_path1, char *file_path2){
  89.     FILE *file1 = fopen(file_path1, "r");
  90.     FILE *file2 = fopen(file_path2, "r");
  91.  
  92.     int max_lines = 100;
  93.     int buffer_length = 100;
  94.     char buffer[buffer_length];
  95.    
  96.     char **block = (char **)calloc(max_lines, sizeof(char *));
  97.     for(int i = 0; i < max_lines; i++){
  98.         block[i] = (char *)calloc(buffer_length, sizeof(char *));
  99.     }
  100.    
  101.     int index = 0;
  102.     int full_files = 2;
  103.     while(full_files != 0){
  104.         full_files = 0;
  105.         if(fgets(buffer, buffer_length, file1)){
  106.             strcpy(block[index], buffer);
  107.             index++;
  108.             full_files++;
  109.         }
  110.  
  111.         if(fgets(buffer, buffer_length, file2)){
  112.             strcpy(block[index], buffer);
  113.             index++;
  114.             full_files++;
  115.         }
  116.     }
  117.    
  118.     blocks[filled_up_to] = block;
  119.     filled_up_to++;
  120. }
  121.  
  122. void print_all(){
  123.     for(int i = 0; i < filled_up_to; i++){
  124.         for(int j = 0; j < max_lines; j++){
  125.             printf("%s", blocks[i][j]);
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement