das2C
das core C utilities (v3)
util.h
Go to the documentation of this file.
1 /* Copyright (C) 1997-2020 Chris Piker <chris-piker@uiowa.edu>
2  * Larry Granroth <larry-granroth@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 <stdlib.h>
26 #include <stdio.h>
27 #include <stdarg.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 
31 #include <das2/defs.h>
32 
34 #define DASERR_DIS_EXIT 0
35 
37 #define DASERR_DIS_RET 1
38 
40 #define DASERR_DIS_ABORT 43
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
58 typedef void (*das_log_handler_t)(int nLevel, const char* sMsg, bool bPrnTime);
59 
117 DAS_API void das_init(
118  const char* sProgName, int nErrDis, int nErrBufSz, int nLevel,
119  das_log_handler_t logfunc
120 );
121 
123 DAS_API void das_finish(void);
124 
125 DasErrCode das_error_func(
126  const char* sFile, const char* sFunc, int nLine, DasErrCode nCode,
127  const char* sFmt, ...
128 );
129 
130 DasErrCode das_error_func_fixed(
131  const char* sFile, const char* sFunc, int nLine, DasErrCode nCode,
132  const char* sMsg
133 );
134 
185 #define das_error(nErrCode, ...) \
186  das_error_func(__FILE__, __func__, __LINE__, nErrCode, __VA_ARGS__ )
187 
188 
197 DAS_API void das_abort_on_error(void);
198 
203 DAS_API void das_exit_on_error(void);
204 
210 DAS_API void das_return_on_error(void);
211 
218 DAS_API int das_error_disposition(void);
219 
228 DAS_API void das_errdisp_get_lock();
229 
237 
238 
239 
243 DAS_API void das_error_setdisp(int nDisp);
244 
249 DAS_API void das_print_error(void);
250 
260 DAS_API bool das_save_error(int maxmsg);
261 
267 typedef struct das_error_message {
268  int nErr;
269  char * message;
270  size_t maxmsg;
271  char sFile[256];
272  char sFunc[64];
273  int nLine;
274 } das_error_msg;
275 
276 
285 
291 DAS_API void das_error_free(das_error_msg* pMsg);
292 
295 #define das_within(A, B, E) (fabs(A - B) < E ? true : false)
296 
298 #define DAS_XML_BUF_LEN 1000000
299 
301 #define DAS_XML_NODE_NAME_LEN 256
302 
308 DAS_API const char* das_lib_version(void);
309 
311 #define DAS_MAX_ID_BUFSZ 64
312 
325 DAS_API bool das_assert_valid_id(const char* sId);
326 
327 
334 DAS_API void das_store_str(char** psDest, size_t* puLen, const char* sSrc);
335 
345 DAS_API char* das_string(const char* fmt, ...);
346 
361 DAS_API size_t das_tokncpy(char* dest, const char* src, size_t n);
362 
363 
376 DAS_API const char* das_xml_escape(char* dest, const char* src, size_t uOutLen);
377 
385 DAS_API char* das_strdup(const char* sIn);
386 
402 DAS_API uint8_t* das_memset(
403  uint8_t* pDest, const uint8_t* pSrc, size_t uElemSz, size_t uCount
404 );
405 
406 
407 
422 DAS_API char* das_vstring(const char* fmt, va_list ap);
423 
424 
429 DAS_API bool das_isdir(const char* path);
430 
439 DAS_API DasErrCode das_mkdirsto(const char* path);
440 
441 
448 DAS_API const char* das_userhome(void);
449 
466 #ifdef _WIN32
467 DAS_API bool das_copyfile(const char* src, const char* dest);
468 #else
469 DAS_API bool das_copyfile(const char* src, const char* dest, mode_t mode);
470 #endif
471 
472 
477 DAS_API bool das_isfile(const char* path);
478 
499 DAS_API int das_dirlist(
500  const char* sPath, char ppDirList[][256], size_t uMaxDirs, char cType
501 );
502 
515 DAS_API double das_strtod_c(const char *nptr, char **endptr);
516 
520 #ifdef __cplusplus
521 }
522 #endif
523 
524 #endif /* _das_util_h_ */
Minimal definitions for das2 utilities that can safely be run without calling das_init().
int DasErrCode
return code type 0 indicates success, negative integer indicates failure
Definition: defs.h:164
Structure returned from das_get_error().
Definition: util.h:267
DAS_API das_error_msg * das_get_error(void)
Return the saved das2 error message buffer.
DAS_API void das_error_free(das_error_msg *pMsg)
Free an error message structure allocated on the heap.
DAS_API DasErrCode das_mkdirsto(const char *path)
Insure directories to a specific location exist.
void(* das_log_handler_t)(int nLevel, const char *sMsg, bool bPrnTime)
Definition of a message handler function pointer.
Definition: util.h:58
DAS_API bool das_isfile(const char *path)
Is the path a file.
DAS_API size_t das_tokncpy(char *dest, const char *src, size_t n)
Copy string as an XML token.
DAS_API uint8_t * das_memset(uint8_t *pDest, const uint8_t *pSrc, size_t uElemSz, size_t uCount)
A memset that handles multi-byte items
DAS_API int das_error_disposition(void)
Error handling: get the library's error disposition.
DAS_API void das_errdisp_release_lock()
Used for co-operative locking of time-limited error disposition changse.
DAS_API const char * das_userhome(void)
Get the home directory for the current account.
DAS_API void das_return_on_error(void)
Error handling: Normal Return Set the library to return normally to the calling function with a retur...
DAS_API char * das_string(const char *fmt,...)
Allocate a new string on the heap and format it.
DAS_API const char * das_xml_escape(char *dest, const char *src, size_t uOutLen)
Translate unsafe characters for XML string output.
DAS_API char * das_strdup(const char *sIn)
Copy a string into a new buffer allocated on the heap.
DAS_API bool das_isdir(const char *path)
Is the path a directory.
DAS_API int das_dirlist(const char *sPath, char ppDirList[][256], size_t uMaxDirs, char cType)
Get a sorted directory listing.
DAS_API void das_finish(void)
A do nothing function on Unix, closes network sockets on windows.
DAS_API bool das_assert_valid_id(const char *sId)
Check that a string is suitable for use as an object ID.
DAS_API void das_print_error(void)
Error handling: Print formatted error to standard error stream Set the library to ouput formatted err...
DAS_API void das_store_str(char **psDest, size_t *puLen, const char *sSrc)
Store string in a buffer that is reallocated if need be.
DAS_API char * das_vstring(const char *fmt, va_list ap)
Store a formatted string in a newly allocated buffer.
DAS_API void das_abort_on_error(void)
Error handling: Trigger Core Dumps.
DAS_API const char * das_lib_version(void)
Get the library version.
DAS_API 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.
DAS_API bool das_save_error(int maxmsg)
Error handling: Save formatted error in a message buffer.
DAS_API double das_strtod_c(const char *nptr, char **endptr)
A C locale string to double converter.
DAS_API void das_error_setdisp(int nDisp)
The inverse of das_error_disposition.
DAS_API bool das_copyfile(const char *src, const char *dest, mode_t mode)
Copy a file to a distination creating directories as needed.
DAS_API void das_exit_on_error(void)
Error handling: Normal Exit Set the library to call exit(ErrorCode) when a problem is detected.
DAS_API void das_errdisp_get_lock()
Used for co-operative locking of time-limited error disposition changse.