Use selected text on reply/forward.
[claws.git] / src / utils.c
index afc93634afe619110b21154e48796416671f3397..67df6503fb5c6584c8e82ae8f584e4ba8a55e2be 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2001 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2002 Hiroyuki Yamamoto
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -940,6 +940,22 @@ void subst_char(gchar *str, gchar orig, gchar subst)
        }
 }
 
+void subst_chars(gchar *str, gchar *orig, gchar subst)
+{
+       register gchar *p = str;
+
+       while (*p) {
+               if (strchr(orig, *p) != NULL)
+                       *p = subst;
+               p++;
+       }
+}
+
+void subst_for_filename(gchar *str)
+{
+       subst_chars(str, " \t\r\n\"/\\", '_');
+}
+
 gboolean is_header_line(const gchar *str)
 {
        if (str[0] == ':') return FALSE;
@@ -1009,39 +1025,6 @@ gint get_quote_level(const gchar *str)
        return quote_level;
 }
 
-GList *uri_list_extract_filenames(const gchar *uri_list)
-{
-       GList *result = NULL;
-       const gchar *p, *q;
-       gchar *file;
-
-       p = uri_list;
-
-       while (p) {
-               if (*p != '#') {
-                       while (isspace(*p)) p++;
-                       if (!strncmp(p, "file:", 5)) {
-                               p += 5;
-                               q = p;
-                               while (*q && *q != '\n' && *q != '\r') q++;
-
-                               if (q > p) {
-                                       q--;
-                                       while (q > p && isspace(*q)) q--;
-                                       file = g_malloc(q - p + 2);
-                                       strncpy(file, p, q - p + 1);
-                                       file[q - p + 1] = '\0';
-                                       result = g_list_append(result,file);
-                               }
-                       }
-               }
-               p = strchr(p, '\n');
-               if (p) p++;
-       }
-
-       return result;
-}
-
 gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle)
 {
        register guint haystack_len, needle_len;
@@ -1130,10 +1113,58 @@ gchar **strsplit_with_quote(const gchar *str, const gchar *delim,
        return str_array;
 }
 
+GList *uri_list_extract_filenames(const gchar *uri_list)
+{
+       GList *result = NULL;
+       const gchar *p, *q;
+       gchar *file;
+
+       p = uri_list;
+
+       while (p) {
+               if (*p != '#') {
+                       while (isspace(*p)) p++;
+                       if (!strncmp(p, "file:", 5)) {
+                               p += 5;
+                               q = p;
+                               while (*q && *q != '\n' && *q != '\r') q++;
+
+                               if (q > p) {
+                                       q--;
+                                       while (q > p && isspace(*q)) q--;
+                                       file = g_malloc(q - p + 2);
+                                       strncpy(file, p, q - p + 1);
+                                       file[q - p + 1] = '\0';
+                                       result = g_list_append(result,file);
+                               }
+                       }
+               }
+               p = strchr(p, '\n');
+               if (p) p++;
+       }
+
+       return result;
+}
+
+#define HEX_TO_INT(val, hex) \
+{ \
+       gchar c = hex; \
+ \
+       if ('0' <= c && c <= '9') { \
+               val = c - '0'; \
+       } else if ('a' <= c && c <= 'f') { \
+               val = c - 'a' + 10; \
+       } else if ('A' <= c && c <= 'F') { \
+               val = c - 'A' + 10; \
+       } else { \
+               val = 0; \
+       } \
+}
+
 /*
  * We need this wrapper around g_get_home_dir(), so that
  * we can fix some Windoze things here.  Should be done in glibc of course
- * but as long as we are not able to do our own extensions to glibc, we do 
+ * but as long as we are not able to do our own extensions to glibc, we do
  * it here.
  */
 gchar *get_home_dir(void)
@@ -1268,6 +1299,32 @@ off_t get_file_size(const gchar *file)
        return s.st_size;
 }
 
+off_t get_file_size_as_crlf(const gchar *file)
+{
+       FILE *fp;
+       off_t size = 0;
+       gchar buf[BUFFSIZE];
+
+       if ((fp = fopen(file, "r")) == NULL) {
+               FILE_OP_ERROR(file, "fopen");
+               return -1;
+       }
+
+       while (fgets(buf, sizeof(buf), fp) != NULL) {
+               strretchomp(buf);
+               size += strlen(buf) + 2;
+       }
+
+       if (ferror(fp)) {
+               FILE_OP_ERROR(file, "fgets");
+               size = -1;
+       }
+
+       fclose(fp);
+
+       return size;
+}
+
 off_t get_left_file_size(FILE *fp)
 {
        glong pos;
@@ -1785,6 +1842,22 @@ FILE *my_tmpfile(void)
        return tmpfile();
 }
 
+gchar *write_buffer_to_file(const gchar *buf, guint bufsize)
+{
+       FILE *fp;
+       gchar *tmp_file = NULL;
+
+       tmp_file = get_tmp_file();
+       fp = fopen(tmp_file, "w");
+       if (fp) {
+               fwrite(buf, 1, bufsize, fp);
+               fclose(fp);
+       }
+       else tmp_file = NULL;
+
+       return tmp_file;
+}
+
 gint execute_async(gchar *const argv[])
 {
        pid_t pid;
@@ -2012,16 +2085,16 @@ time_t remote_tzoffset_sec(const gchar *zone)
        gchar *p;
        gchar c;
        gint iustz;
-       gint h, m;
+       gint offset;
        time_t remoteoffset;
 
        strncpy(zone3, zone, 3);
        zone3[3] = '\0';
        remoteoffset = 0;
 
-       if (sscanf(zone, "%c%2d%2d", &c, &h, &m) == 3 &&
+       if (sscanf(zone, "%c%d", &c, &offset) == 2 &&
            (c == '+' || c == '-')) {
-               remoteoffset = ((h * 60) + m) * 60;
+               remoteoffset = ((offset / 100) * 60 + (offset % 100)) * 60;
                if (c == '-')
                        remoteoffset = -remoteoffset;
        } else if (!strncmp(zone, "UT" , 2) ||
@@ -2320,5 +2393,5 @@ gboolean subject_is_reply(const gchar *subject)
         * advanced Re: detection like "Re[4]", "ANTW:" or
         * Re: Re: Re: Re: Re: Re: Re: Re:" stuff. */
        if (subject == NULL) return FALSE;
-       else return 0 == g_strcasecmp(subject, "Re: ");
+       else return 0 == g_strncasecmp(subject, "Re: ", 4);
 }