1 /* 2 Copyright (c) 2003 Bruno T. C. de Oliveira 3 4 LICENSE INFORMATION: 5 This program is free software; you can redistribute it and/or 6 modify it under the terms of the GNU General Public 7 License as published by the Free Software Foundation; either 8 version 2 of the License, or (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 General Public License for more details. 14 15 You should have received a copy of the GNU General Public 16 License along with this program; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 Copyright (c) 2002 Bruno T. C. de Oliveira 19 20 INFORMA��ES DE LICEN�A: 21 Este programa � um software de livre distribui��o; voc� pode 22 redistribu�-lo e/ou modific�-lo sob os termos da GNU General 23 Public License, conforme publicado pela Free Software Foundation, 24 pela vers�o 2 da licen�a ou qualquer vers�o posterior. 25 26 Este programa � distribu�do na esperan�a de que ele ser� �til 27 aos seus usu�rios, por�m, SEM QUAISQUER GARANTIAS; sem sequer 28 a garantia impl�cita de COMERCIABILIDADE ou DE ADEQUA��O A 29 QUALQUER FINALIDADE ESPEC�FICA. Consulte a GNU General Public 30 License para obter mais detalhes (uma c�pia acompanha este 31 programa, armazenada no arquivo COPYING). 32 */ 33 34 #ifndef bores_util_h 35 #define bores_util_h 36 #include <sys/time.h> 37 #include <stdbool.h> 38 #include <stdio.h> 39 40 #define FMTCALL(x, f) { char *fmts = dsprintf x; f; zfree(&fmts); } 41 42 /* If (*ptr) { free(*ptr), *ptr = NULL; }. Then, makes *ptr = 43 * strdup(new_value) if new_value is not NULL. Returns the value *ptr 44 * after the assignment. */ 45 char *dstrset(char **ptr, const char *new_value); 46 47 /* If *str { free(*str); *str = NULL; } */ 48 void zfree(char **str); 49 50 /* If ptr, free(ptr) */ 51 void sfree(void *ptr); 52 53 /* Does what you expect */ 54 int minimum(int a, int b); 55 56 /* Duh. */ 57 int maximum(int a, int b); 58 59 /* Does what you expect */ 60 double minimum_d(double a, double b); 61 62 /* Duh. */ 63 double maximum_d(double a, double b); 64 65 /* Allocates memory like malloc, with the following differences: (1) exits 66 * program with a fatal error if memory cannot be allocated; (2) zeroes 67 * out the newly allocated buffer before returning. This function returns 68 * NULL if and only if bytes <= 0. */ 69 void *zalloc(int bytes); 70 71 /* If <buf> is not null, works just like realloc. If <buf> is null, 72 * calls zalloc instead of realloc. Returns a pointer to the reallocated 73 * (or newly allocated) buffer; NULL if and only if newsize <= 0. */ 74 void *srealloc(void *buf, int newsize); 75 76 /* Returns true if the passed character ch is printable */ 77 int printable_char(int ch); 78 79 /* Returns a dynamically allocated string whose length is exactly <size>. 80 * If strlen(<orig>) <= <size>, returned string is a copy of <orig> 81 * padded with padch characters. If strlen(<orig>) > <size>, returned 82 * string is a copy of <orig> clipped to <size>. 83 * 84 * Returns NULL if the fitting cannot be done because <size> is invalid */ 85 char *str_fit_ex(const char *orig, int size, int padch); 86 87 /* Convenience function that calls 88 * str_fit_ex(orig, size, ' ') */ 89 char *str_fit(const char *orig, int size); 90 91 /* Returns difference in seconds between the two passed 92 * timevals. This difference will be positive if a > b, 93 * negative if a < b, or zero if a and b are the same */ 94 float timeval_dif(const struct timeval* a, const struct timeval* b); 95 96 /* Saves the string pointed to by <str> in the file <f>, in a format 97 * that can later be read back by floadstr() */ 98 void fsavestr(const char *str, FILE *f); 99 100 /* Loads a string from file <f>, as saved by fsavestr(). Returns a dynamically 101 * allocated string (which the caller must free) or NULL if there is an 102 * error. */ 103 char *floadstr(FILE *f); 104 105 /* Calculates the intersection of two integer intervals with inclusive 106 * endpoints. Interval [a0..a1], where a0 <= a1, is intersected with 107 * interval [b0..b1], where b0 <= b1. The resulting interval [c0..c1] is 108 * stored in variables c0 and c1. An empty interval is denoted by 109 * c0 > c1. */ 110 void interval_intersect(int a0, int a1, int b0, int b1, int *c0, int *c1); 111 112 /* Same as interval_intersect, but with doubles */ 113 void interval_intersect_d(double a0, double a1, double b0, double b1, 114 double *c0, double *c1); 115 116 /* Does what you expect */ 117 int minimum(int a, int b); 118 119 /* Duh. */ 120 int maximum(int a, int b); 121 122 /* Sorts the two values v0 and v1, returning the result in variables 123 * r0 and r1. Return value has the property that *r0 <= *r1 */ 124 void sort_two(int v0, int v1, int *r0, int *r1); 125 126 /* Behaves like sprintf, but returns a dynamically allocated string 127 * as large as necessary to contain the results. Uses zalloc to allocate 128 * memory, so memory allocation does not fail (a fatal error is produced 129 * if system has insufficient memory) */ 130 char *dsprintf(const char *fmt, ...); 131 132 /* Reads a full line from file f, allocating space as needed to contain it. 133 * Returns a dynamically allocated buffer, or NULL if EOF was read before 134 * any characters could be read in. The \n will NOT be included in the 135 * returned string (unlike fgets()). */ 136 char *freadline(FILE *f); 137 138 /* Same as freadline, but in this function you specify a custom function 139 * to read characters from your stream, and the file handle is opaque 140 * (that is, can be anything you want, not necessarily a FILE*). 141 * When this function needs a character, it will call the readch 142 * function passing it the file handle fh, and expect it to return 143 * the next character on the stream or -1 if EOF is reached. */ 144 char *freadline_ex(void *fh, int (*readch)(void*)); 145 146 /* Returns a new string that results from removing leading and trailing 147 * spaces from string s */ 148 char *strtrim(const char *s); 149 150 /* Like strdup(), but aborts the program with a fatal error if memory 151 * cannot be allocated (rather than return NULL as strdup() does). 152 * Also, if s is NULL, returns NULL rather than segfaulting (which is 153 * what strdup() does). */ 154 char *sstrdup(const char *s); 155 156 /* Returns a dynamically allocated string that results from the concatenation 157 * of strings s and t. NULL parameters are NOT an error and will be 158 * equivalent to empty strings. */ 159 char *dstrcat(const char *s, const char *t); 160 161 /* Returns the hexadecimal representation of the given character 162 * as a two-digit hexadecimal number stored in the array ret, which 163 * must be of size 3. A trailing \0 is appended. The hex2chr function 164 * does the reverse */ 165 void chr2hex(int c, char* ret); 166 int hex2chr(const char *hex); 167 168 #endif 169 170