1 /* XQF - Quake server browser and launcher 2 * Copyright (C) 1998-2000 Roman Pozlevich <roma@botik.ru> 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 17 */ 18 19 #ifndef __UTILS_H__ 20 #define __UTILS_H__ 21 22 #include <sys/types.h> 23 #include <stdio.h> /* FILE */ 24 #include <time.h> 25 26 #include <glib.h> 27 28 29 extern short strtosh (const char *str); 30 extern unsigned short strtoush (const char *str); 31 32 extern char *strdup_strip (const char *); 33 extern char *file_in_dir (const char *, const char *); 34 extern int str_isempty (const char *); 35 extern char *expand_tilde (const char *path); 36 37 extern GList *dir_to_list (const char *dirname, char * (*filter) (const char *, const char *)); 38 39 extern GList *merge_sorted_string_lists (GList *list1, GList *list2); 40 41 extern GSList *unique_strings (GSList *strings); 42 43 // build GList from array of char* 44 extern GList* createGListfromchar(char* strings[]); 45 46 extern void on_sig (int signum, void (*func) (int signum)); 47 extern void ignore_sigpipe (void); 48 49 extern void print_dq_string (FILE *f, const char *str); 50 51 extern char *lowcasestrstr (const char *str, const char *substr); 52 53 extern int tokenize (char *str, char *token[], int max, const char *dlm); 54 extern int safe_tokenize (const char *str, char *token[], int max, const char *dlm); 55 extern int tokenize_bychar (char *str, char *token[], int max, char dlm); 56 57 extern int hostname_is_valid (const char *hostname); 58 59 extern char *find_game_dir (const char *basegamedir, const char *game, int *match_result); 60 61 extern char* resolve_path(const char* path); 62 63 /* 64 Find a server setting from the info list in 65 the server struct. The key passed will be converted to lower case. 66 */ 67 extern char* find_server_setting_for_key (char*, char**); 68 69 70 // return "false" if i == 0, "true" otherwise 71 const char* bool2str(int i); 72 73 // returns true if str is "true", false otherwise 74 int str2bool(const char* str); 75 76 77 /** \brief find executable file in $PATH. 78 * 79 * @param files colon seperated list of executables to search for 80 * @returns absolute path to first found file, must be freed manually 81 */ 82 char* find_file_in_path(const char* files); 83 84 /** \brief find executable file in $PATH. 85 * 86 * @param files colon seperated list of executables to search for 87 * @returns first found file, must be freed manually 88 */ 89 char* find_file_in_path_relative(const char* files); 90 91 /** \brief find executable file in $PATH. 92 * 93 * like find_file_in_path but take a NULL terminated list of files 94 */ 95 char* find_file_in_path_list(char** files); 96 97 /** \brief find executable file in $PATH. 98 * 99 * like find_file_in_path_relative but take a NULL terminated list of files 100 */ 101 char* find_file_in_path_list_relative(char** files); 102 103 /** sort list and remove duplicates 104 * @param list list to sort 105 * @compare_func function to use for comparing 106 * @unref_func function to call for each deleted entry 107 * @return sorted list without duplicates 108 */ 109 GSList* slist_sort_remove_dups(GSList* list, GCompareFunc compare_func, void (*unref_func)(void*)); 110 111 /** 112 * return locale's string representation of t. must be freed manually 113 */ 114 char* timet2string(const time_t* t); 115 116 /** set fd non blocking 117 @param fd the file descriptor 118 @return zero on success, -1 on failure 119 */ 120 int set_nonblock (int fd); 121 122 struct external_program_connection 123 { 124 pid_t pid; 125 int fd; 126 gint tag; // for gdkinput 127 char* buf; 128 size_t bufsize; 129 size_t pos; 130 unsigned linenr; 131 132 // contains the \0 terminated line without \n when linefunc is called 133 const char* current_line; 134 135 // function to be called when a complete line was received 136 void (*linefunc)(struct external_program_connection* conn); 137 138 // call gtk_main_quit 139 gboolean do_quit; 140 141 gpointer data; 142 143 int result; 144 }; 145 146 int start_prog_and_return_fd(char *const argv[], pid_t *pid); 147 void external_program_input_callback(struct external_program_connection* conn, int fd, GIOCondition condition); 148 149 /** enters gtk main loop */ 150 int external_program_foreach_line(char* argv[], void (*linefunc)(struct external_program_connection* conn), gpointer data); 151 152 int run_program_sync(const char* argv[]); 153 154 int run_program_sync_callback(const char* argv[], void(*child_callback)(void*), gpointer data); 155 156 /** \brief copy a file 157 * 158 * @param src source file 159 * @param dest destination file 160 * @returns NULL on success, error message on failure 161 */ 162 const char* copy_file(const char* src, const char* dest); 163 164 /** \brief read file into memory 165 * 166 * @param name file name 167 * @param size pointer to store buffer size 168 * @returns pointer to allocated data or NULL on error 169 */ 170 char* load_file_mem(const char* name, size_t* size); 171 172 void close_fds(int exclude); 173 174 #endif /* __UTILS_H__ */ 175