Code cleanup around glib version check (2.28 minimum).
[claws.git] / src / common / utils.c
index 10c3764ad30508f73763bdca3429da78cf7116b5..0a7f4211b81bf3da9fcbe76b6707abcf18a27330 100644 (file)
 
 static gboolean debug_mode = FALSE;
 
-#if !GLIB_CHECK_VERSION(2, 26, 0)
-guchar *g_base64_decode_wa(const gchar *text, gsize *out_len)
-{
-       guchar *ret;
-       gsize input_length;
-       gint state = 0;
-       guint save = 0;
-
-       input_length = strlen(text);
-
-       ret = g_malloc0((input_length / 4) * 3 + 1);
-
-       *out_len = g_base64_decode_step(text, input_length, ret, &state, &save);
-
-       return ret;
-}
-#endif
-
 /* Return true if we are running as root.  This function should beused
    instead of getuid () == 0.  */
 gboolean superuser_p (void)
@@ -134,32 +116,14 @@ GSList *slist_copy_deep(GSList *list, GCopyFunc func)
 #endif
 }
 
-void list_free_strings(GList *list)
+void list_free_strings_full(GList *list)
 {
-       list = g_list_first(list);
-
-       while (list != NULL) {
-               g_free(list->data);
-               list = list->next;
-       }
-}
-
-void slist_free_strings(GSList *list)
-{
-       while (list != NULL) {
-               g_free(list->data);
-               list = list->next;
-       }
+       g_list_free_full(list, (GDestroyNotify)g_free);
 }
 
 void slist_free_strings_full(GSList *list)
 {
-#if GLIB_CHECK_VERSION(2,28,0)
        g_slist_free_full(list, (GDestroyNotify)g_free);
-#else
-       g_slist_foreach(list, (GFunc)g_free, NULL);
-       g_slist_free(list);
-#endif
 }
 
 static void hash_free_strings_func(gpointer key, gpointer value, gpointer data)
@@ -1054,7 +1018,7 @@ void subst_for_filename(gchar *str)
        if (!str)
                return;
 #ifdef G_OS_WIN32
-       subst_chars(str, "\t\r\n\\/*:", '_');
+       subst_chars(str, "\t\r\n\\/*?:", '_');
 #else
        subst_chars(str, "\t\r\n\\/*", '_');
 #endif
@@ -1089,13 +1053,13 @@ static const gchar * line_has_quote_char_last(const gchar * str, const gchar *qu
        gchar * tmp_pos = NULL;
        int i;
 
-       if (quote_chars == NULL)
+       if (str == NULL || quote_chars == NULL)
                return NULL;
 
        for (i = 0; i < strlen(quote_chars); i++) {
-               tmp_pos = strrchr (str, quote_chars[i]);
+               tmp_pos = strrchr (str, quote_chars[i]);
                if(position == NULL
-                  || (tmp_pos != NULL && position <= tmp_pos) )
+                               || (tmp_pos != NULL && position <= tmp_pos) )
                        position = tmp_pos;
        }
        return position;
@@ -1178,13 +1142,13 @@ const gchar * line_has_quote_char(const gchar * str, const gchar *quote_chars)
        gchar * tmp_pos = NULL;
        int i;
 
-       if (quote_chars == NULL)
-               return FALSE;
+       if (str == NULL || quote_chars == NULL)
+               return NULL;
 
        for (i = 0; i < strlen(quote_chars); i++) {
-               tmp_pos = strchr (str,  quote_chars[i]);
+               tmp_pos = strchr (str, quote_chars[i]);
                if(position == NULL
-                  || (tmp_pos != NULL && position >= tmp_pos) )
+                               || (tmp_pos != NULL && position >= tmp_pos) )
                        position = tmp_pos;
        }
        return position;
@@ -1604,7 +1568,6 @@ gint scan_mailto_url(const gchar *mailto, gchar **from, gchar **to, gchar **cc,
                                g_warning("couldn't set insert file '%s' in body", value);
                        }
                        g_free(tmp);
-                       tmp = NULL;
                } else if (attach && !g_ascii_strcasecmp(field, "attach")) {
                        int i = 0;
                        gchar *tmp = decode_uri_gdup(value);
@@ -1613,7 +1576,6 @@ gint scan_mailto_url(const gchar *mailto, gchar **from, gchar **to, gchar **cc,
                                        g_print("Refusing to attach '%s', potential private data leak\n",
                                                        tmp);
                                        g_free(tmp);
-                                       tmp = NULL;
                                        break;
                                }
                        }
