Advertisement
Guest User

Untitled

a guest
Apr 30th, 2022
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.26 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include "diff.h"
  3. #include "ctype.h"
  4. #include "stdio.h"
  5. #include "smart_buffer.h"
  6. #include "string.h"
  7.  
  8. int is_printable(char c) {
  9.     return isprint(c) || c == '\0' || c == '\n';
  10. }
  11.  
  12. bool check_buf_is_printable(char buf[BUFFER_SIZE], size_t n){
  13.     for (int i = 0; i < n; ++i) {
  14.         if(!is_printable(buf[i])) return false;
  15.     }
  16.     return true;
  17. }
  18.  
  19. // return -1 if diff not found in char array
  20. int get_diff_offset(const char *first_file_buffer, const char *second_file_buffer, size_t n) {
  21.     int offset = 0;
  22.     for (int i = 0; i < n; i++) {
  23.         offset++;
  24.         if (first_file_buffer[i] != second_file_buffer[i]) {
  25.             return offset;
  26.         }
  27.     }
  28.     return -1;
  29. }
  30.  
  31.  
  32. void report_text_diff(char first_file_buffer[BUFFER_SIZE], char second_file_buffer[BUFFER_SIZE], size_t n){
  33.     printf("%s\n", first_file_buffer);
  34.     for (int i = 0; i < n; ++i) {
  35.         first_file_buffer[i] != second_file_buffer[i] ? printf("+") : printf(" ");
  36.     }
  37.     printf("\n%s", second_file_buffer);
  38. }
  39.  
  40. void print_buf_by_hex(const char *buf, size_t n) {
  41.     size_t byte_counter = 0;
  42.     for (int i = 0; i < n; ++i) {
  43.         if(i == 15) printf(" | ");
  44.         printf("%02x ", (unsigned char)buf[i]);
  45.         byte_counter++;
  46.     }
  47.     printf("\n");
  48. }
  49.  
  50. void report_binary_diff(const char first_file_buffer[BUFFER_SIZE], const char second_file_buffer[BUFFER_SIZE], size_t n){
  51.     print_buf_by_hex(first_file_buffer, n);
  52.     for (int i = 0; i < n; i++) {
  53.         if(i == 15) printf("   ");
  54.         first_file_buffer[i] != second_file_buffer[i] ? printf("++ ") : printf("   ");
  55.     }
  56.     printf("\n");
  57.     print_buf_by_hex(second_file_buffer, n);
  58. }
  59.  
  60.  
  61. void clear_line_buffers(struct SmartBuf *first_sub_buf, struct SmartBuf *second_sub_buf) {
  62.     first_sub_buf->length = 0;
  63.     memset(&first_sub_buf->buf[0], 0, sizeof(first_sub_buf->buf[0]));
  64.     second_sub_buf->length = 0;
  65.     memset(&second_sub_buf->buf[0], 0, sizeof(second_sub_buf->buf[0]));
  66. }
  67.  
  68. void handle_offset(struct SmartBuf *first_sub_buf, struct SmartBuf *second_sub_buf, size_t lines_count, size_t bytes_count,
  69.               int offset) {
  70.     if(offset <= 0) return;
  71.     printf("\nDiscrepancy at byte %lu, at line %lu\n", bytes_count + offset, lines_count);
  72.     if(check_buf_is_printable(first_sub_buf->buf, first_sub_buf->length) && check_buf_is_printable(second_sub_buf->buf, second_sub_buf->length)){
  73.         report_text_diff(first_sub_buf->buf, second_sub_buf->buf, first_sub_buf->length);
  74.     }else{
  75.         report_binary_diff(first_sub_buf->buf, second_sub_buf->buf, first_sub_buf->length);
  76.     }
  77. }
  78.  
  79. void compare_files(struct T_file first, struct T_file second) {
  80.     char first_file_buffer[BUFFER_SIZE];
  81.     char second_file_buffer[BUFFER_SIZE];
  82.     struct SmartBuf* first_sub_buf = create_smart_buffer();
  83.     struct SmartBuf* second_sub_buf = create_smart_buffer();
  84.  
  85.     size_t lines_count = 1;
  86.     size_t bytes_count = 0;
  87.     int offset = 0;
  88.  
  89.     while (1) {
  90.         size_t n = fread(first_file_buffer, 1, sizeof(first_file_buffer), first.file);
  91.         if (n == 0) break;
  92.         n = fread(second_file_buffer, 1, sizeof(second_file_buffer), second.file);
  93.         if (n == 0) break;
  94.  
  95.         if(get_diff_offset(first_file_buffer, second_file_buffer, n) > 0){
  96.             for (int i = 0; i < n; ++i) {
  97.                 bytes_count += 1;
  98.                 if(first_file_buffer[i] == '\n' || second_file_buffer[i] == '\n'){
  99.                     lines_count++;
  100.                     offset = get_diff_offset(first_sub_buf->buf, second_sub_buf->buf, first_sub_buf->length);
  101.                     handle_offset(first_sub_buf, second_sub_buf, lines_count, bytes_count, offset);
  102.  
  103.                     clear_line_buffers(first_sub_buf, second_sub_buf);
  104.                     continue;
  105.                 }
  106.                 push_char_to_buffer(first_sub_buf, first_file_buffer[i]);
  107.                 push_char_to_buffer(second_sub_buf, second_file_buffer[i]);
  108.             }
  109.             offset = get_diff_offset(first_sub_buf->buf, second_sub_buf->buf, first_sub_buf->length);
  110.             handle_offset(first_sub_buf, second_sub_buf, lines_count, bytes_count, offset);
  111.             break;
  112.         }
  113.     }
  114.  
  115.     free_smart_buffer(first_sub_buf);
  116.     free_smart_buffer(second_sub_buf);
  117.  
  118. }
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement