2005-07-01 [colin] 1.9.12cvs8
[claws.git] / src / common / utils.c
index 213c45b739c102ac9364dcc262836cb0d40a3284..ffe51f67c750f59fcaf7e6f1f3d940ff5de9c578 100644 (file)
@@ -335,20 +335,14 @@ gpointer my_memmem(gconstpointer haystack, size_t haystacklen,
 /* Copy no more than N characters of SRC to DEST, with NULL terminating.  */
 gchar *strncpy2(gchar *dest, const gchar *src, size_t n)
 {
-       register gchar c;
-       gchar *s = dest;
+       register const gchar *s = src;
+       register gchar *d = dest;
 
-       do {
-               if (--n == 0) {
-                       *dest = '\0';
-                       return s;
-               }
-               c = *src++;
-               *dest++ = c;
-       } while (c != '\0');
+       while (--n && *s)
+               *d++ = *s++;
+       *d = '\0';
 
-       /* don't do zero fill */
-       return s;
+       return dest;
 }
 
 #if !HAVE_ISWALNUM
@@ -642,34 +636,38 @@ void trim_subject_for_sort(gchar *str)
 
 void trim_subject(gchar *str)
 {
-       register guchar *srcp, *destp;
+       register guchar *srcp;
        gchar op, cl;
        gint in_brace;
+       
+       g_strstrip(str);
 
-       destp = str + subject_get_prefix_length(str);
+       srcp = str + subject_get_prefix_length(str);
 
-       if (*destp == '[') {
+       if (*srcp == '[') {
                op = '[';
                cl = ']';
-       } else if (*destp == '(') {
+       } else if (*srcp == '(') {
                op = '(';
                cl = ')';
        } else
-               return;
+               op = 0;
 
-       srcp = destp + 1;
-       in_brace = 1;
-       while (*srcp) {
-               if (*srcp == op)
-                       in_brace++;
-               else if (*srcp == cl)
-                       in_brace--;
-               srcp++;
-               if (in_brace == 0)
-                       break;
+       if (op) {
+               ++srcp;
+               in_brace = 1;
+               while (*srcp) {
+                       if (*srcp == op)
+                               in_brace++;
+                       else if (*srcp == cl)
+                               in_brace--;
+                       srcp++;
+                       if (in_brace == 0)
+                               break;
+               }
        }
        while (isspace(*srcp)) srcp++;
-       memmove(destp, srcp, strlen(srcp) + 1);
+       memmove(str, srcp, strlen(srcp) + 1);
 }
 
 void eliminate_parenthesis(gchar *str, gchar op, gchar cl)
@@ -923,7 +921,7 @@ GSList *address_list_append_with_comments(GSList *addr_list, const gchar *str)
        return address_list_append_real(addr_list, str, FALSE);
 }
 
-GSList *references_list_append(GSList *msgid_list, const gchar *str)
+GSList *references_list_prepend(GSList *msgid_list, const gchar *str)
 {
        const gchar *strp;
 
@@ -943,7 +941,7 @@ GSList *references_list_append(GSList *msgid_list, const gchar *str)
                msgid = g_strndup(start + 1, end - start - 1);
                g_strstrip(msgid);
                if (*msgid)
-                       msgid_list = g_slist_append(msgid_list, msgid);
+                       msgid_list = g_slist_prepend(msgid_list, msgid);
                else
                        g_free(msgid);
 
@@ -953,6 +951,17 @@ GSList *references_list_append(GSList *msgid_list, const gchar *str)
        return msgid_list;
 }
 
+GSList *references_list_append(GSList *msgid_list, const gchar *str)
+{
+       GSList *list;
+
+       list = references_list_prepend(NULL, str);
+       list = g_slist_reverse(list);
+       msgid_list = g_slist_concat(msgid_list, list);
+
+       return msgid_list;
+}
+
 GSList *newsgroup_list_append(GSList *group_list, const gchar *str)
 {
        gchar *work;
@@ -1079,11 +1088,15 @@ void subst_chars(gchar *str, gchar *orig, gchar subst)
 
 void subst_for_filename(gchar *str)
 {
+       if (!str)
+               return;
        subst_chars(str, "\t\r\n\\/*", '_');
 }
 
 void subst_for_shellsafe_filename(gchar *str)
 {
+       if (!str)
+               return;
        subst_for_filename(str);
        subst_chars(str, " \"'|&;()<>'!{}[]",'_');
 }
@@ -1443,7 +1456,6 @@ GList *uri_list_extract_filenames(const gchar *uri_list)
        GList *result = NULL;
        const gchar *p, *q;
        gchar *escaped_utf8uri;
-       gchar *file;
 
        p = uri_list;
 
@@ -1467,7 +1479,6 @@ GList *uri_list_extract_filenames(const gchar *uri_list)
                                        strncpy(escaped_utf8uri, p, q - p + 1);
                                        escaped_utf8uri[q - p + 1] = '\0';
                                        decode_uri(file, escaped_utf8uri);
-#warning FIXME_GTK2 /* should we use g_filename_from_utf8()? */
                     /*
                     * g_filename_from_uri() rejects escaped/locale encoded uri
                     * string which come from Nautilus.
@@ -2395,7 +2406,7 @@ gint append_file(const gchar *src, const gchar *dest, gboolean keep_backup)
        while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
                if (n_read < sizeof(buf) && ferror(src_fp))
                        break;
-               if (fwrite(buf, n_read, 1, dest_fp) < 1) {
+               if (fwrite(buf, 1, n_read, dest_fp) < n_read) {
                        g_warning("writing to %s failed.\n", dest);
                        fclose(dest_fp);
                        fclose(src_fp);
@@ -2463,7 +2474,7 @@ gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup)
        while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
                if (n_read < sizeof(buf) && ferror(src_fp))
                        break;
-               if (fwrite(buf, n_read, 1, dest_fp) < 1) {
+               if (fwrite(buf, 1, n_read, dest_fp) < n_read) {
                        g_warning("writing to %s failed.\n", dest);
                        fclose(dest_fp);
                        fclose(src_fp);
@@ -2543,7 +2554,7 @@ gint copy_file_part_to_fp(FILE *fp, off_t offset, size_t length, FILE *dest_fp)
        while ((n_read = fread(buf, sizeof(gchar), to_read, fp)) > 0) {
                if (n_read < to_read && ferror(fp))
                        break;
-               if (fwrite(buf, n_read, 1, dest_fp) < 1) {
+               if (fwrite(buf, 1, n_read, dest_fp) < n_read) {
                        return -1;
                }
                bytes_left -= n_read;
@@ -2666,8 +2677,8 @@ gint canonicalize_file(const gchar *src, const gchar *dest)
                        r = fputs(buf, dest_fp);
                } else {
                        if (len > 1) {
-                               r = fwrite(buf, len - 1, 1, dest_fp);
-                               if (r != 1)
+                               r = fwrite(buf, 1, len - 1, dest_fp);
+                               if (r != (len -1))
                                        r = EOF;
                        }
                        if (r != EOF)
@@ -2994,7 +3005,7 @@ FILE *str_open_as_stream(const gchar *str)
        len = strlen(str);
        if (len == 0) return fp;
 
-       if (fwrite(str, len, 1, fp) != 1) {
+       if (fwrite(str, 1, len, fp) != len) {
                FILE_OP_ERROR("str_open_as_stream", "fwrite");
                fclose(fp);
                return NULL;
@@ -3023,7 +3034,7 @@ gint str_write_to_file(const gchar *str, const gchar *file)
                return 0;
        }
 
-       if (fwrite(str, len, 1, fp) != 1) {
+       if (fwrite(str, 1, len, fp) != len) {
                FILE_OP_ERROR(file, "fwrite");
                fclose(fp);
                unlink(file);
@@ -3181,39 +3192,18 @@ gint execute_command_line(const gchar *cmdline, gboolean async)
 
 gchar *get_command_output(const gchar *cmdline)
 {
-       gchar buf[BUFFSIZE];
-       FILE *fp;
-       GString *str;
-       gchar *ret;
+       gchar *child_stdout;
+       gint status;
 
        g_return_val_if_fail(cmdline != NULL, NULL);
 
-       if ((fp = popen(cmdline, "r")) == NULL) {
-               FILE_OP_ERROR(cmdline, "popen");
+       if (g_spawn_command_line_sync(cmdline, &child_stdout, NULL, &status,
+                                     NULL) == FALSE) {
+               g_warning("Can't execute command: %s\n", cmdline);
                return NULL;
        }
 
-       str = g_string_new("");
-
-       while (fgets(buf, sizeof(buf), fp) != NULL)
-               g_string_append(str, buf);
-
-       pclose(fp);
-
-       ret = str->str;
-       g_string_free(str, FALSE);
-
-       if (!g_utf8_validate(ret, -1, NULL)) {
-               const gchar *src_codeset, *dest_codeset;
-               gchar *tmp = NULL;
-               src_codeset = conv_get_locale_charset_str();
-               dest_codeset = CS_UTF_8;
-               tmp = conv_codeset_strdup(ret, src_codeset, dest_codeset);
-               g_free(ret);
-               ret = tmp;
-       }
-       
-       return ret;
+       return child_stdout;
 }
 
 static gint is_unchanged_uri_char(char c)
@@ -3448,7 +3438,7 @@ void debug_print_real(const gchar *format, ...)
        g_vsnprintf(buf, sizeof(buf), format, args);
        va_end(args);
 
-       fputs(buf, stdout);
+       g_print("%s", buf);
 }
 
 void * subject_table_lookup(GHashTable *subject_table, gchar * subject)
@@ -3851,8 +3841,9 @@ GAuto *g_auto_pointer_new(gpointer p)
        ptr->ref = ref;
        ptr->ptr = p;
 
+#ifdef REF_DEBUG
        G_PRINT_REF ("XXXX ALLOC(%lx)\n", p);
-
+#endif
        return ptr;
 }
 
@@ -3906,8 +3897,9 @@ GAuto *g_auto_pointer_copy(GAuto *auto_ptr)
        newp->ptr = ref->pointer;
        ++(ref->cnt);
        
+#ifdef REF_DEBUG
        G_PRINT_REF ("XXXX COPY(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
-
+#endif
        return newp;
 }
 
@@ -3926,11 +3918,28 @@ void g_auto_pointer_free(GAuto *auto_ptr)
        ref = ptr->ref;
 
        if (--(ref->cnt) == 0) {
+#ifdef REF_DEBUG
                G_PRINT_REF ("XXXX FREE(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
+#endif
                ref->free(ref->pointer);
                g_free(ref);
-       } else
+       } 
+#ifdef REF_DEBUG
+       else
                G_PRINT_REF ("XXXX DEREF(%lx) -- REF (%d)\n", ref->pointer, ref->cnt);
+#endif
        g_free(ptr);            
 }
 
+void replace_returns(gchar *str)
+{
+       if (!str)
+               return;
+
+       while (strstr(str, "\n")) {
+               *strstr(str, "\n") = ' ';
+       }
+       while (strstr(str, "\r")) {
+               *strstr(str, "\r") = ' ';
+       }
+}