libdas2
das2 core C utilities
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
util.h
Go to the documentation of this file.
1 /* Copyright (C) 1997-2017 Larry Granroth <larry-granroth@uiowa.edu>
2  * Chris Piker <chris-piker@uiowa.edu>
3  * Jeremy Faden <jeremy-faden@uiowa.edu>
4  *
5  * This file is part of libdas2, the Core Das2 C Library.
6  *
7  * Libdas2 is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Lesser General Public License version 2.1 as published
9  * by the Free Software Foundation.
10  *
11  * Libdas2 is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * version 2.1 along with libdas2; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
22 #ifndef _das_util_h_
23 #define _das_util_h_
24 
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <limits.h>
31 
32 /* Get compile time byte order, results in faster code that avoids
33  * runtime checks. For some newer chips this may not work as the
34  * processor can be switched from big endian to little endian at runtime.
35  *
36  * At the end of the day either HOST_IS_LSB_FIRST will be defined, or it won't.
37  * If this macro is defined then the host computer stores the least significant
38  * byte of a word in the lowest address, i.e. it's a little endian machine. If
39  * this macro is not defined then the host computer stores the list significant
40  * byte of a word in the highest address, i.e. it a big endian machine.
41  */
42 
43 #if defined __linux || defined __APPLE__
44 
45 #if defined __linux
46 #include <endian.h>
47 #elif defined __APPLE__
48 #include <machine/endian.h>
49 #endif
50 
51 #if __BYTE_ORDER == __LITTLE_ENDIAN
52 #define HOST_IS_LSB_FIRST
53 #else
54 #undef HOST_IS_LSB_FIRST
55 #endif
56 
57 #else /* End Linux Section */
58 
59 #ifdef __sun
60 
61 #include <sys/isa_defs.h>
62 #ifdef _LITTLE_ENDIAN
63 #define HOST_IS_LSB_FIRST
64 #else
65 #undef HOST_IS_LSB_FIRST
66 #endif
67 
68 #else
69 
70 #ifdef WIN32
71 
75 #define HOST_IS_LSB_FIRST
76 
77 #else
78 
79 #error "unknown byte order!"
80 
81 #endif /* WIN32 */
82 #endif /* __sun */
83 #endif /* __linux */
84 
85 
86 #ifdef __cplusplus
87 extern "C" {
88 #endif
89 
100 #define DAS_22_STREAM_VER "2.2"
101 
102 
103 /* On Solaris systems NAME_MAX is not defined because pathconf() is supposed
104  * to be used to get the exact limit by filesystem. Since all the filesystems
105  * in common use today have support 255 characters, let's just define that
106  * to be NAME_MAX in the absence of something better.
107  */
108 #ifdef __sun
109 #ifndef NAME_MAX
110 #define NAME_MAX 255
111 #endif
112 #endif
113 
117 typedef int DasErrCode;
118 
120 #define DAS_OKAY 0
121 
123 #define DASERR_DIS_EXIT 0
124 
126 #define DASERR_DIS_RET 1
127 
129 #define DASERR_DIS_ABORT 43
130 
143 typedef void (*das_log_handler_t)(int nLevel, const char* sMsg, bool bPrnTime);
144 
198 void das_init(
199  const char* sProgName, int nErrDis, int nErrBufSz, int nLevel,
200  das_log_handler_t logfunc
201 );
202 
203 DasErrCode das_error_func(
204  const char* sFile, const char* sFunc, int nLine, DasErrCode nCode,
205  const char* sFmt, ...
206 );
207 
208 DasErrCode das_error_func_fixed(
209  const char* sFile, const char* sFunc, int nLine, DasErrCode nCode,
210  const char* sMsg
211 );
212 
213 #define DASERR_NOTIMP 8
214 #define DASERR_ASSERT 9
215 #define DASERR_INIT 11
216 #define DASERR_BUF 12
217 #define DASERR_UTIL 13
218 #define DASERR_ENC 14
219 #define DASERR_UNITS 15
220 #define DASERR_DESC 16
221 #define DASERR_PLANE 17
222 #define DASERR_PKT 18
223 #define DASERR_STREAM 19
224 #define DASERR_OOB 20
225 #define DASERR_IO 22
226 #define DASERR_DSDF 23
227 #define DASERR_DFT 24
228 #define DASERR_LOG 25
229 #define DASERR_ARRAY 26
230 #define DASERR_VAR 27
231 #define DASERR_DIM 28
232 #define DASERR_DS 29
233 #define DASERR_BLDR 30
234 #define DASERR_HTTP 31
235 #define DASERR_DATUM 32
236 #define DASERR_VALUE 33
237 #define DASERR_OP 34
238 #define DASERR_CRED 35
239 #define DASERR_NODE 36
240 
241 
291 #define das_error(nErrCode, ...) \
292  das_error_func(__FILE__, __func__, __LINE__, nErrCode, __VA_ARGS__ )
293 
294 
303 void das_abort_on_error(void);
304 
309 void das_exit_on_error(void);
310 
316 void das_return_on_error(void);
317 
324 int das_error_disposition(void);
325 
330 void das_print_error(void);
331 
341 bool das_save_error(int maxmsg);
342 
348 typedef struct das_error_message {
349  int nErr;
350  char * message;
351  size_t maxmsg;
352  char sFile[256];
353  char sFunc[64];
354  int nLine;
355 } das_error_msg;
356 
357 
366 
372 void das_error_free(das_error_msg* pMsg);
373 
376 #define das_within(A, B, E) (fabs(A - B) < E ? true : false)
377 
379 #define DAS_XML_MAXPROPS 400
380 
382 #define DAS_XML_BUF_LEN 1000000
383 
385 #define DAS_XML_NODE_NAME_LEN 256
386 
392 const char* das_lib_version( void );
393 
406 bool das_assert_valid_id(const char* sId);
407 
408 
415 void das_store_str(char** psDest, size_t* puLen, const char* sSrc);
416 
426 char* das_string(const char* fmt, ...);
427 
435 char* das_strdup(const char* sIn);
436 
437 
452 char* das_vstring(const char* fmt, va_list ap);
453 
454 
455 
460 bool das_isdir(const char* path);
461 
466 bool das_isfile(const char* path);
467 
488 int das_dirlist(
489  const char* sPath, char ppDirList[][256], size_t uMaxDirs, char cType
490 );
491 
495 #ifdef __cplusplus
496 }
497 #endif
498 
499 #endif /* _das_util_h_ */
void das_return_on_error(void)
Error handling: Normal Return Set the library to return normally to the calling function with a retur...
bool das_isdir(const char *path)
Is the path a directory.
char * das_vstring(const char *fmt, va_list ap)
Store a formatted string in a newly allocated buffer.
const char * das_lib_version(void)
Get the library version.
int DasErrCode
return code type 0 indicates success, negative integer indicates failure
Definition: util.h:117
int das_error_disposition(void)
Error handling: get the library&#39;s error disposition.
void das_error_free(das_error_msg *pMsg)
Free an error message structure allocated on the heap.
bool das_isfile(const char *path)
Is the path a file.
void das_store_str(char **psDest, size_t *puLen, const char *sSrc)
Store string in a buffer that is reallocated if need be.
void(* das_log_handler_t)(int nLevel, const char *sMsg, bool bPrnTime)
Definition of a message handler function pointer.
Definition: util.h:143
int das_dirlist(const char *sPath, char ppDirList[][256], size_t uMaxDirs, char cType)
Get a sorted directory listing.
Structure returned from das_get_error().
Definition: util.h:348
void das_init(const char *sProgName, int nErrDis, int nErrBufSz, int nLevel, das_log_handler_t logfunc)
Initialize any global structures in the Das2 library.
char * das_strdup(const char *sIn)
Copy a string into a new buffer allocated on the heap.
char * das_string(const char *fmt,...)
Allocate a new string on the heap and format it.
bool das_save_error(int maxmsg)
Error handling: Save formatted error in a message buffer.
void das_print_error(void)
Error handling: Print formatted error to standard error stream Set the library to ouput formatted err...
bool das_assert_valid_id(const char *sId)
Check that a string is suitable for use as an object ID.
void das_exit_on_error(void)
Error handling: Normal Exit Set the library to call exit(ErrorCode) when a problem is detected...
void das_abort_on_error(void)
Error handling: Trigger Core Dumps.
das_error_msg * das_get_error(void)
Return the saved das2 error message buffer.