File: base.h

package info (click to toggle)
grace6 5.99.1%2Bdev4-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 10,492 kB
  • ctags: 13,269
  • sloc: ansic: 103,384; sh: 5,021; yacc: 617; makefile: 574; lex: 253; fortran: 56
file content (319 lines) | stat: -rw-r--r-- 9,488 bytes parent folder | download | duplicates (2)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Grace - GRaphing, Advanced Computation and Exploration of data
 * 
 * Home page: http://plasma-gate.weizmann.ac.il/Grace/
 * 
 * Copyright (c) 1996-2005 Grace Development Team
 * 
 * Maintained by Evgeny Stambulchik
 * 
 * 
 *                           All Rights Reserved
 * 
 *    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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * Base stuff - public declarations
 */

#ifndef __BASE_H_
#define __BASE_H_

#include <config.h>

/* for FILE */
#include <stdio.h>

/* for size_t */
#include <sys/types.h>

/* boolean values */
#ifndef TRUE
#  define TRUE 1
#endif
#ifndef FALSE
#  define FALSE 0
#endif

/* function return codes */
#define RETURN_SUCCESS (0)
#define RETURN_FAILURE (1)

#define NICE_FLOOR   0
#define NICE_CEIL    1
#define NICE_ROUND   2


/* FIXME! */
extern void errmsg(const char *msg);

#ifndef ADVANCED_MEMORY_HANDLERS
/* generic memory allocation & friends */
void *xmalloc(size_t size);
void *xcalloc(size_t nmemb, size_t size);
void *xrealloc(void *ptr, size_t size);
void xfree(void *ptr);
# define XCFREE(ptr) xfree(ptr); ptr = NULL

/* string (re)allocation */
char *copy_string(char *dest, const char *src);
char *concat_strings(char *dest, const char *src);
#else
# define xmalloc bad_malloc
# define xcalloc bad_calloc
# define xrealloc bad_realloc
# define xfree bad_free
# define XCFREE BAD_CFREE
# define copy_string bad_copy_string
# define concat_strings bad_concat_strings
#endif

#define AMEM_MODEL_SIMPLE   0
#define AMEM_MODEL_LIBUNDO  1

/* advanced memory allocation & friends */
typedef struct _AMem AMem;
AMem *amem_amem_new(int model);
void amem_amem_free(AMem *amem);
void *amem_malloc(AMem *amem, size_t size);
void *amem_calloc(AMem *amem, size_t nmemb, size_t size);
void *amem_realloc(AMem *amem, void *ptr, size_t size);
void amem_free(AMem *amem, void *ptr);
# define AMEM_CFREE(amem, ptr) amem_free(amem, ptr); ptr = NULL

int amem_set_undo_limit(AMem *amem, size_t max_memory);

int amem_snapshot(AMem *amem);
int amem_undo(AMem *amem);
int amem_redo(AMem *amem);
unsigned int amem_get_undo_count(AMem *amem);
unsigned int amem_get_redo_count(AMem *amem);

char *amem_strdup(AMem *amem, const char *s);
char *amem_strcpy(AMem *amem, char *dest, const char *src);


/* string comparison etc */
int strings_are_equal(const char *s1, const char *s2);
int string_is_empty(const char *s);

/* bit manipulations */
#define PADBITS(bits, pad)  (((bits)+(pad)-1)&-(pad))
int bin_dump(char *value, int i, int pad);
unsigned char reversebits(unsigned char inword);

/* file i/o */
char *grace_fgets(char *s, int size, FILE *stream);

/* dates */

/* rounding types for dates */
#define ROUND_SECOND 1
#define ROUND_MINUTE 2
#define ROUND_HOUR   3
#define ROUND_DAY    4
#define ROUND_MONTH  5

long cal_to_jul(int y, int m, int d);
void jul_to_cal(long n, int *y, int *m, int *d);
double jul_and_time_to_jul(long jul, int hour, int min, double sec);
double cal_and_time_to_jul(int y, int m, int d,
                           int hour, int min, double sec);
void jul_to_cal_and_time(double jday, int rounding,
                         int *y, int *m, int *d,
                         int *hour, int *min, int *sec);

/* misc numerics */
int sign(double a);
double nicenum(double x, int nrange, int round);
void fswap(double *x, double *y);
void iswap(int *x, int *y);
void uswap(unsigned int *x, unsigned int *y);
void sswap(char **x, char **y);
void minmax(double *x, int n, double *xmin, double *xmax, int *imin, int *imax);

/* locale */
int init_locale(void);
void set_locale_num(int flag);

/* dict3 stuff */
typedef struct {
    int key;     /* key */
    char *name;  /* name */
    char *descr; /* textual description */
} DictEntry;

typedef struct {
    DictEntry defaults;
    unsigned int asize; /* allocated size */
    unsigned int size;  /* dictionary size */
    DictEntry *entries; /* dictionary entries */
} Dictionary;

Dictionary *dict_new(void);
void dict_free(Dictionary *dict);
Dictionary *dict_new_from_array(unsigned int nentries, const DictEntry *entries,
    const DictEntry *defaults);
int dict_resize(Dictionary *dict, unsigned int size);
int dict_entry_copy(DictEntry *dest, const DictEntry *src);

