[PATCH] eliminate buffer in screen.c



Hi,

this patch was inspired by the UTF8 patch used by various distributions.
This is just a clean up: I realized that buffer[] in repaint_file is unnecessary. Instead of formatting into this buffer and then using addstr() one can just addstr and printw directly.

Just for the record: I (but then I'm an outsider, so this opinion can be completely worthless, take it with a big grain of salt ;) don't think using arrays of wchar_t (like in ecs.c) is the right approach for mc, at least outside the editor. Rather strings can just be kept UTF8 (or a few other mb encodings such as EUC-JP perhaps, but Slang only supports UTF8, that would be ncurses only then).

What is needed (IMHO) are a few support functions that calculate the width of a (partial) multibyte string, and the byte length of a substring that matches a certain width. It's the width that matters namely, not the number of characters in the corresponding wchar_t string: think of double-width and combining characters.

Bart

2005-08-27  Bart Oldeman  <bartoldeman users sourceforge net>

	* screen.c (add_permission_string): Use const for dest.
	* screen.c (to_buffer): Rename to add_string. Output directly to
	  the screen instead of storing the output into a buffer.
	* screen.c (format_file): Rename to display_file. Eliminate dest
	  buffer.
	* screen.c (repaint_file): Eliminate buffer.

--- screen.c.~1.216.~	2005-08-27 15:51:38.000000000 +1200
+++ screen.c	2005-08-27 18:48:23.000000000 +1200
@@ -137,7 +137,7 @@

 /* This code relies on the default justification!!! */
 static void
-add_permission_string (char *dest, int width, file_entry *fe, int attr, int color, int is_octal)
+add_permission_string (const char *dest, int width, file_entry *fe, int attr, int color, int is_octal)
 {
     int i, r, l;

@@ -450,15 +450,12 @@
 { "dot",   1,  0, J_RIGHT,	" ",		0, string_dot,		   NULL },
 };

-static char *
-to_buffer (char *dest, int just_mode, int len, const char *txt)
+static void
+add_string (int just_mode, int len, const char *txt)
 {
     int txtlen = strlen (txt);
     int still, over;

-    /* Fill buffer with spaces */
-    memset (dest, ' ', len);
-
     still = (over=(txtlen > len)) ? (txtlen - len) : (len - txtlen);

     switch (HIDE_FIT(just_mode)){
@@ -475,15 +472,11 @@

     if (over){
 	if (IS_FIT(just_mode))
-	    strcpy (dest, name_trunc(txt, len));
+	    addstr (name_trunc(txt, len));
 	else
-	    strncpy (dest, txt+still, len);
+	    printw ("%*s", len, txt+still);
     } else
-	strncpy (dest+still, txt, txtlen);
-
-    dest[len] = '\0';
-
-    return (dest + len);
+	printw ("%*s%-*s", still, "", len-still, txt);
 }

 static int
@@ -537,14 +530,12 @@
     return (NORMAL_COLOR);
 }

-/* Formats the file number file_index of panel in the buffer dest */
+/* Displays the file number file_index of panel */
 static void
-format_file (char *dest, int limit, WPanel *panel, int file_index, int width, int attr, int isstatus)
+display_file (WPanel *panel, int file_index, int width, int attr, int isstatus)
 {
     int      color, length, empty_line;
     const char *txt;
-    char     *old_pos;
-    char     *cdest = dest;
     format_e *format, *home;
     file_entry *fe;

@@ -571,26 +562,21 @@
 	    else
 		txt = (*format->string_fn)(fe, format->field_len);

-	    old_pos = cdest;
-
 	    len = format->field_len;
 	    if (len + length > width)
 		len = width - length;
-	    if (len + (cdest - dest) > limit)
-		len = limit - (cdest - dest);
 	    if (len <= 0)
 		break;
-	    cdest = to_buffer (cdest, format->just_mode, len, txt);
 	    length += len;

             attrset (color);

             if (permission_mode && !strcmp(format->id, "perm"))
-                add_permission_string (old_pos, format->field_len, fe, attr, color, 0);
+                add_permission_string (txt, format->field_len, fe, attr, color, 0);
             else if (permission_mode && !strcmp(format->id, "mode"))
-                add_permission_string (old_pos, format->field_len, fe, attr, color, 1);
+                add_permission_string (txt, format->field_len, fe, attr, color, 1);
             else
-		addstr (old_pos);
+		add_string (format->just_mode, len, txt);

 	} else {
             if (attr == SELECTED || attr == MARKED_SELECTED)
@@ -614,7 +600,6 @@
 {
     int    second_column = 0;
     int	   width, offset;
-    char   buffer [BUF_MEDIUM];

     offset = 0;
     if (!isstatus && panel->split){
@@ -643,7 +628,7 @@
 	    widget_move (&panel->widget, file_index - panel->top_file + 2, 1);
     }

-    format_file (buffer, sizeof(buffer), panel, file_index, width, attr, isstatus);
+    display_file (panel, file_index, width, attr, isstatus);

     if (!isstatus && panel->split){
 	if (second_column)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]