1 /** 2 * @file utils.h 3 * @brief 4 * 5 * Copyright (C) 2009 Gummi Developers 6 * All Rights reserved. 7 * 8 * Permission is hereby granted, free of charge, to any person 9 * obtaining a copy of this software and associated documentation 10 * files (the "Software"), to deal in the Software without 11 * restriction, including without limitation the rights to use, 12 * copy, modify, merge, publish, distribute, sublicense, and/or sell 13 * copies of the Software, and to permit persons to whom the 14 * Software is furnished to do so, subject to the following 15 * conditions: 16 * 17 * The above copyright notice and this permission notice shall be 18 * included in all copies or substantial portions of the Software. 19 * 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 * OTHER DEALINGS IN THE SOFTWARE. 28 */ 29 30 31 #ifndef __GUMMI_UTILS__ 32 #define __GUMMI_UTILS__ 33 34 #include <glib.h> 35 #include <gtk/gtk.h> 36 37 #ifdef WIN32 38 #define DIR_PERMS (S_IRWXU) 39 #else 40 #define DIR_PERMS (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) 41 #endif 42 43 #define TO_BOOL(X) ((X)? TRUE: FALSE) 44 #define STR_EQU(X, Y) (g_strcmp0((X), (Y)) == 0) 45 46 #define L_IS_TYPE(level, type) ((level & type) == type) 47 #define L_IS_GUI(level) (level & 0xf0) 48 #define L_INFO 0x00 /* for informative messages */ 49 #define L_WARNING 0x01 /* warnning */ 50 #define L_DEBUG 0x02 /* debug messages, only print if -d flags is used */ 51 #define L_ERROR 0x04 /* reconverable error */ 52 #define L_FATAL 0x08 /* inrecoverable error */ 53 #define L_G_INFO 0x10 /* GUI info */ 54 #define L_G_ERROR 0x20 /* recoverable error */ 55 #define L_G_FATAL 0x40 /* inrecoverable error */ 56 57 #define L_F_DEBUG slog(L_DEBUG, "%s ()\n", __func__); 58 59 /** 60 * Tuple2: 61 * @first: a gpointer that points to the first field 62 * @second: a gpointer that points to the second field 63 * 64 * General purpose Tuple with 2 fields. 65 */ 66 typedef struct _Tuple2 { 67 /*< private >*/ 68 struct _Tuple2* next; 69 70 /*< public >*/ 71 gpointer first; 72 gpointer second; 73 } Tuple2; 74 75 #define TUPLE2(x) ((Tuple2*)x) 76 77 /** 78 * Tuple2: 79 * @first: a gpointer that points to the first field 80 * @second: a gpointer that points to the second field 81 * @third: a gpointer that points to the third field 82 * 83 * General purpose Tuple with 3 fields. 84 */ 85 typedef struct _Tuple3 { 86 /*< private >*/ 87 struct _Tuple3* next; 88 89 /*< public >*/ 90 gpointer first; 91 gpointer second; 92 gpointer third; 93 } Tuple3; 94 95 #define TUPLE3(x) ((Tuple3*)x) 96 97 /** 98 * slist: 99 * @first: a gchar* that points to the key 100 * @second: a gchar* that points to the value 101 * 102 * list for storing gummi settings, snippets. 103 * Deprecated: Warning this sturct may be replaced with glist or Tuple2 in the 104 * future. 105 */ 106 typedef struct _slist { 107 /*< private >*/ 108 struct _slist* next; 109 110 /*< public >*/ 111 gchar* first; 112 gchar* second; 113 } slist; 114 115 void slog_init (gint debug); 116 gboolean in_debug_mode(); 117 void slog_set_gui_parent (GtkWindow* p); 118 void slog (gint level, const gchar *fmt, ...); 119 gint utils_yes_no_dialog (const gchar* message); 120 gint utils_save_reload_dialog (const gchar* message); 121 gboolean utils_path_exists (const gchar* path); 122 gboolean utils_uri_path_exists (const gchar* uri); 123 gboolean utils_set_file_contents (const gchar *filename, const gchar *text, 124 gssize length); 125 126 /** 127 * utils_copy_file: 128 * 129 * Returns: return TRUE if succeed 130 * 131 * Platform independent file copy operation. 132 */ 133 gboolean utils_copy_file (const gchar* source, const gchar* dest, GError** err); 134 135 /** 136 * utils_popen_r: 137 * 138 * Returns: A Tuple2 with Tuple2::first storing the exit code and 139 * Tuple2::second pointing to a newly allocated gchar* array 140 * 141 * Platform independent interface for calling popen (). 142 */ 143 Tuple2 utils_popen_r (const gchar* cmd, const gchar* chdir); 144 145 /** 146 * utils_path_to_relative: 147 * 148 * Returns: A newly allocated pointer to gchar* to the relative path, if target 149 * isn't relative to root, target is simply duplicated and returned. 150 * 151 * Transforms target to path relative to root. 152 */ 153 gchar* utils_path_to_relative (const gchar* root, const gchar* target); 154 155 /** 156 * utils_subinstr: 157 * 158 * Returns: A gboolean that states whether or the string in the first 159 * argument is a substring of the second argument. When the case_sens arg 160 * is passed as TRUE, case sensitivity of the two strings is ignored. 161 */ 162 gboolean utils_subinstr (const gchar* substr, const gchar* target, 163 gboolean case_insens); 164 165 gchar* utils_get_tmp_tmp_dir (void); /* TODO: remove when we can */ 166 167 168 gboolean utils_glist_is_member (GList *list, gchar* item); 169 170 gchar* g_substr(gchar* src, gint start, gint end); 171 172 /** 173 * slist_find: 174 * @head: the list head 175 * @term: the term to find 176 * @n: TRUE if only compare first n characters (using strncmp) 177 * @create: TRUE to create new entry for term that isn't found 178 * 179 * Returns: a pointer to the slist node 180 * 181 * Find term in slist. 182 */ 183 slist* slist_find (slist* head, const gchar* term, gboolean n, gboolean create); 184 185 slist* slist_append (slist* head, slist* node); 186 slist* slist_remove (slist* head, slist* node); 187 188 #endif /* __GUMMI_UTILS__ */ 189