2011-05-22 09:27:12 +02:00
|
|
|
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
|
|
|
|
|
2011-09-12 19:38:40 +02:00
|
|
|
/*
|
2011-05-22 09:27:12 +02:00
|
|
|
** Copyright (C) 2008-2011 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
2009-11-25 21:55:06 +01:00
|
|
|
**
|
|
|
|
** 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
|
|
|
|
** the Free Software Foundation; either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
2011-09-12 19:38:40 +02:00
|
|
|
**
|
2009-11-25 21:55:06 +01:00
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
2011-09-12 19:38:40 +02:00
|
|
|
**
|
2009-11-25 21:55:06 +01:00
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program; if not, write to the Free Software Foundation,
|
2011-09-12 19:38:40 +02:00
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
2009-11-25 21:55:06 +01:00
|
|
|
*/
|
2010-11-20 16:27:17 +01:00
|
|
|
|
|
|
|
|
2010-11-14 21:04:16 +01:00
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif /*HAVE_CONFIG_H*/
|
|
|
|
|
2009-11-25 21:55:06 +01:00
|
|
|
|
|
|
|
#include <glib.h>
|
2010-09-11 10:19:58 +02:00
|
|
|
#include <string.h>
|
2010-11-11 21:04:47 +01:00
|
|
|
#include <ctype.h>
|
2010-11-24 22:30:41 +01:00
|
|
|
#include <stdlib.h>
|
2010-12-11 12:52:03 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
/* hopefully, this should get us a sane PATH_MAX */
|
|
|
|
#include <limits.h>
|
|
|
|
/* not all systems provide PATH_MAX in limits.h */
|
|
|
|
#ifndef PATH_MAX
|
|
|
|
#include <sys/param.h>
|
2011-01-22 02:13:18 +01:00
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define PATH_MAX MAXPATHLEN
|
|
|
|
#endif /*!PATH_MAX*/
|
|
|
|
#endif /*PATH_MAX*/
|
|
|
|
|
|
|
|
#include "mu-str.h"
|
|
|
|
#include "mu-msg-fields.h"
|
|
|
|
|
|
|
|
|
|
|
|
const char*
|
|
|
|
mu_str_size_s (size_t s)
|
|
|
|
{
|
|
|
|
static char buf[32];
|
|
|
|
|
|
|
|
#ifdef HAVE_GLIB216
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
tmp = g_format_size_for_display ((goffset)s);
|
|
|
|
strncpy (buf, tmp, sizeof(buf));
|
|
|
|
buf[sizeof(buf) -1] = '\0'; /* just in case */
|
|
|
|
g_free (tmp);
|
|
|
|
|
|
|
|
#else
|
|
|
|
if (s >= 1000 * 1000)
|
|
|
|
g_snprintf(buf, sizeof(buf), "%.1f MB",
|
|
|
|
(double)s/(1000*1000));
|
|
|
|
else
|
|
|
|
g_snprintf(buf, sizeof(buf), "%.1f kB", (double)s/(1000));
|
|
|
|
#endif /*HAVE_GLIB216*/
|
2010-01-22 18:17:00 +01:00
|
|
|
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2011-09-12 19:38:40 +02:00
|
|
|
char*
|
2011-01-22 02:13:18 +01:00
|
|
|
mu_str_size (size_t s)
|
|
|
|
{
|
|
|
|
return g_strdup (mu_str_size_s(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
const char*
|
2011-08-11 21:44:23 +02:00
|
|
|
mu_str_flags_s (MuFlags flags)
|
2011-01-22 02:13:18 +01:00
|
|
|
{
|
2011-08-11 21:44:23 +02:00
|
|
|
return mu_flags_to_str_s (flags, MU_FLAG_TYPE_ANY);
|
2011-01-22 02:13:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
2011-08-11 21:44:23 +02:00
|
|
|
mu_str_flags (MuFlags flags)
|
2011-01-22 02:13:18 +01:00
|
|
|
{
|
|
|
|
return g_strdup (mu_str_flags_s(flags));
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
mu_str_summarize (const char* str, size_t max_lines)
|
|
|
|
{
|
|
|
|
char *summary;
|
|
|
|
size_t nl_seen;
|
|
|
|
unsigned i,j;
|
|
|
|
gboolean last_was_blank;
|
|
|
|
|
|
|
|
g_return_val_if_fail (str, NULL);
|
|
|
|
g_return_val_if_fail (max_lines > 0, NULL);
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
/* len for summary <= original len */
|
|
|
|
summary = g_new (gchar, strlen(str) + 1);
|
|
|
|
|
|
|
|
/* copy the string up to max_lines lines, replace CR/LF/tab with
|
|
|
|
* single space */
|
|
|
|
for (i = j = 0, nl_seen = 0, last_was_blank = TRUE;
|
|
|
|
nl_seen < max_lines && str[i] != '\0'; ++i) {
|
|
|
|
|
|
|
|
if (str[i] == '\n' || str[i] == '\r' ||
|
|
|
|
str[i] == '\t' || str[i] == ' ' ) {
|
|
|
|
|
|
|
|
if (str[i] == '\n')
|
|
|
|
++nl_seen;
|
|
|
|
|
|
|
|
/* no double-blanks or blank at end of str */
|
|
|
|
if (!last_was_blank && str[i+1] != '\0')
|
|
|
|
summary[j++] = ' ';
|
|
|
|
|
|
|
|
last_was_blank = TRUE;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
summary[j++] = str[i];
|
|
|
|
last_was_blank = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
summary[j] = '\0';
|
|
|
|
return summary;
|
|
|
|
}
|
|
|
|
|
2011-01-29 14:00:46 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup_contact (char *contact)
|
|
|
|
{
|
|
|
|
char *c, *c2;
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-01-29 14:00:46 +01:00
|
|
|
/* replace "'<> with space */
|
|
|
|
for (c2 = contact; *c2; ++c2)
|
|
|
|
if (*c2 == '"' || *c2 == '\'' || *c2 == '<' || *c2 == '>')
|
|
|
|
*c2 = ' ';
|
|
|
|
|
|
|
|
/* remove everything between '()' if it's after the 5th pos;
|
|
|
|
* good to cleanup corporate contact address spam... */
|
|
|
|
c = g_strstr_len (contact, -1, "(");
|
|
|
|
if (c && c - contact > 5)
|
2011-05-22 09:27:12 +02:00
|
|
|
*c = '\0';
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-01-29 14:00:46 +01:00
|
|
|
g_strstrip (contact);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
/* this is still somewhat simplistic... */
|
|
|
|
const char*
|
|
|
|
mu_str_display_contact_s (const char *str)
|
|
|
|
{
|
|
|
|
static gchar contact[255];
|
|
|
|
gchar *c, *c2;
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
str = str ? str : "";
|
|
|
|
g_strlcpy (contact, str, sizeof(contact));
|
|
|
|
|
|
|
|
/* we check for '<', so we can strip out the address stuff in
|
|
|
|
* e.g. 'Hello World <hello@world.xx>, but only if there is
|
|
|
|
* something alphanumeric before the <
|
|
|
|
*/
|
|
|
|
c = g_strstr_len (contact, -1, "<");
|
|
|
|
if (c != NULL) {
|
|
|
|
for (c2 = contact; c2 < c && !(isalnum(*c2)); ++c2);
|
|
|
|
if (c2 != c) /* apparently, there was something,
|
|
|
|
* so we can remove the <... part*/
|
|
|
|
*c = '\0';
|
|
|
|
}
|
2010-11-07 18:01:34 +01:00
|
|
|
|
2011-01-29 14:00:46 +01:00
|
|
|
cleanup_contact (contact);
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
return contact;
|
|
|
|
}
|
2010-11-07 18:01:34 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
char*
|
|
|
|
mu_str_display_contact (const char *str)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (str, NULL);
|
2010-11-07 18:01:34 +01:00
|
|
|
|
2011-01-22 02:13:18 +01:00
|
|
|
return g_strdup (mu_str_display_contact_s (str));
|
|
|
|
}
|
2010-11-23 21:05:55 +01:00
|
|
|
|
|
|
|
|
2011-07-16 11:50:17 +02:00
|
|
|
gint64
|
|
|
|
mu_str_size_parse_bkm (const char* str)
|
2011-01-22 02:13:18 +01:00
|
|
|
{
|
2011-05-20 20:31:08 +02:00
|
|
|
gint64 num;
|
2011-07-16 11:50:17 +02:00
|
|
|
|
|
|
|
g_return_val_if_fail (str, -1);
|
|
|
|
|
|
|
|
if (!isdigit(str[0]))
|
|
|
|
return -1;
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-07-16 11:50:17 +02:00
|
|
|
num = atoi(str);
|
|
|
|
for (++str; isdigit(*str); ++str);
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-07-16 11:50:17 +02:00
|
|
|
switch (tolower(*str)) {
|
|
|
|
case '\0':
|
|
|
|
case 'b' : return num; /* bytes */
|
|
|
|
case 'k': return num * 1000; /* kilobyte */
|
|
|
|
case 'm': return num * 1000 * 1000; /* megabyte */
|
2011-01-22 02:13:18 +01:00
|
|
|
default:
|
2011-07-16 11:50:17 +02:00
|
|
|
return -1;
|
2011-01-06 15:21:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-15 22:44:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
char*
|
|
|
|
mu_str_from_list (const GSList *lst, char sepa)
|
|
|
|
{
|
|
|
|
const GSList *cur;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
g_return_val_if_fail (sepa, NULL);
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-06-15 22:44:52 +02:00
|
|
|
for (cur = lst, str = NULL; cur; cur = g_slist_next(cur)) {
|
|
|
|
|
|
|
|
char *tmp;
|
2011-07-15 07:41:04 +02:00
|
|
|
/* two extra dummy '\0' so -Wstack-protector won't complain */
|
2011-09-12 19:38:40 +02:00
|
|
|
char sep[4] = { '\0', '\0', '\0', '\0' };
|
2011-06-15 22:44:52 +02:00
|
|
|
sep[0] = cur->next ? sepa : '\0';
|
|
|
|
|
|
|
|
tmp = g_strdup_printf ("%s%s%s",
|
|
|
|
str ? str : "",
|
|
|
|
(gchar*)cur->data,
|
|
|
|
sep);
|
|
|
|
g_free (str);
|
|
|
|
str = tmp;
|
|
|
|
}
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-06-15 22:44:52 +02:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
GSList*
|
2011-07-06 01:12:59 +02:00
|
|
|
mu_str_to_list (const char *str, char sepa, gboolean strip)
|
2011-06-15 22:44:52 +02:00
|
|
|
{
|
|
|
|
GSList *lst;
|
|
|
|
gchar **strs, **cur;
|
2011-07-15 07:41:04 +02:00
|
|
|
/* two extra dummy '\0' so -Wstack-protector won't complain */
|
2011-09-12 19:38:40 +02:00
|
|
|
char sep[4] = { '\0', '\0', '\0', '\0' };
|
|
|
|
|
2011-06-15 22:44:52 +02:00
|
|
|
g_return_val_if_fail (sepa, NULL);
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-06-15 22:44:52 +02:00
|
|
|
if (!str)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
sep[0] = sepa;
|
|
|
|
strs = g_strsplit (str, sep, -1);
|
|
|
|
|
2011-07-06 01:12:59 +02:00
|
|
|
for (cur = strs, lst = NULL; cur && *cur; ++cur) {
|
|
|
|
char *elm;
|
|
|
|
elm = g_strdup(*cur);
|
|
|
|
if (strip)
|
|
|
|
elm = g_strstrip (elm);
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-07-06 01:12:59 +02:00
|
|
|
lst = g_slist_prepend (lst, elm);
|
|
|
|
}
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-06-15 22:44:52 +02:00
|
|
|
lst = g_slist_reverse (lst);
|
|
|
|
g_strfreev (strs);
|
|
|
|
|
|
|
|
return lst;
|
|
|
|
}
|
|
|
|
|
2011-08-29 22:36:10 +02:00
|
|
|
|
|
|
|
static gchar*
|
2012-03-30 17:45:06 +02:00
|
|
|
eat_esc_string (char **strlst, GError **err)
|
2011-08-29 22:36:10 +02:00
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
gboolean quoted;
|
|
|
|
GString *gstr;
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-08-29 22:36:10 +02:00
|
|
|
str = g_strchug (*strlst);
|
|
|
|
gstr = g_string_sized_new (strlen(str));
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-08-29 22:36:10 +02:00
|
|
|
for (quoted = FALSE; *str; ++str) {
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-08-29 22:36:10 +02:00
|
|
|
if (*str == '"') {
|
|
|
|
quoted = !quoted;
|
|
|
|
continue;
|
2012-01-21 10:18:29 +01:00
|
|
|
} else if (*str == '\\') {
|
2011-08-29 22:36:10 +02:00
|
|
|
if (str[1] != ' ' && str[1] != '"' && str[1] != '\\')
|
|
|
|
goto err; /* invalid escaping */
|
2012-01-21 10:18:29 +01:00
|
|
|
g_string_append_c (gstr, str[1]);
|
|
|
|
++str;
|
2011-08-29 22:36:10 +02:00
|
|
|
continue;
|
2012-01-21 10:18:29 +01:00
|
|
|
} else if (*str == ' ' && !quoted) {
|
2011-08-29 22:36:10 +02:00
|
|
|
++str;
|
|
|
|
goto leave;
|
2011-09-12 19:38:40 +02:00
|
|
|
} else
|
2011-08-29 22:36:10 +02:00
|
|
|
g_string_append_c (gstr, *str);
|
|
|
|
}
|
|
|
|
leave:
|
|
|
|
*strlst = str;
|
|
|
|
return g_string_free (gstr, FALSE);
|
|
|
|
err:
|
2012-04-01 12:08:02 +02:00
|
|
|
g_set_error (err, MU_ERROR_DOMAIN, MU_ERROR_IN_PARAMETERS,
|
2012-03-30 17:45:06 +02:00
|
|
|
"error parsing string '%s'", g_strchug(*strlst));
|
2011-08-29 22:36:10 +02:00
|
|
|
*strlst = NULL;
|
|
|
|
return g_string_free (gstr, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GSList*
|
2012-03-30 17:45:06 +02:00
|
|
|
mu_str_esc_to_list (const char *strings, GError **err)
|
2011-08-29 22:36:10 +02:00
|
|
|
{
|
|
|
|
GSList *lst;
|
2011-09-12 19:38:40 +02:00
|
|
|
char *mystrings, *freeme;
|
|
|
|
const char* cur;
|
|
|
|
|
2011-08-29 22:36:10 +02:00
|
|
|
g_return_val_if_fail (strings, NULL);
|
|
|
|
|
2011-09-12 19:38:40 +02:00
|
|
|
for (cur = strings; *cur && (*cur == ' ' || *cur == '\t'); ++cur);
|
|
|
|
freeme = mystrings = g_strdup (cur);
|
|
|
|
|
|
|
|
lst = NULL;
|
2011-08-29 22:36:10 +02:00
|
|
|
do {
|
2011-09-12 19:38:40 +02:00
|
|
|
gchar *str;
|
2012-03-30 17:45:06 +02:00
|
|
|
str = eat_esc_string (&mystrings, err);
|
2011-08-29 22:36:10 +02:00
|
|
|
if (str)
|
|
|
|
lst = g_slist_prepend (lst, str);
|
2012-03-30 17:45:06 +02:00
|
|
|
else {
|
|
|
|
g_free (freeme);
|
|
|
|
mu_str_free_list (lst);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-08-29 22:36:10 +02:00
|
|
|
} while (mystrings && *mystrings);
|
|
|
|
|
|
|
|
g_free (freeme);
|
|
|
|
return g_slist_reverse (lst);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-06-15 22:44:52 +02:00
|
|
|
void
|
|
|
|
mu_str_free_list (GSList *lst)
|
|
|
|
{
|
|
|
|
g_slist_foreach (lst, (GFunc)g_free, NULL);
|
2011-09-12 19:38:40 +02:00
|
|
|
g_slist_free (lst);
|
2011-06-15 22:44:52 +02:00
|
|
|
}
|
|
|
|
|
2011-06-18 17:43:09 +02:00
|
|
|
const gchar*
|
|
|
|
mu_str_subject_normalize (const gchar* str)
|
|
|
|
{
|
|
|
|
gchar *last_colon;
|
|
|
|
g_return_val_if_fail (str, NULL);
|
|
|
|
|
|
|
|
last_colon = g_strrstr (str, ":");
|
|
|
|
if (!last_colon)
|
|
|
|
return str;
|
2011-07-02 11:48:46 +02:00
|
|
|
else {
|
|
|
|
gchar *str;
|
|
|
|
str = last_colon + 1;
|
|
|
|
while (*str == ' ')
|
|
|
|
++str;
|
|
|
|
return str;
|
|
|
|
}
|
2011-06-18 17:43:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-13 20:51:57 +02:00
|
|
|
struct _CheckPrefix {
|
|
|
|
const char *str;
|
|
|
|
gboolean match;
|
|
|
|
gboolean range_field;
|
|
|
|
};
|
|
|
|
typedef struct _CheckPrefix CheckPrefix;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
each_check_prefix (MuMsgFieldId mfid, CheckPrefix *cpfx)
|
|
|
|
{
|
|
|
|
const char *pfx;
|
|
|
|
char pfx_short[3] = { 'X', ':', '\0'};
|
|
|
|
char k;
|
|
|
|
|
|
|
|
if (!cpfx || cpfx->match)
|
|
|
|
return;
|
|
|
|
|
|
|
|
k = pfx_short[0] = mu_msg_field_shortcut (mfid);
|
|
|
|
if (k && g_str_has_prefix (cpfx->str, pfx_short)) {
|
|
|
|
cpfx->match = TRUE;
|
|
|
|
cpfx->range_field = mu_msg_field_is_range_field (mfid);
|
|
|
|
}
|
|
|
|
|
|
|
|
pfx = mu_msg_field_name (mfid);
|
|
|
|
if (pfx && g_str_has_prefix (cpfx->str, pfx) &&
|
|
|
|
cpfx->str[strlen(pfx)] == ':') {
|
|
|
|
cpfx->match = TRUE;
|
|
|
|
cpfx->range_field = mu_msg_field_is_range_field (mfid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
check_for_field (const char *str, gboolean *is_field, gboolean *is_range_field)
|
|
|
|
{
|
|
|
|
CheckPrefix pfx;
|
|
|
|
|
|
|
|
pfx.str = str;
|
2012-05-04 19:58:34 +02:00
|
|
|
|
|
|
|
/* skip any non-alphanum starts in cpfx->str; this is to
|
|
|
|
* handle the case where we have e.g. "(maildir:/abc)"
|
|
|
|
*/
|
|
|
|
while (pfx.str && !isalnum(*pfx.str))
|
|
|
|
++pfx.str;
|
|
|
|
|
2012-04-13 20:51:57 +02:00
|
|
|
pfx.match = pfx.range_field = FALSE;
|
|
|
|
|
|
|
|
mu_msg_field_foreach ((MuMsgFieldForeachFunc)each_check_prefix,
|
|
|
|
&pfx);
|
|
|
|
|
|
|
|
*is_field = pfx.match;
|
|
|
|
*is_range_field = pfx.range_field;
|
|
|
|
}
|
2011-06-15 22:44:52 +02:00
|
|
|
|
2011-05-22 09:27:12 +02:00
|
|
|
/*
|
|
|
|
* Xapian treats various characters such as '@', '-', ':' and '.'
|
|
|
|
* specially; function below is an ugly hack to make it DWIM in most
|
2012-04-13 20:51:57 +02:00
|
|
|
* cases...
|
|
|
|
*
|
|
|
|
* function expects search terms (not complete queries)
|
|
|
|
* */
|
2010-11-29 20:21:55 +01:00
|
|
|
char*
|
2012-04-16 00:10:46 +02:00
|
|
|
mu_str_xapian_escape_in_place (char *term, gboolean esc_space)
|
2010-11-29 20:21:55 +01:00
|
|
|
{
|
2012-04-16 00:10:46 +02:00
|
|
|
unsigned char *cur;
|
2011-11-13 11:47:28 +01:00
|
|
|
const char escchar = '_';
|
2012-04-13 20:51:57 +02:00
|
|
|
gboolean is_field, is_range_field;
|
2012-04-13 21:12:44 +02:00
|
|
|
unsigned colon;
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2012-04-13 20:51:57 +02:00
|
|
|
g_return_val_if_fail (term, NULL);
|
|
|
|
|
|
|
|
check_for_field (term, &is_field, &is_range_field);
|
2010-11-29 20:21:55 +01:00
|
|
|
|
2012-04-16 00:10:46 +02:00
|
|
|
for (colon = 0, cur = (unsigned char*)term; *cur; ++cur) {
|
2011-05-22 12:45:10 +02:00
|
|
|
|
|
|
|
switch (*cur) {
|
2012-04-16 00:10:46 +02:00
|
|
|
|
2012-04-13 20:51:57 +02:00
|
|
|
case '.': /* escape '..' if it's not a range field*/
|
|
|
|
if (is_range_field && cur[1] == '.')
|
2012-01-21 10:18:29 +01:00
|
|
|
cur += 1;
|
2010-11-29 20:21:55 +01:00
|
|
|
else
|
2011-11-13 11:47:28 +01:00
|
|
|
*cur = escchar;
|
2011-05-22 12:45:10 +02:00
|
|
|
break;
|
|
|
|
case ':':
|
2012-04-13 20:51:57 +02:00
|
|
|
/* if there's a registered xapian prefix
|
2012-04-13 21:12:44 +02:00
|
|
|
* before the *first* ':', don't touch
|
|
|
|
* it. Otherwise replace ':' with '_'... ugh
|
|
|
|
* yuck ugly...
|
2011-09-12 19:38:40 +02:00
|
|
|
*/
|
2012-04-13 21:12:44 +02:00
|
|
|
if (colon != 0 || !is_field)
|
2012-01-21 10:18:29 +01:00
|
|
|
*cur = escchar;
|
2012-04-13 21:12:44 +02:00
|
|
|
++colon;
|
2011-05-22 12:45:10 +02:00
|
|
|
break;
|
2012-05-04 19:58:34 +02:00
|
|
|
case '(':
|
|
|
|
case ')':
|
2012-03-30 17:45:06 +02:00
|
|
|
case '\'':
|
|
|
|
case '*': /* wildcard */
|
|
|
|
break;
|
|
|
|
default:
|
2012-04-16 00:10:46 +02:00
|
|
|
/* escape all other special stuff */
|
2012-04-30 16:32:34 +02:00
|
|
|
if (*cur < 0x80 && !isalnum (*cur))
|
2012-03-30 17:45:06 +02:00
|
|
|
*cur = escchar;
|
2011-05-22 09:27:12 +02:00
|
|
|
}
|
2010-11-29 20:21:55 +01:00
|
|
|
}
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2012-04-16 00:10:46 +02:00
|
|
|
/* downcase try to remove accents etc. */
|
|
|
|
return mu_str_normalize_in_place (term, TRUE);
|
2010-11-29 20:21:55 +01:00
|
|
|
}
|
|
|
|
|
2010-12-07 22:19:13 +01:00
|
|
|
char*
|
2012-04-16 00:10:46 +02:00
|
|
|
mu_str_xapian_escape (const char *query, gboolean esc_space)
|
2010-12-07 22:19:13 +01:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (query, NULL);
|
|
|
|
|
2012-04-16 00:10:46 +02:00
|
|
|
return mu_str_xapian_escape_in_place (g_strdup(query), esc_space);
|
2010-12-07 22:19:13 +01:00
|
|
|
}
|
2010-12-11 12:52:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* note: this function is *not* re-entrant, it returns a static buffer */
|
|
|
|
const char*
|
|
|
|
mu_str_fullpath_s (const char* path, const char* name)
|
|
|
|
{
|
|
|
|
static char buf[PATH_MAX + 1];
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2010-12-11 12:52:03 +01:00
|
|
|
g_return_val_if_fail (path, NULL);
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2010-12-11 12:52:03 +01:00
|
|
|
snprintf (buf, sizeof(buf), "%s%c%s", path, G_DIR_SEPARATOR,
|
|
|
|
name ? name : "");
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2010-12-11 12:52:03 +01:00
|
|
|
return buf;
|
|
|
|
}
|
2011-01-06 12:55:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
char*
|
2011-07-31 11:14:22 +02:00
|
|
|
mu_str_escape_c_literal (const gchar* str, gboolean in_quotes)
|
2011-01-06 12:55:16 +01:00
|
|
|
{
|
|
|
|
const char* cur;
|
|
|
|
GString *tmp;
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-01-06 12:55:16 +01:00
|
|
|
g_return_val_if_fail (str, NULL);
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-01-06 12:55:16 +01:00
|
|
|
tmp = g_string_sized_new (2 * strlen(str));
|
2011-07-31 11:14:22 +02:00
|
|
|
|
|
|
|
if (in_quotes)
|
|
|
|
g_string_append_c (tmp, '"');
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-01-06 12:55:16 +01:00
|
|
|
for (cur = str; *cur; ++cur)
|
|
|
|
switch (*cur) {
|
2011-01-06 15:55:43 +01:00
|
|
|
case '\\': tmp = g_string_append (tmp, "\\\\"); break;
|
|
|
|
case '"': tmp = g_string_append (tmp, "\\\""); break;
|
|
|
|
default: tmp = g_string_append_c (tmp, *cur);
|
2011-01-06 12:55:16 +01:00
|
|
|
}
|
|
|
|
|
2011-07-31 11:14:22 +02:00
|
|
|
if (in_quotes)
|
|
|
|
g_string_append_c (tmp, '"');
|
2011-08-29 22:36:10 +02:00
|
|
|
|
|
|
|
return g_string_free (tmp, FALSE);
|
2011-01-06 12:55:16 +01:00
|
|
|
}
|
2011-01-06 16:32:42 +01:00
|
|
|
|
2011-03-06 11:14:26 +01:00
|
|
|
|
2011-07-17 13:35:59 +02:00
|
|
|
|
|
|
|
/* turn \0-terminated buf into ascii (which is a utf8 subset); convert
|
|
|
|
* any non-ascii into '.'
|
|
|
|
*/
|
|
|
|
char*
|
|
|
|
mu_str_asciify_in_place (char *buf)
|
|
|
|
{
|
|
|
|
char *c;
|
2012-01-11 23:19:35 +01:00
|
|
|
|
|
|
|
g_return_val_if_fail (buf, NULL);
|
|
|
|
|
2011-07-17 13:35:59 +02:00
|
|
|
for (c = buf; c && *c; ++c)
|
|
|
|
if (!isascii(*c))
|
|
|
|
c[0] = '.';
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2012-01-11 23:19:35 +01:00
|
|
|
char*
|
|
|
|
mu_str_utf8ify (const char *buf)
|
|
|
|
{
|
|
|
|
char *utf8;
|
|
|
|
|
|
|
|
g_return_val_if_fail (buf, NULL);
|
|
|
|
|
|
|
|
utf8 = g_strdup (buf);
|
|
|
|
|
|
|
|
if (!g_utf8_validate (buf, -1, NULL))
|
|
|
|
mu_str_asciify_in_place (utf8);
|
|
|
|
|
|
|
|
return utf8;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-17 13:35:59 +02:00
|
|
|
gchar*
|
|
|
|
mu_str_convert_to_utf8 (const char* buffer, const char *charset)
|
|
|
|
{
|
|
|
|
GError *err;
|
|
|
|
gchar * utf8;
|
|
|
|
|
|
|
|
g_return_val_if_fail (buffer, NULL);
|
|
|
|
g_return_val_if_fail (charset, NULL );
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-07-17 13:35:59 +02:00
|
|
|
err = NULL;
|
|
|
|
utf8 = g_convert_with_fallback (buffer, -1, "UTF-8",
|
2011-09-12 19:38:40 +02:00
|
|
|
charset, NULL,
|
2011-07-17 13:35:59 +02:00
|
|
|
NULL, NULL, &err);
|
|
|
|
if (!utf8) {
|
|
|
|
g_debug ("%s: conversion failed from %s: %s",
|
|
|
|
__FUNCTION__, charset, err ? err->message : "");
|
|
|
|
if (err)
|
|
|
|
g_error_free (err);
|
|
|
|
}
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-07-17 13:35:59 +02:00
|
|
|
return utf8;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-03-06 11:14:26 +01:00
|
|
|
gchar*
|
|
|
|
mu_str_guess_last_name (const char *name)
|
|
|
|
{
|
|
|
|
const gchar *lastsp;
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
return g_strdup ("");
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-03-06 11:14:26 +01:00
|
|
|
lastsp = g_strrstr (name, " ");
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-03-06 11:14:26 +01:00
|
|
|
return g_strdup (lastsp ? lastsp + 1 : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gchar*
|
|
|
|
mu_str_guess_first_name (const char *name)
|
|
|
|
{
|
|
|
|
const gchar *lastsp;
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
return g_strdup ("");
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-03-06 11:14:26 +01:00
|
|
|
lastsp = g_strrstr (name, " ");
|
|
|
|
|
|
|
|
if (lastsp)
|
|
|
|
return g_strndup (name, lastsp - name);
|
|
|
|
else
|
|
|
|
return g_strdup (name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar*
|
|
|
|
cleanup_str (const char* str)
|
|
|
|
{
|
|
|
|
gchar *s;
|
|
|
|
const gchar *cur;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
if (mu_str_is_empty(str))
|
|
|
|
return g_strdup ("");
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-03-06 11:14:26 +01:00
|
|
|
s = g_new0 (char, strlen(str) + 1);
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-03-06 11:14:26 +01:00
|
|
|
for (cur = str, i = 0; *cur; ++cur) {
|
|
|
|
if (ispunct(*cur) || isspace(*cur))
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
s[i++] = *cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gchar*
|
|
|
|
mu_str_guess_nick (const char* name)
|
|
|
|
{
|
|
|
|
gchar *fname, *lname, *nick;
|
|
|
|
gchar initial[7];
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-03-06 11:14:26 +01:00
|
|
|
fname = mu_str_guess_first_name (name);
|
|
|
|
lname = mu_str_guess_last_name (name);
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-03-06 11:14:26 +01:00
|
|
|
/* if there's no last name, use first name as the nick */
|
|
|
|
if (mu_str_is_empty(fname) || mu_str_is_empty(lname)) {
|
|
|
|
g_free (lname);
|
|
|
|
nick = fname;
|
|
|
|
goto leave;
|
|
|
|
}
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-03-06 11:14:26 +01:00
|
|
|
memset (initial, 0, sizeof(initial));
|
|
|
|
/* couldn't we get an initial for the last name? */
|
|
|
|
if (g_unichar_to_utf8 (g_utf8_get_char (lname), initial) == 0) {
|
|
|
|
g_free (lname);
|
|
|
|
nick = fname;
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
nick = g_strdup_printf ("%s%s", fname, initial);
|
|
|
|
g_free (fname);
|
2011-05-25 07:12:58 +02:00
|
|
|
g_free (lname);
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-03-06 11:14:26 +01:00
|
|
|
leave:
|
|
|
|
{
|
|
|
|
gchar *tmp;
|
|
|
|
tmp = cleanup_str (nick);
|
|
|
|
g_free (nick);
|
|
|
|
nick = tmp;
|
|
|
|
}
|
2011-09-12 19:38:40 +02:00
|
|
|
|
2011-03-06 11:14:26 +01:00
|
|
|
return nick;
|
|
|
|
}
|