#define DICT_NEW_STATIC(arr, defs) \
    dict_new_from_array(sizeof(arr)/sizeof(DictEntry), arr, defs)

int dict_get_key_by_name(const Dictionary *dict, const char *name, int *key);
int dict_get_name_by_key(const Dictionary *dict, int key, char **name);
int dict_get_descr_by_key(const Dictionary *dict, int key, char **descr);


/* Double precision arrays */
typedef struct {
    unsigned int size;
    unsigned int asize; /* allocated size */
    double *x;
    int allocated;
} DArray;

DArray *darray_new(unsigned int size);
void darray_free(DArray *da);

int darray_set_val(DArray *da, unsigned int i, double val);
int darray_set_const(DArray *da, double val);
int darray_append_val(DArray *da, double val);
int darray_get_val(const DArray *da, unsigned int i, double *val);

DArray *darray_copy(const DArray *da);
int darray_add_val(DArray *da, double val);
int darray_mul_val(DArray *da, double val);
int darray_pow(DArray *da, double y);
int darray_add(DArray *da, const DArray *da2);
int darray_sub(DArray *da, const DArray *da2);
int darray_mul(DArray *da, const DArray *da2);
int darray_div(DArray *da, const DArray *da2);
DArray *darray_slice(const DArray *da, unsigned int from, unsigned int to);
DArray *darray_concat(const DArray *da1, const DArray *da2);

int darray_has_zero(const DArray *da);
int darray_min(const DArray *da, double *val);
int darray_max(const DArray *da, double *val);
int darray_avg(const DArray *da, double *val);
int darray_std(const DArray *da, double *val);

/*
 *
 * DLL (Double Linked List) data storage
 *
 */

/* errno codes */
#define STORAGE_ENOENT  1
#define STORAGE_ENOMEM  2
#define STORAGE_ENULLP  3
#define STORAGE_EPARAM  4
#define STORAGE_EFATAL  5

/* error types */
#define STORAGE_ETYPE_DEBUG 0
#define STORAGE_ETYPE_INFO  1
#define STORAGE_ETYPE_WARN  2
#define STORAGE_ETYPE_ERROR 3
#define STORAGE_ETYPE_FATAL 4

typedef void (*Storage_data_free)(AMem *amem, void *data); 
typedef void *(*Storage_data_copy)(AMem *amem, void *data); 
typedef void (*Storage_exception_handler)(int type, const char *msg); 
typedef int (*Storage_comp_proc)(const void *d1, const void *d2, void *udata); 


typedef struct _LLNode {
    struct _LLNode *next;
    struct _LLNode *prev;
    void *data;
} LLNode;

typedef struct _Storage {
    AMem *amem;
    LLNode *start;
    LLNode *cp;
    int count;
    int ierrno;
    Storage_data_free data_free;
    Storage_data_copy data_copy;
    Storage_exception_handler exception_handler;
} Storage;

typedef int (*Storage_traverse_hook)(unsigned int step, void *data, void *udata); 

Storage *storage_new(AMem *amem,
    Storage_data_free data_free, Storage_data_copy data_copy,
    Storage_exception_handler exception_handler);
void storage_free(Storage *sto);

Storage *storage_copy(Storage *sto);

void storage_empty(Storage *sto);

int storage_count(Storage *sto);

int storage_next(Storage *sto);
int storage_prev(Storage *sto);
int storage_rewind(Storage *sto);
int storage_eod(Storage *sto);

int storage_scroll(Storage *sto, int skip, int loop);
int storage_scroll_to_id(Storage *sto, int id);
int storage_scroll_to_data(Storage *sto, const void *data);

int storage_id_exists(Storage *sto, int id);
int storage_data_exists(Storage *sto, const void *data);

int storage_add(Storage *sto, void *data);
int storage_delete(Storage *sto);

void *storage_duplicate(Storage *sto);

int storage_get_data(Storage *sto, void **datap);

int storage_push(Storage *sto, int forward);
int storage_push_id(Storage *sto, int id, int forward);

int storage_move(Storage *sto, int forward);

int storage_get_id(Storage *sto);

int storage_get_data_next(Storage *sto, void **datap);
int storage_get_data_by_id(Storage *sto, int id, void **datap);
int storage_delete_by_id(Storage *sto, int id);
int storage_delete_by_data(Storage *sto, void *data);

int storage_data_copy_by_id(Storage *sto, int id1, int id2);
int storage_data_move_by_id(Storage *sto, int id1, int id2);
int storage_data_swap(Storage *sto, int id1, int id2);

void storage_traverse(Storage *sto, Storage_traverse_hook hook, void *udata);

int storage_extract_data(Storage *sto, void *data);

int storage_sort(Storage *sto, Storage_comp_proc fcomp, void *udata);

int storage2_data_copy_by_id(Storage *sto1, int id1, Storage *sto2, int id2);
int storage2_data_move_by_id(Storage *sto1, int id1, Storage *sto2, int id2);
int storage2_data_swap(Storage *sto1, int id1, Storage *sto2, int id2);

int storage2_data_copy(Storage *sto1, Storage *sto2);
int storage2_data_move(Storage *sto1, Storage *sto2);
int storage2_data_flush(Storage *sto1, Storage *sto2);

#endif /* __BASE_H_ */