#include <string.h>
#include <ctype.h>
#include <errno.h>
+#include <netdb.h>
#if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
# include <wchar.h>
return TRUE;
}
-gint get_quote_level(const gchar *str)
+gint get_quote_level(const gchar *str, const gchar *quote_chars)
{
const gchar *first_pos;
const gchar *last_pos;
gint quote_level = -1;
/* speed up line processing by only searching to the last '>' */
- if ((first_pos = strchr(str, '>')) != NULL) {
+ if ((first_pos = line_has_quote_char(str, quote_chars)) != NULL) {
/* skip a line if it contains a '<' before the initial '>' */
if (memchr(str, '<', first_pos - str) != NULL)
return -1;
- last_pos = strrchr(first_pos, '>');
+ last_pos = line_has_quote_char_last(first_pos, quote_chars);
} else
return -1;
break;
}
- if (*p == '>')
+ if (strchr(quote_chars, *p))
quote_level++;
else if (*p != '-' && !isspace(*p) && p <= last_pos) {
/* any characters are allowed except '-' and space */
- while (*p != '-' && *p != '>' && !isspace(*p) &&
- p < last_pos)
+ while (*p != '-'
+ && !strchr(quote_chars, *p)
+ && !isspace(*p)
+ && p < last_pos)
p++;
- if (*p == '>')
+ if (strchr(quote_chars, *p))
quote_level++;
else
break;
return quote_level;
}
+const gchar * line_has_quote_char(const gchar * str, const gchar *quote_chars)
+{
+ gchar * position = NULL;
+ gchar * tmp_pos = NULL;
+ int i;
+
+ if (quote_chars == NULL)
+ return FALSE;
+
+ for (i = 0; i < strlen(quote_chars); i++) {
+ tmp_pos = strchr (str, quote_chars[i]);
+ if(position == NULL
+ || (tmp_pos != NULL && position >= tmp_pos) )
+ position = tmp_pos;
+ }
+ return position;
+}
+
+const gchar * line_has_quote_char_last(const gchar * str, const gchar *quote_chars)
+{
+ gchar * position = NULL;
+ gchar * tmp_pos = NULL;
+ int i;
+
+ if (quote_chars == NULL)
+ return FALSE;
+
+ for (i = 0; i < strlen(quote_chars); i++) {
+ tmp_pos = strrchr (str, quote_chars[i]);
+ if(position == NULL
+ || (tmp_pos != NULL && position <= tmp_pos) )
+ position = tmp_pos;
+ }
+ return position;
+}
+
gchar *strstr_with_skip_quote(const gchar *haystack, const gchar *needle)
{
register guint haystack_len, needle_len;
return NULL;
}
-/* this fuction was taken from gstrfuncs.c in glib. */
+gchar *strchr_parenthesis_close(const gchar *str, gchar op, gchar cl)
+{
+ const gchar *p;
+ gchar quote_chr = '"';
+ gint in_brace;
+ gboolean in_quote = FALSE;
+
+ p = str;
+
+ if ((p = strchr_with_skip_quote(p, quote_chr, op))) {
+ p++;
+ in_brace = 1;
+ while (*p) {
+ if (*p == op && !in_quote)
+ in_brace++;
+ else if (*p == cl && !in_quote)
+ in_brace--;
+ else if (*p == quote_chr)
+ in_quote ^= TRUE;
+
+ if (in_brace == 0)
+ return (gchar *)p;
+
+ p++;
+ }
+ }
+
+ return NULL;
+}
+
+gchar **strsplit_parenthesis(const gchar *str, gchar op, gchar cl,
+ gint max_tokens)
+{
+ GSList *string_list = NULL, *slist;
+ gchar **str_array;
+ const gchar *s_op, *s_cl;
+ guint i, n = 1;
+
+ g_return_val_if_fail(str != NULL, NULL);
+
+ if (max_tokens < 1)
+ max_tokens = G_MAXINT;
+
+ s_op = strchr_with_skip_quote(str, '"', op);
+ if (!s_op) return NULL;
+ str = s_op;
+ s_cl = strchr_parenthesis_close(str, op, cl);
+ if (s_cl) {
+ do {
+ guint len;
+ gchar *new_string;
+
+ str++;
+ len = s_cl - str;
+ new_string = g_new(gchar, len + 1);
+ strncpy(new_string, str, len);
+ new_string[len] = 0;
+ string_list = g_slist_prepend(string_list, new_string);
+ n++;
+ str = s_cl + 1;
+
+ while (*str && isspace(*str)) str++;
+ if (*str != op) {
+ string_list = g_slist_prepend(string_list,
+ g_strdup(""));
+ n++;
+ s_op = strchr_with_skip_quote(str, '"', op);
+ if (!--max_tokens || !s_op) break;
+ str = s_op;
+ } else
+ s_op = str;
+ s_cl = strchr_parenthesis_close(str, op, cl);
+ } while (--max_tokens && s_cl);
+ }
+
+ str_array = g_new(gchar*, n);
+
+ i = n - 1;
+
+ str_array[i--] = NULL;
+ for (slist = string_list; slist; slist = slist->next)
+ str_array[i--] = slist->data;
+
+ g_slist_free(string_list);
+
+ return str_array;
+}
+
gchar **strsplit_with_quote(const gchar *str, const gchar *delim,
gint max_tokens)
{
return str_array;
}
+gchar *get_abbrev_newsgroup_name(const gchar *group)
+{
+ gchar *abbrev_group;
+ gchar *ap;
+ const gchar *p = group;
+
+ abbrev_group = ap = g_malloc(strlen(group) + 1);
+
+ while (*p) {
+ while (*p == '.')
+ *ap++ = *p++;
+ if (strchr(p, '.')) {
+ *ap++ = *p++;
+ while (*p != '.') p++;
+ } else {
+ strcpy(ap, p);
+ return abbrev_group;
+ }
+ }
+
+ *ap = '\0';
+ return abbrev_group;
+}
+
+gchar *trim_string(const gchar *str, gint len)
+{
+ const gchar *p = str;
+ gint mb_len;
+ gchar *new_str;
+ gint new_len = 0;
+
+ if (!str) return NULL;
+ if (strlen(str) <= len)
+ return g_strdup(str);
+
+ while (*p != '\0') {
+ mb_len = mblen(p, MB_LEN_MAX);
+ if (mb_len == 0)
+ break;
+ else if (mb_len < 0)
+ return g_strdup(str);
+ else if (new_len + mb_len > len)
+ break;
+ else
+ new_len += mb_len;
+ p += mb_len;
+ }
+
+ Xstrndup_a(new_str, str, new_len, return g_strdup(str));
+ return g_strconcat(new_str, "...", NULL);
+}
+
GList *uri_list_extract_filenames(const gchar *uri_list)
{
GList *result = NULL;
return template_dir;
}
+gchar *get_header_cache_dir(void)
+{
+ static gchar *header_dir = NULL;
+
+ if (!header_dir)
+ header_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
+ HEADER_CACHE_DIR, NULL);
+
+ return header_dir;
+}
+
gchar *get_tmp_file(void)
{
static gchar *tmp_file = NULL;
gchar *get_domain_name(void)
{
static gchar *domain_name = NULL;
+ struct hostent *myfqdn = NULL;
if (!domain_name) {
gchar buf[BUFFSIZE] = "";
if (gethostname(buf, sizeof(buf)) < 0) {
perror("gethostname");
strcpy(buf, "unknown");
- }
+ } else {
+ myfqdn = gethostbyname(buf);
+ if (myfqdn != NULL) {
+ memset(buf, '\0', strlen(buf));
+ strcpy(buf, myfqdn->h_name);
+ } else {
+ perror("gethostbyname");
+ strcpy(buf, "unknown");
+ }
+ }
domain_name = g_strdup(buf);
}
off_t size = 0;
gchar buf[BUFFSIZE];
- if ((fp = fopen(file, "r")) == NULL) {
+ if ((fp = fopen(file, "rb")) == NULL) {
FILE_OP_ERROR(file, "fopen");
return -1;
}
return FALSE;
}
+gboolean is_file_entry_exist(const gchar *file)
+{
+ struct stat s;
+
+ if (stat(file, &s) < 0) {
+ if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
gint change_dir(const gchar *dir)
{
gchar *prevdir = NULL;
gchar *dest_bak = NULL;
gboolean err = FALSE;
- if ((src_fp = fopen(src, "r")) == NULL) {
+ if ((src_fp = fopen(src, "rb")) == NULL) {
FILE_OP_ERROR(src, "fopen");
return -1;
}
}
}
- if ((dest_fp = fopen(dest, "w")) == NULL) {
+ if ((dest_fp = fopen(dest, "wb")) == NULL) {
FILE_OP_ERROR(dest, "fopen");
fclose(src_fp);
if (dest_bak) {
void set_log_file(const gchar *filename)
{
if (log_fp) return;
- log_fp = fopen(filename, "w");
+ log_fp = fopen(filename, "wb");
if (!log_fp)
FILE_OP_ERROR(filename, "fopen");
}