/* * File: misc.c * * Copyright (C) 2000 Jorge Arellano Cid , * Jörgen Viksell * * 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 2 of the License, or * (at your option) any later version. */ #include #include #include #include #include #include #include "msg.h" #include "misc.h" /* * Prepend the users home-dir to 'file' string i.e, * pass in .dillo/bookmarks.html and it will return * /home/imain/.dillo/bookmarks.html * * Remember to g_free() returned value! */ gchar *a_Misc_prepend_user_home(const char *file) { return ( g_strconcat(g_get_home_dir(), "/", file, NULL) ); } /* * Escape characters as %XX sequences. * Return value: New string, or NULL if there's no need to escape. */ gchar *a_Misc_escape_chars(const gchar *str, gchar *esc_set) { static const char *hex = "0123456789ABCDEF"; gchar *p = NULL; GString *gstr; gint i; for (i = 0; str[i]; ++i) if (str[i] <= 0x1F || str[i] == 0x7F || strchr(esc_set, str[i])) break; if (str[i]) { /* needs escaping */ gstr = g_string_sized_new(64); for (i = 0; str[i]; ++i) { if (str[i] <= 0x1F || str[i] == 0x7F || strchr(esc_set, str[i])) { g_string_append_c(gstr, '%'); g_string_append_c(gstr, hex[(str[i] >> 4) & 15]); g_string_append_c(gstr, hex[str[i] & 15]); } else { g_string_append_c(gstr, str[i]); } } p = gstr->str; g_string_free(gstr, FALSE); } return p; } /* * Case insensitive strstr */ gchar *a_Misc_stristr(char *src, char *str) { int i, j; for (i = 0, j = 0; src[i] && str[j]; ++i) if (tolower(src[i]) == tolower(str[j])) { ++j; } else if (j) { i -= j; j = 0; } if (!str[j]) /* Got all */ return (src + i - j); return NULL; } /* * strsep implementation */ gchar *a_Misc_strsep(char **orig, const char *delim) { gchar *str, *p; if (!(str = *orig)) return NULL; p = strpbrk(str, delim); if (p) { *p++ = 0; *orig = p; } else { *orig = NULL; } return str; } #define TAB_SIZE 8 /* * Takes a string and converts any tabs to spaces. */ gchar *a_Misc_expand_tabs(const char *str) { GString *New = g_string_new(""); int len, i, j, pos, old_pos; char *val; if ( (len = strlen(str)) ) { for (pos = 0, i = 0; i < len; i++) { if (str[i] == '\t') { /* Fill with whitespaces until the next tab. */ old_pos = pos; pos += TAB_SIZE - (pos % TAB_SIZE); for (j = old_pos; j < pos; j++) g_string_append_c(New, ' '); } else { g_string_append_c(New, str[i]); pos++; } } } val = New->str; g_string_free(New, FALSE); return val; } /* * Split a string into tokens, at any character contained by delim, * and return the starting and ending positions within the string. For * n tokens, the returned array has at least 2 * n + 1 elements, and * contains the start of token i at 2 * i, the end at 2 * i + 1. The * array is terminated by -1. */ gint *a_Misc_strsplitpos(const gchar *str, const gchar *delim) { gint array_max = 4; gint *array = g_new(gint, array_max); gint n = 0; gint p1 = 0, p2; while (TRUE) { while (str[p1] != 0 && strchr(delim, str[p1]) != NULL) p1++; if (str[p1] == 0) break; p2 = p1; while (str[p2] != 0 && strchr(delim, str[p2]) == NULL) p2++; if (array_max < 2 * n + 3) { array_max <<= 2; array = g_realloc(array, array_max * sizeof(gint)); } array[2 * n] = p1; array[2 * n + 1] = p2; n++; if (str[p2] == 0) break; else { p1 = p2; } } array[2 * n] = -1; return array; } /* * Return a copy of an array which was created by a_Misc_strsplitpos. */ gint *a_Misc_strsplitposdup(gint *pos) { gint n = 0; gint *pos2; while (pos[2 * n] != -1) n++; pos2 = g_new(gint, 2 * n + 1); memcpy(pos2, pos, (2 * n + 1) * sizeof(gint)); return pos2; } /* TODO: could use dStr ADT! */ typedef struct ContentType_ { const char *str; int len; } ContentType_t; static const ContentType_t MimeTypes[] = { { "application/octet-stream", 24 }, { "text/html", 9 }, { "text/plain", 10 }, { "image/gif", 9 }, { "image/png", 9 }, { "image/jpeg", 10 }, { NULL, 0 } }; /* * Detects 'Content-Type' from a data stream sample. * * It uses the magic(5) logic from file(1). Currently, it * only checks the few mime types that Dillo supports. * * 'Data' is a pointer to the first bytes of the raw data. * * Return value: (0 on success, 1 on doubt, 2 on lack of data). */ int a_Misc_get_content_type_from_data(void *Data, size_t Size, const char **PT) { int st = 1; /* default to "doubt' */ int Type = 0; /* default to "application/octet-stream" */ char *p = Data; size_t i, non_ascci; /* HTML try */ for (i = 0; i < Size && isspace(p[i]); ++i); if ((Size - i >= 5 && !g_strncasecmp(p+i, "= 5 && !g_strncasecmp(p+i, "= 6 && !g_strncasecmp(p+i, "= 14 && !g_strncasecmp(p+i, "= 17 && !g_strncasecmp(p+i, "