mu: remove some unneeded code

Dead code, and replace mu_canonicalize_filename with g_canonicalize_filename.
This commit is contained in:
Dirk-Jan C. Binnema 2021-11-02 22:20:58 +02:00
parent 1242825a46
commit 4e6f2a3c26
5 changed files with 5 additions and 228 deletions

View File

@ -113,7 +113,7 @@ init_file_metadata(MuMsgFile* self, const char* path, const gchar* mdir, GError*
self->_timestamp = statbuf.st_mtime;
self->_size = (size_t)statbuf.st_size;
self->_path = mu_canonicalize_filename(path, NULL);
self->_path = g_canonicalize_filename(path, NULL);
self->_maildir = g_strdup(mdir ? mdir : "");
return TRUE;

View File

@ -737,76 +737,6 @@ Mu::mu_msg_contact_foreach(MuMsg* msg, MuMsgContactForeachFunc func, gpointer us
g_return_if_reached();
}
static int
cmp_str(const char* s1, const char* s2)
{
if (s1 == s2)
return 0;
else if (!s1)
return -1;
else if (!s2)
return 1;
/* optimization 1: ascii */
if (isascii(s1[0]) && isascii(s2[0])) {
int diff;
diff = tolower(s1[0]) - tolower(s2[0]);
if (diff != 0)
return diff;
}
/* utf 8 */
{
char *u1, *u2;
int diff;
u1 = g_utf8_strdown(s1, -1);
u2 = g_utf8_strdown(s2, -1);
diff = g_utf8_collate(u1, u2);
g_free(u1);
g_free(u2);
return diff;
}
}
static int
cmp_subject(const char* s1, const char* s2)
{
if (s1 == s2)
return 0;
else if (!s1)
return -1;
else if (!s2)
return 1;
return cmp_str(mu_str_subject_normalize(s1), mu_str_subject_normalize(s2));
}
int
Mu::mu_msg_cmp(MuMsg* m1, MuMsg* m2, MuMsgFieldId mfid)
{
g_return_val_if_fail(m1, 0);
g_return_val_if_fail(m2, 0);
g_return_val_if_fail(mu_msg_field_id_is_valid(mfid), 0);
/* even though date is a numeric field, we can sort it by its
* string repr. in the database, which is much faster */
if (mfid == MU_MSG_FIELD_ID_DATE || mu_msg_field_is_string(mfid))
return cmp_str(get_str_field(m1, mfid), get_str_field(m2, mfid));
if (mfid == MU_MSG_FIELD_ID_SUBJECT)
return cmp_subject(get_str_field(m1, mfid), get_str_field(m2, mfid));
/* TODO: note, we cast (potentially > MAXINT to int) */
if (mu_msg_field_is_numeric(mfid))
return get_num_field(m1, mfid) - get_num_field(m2, mfid);
return 0; /* TODO: handle lists */
}
gboolean
Mu::mu_msg_is_readable(MuMsg* self)
{

View File

@ -496,143 +496,5 @@ mu_util_read_password (const char *prompt)
return NULL;
}
return g_strdup (pass);
}
/* Pick g_canonicalize_file name from glib >= 2.58 */
/**
* g_canonicalize_filename:
* @filename: (type filename): the name of the file
* @relative_to: (type filename) (nullable): the relative directory, or %NULL
* to use the current working directory
*
* Gets the canonical file name from @filename. All triple slashes are turned into
* single slashes, and all `..` and `.`s resolved against @relative_to.
*
* Symlinks are not followed, and the returned path is guaranteed to be absolute.
*
* If @filename is an absolute path, @relative_to is ignored. Otherwise,
* @relative_to will be prepended to @filename to make it absolute. @relative_to
* must be an absolute path, or %NULL. If @relative_to is %NULL, it'll fallback
* to g_get_current_dir().
*
* This function never fails, and will canonicalize file paths even if they don't
* exist.
*
* No file system I/O is done.
*
* Returns: (type filename) (transfer full): a newly allocated string with the
* canonical file path
* Since: 2.58
*/
gchar *
mu_canonicalize_filename (const gchar *filename,
const gchar *relative_to)
{
gchar *canon, *start, *p, *q;
guint i;
g_return_val_if_fail (relative_to == NULL || g_path_is_absolute (relative_to), NULL);
if (!g_path_is_absolute (filename))
{
gchar *cwd_allocated = NULL;
const gchar *cwd;
if (relative_to != NULL)
cwd = relative_to;
else
cwd = cwd_allocated = g_get_current_dir ();
canon = g_build_filename (cwd, filename, NULL);
g_free (cwd_allocated);
}
else
{
canon = g_strdup (filename);
}
start = (char *)g_path_skip_root (canon);
if (start == NULL)
{
/* This shouldn't really happen, as g_get_current_dir() should
return an absolute pathname, but bug 573843 shows this is
not always happening */
g_free (canon);
return g_build_filename (G_DIR_SEPARATOR_S, filename, NULL);
}
/* POSIX allows double slashes at the start to
* mean something special (as does windows too).
* So, "//" != "/", but more than two slashes
* is treated as "/".
*/
i = 0;
for (p = start - 1;
(p >= canon) &&
G_IS_DIR_SEPARATOR (*p);
p--)
i++;
if (i > 2)
{
i -= 1;
start -= i;
memmove (start, start+i, strlen (start+i) + 1);
}
/* Make sure we're using the canonical dir separator */
p++;
while (p < start && G_IS_DIR_SEPARATOR (*p))
*p++ = G_DIR_SEPARATOR;
p = start;
while (*p != 0)
{
if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
{
memmove (p, p+1, strlen (p+1)+1);
}
else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
{
q = p + 2;
/* Skip previous separator */
p = p - 2;
if (p < start)
p = start;
while (p > start && !G_IS_DIR_SEPARATOR (*p))
p--;
if (G_IS_DIR_SEPARATOR (*p))
*p++ = G_DIR_SEPARATOR;
memmove (p, q, strlen (q)+1);
}
else
{
/* Skip until next separator */
while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
p++;
if (*p != 0)
{
/* Canonicalize one separator */
*p++ = G_DIR_SEPARATOR;
}
}
/* Remove additional separators */
q = p;
while (*q && G_IS_DIR_SEPARATOR (*q))
q++;
if (p != q)
memmove (p, q, strlen (q) + 1);
}
/* Remove trailing slashes */
if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
*(p-1) = 0;
return canon;
return g_strdup(pass);
}

View File

@ -46,22 +46,7 @@ G_BEGIN_DECLS
* @return the expanded path as a newly allocated string, or NULL in
* case of error
*/
char* mu_util_dir_expand (const char* path)
G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
/**
* See g_canonicalize_filename
*
* @param filename
* @param relative_to
*
* @return
*/
char *mu_canonicalize_filename (const gchar *filename,
const gchar *relative_to)
G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
char* mu_util_dir_expand(const char* path) G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT;
/**
* guess the maildir; first try $MAILDIR; if it is unset or

View File

@ -458,8 +458,8 @@ Mu::size_to_string(const std::string& val, bool is_first)
std::string
Mu::canonicalize_filename(const std::string& path, const std::string& relative_to)
{
char* fname = mu_canonicalize_filename(path.c_str(),
relative_to.empty() ? NULL : relative_to.c_str());
char* fname =
g_canonicalize_filename(path.c_str(), relative_to.empty() ? NULL : relative_to.c_str());
std::string rv{fname};
g_free(fname);