Просмотр исходного кода

replaced GString stuff by static buffers

Enrico Weigelt, metux IT service 16 лет назад
Родитель
Сommit
f235b1976e
4 измененных файлов с 36 добавлено и 39 удалено
  1. 1 0
      ChangeLog
  2. 17 24
      edit/edit.c
  3. 9 8
      edit/editcmd.c
  4. 9 7
      edit/syntax.c

+ 1 - 0
ChangeLog

@@ -1,6 +1,7 @@
 2009-01-31 Enrico Weigelt, metux ITS <weigelt@metux.de>
 
 	* replaced buggy concat_dir_and_file() by mhl_str_dir_plus_file() (in mhl/string.h)
+	* replaced GString stuff by static buffers
 
 2009-01-30 Enrico Weigelt, metux ITS <weigelt@metux.de>
 

+ 17 - 24
edit/edit.c

@@ -151,10 +151,9 @@ edit_load_file_fast (WEdit *edit, const char *filename)
     buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
 
     if ((file = mc_open (filename, O_RDONLY | O_BINARY)) == -1) {
-	GString *errmsg = g_string_new(NULL);
-	g_string_sprintf(errmsg, _(" Cannot open %s for reading "), filename);
-	edit_error_dialog (_("Error"), get_sys_error (errmsg->str));
-	g_string_free (errmsg, TRUE);
+	char errmsg[8192];
+	snprintf(errmsg, sizeof(errmsg), _(" Cannot open %s for reading "), filename);
+	edit_error_dialog (_("Error"), get_sys_error (errmsg));
 	return 1;
     }
 
@@ -273,18 +272,16 @@ edit_insert_file (WEdit *edit, const char *filename)
 	    edit_insert_stream (edit, f);
 	    edit_cursor_move (edit, current - edit->curs1);
 	    if (pclose (f) > 0) {
-	        GString *errmsg = g_string_new (NULL);
-		g_string_sprintf (errmsg, _(" Error reading from pipe: %s "), p);
-		edit_error_dialog (_("Error"), errmsg->str);
-		g_string_free (errmsg, TRUE);
+		char errmsg[8192];
+		snprintf(errmsg, sizeof(errmsg), _(" Error reading from pipe: %s "), p);
+		edit_error_dialog (_("Error"), errmsg);
 		g_free (p);
 		return 0;
 	    }
 	} else {
-	    GString *errmsg = g_string_new (NULL);
-	    g_string_sprintf (errmsg, _(" Cannot open pipe for reading: %s "), p);
-	    edit_error_dialog (_("Error"), errmsg->str);
-	    g_string_free (errmsg, TRUE);
+	    char errmsg[8192];
+	    snprintf(errmsg, sizeof(errmsg), _(" Cannot open pipe for reading: %s "), p);
+	    edit_error_dialog (_("Error"), errmsg);
 	    g_free (p);
 	    return 0;
 	}
@@ -314,7 +311,8 @@ static int
 check_file_access (WEdit *edit, const char *filename, struct stat *st)
 {
     int file;
-    GString *errmsg = (GString *) 0;
+    char errmsg[8192];
+    errmsg[0] = 0;
 
     /* Try opening an existing file */
     file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
@@ -329,8 +327,7 @@ check_file_access (WEdit *edit, const char *filename, struct stat *st)
 		     O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL,
 		     0666);
 	if (file < 0) {
-	    g_string_sprintf (errmsg = g_string_new (NULL),
-		_(" Cannot open %s for reading "), filename);
+	    snprintf (errmsg, sizeof(errmsg), _(" Cannot open %s for reading "), filename);
 	    goto cleanup;
 	} else {
 	    /* New file, delete it if it's not modified or saved */
@@ -340,15 +337,13 @@ check_file_access (WEdit *edit, const char *filename, struct stat *st)
 
     /* Check what we have opened */
     if (mc_fstat (file, st) < 0) {
-	g_string_sprintf (errmsg = g_string_new (NULL),
-	    _(" Cannot get size/permissions for %s "), filename);
+	snprintf (errmsg, sizeof(errmsg), _(" Cannot get size/permissions for %s "), filename);
 	goto cleanup;
     }
 
     /* We want to open regular files only */
     if (!S_ISREG (st->st_mode)) {
-	g_string_sprintf (errmsg = g_string_new (NULL),
-	    _(" %s is not a regular file "), filename);
+	snprintf (errmsg, sizeof(errmsg), _(" %s is not a regular file "), filename);
 	goto cleanup;
     }
 
@@ -361,16 +356,14 @@ check_file_access (WEdit *edit, const char *filename, struct stat *st)
     }
 
     if (st->st_size >= SIZE_LIMIT) {
-        g_string_sprintf (errmsg = g_string_new (NULL),
-	    _(" File %s is too large "), filename);
+	snprintf (errmsg, sizeof(errmsg), _(" File %s is too large "), filename);
 	goto cleanup;
     }
 
 cleanup:
     (void) mc_close (file);
-    if (errmsg) {
-	edit_error_dialog (_("Error"), errmsg->str);
-	g_string_free (errmsg, TRUE);
+    if (errmsg[0]) {
+	edit_error_dialog (_("Error"), errmsg);
 	return 1;
     }
     return 0;

+ 9 - 8
edit/editcmd.c

@@ -1235,14 +1235,16 @@ edit_replace_prompt (WEdit * edit, char *replace_text, int xpos, int ypos)
 	 0, 0, 0, 0, 0},
 	 NULL_QuickWidget};
 
-    GString *label_text = g_string_new (_(" Replace with: "));
-    if (*replace_text) {
-        size_t label_len;
-	label_len = label_text->len;
-        g_string_append (label_text, replace_text);
-        convert_to_display (label_text->str + label_len);
+    const char* label_nls = _(" Replace with: ");
+    char label_text[8192];
+    if (*replace_text)
+    {
+	size_t label_len = strlen(label_nls);
+	snprintf(label_text, sizeof(label_text), "%s%s", label_nls, replace_text);
+	convert_to_display((&label_text)+label_len);
     }
-    quick_widgets[5].text = label_text->str;
+
+    quick_widgets[5].text = label_text;
 
     {
 	int retval;
@@ -1261,7 +1263,6 @@ edit_replace_prompt (WEdit * edit, char *replace_text, int xpos, int ypos)
 
 	Quick_input.ypos = ypos;
 	retval = quick_dialog (&Quick_input);
-	g_string_free (label_text, TRUE);
 	return retval;
     }
 }

+ 9 - 7
edit/syntax.c

@@ -506,10 +506,14 @@ void edit_get_syntax_color (WEdit * edit, long byte_index, int *color)
  */
 static int read_one_line (char **line, FILE * f)
 {
-    GString *p = g_string_new ("");
-    int c, r = 0;
+    char buffer[8192];
+    int index = 0, c, r = 0;
+    buffer[0] = 0;
 
     for (;;) {
+	if (index >= (sizeof(buffer)-1))
+	    break;
+
 	c = fgetc (f);
 	if (c == EOF) {
 	    if (ferror (f)) {
@@ -531,13 +535,11 @@ static int read_one_line (char **line, FILE * f)
 	if (c == '\n')
 	    break;
 
-	g_string_append_c (p, c);
+	buffer[index] = c;
+	index++;
     }
     if (r != 0) {
-	*line = p->str;
-	g_string_free (p, FALSE);
-    } else {
-	g_string_free (p, TRUE);
+	*line = mhl_str_dup(buffer);
     }
     return r;
 }