das2C
das core C utilities (v3)
Loading...
Searching...
No Matches
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 das2C, the Core Das2 C Library.
6 *
7 * Das2C 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 * Das2C 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 das2C; 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 <das3/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
43extern "C" {
44#endif
45
58typedef void (*das_log_handler_t)(int nLevel, const char* sMsg, bool bPrnTime);
59
118DAS_API void das_init(
119 const char* sProgName, int nErrDis, int nErrBufSz, int nLevel,
120 das_log_handler_t logfunc
121);
122
124DAS_API const char* das_version(void);
125
127DAS_API void das_finish(void);
128
129DAS_API DasErrCode das_error_func(
130 const char* sFile, const char* sFunc, int nLine, DasErrCode nCode,
131 const char* sFmt, ...
132) _das_fmt_check(5, 6);
133
134DAS_API DasErrCode das_error_func_fixed(
135 const char* sFile, const char* sFunc, int nLine, DasErrCode nCode,
136 const char* sMsg
137);
138
195#define das_error(nErrCode, ...) \
196 das_error_func(__FILE__, __func__, __LINE__, nErrCode, __VA_ARGS__ )
197
204#define das_error_str(nErrCode, sMsg) \
205 das_error_func_fixed(__FILE__, __func__, __LINE__, nErrCode, sMsg)
206
217#define das_error_false(nErrCode, ...) \
218 (das_error_func(__FILE__, __func__, __LINE__, nErrCode, __VA_ARGS__), false)
219
222#define das_error_null(nErrCode, ...) \
223 (das_error_func(__FILE__, __func__, __LINE__, nErrCode, __VA_ARGS__), NULL)
224
225
234DAS_API void das_abort_on_error(void);
235
240DAS_API void das_exit_on_error(void);
241
247DAS_API void das_return_on_error(void);
248
255DAS_API int das_error_disposition(void);
256
265DAS_API void das_errdisp_get_lock(void);
266
273DAS_API void das_errdisp_release_lock(void);
274
275
276
280DAS_API void das_error_setdisp(int nDisp);
281
286DAS_API void das_print_error(void);
287
297DAS_API bool das_save_error(int maxmsg);
298
304typedef struct das_error_message {
305 int nErr;
306 char * message;
307 size_t maxmsg;
308 char sFile[256];
309 char sFunc[64];
310 int nLine;
312
313
322
328DAS_API void das_error_free(das_error_msg* pMsg);
329
332#define das_within(A, B, E) (fabs((A) - (B)) < (E) ? true : false)
333
335#define DAS_XML_BUF_LEN 1000000
336
338#define DAS_XML_NODE_NAME_LEN 256
339
345DAS_API const char* das_lib_version(void);
346
348#define DAS_MAX_ID_BUFSZ 64
349
362DAS_API bool das_assert_valid_id(const char* sId);
363
364
371DAS_API void das_store_str(char** psDest, size_t* puLen, const char* sSrc);
372
382DAS_API char* das_string(const char* fmt, ...);
383
399DAS_API char* das_strip(char* sLine, char cComment);
400
415DAS_API size_t das_tokncpy(char* dest, const char* src, size_t n);
416
417
432DAS_API const char* das_xml_escape(char* dest, const char* src, size_t uOutLen);
433
441DAS_API char* das_strdup(const char* sIn);
442
458DAS_API uint8_t* das_memset(
459 uint8_t* pDest, const uint8_t* pSrc, size_t uElemSz, size_t uCount
460);
461
462
463
478DAS_API char* das_vstring(const char* fmt, va_list ap);
479
480
485DAS_API bool das_isdir(const char* path);
486
495DAS_API DasErrCode das_mkdirsto(const char* path);
496
497
504DAS_API const char* das_userhome(void);
505
522#ifdef _WIN32
523DAS_API bool das_copyfile(const char* src, const char* dest);
524#else
525DAS_API bool das_copyfile(const char* src, const char* dest, mode_t mode);
526#endif
527
528
533DAS_API bool das_isfile(const char* path);
534
545DAS_API void das_stdio_binary(FILE* pFile);
546
567DAS_API int das_dirlist(
568 const char* sPath, char ppDirList[][256], size_t uMaxDirs, char cType
569);
570
583DAS_API double das_strtod_c(const char *nptr, char **endptr);
584
596DAS_API char* das_b64_encode(
597 const unsigned char* data, size_t input_length, size_t* output_length
598);
599
614 const char* data, size_t input_length, unsigned char* out, size_t out_cap,
615 size_t* output_length
616);
617
624DAS_API unsigned char* das_b64_decode(
625 const char* data, size_t input_length, size_t* output_length
626);
627
647DAS_API bool dascmd_isArg(const char* sArg, const char* sShort, const char* sLong, bool* pLong);
648
649
679DAS_API bool dascmd_getArgVal(
680 char* sDest, size_t uDest, char** argv, int argc, int* pArgIdx,
681 const char* sShort, const char* sLong
682);
683
687#ifdef __cplusplus
688}
689#endif
690
691#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:189
Structure returned from das_get_error().
Definition util.h:304
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 const char * das_xml_escape(char *dest, const char *src, size_t uOutLen)
Translate unsafe characters for XML string output.
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 void das_stdio_binary(FILE *pFile)
Put a stdio stream in binary mode.
DAS_API void das_errdisp_get_lock(void)
Used for co-operative locking of time-limited error disposition changes.
DAS_API int das_error_disposition(void)
Error handling: get the library's error disposition.
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 void das_errdisp_release_lock(void)
Used for co-operative locking of time-limited error disposition changes.
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 char * das_string(const char *fmt,...)
Allocate a new string on the heap and format it.
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 output formatted er...
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 bool dascmd_getArgVal(char *sDest, size_t uDest, char **argv, int argc, int *pArgIdx, const char *sShort, const char *sLong)
Get the value of an argument if the current entry in argv matches either the short or long form of an...
DAS_API void das_abort_on_error(void)
Error handling: Trigger Core Dumps.
DAS_API const char * das_userhome(void)
Get the home directory for the current account.
DAS_API char * das_b64_encode(const unsigned char *data, size_t input_length, size_t *output_length)
Encode binary data as base64 (RFC 4648) characters in a new buffer.
DAS_API const char * das_version(void)
Return the version of this library.
DAS_API bool dascmd_isArg(const char *sArg, const char *sShort, const char *sLong, bool *pLong)
See a given string matches the short or long form of an option.
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 const char * das_lib_version(void)
Get the library version.
DAS_API void das_error_setdisp(int nDisp)
The inverse of das_error_disposition.
DAS_API unsigned char * das_b64_decode(const char *data, size_t input_length, size_t *output_length)
Decode base64 (RFC 4648) into a newly allocated buffer, symmetric with das_b64_encode.
DAS_API char * das_vstring(const char *fmt, va_list ap)
Store a formatted string in a newly allocated buffer.
DAS_API bool das_copyfile(const char *src, const char *dest, mode_t mode)
Copy a file to a destination creating directories as needed.
DAS_API char * das_strip(char *sLine, char cComment)
Strip whitespace from a string.
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 int das_b64_decode_buf(const char *data, size_t input_length, unsigned char *out, size_t out_cap, size_t *output_length)
Decode base64 (RFC 4648) into a caller-provided buffer, no allocation.