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
117DAS_API void das_init(
118 const char* sProgName, int nErrDis, int nErrBufSz, int nLevel,
119 das_log_handler_t logfunc
120);
121
123DAS_API const char* das_version(void);
124
126DAS_API void das_finish(void);
127
128DAS_API DasErrCode das_error_func(
129 const char* sFile, const char* sFunc, int nLine, DasErrCode nCode,
130 const char* sFmt, ...
131) _das_fmt_check(5, 6);
132
133DAS_API DasErrCode das_error_func_fixed(
134 const char* sFile, const char* sFunc, int nLine, DasErrCode nCode,
135 const char* sMsg
136);
137
196#define das_error(nErrCode, ...) \
197 das_error_func(__FILE__, __func__, __LINE__, nErrCode, __VA_ARGS__ )
198
205#define das_error_str(nErrCode, sMsg) \
206 das_error_func_fixed(__FILE__, __func__, __LINE__, nErrCode, sMsg)
207
218#define das_error_false(nErrCode, ...) \
219 (das_error_func(__FILE__, __func__, __LINE__, nErrCode, __VA_ARGS__), false)
220
223#define das_error_null(nErrCode, ...) \
224 (das_error_func(__FILE__, __func__, __LINE__, nErrCode, __VA_ARGS__), NULL)
225
226
235DAS_API void das_abort_on_error(void);
236
241DAS_API void das_exit_on_error(void);
242
248DAS_API void das_return_on_error(void);
249
256DAS_API int das_error_disposition(void);
257
266DAS_API void das_errdisp_get_lock(void);
267
274DAS_API void das_errdisp_release_lock(void);
275
276
277
281DAS_API void das_error_setdisp(int nDisp);
282
287DAS_API void das_print_error(void);
288
298DAS_API bool das_save_error(int maxmsg);
299
305typedef struct das_error_message {
306 int nErr;
307 char * message;
308 size_t maxmsg;
309 char sFile[256];
310 char sFunc[64];
311 int nLine;
313
314
323
329DAS_API void das_error_free(das_error_msg* pMsg);
330
333#define das_within(A, B, E) (fabs((A) - (B)) < (E) ? true : false)
334
336#define DAS_XML_BUF_LEN 1000000
337
339#define DAS_XML_NODE_NAME_LEN 256
340
346DAS_API const char* das_lib_version(void);
347
349#define DAS_MAX_ID_BUFSZ 64
350
363DAS_API bool das_assert_valid_id(const char* sId);
364
365
372DAS_API void das_store_str(char** psDest, size_t* puLen, const char* sSrc);
373
383DAS_API char* das_string(const char* fmt, ...);
384
400DAS_API char* das_strip(char* sLine, char cComment);
401
416DAS_API size_t das_tokncpy(char* dest, const char* src, size_t n);
417
418
431DAS_API const char* das_xml_escape(char* dest, const char* src, size_t uOutLen);
432
440DAS_API char* das_strdup(const char* sIn);
441
457DAS_API uint8_t* das_memset(
458 uint8_t* pDest, const uint8_t* pSrc, size_t uElemSz, size_t uCount
459);
460
461
462
477DAS_API char* das_vstring(const char* fmt, va_list ap);
478
479
484DAS_API bool das_isdir(const char* path);
485
494DAS_API DasErrCode das_mkdirsto(const char* path);
495
496
503DAS_API const char* das_userhome(void);
504
521#ifdef _WIN32
522DAS_API bool das_copyfile(const char* src, const char* dest);
523#else
524DAS_API bool das_copyfile(const char* src, const char* dest, mode_t mode);
525#endif
526
527
532DAS_API bool das_isfile(const char* path);
533
554DAS_API int das_dirlist(
555 const char* sPath, char ppDirList[][256], size_t uMaxDirs, char cType
556);
557
570DAS_API double das_strtod_c(const char *nptr, char **endptr);
571
583DAS_API char* das_b64_encode(
584 const unsigned char* data, size_t input_length, size_t* output_length
585);
586
601 const char* data, size_t input_length, unsigned char* out, size_t out_cap,
602 size_t* output_length
603);
604
611DAS_API unsigned char* das_b64_decode(
612 const char* data, size_t input_length, size_t* output_length
613);
614
634bool dascmd_isArg(const char* sArg, const char* sShort, const char* sLong, bool* pLong);
635
636
667 char* sDest, size_t uDest, char** argv, int argc, int* pArgIdx,
668 const char* sShort, const char* sLong
669);
670
674#ifdef __cplusplus
675}
676#endif
677
678#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:184
Structure returned from das_get_error().
Definition util.h:305
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.
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 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_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 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 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.
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 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.