/** * vimb - a webkit based vim like browser. * * Copyright (C) 2012-2013 Daniel Carl * * 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. * * 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. * * You should have received a copy of the GNU General Public License * along with this program. If not, see http://www.gnu.org/licenses/. */ #include #include "ctype.h" #include "util.h" char *util_get_config_dir(void) { char *path = g_build_filename(g_get_user_config_dir(), "vimb", NULL); util_create_dir_if_not_exists(path); return path; } char *util_get_cache_dir(void) { char *path = g_build_filename(g_get_user_cache_dir(), "vimb", NULL); util_create_dir_if_not_exists(path); return path; } const char *util_get_home_dir(void) { const char *dir = g_getenv("HOME"); if (!dir) { dir = g_get_home_dir(); } return dir; } void util_create_dir_if_not_exists(const char *dirpath) { if (!g_file_test(dirpath, G_FILE_TEST_IS_DIR)) { g_mkdir_with_parents(dirpath, 0755); } } void util_create_file_if_not_exists(const char *filename) { if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) { FILE *f = fopen(filename, "a"); fclose(f); } } /** * Retrieves the length bytes from given file. * * The memory of returned string have to be freed! */ char *util_get_file_contents(const char *filename, gsize *length) { GError *error = NULL; char *content = NULL; if (!(g_file_test(filename, G_FILE_TEST_IS_REGULAR) && g_file_get_contents(filename, &content, length, &error)) ) { fprintf(stderr, "Cannot open %s: %s\n", filename, error ? error->message : "file not found"); g_clear_error(&error); } return content; } /** * Retrieves the file content as lines. * * The result have to be freed by g_strfreev(). */ char **util_get_lines(const char *filename) { char *content = util_get_file_contents(filename, NULL); char **lines = NULL; if (content) { /* split the file content into lines */ lines = g_strsplit(content, "\n", -1); g_free(content); } return lines; } char *util_strcasestr(const char *haystack, const char *needle) { unsigned char c1, c2; int i, j; int nlen = strlen(needle); int hlen = strlen(haystack) - nlen + 1; for (i = 0; i < hlen; i++) { for (j = 0; j < nlen; j++) { c1 = haystack[i + j]; c2 = needle[j]; if (toupper(c1) != toupper(c2)) { goto next; } } return (char*)haystack + i; next: ; } return NULL; } /** * Checks if given printf style format string is valid. That means that it * contains only one defined placeholder given as char. */ gboolean util_valid_format_string(const char *format, char type, unsigned int count) { unsigned int c; for (c = 0; *format; format++) { if (*format == '%') { format++; if (*format == type) { c++; } } } return c == count; } /** * Creates a temporary file with given content. * * Upon success, and if file is non-NULL, the actual file path used is * returned in file. This string should be freed with g_free() when not * needed any longer. */ gboolean util_create_tmp_file(const char *content, char **file) { int fp; ssize_t bytes, len; fp = g_file_open_tmp(PROJECT "-XXXXXX", file, NULL); if (fp == -1) { fprintf(stderr, "Could not create temporary file %s", *file); g_free(*file); return false; } len = strlen(content); /* write content into temporary file */ bytes = write(fp, content, len); if (bytes < len) { close(fp); unlink(*file); fprintf(stderr, "Could not write temporary file %s", *file); g_free(*file); return false; } close(fp); return true; }