[go: up one dir, main page]

File: util.h

package info (click to toggle)
aewan 1.0.01-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 816 kB
  • sloc: ansic: 4,439; python: 122; makefile: 67; sh: 14
file content (169 lines) | stat: -rw-r--r-- 6,621 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
Copyright (c) 2003 Bruno T. C. de Oliveira

LICENSE INFORMATION:
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 2 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
Copyright (c) 2002 Bruno T. C. de Oliveira

INFORMAES DE LICENA:
Este programa  um software de livre distribuio; voc pode
redistribu-lo e/ou modific-lo sob os termos da GNU General
Public License, conforme publicado pela Free Software Foundation,
pela verso 2 da licena ou qualquer verso posterior.

Este programa  distribudo na esperana de que ele ser til
aos seus usurios, porm, SEM QUAISQUER GARANTIAS; sem sequer
a garantia implcita de COMERCIABILIDADE ou DE ADEQUAO A
QUALQUER FINALIDADE ESPECFICA. Consulte a GNU General Public
License para obter mais detalhes (uma cpia acompanha este
programa, armazenada no arquivo COPYING).
*/

#ifndef bores_util_h
#define bores_util_h
#include <sys/time.h>
#include <stdbool.h>
#include <stdio.h>

#define FMTCALL(x, f) { char *fmts = dsprintf x; f; zfree(&fmts); }

/* If (*ptr) { free(*ptr), *ptr = NULL; }. Then, makes *ptr = 
 * strdup(new_value) if new_value is not NULL. Returns the value *ptr 
 * after the assignment. */
char *dstrset(char **ptr, const char *new_value);

/* If *str { free(*str); *str = NULL; } */
void zfree(char **str);

/* If ptr, free(ptr) */
void sfree(void *ptr);

/* Does what you expect */
int minimum(int a, int b);

/* Duh. */
int maximum(int a, int b);

/* Does what you expect */
double minimum_d(double a, double b);

/* Duh. */
double maximum_d(double a, double b);

/* Allocates memory like malloc, with the following differences: (1) exits
 * program with a fatal error if memory cannot be allocated; (2) zeroes
 * out the newly allocated buffer before returning. This function returns
 * NULL if and only if bytes <= 0. */
void *zalloc(int bytes);

/* If <buf> is not null, works just like realloc. If <buf> is null,
 * calls zalloc instead of realloc. Returns a pointer to the reallocated
 * (or newly allocated) buffer; NULL if and only if newsize <= 0. */
void *srealloc(void *buf, int newsize);

/* Returns true if the passed character ch is printable */
int printable_char(int ch);

/* Returns a dynamically allocated string whose length is exactly <size>.
 * If strlen(<orig>) <= <size>, returned string is a copy of <orig>
 * padded with padch characters. If strlen(<orig>) > <size>, returned
 * string is a copy of <orig> clipped to <size>.
 *
 * Returns NULL if the fitting cannot be done because <size> is invalid */
char *str_fit_ex(const char *orig, int size, int padch);

/* Convenience function that calls
 * str_fit_ex(orig, size, ' ') */
char *str_fit(const char *orig, int size);

/* Returns difference in seconds between the two passed
 * timevals. This difference will be positive if a > b,
 * negative if a < b, or zero if a and b are the same */
float timeval_dif(const struct timeval* a, const struct timeval* b);

/* Saves the string pointed to by <str> in the file <f>, in a format
 * that can later be read back by floadstr() */
void fsavestr(const char *str, FILE *f);

/* Loads a string from file <f>, as saved by fsavestr(). Returns a dynamically
 * allocated string (which the caller must free) or NULL if there is an
 * error. */
char *floadstr(FILE *f);

/* Calculates the intersection of two integer intervals with inclusive
 * endpoints. Interval [a0..a1], where a0 <= a1, is intersected with
 * interval [b0..b1], where b0 <= b1. The resulting interval [c0..c1] is
 * stored in variables c0 and c1. An empty interval is denoted by
 * c0 > c1. */
void interval_intersect(int a0, int a1, int b0, int b1, int *c0, int *c1);

/* Same as interval_intersect, but with doubles */
void interval_intersect_d(double a0, double a1, double b0, double b1, 
                          double *c0, double *c1);

/* Does what you expect */
int minimum(int a, int b);

/* Duh. */
int maximum(int a, int b);

/* Sorts the two values v0 and v1, returning the result in variables
 * r0 and r1. Return value has the property that *r0 <= *r1 */
void sort_two(int v0, int v1, int *r0, int *r1);

/* Behaves like sprintf, but returns a dynamically allocated string
 * as large as necessary to contain the results. Uses zalloc to allocate
 * memory, so memory allocation does not fail (a fatal error is produced
 * if system has insufficient memory) */
char *dsprintf(const char *fmt, ...);

/* Reads a full line from file f, allocating space as needed to contain it.
 * Returns a dynamically allocated buffer, or NULL if EOF was read before
 * any characters could be read in. The \n will NOT be included in the
 * returned string (unlike fgets()). */
char *freadline(FILE *f);

/* Same as freadline, but in this function you specify a custom function
 * to read characters from your stream, and the file handle is opaque
 * (that is, can be anything you want, not necessarily a FILE*). 
 * When this function needs a character, it will call the readch
 * function passing it the file handle fh, and expect it to return
 * the next character on the stream or -1 if EOF is reached. */
char *freadline_ex(void *fh, int (*readch)(void*));

/* Returns a new string that results from removing leading and trailing
 * spaces from string s */
char *strtrim(const char *s);

/* Like strdup(), but aborts the program with a fatal error if memory
 * cannot be allocated (rather than return NULL as strdup() does). 
 * Also, if s is NULL, returns NULL rather than segfaulting (which is
 * what strdup() does). */
char *sstrdup(const char *s);

/* Returns a dynamically allocated string that results from the concatenation
 * of strings s and t. NULL parameters are NOT an error and will be
 * equivalent to empty strings. */
char *dstrcat(const char *s, const char *t);

/* Returns the hexadecimal representation of the given character
 * as a two-digit hexadecimal number stored in the array ret, which
 * must be of size 3. A trailing \0 is appended. The hex2chr function
 * does the reverse */
void chr2hex(int c, char* ret);
int  hex2chr(const char *hex);

#endif