@@ -66,8 +66,6 @@ int32_t FOSSIL_IO_COLOR_ENABLE = 1; // Flag to enable/disable color output
6666
6767// Function to apply color
6868void fossil_io_apply_color (const char * color ) {
69- if (!FOSSIL_IO_COLOR_ENABLE || !color ) return ;
70-
7169 if (strcmp (color , "red" ) == 0 ) {
7270 printf (FOSSIL_IO_COLOR_RED );
7371 } else if (strcmp (color , "green" ) == 0 ) {
@@ -98,15 +96,13 @@ void fossil_io_apply_color(const char *color) {
9896 printf (FOSSIL_IO_COLOR_BRIGHT_CYAN );
9997 } else if (strcmp (color , "bright_white" ) == 0 ) {
10098 printf (FOSSIL_IO_COLOR_BRIGHT_WHITE );
101- } else {
99+ } else if ( strcmp ( color , "reset" ) == 0 ) {
102100 printf (FOSSIL_IO_COLOR_RESET ); // Reset to default if color not recognized
103101 }
104102}
105103
106104// Function to apply text attributes (e.g., bold, underline)
107105void fossil_io_apply_attribute (const char * attribute ) {
108- if (!attribute ) return ;
109-
110106 if (strcmp (attribute , "bold" ) == 0 ) {
111107 printf (FOSSIL_IO_ATTR_BOLD );
112108 } else if (strcmp (attribute , "underline" ) == 0 ) {
@@ -123,35 +119,35 @@ void fossil_io_apply_attribute(const char *attribute) {
123119 printf (FOSSIL_IO_ATTR_ITALIC );
124120 } else if (strcmp (attribute , "strikethrough" ) == 0 ) {
125121 printf (FOSSIL_IO_ATTR_STRIKETHROUGH );
122+ } else if (strcmp (attribute , "reset" ) == 0 ) {
123+ printf (FOSSIL_IO_ATTR_NORMAL ); // Reset to normal if attribute not recognized
126124 }
127125}
128126
129127// Function to handle named positions (like top, bottom, left, right)
130128void fossil_io_apply_position (const char * pos ) {
131- if (!pos ) return ;
132-
133129 if (strcmp (pos , "top" ) == 0 ) {
134130 printf ("\033[1;1H" ); // Move to top
135131 } else if (strcmp (pos , "bottom" ) == 0 ) {
136- printf ("\033[999 ;1H" ); // Move cursor to bottom-left (reduced from 1000)
132+ printf ("\033[1000 ;1H" ); // Move cursor to bottom-left
137133 } else if (strcmp (pos , "left" ) == 0 ) {
138134 printf ("\033[1;1H" ); // Move to top-left (as a general left start)
139135 } else if (strcmp (pos , "right" ) == 0 ) {
140- printf ("\033[1;999H " ); // Move to top-right (reduced from 1000)
136+ printf ("\033[1;1000H " ); // Move to top-right
141137 } else if (strcmp (pos , "center" ) == 0 ) {
142138 printf ("\033[25;40H" ); // Approximate center for 80x50 terminal
143139 } else if (strcmp (pos , "top-left" ) == 0 ) {
144140 printf ("\033[1;1H" );
145141 } else if (strcmp (pos , "top-right" ) == 0 ) {
146- printf ("\033[1;999H " );
142+ printf ("\033[1;1000H " );
147143 } else if (strcmp (pos , "bottom-left" ) == 0 ) {
148- printf ("\033[999 ;1H" );
144+ printf ("\033[1000 ;1H" );
149145 } else if (strcmp (pos , "bottom-right" ) == 0 ) {
150- printf ("\033[999;999H " );
146+ printf ("\033[1000;1000H " );
151147 } else if (strcmp (pos , "middle-left" ) == 0 ) {
152148 printf ("\033[25;1H" ); // Mid vertical, far left
153149 } else if (strcmp (pos , "middle-right" ) == 0 ) {
154- printf ("\033[25;999H " ); // Mid vertical, far right
150+ printf ("\033[25;1000H " ); // Mid vertical, far right
155151 } else {
156152 fprintf (stderr , "Unknown position: %s\n" , pos );
157153 }
@@ -170,46 +166,41 @@ void fossil_io_print_with_attributes(const char *str) {
170166
171167 while ((start = strchr (current_pos , '{' )) != NULL ) {
172168 // Output text before '{'
173- size_t len = start - current_pos ;
174- if (len > 0 ) {
175- fwrite (current_pos , 1 , len , stdout );
176- }
169+ fwrite (current_pos , 1 , start - current_pos , stdout );
177170
178171 // Find the matching '}'
179172 end = strchr (start , '}' );
180173 if (end ) {
181174 // Extract attributes inside '{}'
182175 size_t length = end - start - 1 ;
183- if (length > 0 && length < 256 ) { // Reasonable size limit
184- char attributes [257 ];
185- strncpy (attributes , start + 1 , length );
186- attributes [length ] = '\0' ;
187-
188- // Split by comma to separate color, attribute, or position
189- char * color = NULL ;
190- char * attribute = NULL ;
191- char * pos = NULL ;
192- char * comma_pos = strchr (attributes , ',' );
193- if (comma_pos ) {
194- * comma_pos = '\0' ; // Null-terminate the first part
195- color = attributes ; // Color or position part
196- attribute = comma_pos + 1 ; // Attribute part
197- } else {
198- color = attributes ; // Only one part (could be color, attribute, or position)
199- }
176+ char attributes [length + 1 ];
177+ strncpy (attributes , start + 1 , length );
178+ attributes [length ] = '\0' ;
179+
180+ // Split by comma to separate color, attribute, or position
181+ char * color = NULL ;
182+ char * attribute = NULL ;
183+ char * pos = NULL ;
184+ char * comma_pos = strchr (attributes , ',' );
185+ if (comma_pos ) {
186+ * comma_pos = '\0' ; // Null-terminate the first part
187+ color = attributes ; // Color or position part
188+ attribute = comma_pos + 1 ; // Attribute part
189+ } else {
190+ color = attributes ; // Only one part (could be color, attribute, or position)
191+ }
200192
201- // Handle positions (like {pos:name})
202- if (color && strncmp (color , "pos:" , 4 ) == 0 ) {
203- pos = color + 4 ; // Skip the "pos:" prefix
204- fossil_io_apply_position (pos );
205- } else {
206- // Apply color and/or attribute based on flags
207- if (FOSSIL_IO_COLOR_ENABLE && color ) {
208- fossil_io_apply_color (color );
209- }
210- if (attribute ) {
211- fossil_io_apply_attribute (attribute );
212- }
193+ // Handle positions (like {pos:name})
194+ if (strstr (color , "pos:" ) == color ) {
195+ pos = color + 4 ; // Skip the "pos:" prefix
196+ fossil_io_apply_position (pos );
197+ } else {
198+ // Apply color and/or attribute based on flags
199+ if (FOSSIL_IO_COLOR_ENABLE && color ) {
200+ fossil_io_apply_color (color );
201+ }
202+ if (attribute ) {
203+ fossil_io_apply_attribute (attribute );
213204 }
214205 }
215206
@@ -223,17 +214,15 @@ void fossil_io_print_with_attributes(const char *str) {
223214 }
224215
225216 // Output remaining text after last '}'
226- if (current_pos && * current_pos ) {
227- fputs (current_pos , stdout );
228- }
217+ fputs (current_pos , stdout );
229218 fflush (stdout );
230219}
231220
232221// Function to print a sanitized formatted string to a specific file stream with attributes
233222void fossil_io_fprint_with_attributes (fossil_fstream_t * stream , const char * str ) {
234- if (str != NULL && stream != NULL && stream -> file != NULL ) {
223+ if (str != NULL && stream != NULL ) {
235224 char sanitized_str [FOSSIL_IO_BUFFER_SIZE ];
236- strncpy (sanitized_str , str , sizeof (sanitized_str ) - 1 );
225+ strncpy (sanitized_str , str , sizeof (sanitized_str ));
237226 sanitized_str [sizeof (sanitized_str ) - 1 ] = '\0' ; // Ensure null termination
238227
239228 // Remove attribute/color escape codes for file output
@@ -242,10 +231,7 @@ void fossil_io_fprint_with_attributes(fossil_fstream_t *stream, const char *str)
242231 const char * end = NULL ;
243232 while ((start = strchr (current_pos , '{' )) != NULL ) {
244233 // Write text before '{' to the file
245- size_t len = start - current_pos ;
246- if (len > 0 ) {
247- fwrite (current_pos , 1 , len , stream -> file );
248- }
234+ fwrite (current_pos , 1 , start - current_pos , stream -> file );
249235 end = strchr (start , '}' );
250236 if (end ) {
251237 // Skip the attribute section
@@ -257,9 +243,9 @@ void fossil_io_fprint_with_attributes(fossil_fstream_t *stream, const char *str)
257243 }
258244 }
259245 // Write remaining text after last '}'
260- if (current_pos && * current_pos ) {
261- fputs ( current_pos , stream -> file );
262- }
246+ fputs (current_pos , stream -> file );
247+ } else {
248+ //fputs("cnullptr\n", stderr);
263249 }
264250}
265251
@@ -271,7 +257,7 @@ void fossil_io_fprint_with_attributes(fossil_fstream_t *stream, const char *str)
271257void fossil_io_puts (const char * str ) {
272258 if (str != NULL ) {
273259 char sanitized_str [FOSSIL_IO_BUFFER_SIZE ];
274- strncpy (sanitized_str , str , sizeof (sanitized_str ) - 1 );
260+ strncpy (sanitized_str , str , sizeof (sanitized_str ));
275261 sanitized_str [sizeof (sanitized_str ) - 1 ] = '\0' ; // Ensure null termination
276262
277263 // Print the sanitized string with attributes
@@ -288,28 +274,24 @@ void fossil_io_putchar(char c) {
288274
289275// Function to print sanitized formatted output with attributes
290276void fossil_io_printf (const char * format , ...) {
291- if (!format ) return ;
292-
293277 va_list args ;
294278 va_start (args , format );
295279
296280 // Create a buffer to hold the formatted string
297281 char buffer [FOSSIL_IO_BUFFER_SIZE ];
298- int result = vsnprintf (buffer , sizeof (buffer ), format , args );
299-
300- if (result > 0 && result < (int )sizeof (buffer )) {
301- // Print the sanitized output with attributes
302- fossil_io_print_with_attributes (buffer );
303- }
282+ vsnprintf (buffer , sizeof (buffer ), format , args );
283+
284+ // Print the sanitized output with attributes
285+ fossil_io_print_with_attributes (buffer );
304286
305287 va_end (args );
306288}
307289
308290// Function to print a sanitized string to a specific file stream
309291void fossil_io_fputs (fossil_fstream_t * stream , const char * str ) {
310- if (str != NULL && stream != NULL && stream -> file != NULL ) {
292+ if (str != NULL && stream != NULL ) {
311293 char sanitized_str [FOSSIL_IO_BUFFER_SIZE ];
312- strncpy (sanitized_str , str , sizeof (sanitized_str ) - 1 );
294+ strncpy (sanitized_str , str , sizeof (sanitized_str ));
313295 sanitized_str [sizeof (sanitized_str ) - 1 ] = '\0' ; // Ensure null termination
314296
315297 // Apply color/attributes and sanitize the string before printing
@@ -321,19 +303,15 @@ void fossil_io_fputs(fossil_fstream_t *stream, const char *str) {
321303
322304// Function to print a sanitized formatted string to a specific file stream
323305void fossil_io_fprintf (fossil_fstream_t * stream , const char * format , ...) {
324- if (!format || !stream || !stream -> file ) return ;
325-
326306 va_list args ;
327307 va_start (args , format );
328308
329309 // Create a buffer to hold the formatted string
330310 char buffer [FOSSIL_IO_BUFFER_SIZE ];
331- int result = vsnprintf (buffer , sizeof (buffer ), format , args );
311+ vsnprintf (buffer , sizeof (buffer ), format , args );
332312
333- if (result > 0 && result < (int )sizeof (buffer )) {
334- // Print the sanitized formatted string with attributes to the specified stream
335- fossil_io_fprint_with_attributes (stream , buffer );
336- }
313+ // Print the sanitized formatted string with attributes to the specified stream
314+ fossil_io_fprint_with_attributes (stream , buffer );
337315
338316 va_end (args );
339317}
0 commit comments