1 /* util.h 2 * 3 * Copyright (C) 2008 Till Kamppeter <till.kamppeter@gmail.com> 4 * Copyright (C) 2008 Lars Karlitski (formerly Uebernickel) <lars@karlitski.net> 5 * 6 * This file is part of foomatic-rip. 7 * 8 * Foomatic-rip is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * Foomatic-rip is distributed in the hope that it will be useful, but 14 * WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with this library; if not, write to the 20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 21 * Boston, MA 02111-1307, USA. 22 */ 23 24 #ifndef util_h 25 #define util_h 26 27 #ifndef _GNU_SOURCE 28 #define _GNU_SOURCE 29 #endif 30 31 #include "config.h" 32 #include <string.h> 33 #include <stdio.h> 34 35 36 extern const char* shellescapes; 37 38 int isempty(const char *string); 39 const char * temp_dir(); 40 int prefixcmp(const char *str, const char *prefix); 41 int prefixcasecmp(const char *str, const char *prefix); 42 43 int startswith(const char *str, const char *prefix); 44 int endswith(const char *str, const char *postfix); 45 46 const char * skip_whitespace(const char *str); 47 48 void strlower(char *dest, size_t destlen, const char *src); 49 50 /* 51 * Like strncpy, but omits characters for which omit_func returns true 52 * It also assures that dest is zero terminated. 53 * Returns a pointer to the position in 'src' right after the last byte that has been copied. 54 */ 55 const char * strncpy_omit(char* dest, const char* src, size_t n, int (*omit_func)(int)); 56 57 int omit_unprintables(int c); 58 int omit_shellescapes(int c); 59 int omit_specialchars(int c); 60 int omit_whitespace(int c); 61 int omit_whitespace_newline(int c); 62 63 #ifndef HAVE_STRCASESTR 64 /* strcasestr() is not available under Solaris */ 65 char * strcasestr (const char *haystack, const char *needle); 66 #endif 67 68 /* TODO check for platforms which already have strlcpy and strlcat */ 69 70 /* Copy at most size-1 characters from src to dest 71 dest will always be \0 terminated (unless size == 0) 72 returns strlen(src) */ 73 #ifndef HAVE_STRLCPY 74 size_t strlcpy(char *dest, const char *src, size_t size); 75 #endif /* ! HAVE_STRLCPY */ 76 #ifndef HAVE_STRLCAT 77 size_t strlcat(char *dest, const char *src, size_t size); 78 #endif /* ! HAVE_STRLCAT */ 79 80 /* Replace all occurences of each of the characters in 'chars' by 'repl' */ 81 void strrepl(char *str, const char *chars, char repl); 82 83 /* Replace all occurences of each of the characters in 'chars' by 'repl', 84 but do not allow consecutive 'repl' chars */ 85 void strrepl_nodups(char *str, const char *chars, char repl); 86 87 /* clears 'str' with \0s */ 88 void strclr(char *str); 89 90 char * strnchr(const char *str, int c, size_t n); 91 92 void escapechars(char *dest, size_t size, const char *src, const char *esc_chars); 93 94 /* copies characters from 'src' to 'dest', until 'src' contains a character from 'stopchars' 95 will not copy more than 'max' chars 96 dest will be zero terminated in either case 97 returns a pointer to the position right after the last byte that has been copied 98 */ 99 const char * strncpy_tochar(char *dest, const char *src, size_t max, const char *stopchars); 100 101 /* "safe" versions of standard <cstdio> fwrite and fread that will cause the 102 * program to exit gracefully when a write/read fails */ 103 size_t fwrite_or_die(const void* ptr, size_t size, size_t count, FILE* stream); 104 size_t fread_or_die(void* ptr, size_t size, size_t count, FILE* stream); 105 106 /* 'paths' is a colon seperated list of paths (like $PATH) 107 * 'found_in' may be NULL if it is not needed */ 108 int find_in_path(const char *progname, const char *paths, char *found_in); 109 110 /* extracts the base name of 'path', i.e. only the filename, without path or extension */ 111 void file_basename(char *dest, const char *path, size_t dest_size); 112 113 /* if 'path' is relative, prepend cwd */ 114 void make_absolute_path(char *path, int len); 115 116 int is_true_string(const char *str); /* "1", "Yes", "On", "True" */ 117 int is_false_string(const char *str); /* "0", "No", "Off", "False", "None" */ 118 119 int digit(char c); /* returns 0-9 if c is a digit, otherwise -1 */ 120 121 int line_count(const char *str); 122 123 /* returns the index of the beginning of the line_number'th line in str */ 124 int line_start(const char *str, int line_number); 125 126 /* Replace hex notation for unprintable characters in PPD files 127 by the actual characters ex: "<0A>" --> chr(hex("0A")) */ 128 void unhexify(char *dest, size_t size, const char *src); 129 130 void extract_command(size_t *start, size_t *end, const char *cmdline, const char *cmd); 131 132 char ** argv_split(const char *string, const char *separators, int *cntp); 133 size_t argv_count(char **argv); 134 void argv_free(char **argv); 135 136 /* 137 * Returns non-zero if 'cmdline' calls 'cmd' in some way 138 */ 139 int contains_command(const char *cmdline, const char *cmd); 140 141 int copy_file(FILE *dest, FILE *src, const char *alreadyread, size_t alreadyread_len); 142 143 /* Dynamic string */ 144 typedef struct dstr { 145 char *data; 146 size_t len; 147 size_t alloc; 148 } dstr_t; 149 150 dstr_t * create_dstr(); 151 void free_dstr(dstr_t *ds); 152 void dstrclear(dstr_t *ds); 153 void dstrassure(dstr_t *ds, size_t alloc); 154 void dstrcpy(dstr_t *ds, const char *src); 155 void dstrncpy(dstr_t *ds, const char *src, size_t n); 156 void dstrncat(dstr_t *ds, const char *src, size_t n); 157 void dstrcpyf(dstr_t *ds, const char *src, ...); 158 void dstrcat(dstr_t *ds, const char *src); 159 void dstrcatf(dstr_t *ds, const char *src, ...); 160 void dstrputc(dstr_t *ds, int c); 161 size_t fgetdstr(dstr_t *ds, FILE *stream); /* returns number of characters read */ 162 int dstrreplace(dstr_t *ds, const char *find, const char *repl, int start); 163 void dstrprepend(dstr_t *ds, const char *str); 164 void dstrinsert(dstr_t *ds, int idx, const char *str); 165 void dstrinsertf(dstr_t *ds, int idx, const char *str, ...); 166 void dstrremove(dstr_t *ds, int idx, size_t count); 167 void dstrcatline(dstr_t *ds, const char *str); /* appends the first line from str to ds (incl. \n) */ 168 169 int dstrendswith(dstr_t *ds, const char *str); 170 void dstrfixnewlines(dstr_t *ds); 171 void dstrremovenewline(dstr_t *ds); 172 void dstrtrim(dstr_t *ds); 173 void dstrtrim_right(dstr_t *ds); 174 175 176 /* Doubly linked list of void pointers */ 177 typedef struct listitem_s { 178 void *data; 179 struct listitem_s *prev, *next; 180 } listitem_t; 181 182 typedef struct { 183 listitem_t *first, *last; 184 } list_t; 185 186 list_t * list_create(); 187 list_t * list_create_from_array(int count, void ** data); /* array values are NOT copied */ 188 void list_free(list_t *list); 189 190 size_t list_item_count(list_t *list); 191 192 list_t * list_copy(list_t *list); 193 194 void list_prepend(list_t *list, void *data); 195 void list_append(list_t *list, void *data); 196 void list_remove(list_t *list, listitem_t *item); 197 198 listitem_t * list_get(list_t *list, int idx); 199 200 201 /* Argument values may be seperated from their keys in the following ways: 202 - with whitespace (i.e. it is in the next list entry) 203 - with a '=' 204 - not at all 205 */ 206 listitem_t * arglist_find(list_t *list, const char *name); 207 listitem_t * arglist_find_prefix(list_t *list, const char *name); 208 209 char * arglist_get_value(list_t *list, const char *name); 210 char * arglist_get(list_t *list, int idx); 211 212 int arglist_remove(list_t *list, const char *name); 213 int arglist_remove_flag(list_t *list, const char *name); 214 215 #endif 216 217