ZEDA  1.6.18
zeda_misc.h
Go to the documentation of this file.
1 /* ZEDA - Elementary Data and Algorithms
2  * Copyright (C) 1998 Tomomichi Sugihara (Zhidao)
3  */
9 #ifndef __ZEDA_MISC_H__
10 #define __ZEDA_MISC_H__
11 
12 #include <zeda/zeda_defs.h>
13 #include <zeda/zeda_errmsg.h>
14 #include <assert.h>
15 
17 
23 #define zAssert( func, expr ) do{\
24  printf( "%s ... ", #func );\
25  assert( expr );\
26  printf( "OK\n" );\
27 } while(0)\
28 
29 
38 #define _zMax(x,y) ( (x)>=(y) ? (x) : (y) )
39 __EXPORT double zMax(double x, double y);
40 #define _zMin(x,y) ( (x)<=(y) ? (x) : (y) )
41 __EXPORT double zMin(double x, double y);
42 #define _zLimit(x,l,u) ( (x)<=(l) ? (l) : ( (x)>=(u) ? (u) : (x) ) )
43 __EXPORT double zLimit(double x, double l, double u);
44 
50 __EXPORT double zBound(double x, double b1, double b2);
51 
56 #define zSwap(type,a,b) do{\
57  type __swap_tmp;\
58  __swap_tmp = a;\
59  a = b;\
60  b = __swap_tmp;\
61 } while(0)
62 
63 /* ********************************************************** *//* ************************************************** */
66 
78 /* NOTE: now zAlloc is implemented with calloc but will be replaced
79  * by malloc for faster operations.
80  */
81 #define zAlloc(t,n) ( (n) == 0 ? NULL : (t *)calloc( (n), sizeof(t) ) )
82 #define zAllocZero(t,n) ( (n) == 0 ? NULL : (t *)calloc( (n), sizeof(t) ) )
83 
84 #define zFree(m) do{ if( (m) ){ free( m ); (m) = NULL; } } while(0)
85 
90 #define zCopy(t,s,d) ( (t *)memcpy( d, s, sizeof(t) ) )
91 
99 #ifndef __KERNEL__
100 #define zRealloc(m,t,n) (t *)realloc( (void *)m, sizeof(t)*(n) )
101 #endif /* __KERNEL__ */
102 
109 #ifndef __KERNEL__
110 __EXPORT void *zClone(void *src, size_t size);
111 #endif /* __KERNEL__ */
112 
116 #ifndef __KERNEL__
117 __EXPORT size_t zFileSize(FILE *fp);
118 #endif /* __KERNEL__ */
119 
127 #ifndef __KERNEL__
128 __EXPORT int fpeek(FILE *fp);
129 #endif /* __KERNEL__ */
130 
131 /* ********************************************************** *//* ************************************************** */
134 
142 #ifndef __KERNEL__
143 #define eprintf(fmt,...) fprintf( stderr, fmt, ##__VA_ARGS__ )
144 #else
145 #define eprintf printk
146 #endif /* __KERNEL__ */
147 
155 extern bool __zeda_echo;
157 #define zEchoOn() ( __zeda_echo = true )
158 #define zEchoOff() ( __zeda_echo = false )
159 
172 #define ZECHO(msg,...) ( __zeda_echo ? eprintf( msg " (%s).\n", ##__VA_ARGS__, __FUNCTION__ ) : 0 )
173 #define ZRUNERROR(msg,...) ZECHO( "run-time error: " msg, ##__VA_ARGS__ )
174 #define ZRUNWARN(msg,...) ZECHO( "warning: " msg, ##__VA_ARGS__ )
175 
176 #define ZOPENERROR(m) ZRUNERROR( "cannot open file: %s", (m) )
177 #define ZALLOCERROR() ZRUNERROR( "cannot allocate memory" )
178 
181 /* ********************************************************** *//* ************************************************** */
184 
191 __EXPORT int atox_c(char c);
192 
204 __EXPORT int atox(char *str);
205 
215 __EXPORT char *itoa(int val, char *buf);
216 
227 __EXPORT char *ftoa(double val, char *buf);
228 
229 #ifndef __KERNEL__
230 
249 __EXPORT char *itoa_fill(int val, int size, char pat, char *buf);
250 
257 #define itoa_zerofill(v,s,b) itoa_fill( (v), (s), '0', (b) )
258 
267 __EXPORT char *itoa_ordinal(int val, char *buf, size_t size);
268 #endif /* __KERNEL__ */
269 
273 
274 #endif /* __ZEDA_MISC_H__ */
error messages.
int fpeek(FILE *fp)
peek charactor.
char * ftoa(double val, char *buf)
convert a d-float value to a string.
double zBound(double x, double b1, double b2)
return a saturated value of x with two boundaries b1 and b2, where the magnitude relation between b1 ...
#define __END_DECLS
Definition: zeda_defs.h:30
void * zClone(void *src, size_t size)
clone a memory space.
double zMax(double x, double y)
int atox_c(char c)
convert a hexadecimal note to a value.
size_t zFileSize(FILE *fp)
count the size of a file.
char * itoa_ordinal(int val, char *buf, size_t size)
convert an integer number to a string that represents an ordinal.
#define __BEGIN_DECLS
Definition: zeda_defs.h:26
double zMin(double x, double y)
base definitions.
#define __EXPORT
Definition: zeda_compat.h:32
int atox(char *str)
convert hexadecimal string to value.
char * itoa(int val, char *buf)
convert an integer to a string.
double zLimit(double x, double l, double u)
char * itoa_fill(int val, int size, char pat, char *buf)
convert an integer to a string with a blank filled by a charactor.