22#include <stdlib.h>
33#include <stdio.h>
44#include <stdint.h>
5+ #include <string.h>
56
67string_table_t * create_string_table (int width ) {
78 string_table_t * string_table = (string_table_t * ) malloc (sizeof (string_table_t ));
@@ -18,45 +19,70 @@ string_table_t *create_string_table(int width) {
1819
1920string_table_t * get_string_table (options_t * options ) {
2021 string_table_t * table = NULL ;
21- string_t * buffer = create_string_empty (12 );
22- string_array_t * line = create_string_array (3 );
22+ string_t * field_buffer = create_string_empty (12 );
23+ string_array_t * line_buffer = create_string_array (3 );
24+
25+ string_t * char_buffer = NULL ;
2326
2427 int input ;
2528 int width = 0 ;
2629 while ((input = getc (options -> stream )) != EOF ) {
2730 if (input == options -> delimiter ) {
28- add_string_to_array (line , buffer );
29- buffer = create_string_empty (12 );
31+ add_string_to_array (line_buffer , field_buffer );
32+ field_buffer = create_string_empty (12 );
3033 width ++ ;
3134 continue ;
3235 }
3336 if (input == '\n' || input == '\r' ) {
34- if ( buffer -> length == 0 ) continue ;
35- add_string_to_array (line , buffer );
36- buffer = create_string_empty (12 );
37+ if ( field_buffer -> length == 0 ) continue ;
38+ add_string_to_array (line_buffer , field_buffer );
39+ field_buffer = create_string_empty (12 );
3740 width ++ ;
3841
3942 if (table == NULL ) {
4043 table = create_string_table (width );
4144 }
42- add_row_to_table (table , line );
43- line = create_string_array (3 );
45+ add_row_to_table (table , line_buffer );
46+ line_buffer = create_string_array (3 );
4447 width = 0 ;
4548 } else {
46- append_char_to_string (buffer , (char ) input );
49+
50+ char current_char = (char ) input ;
51+
52+ if (current_char < 0 ) {
53+ unsigned char value = (unsigned char ) current_char ;
54+ if (value & 0x40 ) {
55+ if (char_buffer && char_buffer -> length > 0 ) {
56+ append_char_to_string (field_buffer , char_buffer -> chars , char_buffer -> length );
57+ free_string (char_buffer );
58+ char_buffer = NULL ;
59+ }
60+ }
61+ if (char_buffer == NULL ) {
62+ char_buffer = create_string_empty (2 );
63+ }
64+ append_char_to_string (char_buffer , & current_char , 1 );
65+ continue ;
66+ }
67+ if (char_buffer && char_buffer -> length > 0 ) {
68+ append_char_to_string (field_buffer , char_buffer -> chars , char_buffer -> length );
69+ free_string (char_buffer );
70+ char_buffer = NULL ;
71+ }
72+ append_char_to_string (field_buffer , & current_char , 1 );
4773 }
4874 }
49- free_string (buffer );
50- free_string_array (line );
75+ free_string (field_buffer );
76+ free_string_array (line_buffer );
5177 return table ;
5278}
5379
5480void recalculate_max_column_length (string_table_t * string_table , string_array_t * new_row ) {
5581 for (int i = 0 ; i < string_table -> width ; ++ i ) {
5682 int max_length = string_table -> max_column_length [i ];
5783 if (i < new_row -> length ) {
58- if (new_row -> strings [i ]-> length > max_length ) {
59- max_length = new_row -> strings [i ]-> length ;
84+ if (new_row -> strings [i ]-> char_count > max_length ) {
85+ max_length = new_row -> strings [i ]-> char_count ;
6086 }
6187 } else {
6288 fprintf (stderr , "Error: column %d is not defined\n" , i );
@@ -69,7 +95,7 @@ void add_row_to_table(string_table_t *string_table, string_array_t *new_row) {
6995 if (string_table -> length + 1 > string_table -> capacity ) {
7096 string_table -> capacity *= 2 ;
7197 string_table -> rows = (string_array_t * * ) realloc (string_table -> rows ,
72- sizeof (* string_table -> rows ) * string_table -> capacity );
98+ sizeof (string_array_t * ) * string_table -> capacity );
7399 }
74100 recalculate_max_column_length (string_table , new_row );
75101 string_table -> rows [string_table -> length ] = new_row ;
@@ -154,7 +180,7 @@ void print_table(string_table_t *string_table, options_t *options) {
154180 }
155181 fprintf (options -> out_stream , " " );
156182 string_t * string = row -> strings [j ];
157- int padding = string_table -> max_column_length [j ] - string -> length ;
183+ int padding = string_table -> max_column_length [j ] - string -> char_count ;
158184 rotate_color (& color_index , COLOR_COUNT , options );
159185 print_aligned_string ("%s" , string -> chars , padding , options -> flags & ALIGN_LEFT , options );
160186 set_frame_color (options );
0 commit comments