@@ -2030,6 +1992,27 @@ const gchar *get_domain_name(void)
 
 off_t get_file_size(const gchar *file)
 {
+#ifdef G_OS_WIN32
+       GFile *f;
+       GFileInfo *fi;
+       GError *error = NULL;
+       goffset size;
+
+       f = g_file_new_for_path(file);
+       fi = g_file_query_info(f, "standard::size",
+                       G_FILE_QUERY_INFO_NONE, NULL, &error);
+       if (error != NULL) {
+               debug_print("get_file_size error: %s\n", error->message);
+               g_error_free(error);
+               g_object_unref(f);
+               return -1;
+       }
+       size = g_file_info_get_size(fi);
+       g_object_unref(fi);
+       g_object_unref(f);
+       return size;
+
+#else
        GStatBuf s;
 
        if (g_stat(file, &s) < 0) {
@@ -2038,6 +2021,7 @@ off_t get_file_size(const gchar *file)
        }
 
        return s.st_size;
+#endif
 }
 
 time_t get_file_mtime(const gchar *file)
@@ -3535,13 +3519,17 @@ static void _get_rfc822_date(gchar *buf, gint len, gboolean hidetz)
        gchar day[4], mon[4];
        gint dd, hh, mm, ss, yyyy;
        struct tm buf1;
-       gchar buf2[BUFFSIZE];
+       gchar buf2[RFC822_DATE_BUFFSIZE];
 
        t = time(NULL);
-       lt = localtime_r(&t, &buf1);
+       if (hidetz)
+               lt = gmtime_r(&t, &buf1);
+       else
+               lt = localtime_r(&t, &buf1);
 
-       sscanf(asctime_r(lt, buf2), "%3s %3s %d %d:%d:%d %d\n",
-              day, mon, &dd, &hh, &mm, &ss, &yyyy);
+       if (sscanf(asctime_r(lt, buf2), "%3s %3s %d %d:%d:%d %d\n",
+              day, mon, &dd, &hh, &mm, &ss, &yyyy) != 7)
+               g_warning("failed reading date/time");
 
        g_snprintf(buf, len, "%s, %d %s %d %02d:%02d:%02d %s",
                   day, dd, mon, yyyy, hh, mm, ss, (hidetz? "-0000": tzoffset(&t)));
@@ -4087,7 +4075,7 @@ void replace_returns(gchar *str)
 }
 
 /* get_uri_part() - retrieves a URI starting from scanpos.
-                   Returns TRUE if succesful */
+                   Returns TRUE if successful */
 gboolean get_uri_part(const gchar *start, const gchar *scanpos,
                             const gchar **bp, const gchar **ep, gboolean hdr)
 {
@@ -4123,7 +4111,7 @@ gboolean get_uri_part(const gchar *start, const gchar *scanpos,
         * should pass some URI type to this function and decide on that whether
         * to perform punctuation stripping */
 
-#define IS_REAL_PUNCT(ch)      (g_ascii_ispunct(ch) && !strchr("/?=-_)", ch))
+#define IS_REAL_PUNCT(ch)      (g_ascii_ispunct(ch) && !strchr("/?=-_~)", ch))
 
        for (; ep_ - 1 > scanpos + 1 &&
               IS_REAL_PUNCT(*(ep_ - 1));
@@ -4184,7 +4172,7 @@ static gboolean is_toplvl_domain(GHashTable *tab, const gchar *first, const gcha
        return g_hash_table_lookup(tab, buf) != NULL;
 }
 
-/* get_email_part() - retrieves an email address. Returns TRUE if succesful */
+/* get_email_part() - retrieves an email address. Returns TRUE if successful */
 gboolean get_email_part(const gchar *start, const gchar *scanpos,
                               const gchar **bp, const gchar **ep, gboolean hdr)
 {
@@ -4335,7 +4323,7 @@ search_again:
                ep_ += 3;
 
                /* go to matching '>' (or next non-rfc822 char, like \n) */
-               for (; *ep_ != '>' && *ep != '\0' && IS_RFC822_CHAR(*ep_); ep_++)
+               for (; *ep_ != '>' && *ep_ != '\0' && IS_RFC822_CHAR(*ep_); ep_++)
                        ;
 
                /* include the bracket */