Re: [PATCH] eliminate buffer in screen.c



On Sat, 27 Aug 2005, Pavel Tsekov wrote:

The text passed to add_permission_string as the first argument is supposed
to be justfied but your patch will sent is just as is . Did I get it wrong
?

No, you were right.
For octal permissions right alignment is used but the field is not expandable, so advancing txt by strlen(txt) - width is enough to get the right-aligned string (just chopping off the left-most digits really).

For non-octal permissions left alignment is used and that works.
Below is the adjusted patch.

Bart

--- screen.c.~1.216.~	2005-08-27 15:51:38.000000000 +1200
+++ screen.c	2005-08-29 23:01:32.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 *txt, int width, file_entry *fe, int attr, int color, int is_octal)
 {
     int i, r, l;

@@ -145,6 +145,7 @@

     if (is_octal){
 	/* Place of the access bit in octal mode */
+	txt += strlen(txt) - width;
         l = width + l - 3;
 	r = l + 1;
     } else {
@@ -162,7 +163,7 @@
         } else
             attrset (color);

-	addch (dest[i]);
+	addch (txt[i]);
     }
 }

@@ -450,15 +451,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 +473,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 +531,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 +563,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 +601,6 @@
 {
     int    second_column = 0;
     int	   width, offset;
-    char   buffer [BUF_MEDIUM];

     offset = 0;
     if (!isstatus && panel->split){
@@ -643,7 +629,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]