das2C
das core C utilities (v3)
io.h
Go to the documentation of this file.
1 /* Copyright (C) 2015-2024 Chris Piker <chris-piker@uiowa.edu>
2  * Copyright (C) 2004-2006 Jeremy Faden <jeremy-faden@uiowa.edu>
3  *
4  * This file is part of das2C, the Core Das C Library.
5  *
6  * das2C is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License version 2.1 as published
8  * by the Free Software Foundation.
9  *
10  * das2C is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * version 2.1 along with das2C; if not, see <http://www.gnu.org/licenses/>.
17  */
18 
22 #ifndef _das_io_h_
23 #define _das_io_h_
24 
25 #include <stdio.h>
26 #include <zlib.h>
27 /* #include <das2/stream.h> */
28 #include <das2/oob.h>
29 #include <das2/processor.h>
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /* max number of stream processor objects */
36 #define DAS2_MAX_PROCESSORS 10
37 
38 #define DASIO_NAME_SZ 128
39 
55 typedef struct das_io_struct {
56  char rw; /* w' for write, 'r' for read, plus the tag style */
57  int model; /* Expected datastructure types in the stream */
58 
59  bool compressed; /* 1 if stream is compressed or should be compressed */
60 
61  int mode; /* STREAM_MODE_STRING, STREAM_MODE_FILE,
62  * STREAM_MODE_SOCKET, STREAM_MODE_SSL */
63 
64  char sName[DASIO_NAME_SZ]; /* A human readable name for data source or sink */
65 
66  long int offset; /* current offset for file reads */
67 
68  int dasver; /* Stream major version number, must be set explicity for output */
69 
70  /* Socket I/O */
71  int nSockFd; /* Socket file descriptor */
72 
73  /* SSL I/O */
74  void* pSsl; /* OpenSSL connection */
75 
76  /* File I/O */
77  FILE *file; /* input/output file (File I/O) */
78 
79  /* Buffer IO */
80  char *sBuffer; /* buffer for string input/output */
81  int nLength; /* length of buffer pointed to by sbuffer */
82 
83  /* Compressed I/O */
84  z_stream *zstrm; /* z_stream for inflate/deflate operations */
85  Byte *inbuf; /* input buffer */
86  Byte *outbuf; /* output buffer */
87  int zerr; /* error code for last stream operation */
88  int eof; /* set if end of input file */
89 
90  /* data object processor's with callbacks (Input / Output) */
91  StreamHandler* pProcs[DAS2_MAX_PROCESSORS+1];
92  bool bSentHeader;
93 
94  /* Sub Object Writing (output) */
95  DasBuf* pDb; /* Sub-Object serializing buffer */
96 
97  int logLevel; /* to-stream logging level. (output) */
98 
99  int taskSize; /* progress indicator max value (output) */
100  long tmLastProgMsg; /* Time the last progress message was emitted (output)*/
101 
102  OobComment cmt; /* Hold buffers for comments and logs */
103 } DasIO;
104 
135 DAS_API DasIO* new_DasIO_cfile(const char* sProg, FILE* file, const char* mode);
136 
155 DAS_API DasErrCode DasIO_model(DasIO* pThis, int nModel);
156 
157 
175 DAS_API DasIO* new_DasIO_cmd(const char* sProg, const char* sCmd);
176 
177 
178 
194 DAS_API DasIO* new_DasIO_file(const char* sProg, const char* sFile, const char* mode);
195 
212 DAS_API DasIO* new_DasIO_socket(const char* sProg, int nSockFd, const char* mode);
213 
230 DAS_API DasIO* new_DasIO_str(const char* sProg, char* sbuf, size_t len, const char* mode);
231 
262 DAS_API DasIO* new_DasIO_ssl(const char* sProg, void* pSsl, const char* mode);
263 
273 DAS_API void del_DasIO(DasIO* pThis);
274 
275 
276 
294 DAS_API int DasIO_addProcessor(DasIO* pThis, StreamHandler* pProc);
295 
309 DAS_API int DasIO_readAll(DasIO* pThis);
310 
311 
321 
330 
342 
348 
356 
357 #define LOGLVL_FINEST 0
358 #define LOGLVL_FINER 300
359 #define LOGLVL_FINE 400
360 #define LOGLVL_CONFIG 500
361 #define LOGLVL_INFO 600
362 #define LOGLVL_WARNING 700
363 #define LOGLVL_ERROR 800
364 
371 DAS_API DasErrCode DasIO_sendLog(DasIO* pThis, int level, char * msg, ... );
372 
392 DAS_API void DasIO_setLogLvl(DasIO* pThis, int minLevel);
393 
399 DAS_API int DasIO_getLogLvl(const DasIO* pThis);
400 
407 DAS_API const char* LogLvl_string(int logLevel);
408 
409 
410 /* Identifies the size of task for producing a stream, in arbitrary units.
411  *
412  * This number is used to generate a relative progress position, and also as
413  * a weight if several Das2 Streams (implicitly of similar type) are combined.
414  * See setTaskProgress().
415  *
416  * @memberof DasIO
417  */
418 DAS_API DasErrCode DasIO_setTaskSize(DasIO* pThis, int size);
419 
432 DAS_API DasErrCode DasIO_setTaskProgress( DasIO* pThis, int progress );
433 
460 DAS_API void DasIO_throwException(
461  DasIO* pThis, StreamDesc* pSd, const char* type, char* msg
462 );
463 
471 DAS_API void DasIO_close(DasIO* pThis);
472 
485 DAS_API int DasIO_serverExcept(DasIO* pThis, const char* fmt, ...);
486 
503 DAS_API int DasIO_queryExcept(DasIO* pThis, const char* fmt, ...);
504 
505 
522 DAS_API int DasIO_closeNoData(DasIO* pThis, const char* fmt, ...);
523 
533 DAS_API int DasIO_printf(DasIO* pThis, const char* format, ...);
534 
538 DAS_API size_t DasIO_write(DasIO* pThis, const char* data, int length);
539 
543 DAS_API int DasIO_read(DasIO* pThis, DasBuf* pBuf, size_t nBytes);
544 
551 DAS_API int DasIO_readUntil(
552  DasIO* pThis, DasBuf* pBuf, size_t nBytes, char cStop
553 );
554 
559 DAS_API int DasIO_getc(DasIO* pThis);
560 
561 #ifdef __cplusplus
562 }
563 #endif
564 
565 #endif /* _das_io_h_ */
int DasErrCode
return code type 0 indicates success, negative integer indicates failure
Definition: defs.h:164
DAS_API void del_DasIO(DasIO *pThis)
Free resources associated with a DasIO structure Typically you don't need to do this as heap memory i...
DAS_API int DasIO_queryExcept(DasIO *pThis, const char *fmt,...)
Throw a bad query exception and close the stream.
DAS_API const char * LogLvl_string(int logLevel)
Returns a string identifying the log level.
DAS_API int DasIO_serverExcept(DasIO *pThis, const char *fmt,...)
Throw a server exception and close the stream.
DAS_API int DasIO_closeNoData(DasIO *pThis, const char *fmt,...)
Send a "no data in interval" message and close the stream.
Defines the "Out of Band" objects in a stream.
Callback processing for das2 stream reads and writes.
#define StreamDesc
Compatability macro For backwards compatability, all functions with the name pattern DasStream are al...
Definition: stream.h:132
Buffer class to handle accumulating byte streams.
Definition: buffer.h:47
Tracks input and output operations for das2 stream headers and data.
Definition: io.h:55
DAS_API int DasIO_read(DasIO *pThis, DasBuf *pBuf, size_t nBytes)
Analog of fread (Low-level API)
DAS_API DasIO * new_DasIO_socket(const char *sProg, int nSockFd, const char *mode)
Create a new DasIO object from a socket.
DAS_API void DasIO_throwException(DasIO *pThis, StreamDesc *pSd, const char *type, char *msg)
Set the logging level.
DAS_API DasIO * new_DasIO_cfile(const char *sProg, FILE *file, const char *mode)
Create a new DasIO object from a standard C FILE.
DAS_API size_t DasIO_write(DasIO *pThis, const char *data, int length)
Anolog of fwrite (Low-level API)
DAS_API void DasIO_setLogLvl(DasIO *pThis, int minLevel)
Set the minimum log level that will be transmitted on the stream.
DAS_API DasErrCode DasIO_writeException(DasIO *pThis, OobExcept *pSe)
Output an exception structure.
DAS_API int DasIO_addProcessor(DasIO *pThis, StreamHandler *pProc)
Add a packet processor to be invoked during I/O operations.
DAS_API int DasIO_getLogLvl(const DasIO *pThis)
Get logging verbosity level.
DAS_API DasErrCode DasIO_writeStreamDesc(DasIO *pThis, StreamDesc *pSd)
Writes the data describing the stream to the output channel (e.g.
DAS_API DasErrCode DasIO_setTaskProgress(DasIO *pThis, int progress)
Place rate-limited progress comments on an output stream.
DAS_API DasErrCode DasIO_writeComment(DasIO *pThis, OobComment *pSc)
Output a StreamComment Stream comments are generally messages interpreted only by humans and may chan...
DAS_API DasErrCode DasIO_writePktDesc(DasIO *pThis, PktDesc *pd)
Writes the data describing a packet type to the output channel (e.g.
DAS_API int DasIO_getc(DasIO *pThis)
Analog of getc (Low-level API)
DAS_API DasIO * new_DasIO_ssl(const char *sProg, void *pSsl, const char *mode)
Create a new DasIO object using an encripted connection.
DAS_API DasIO * new_DasIO_file(const char *sProg, const char *sFile, const char *mode)
Create a new DasIO object from a disk file.
DAS_API DasErrCode DasIO_model(DasIO *pThis, int nModel)
Set the parsed stream data model.
DAS_API DasErrCode DasIO_writePktData(DasIO *pThis, PktDesc *pPd)
Sends the data packet on to the stream after checking validity.
DAS_API DasIO * new_DasIO_cmd(const char *sProg, const char *sCmd)
Create a new DasIO object from a shell command.
DAS_API void DasIO_close(DasIO *pThis)
Normal stream close with no unusual condiditons Closes the output file descriptor,...
DAS_API DasIO * new_DasIO_str(const char *sProg, char *sbuf, size_t len, const char *mode)
Create a new DasIO object for reading/writing to memory buffer.
DAS_API int DasIO_readUntil(DasIO *pThis, DasBuf *pBuf, size_t nBytes, char cStop)
Read until encountering a given byte (Low-level API)
DAS_API int DasIO_readAll(DasIO *pThis)
Starts the processing of the stream read from FILE* infile.
DAS_API int DasIO_printf(DasIO *pThis, const char *format,...)
Print a string with a format specifier (Low-level API) This works similar to the C printf function.
DAS_API DasErrCode DasIO_sendLog(DasIO *pThis, int level, char *msg,...)
Send a log message onto the stream at the given log level.
describes human-consumable messages that exist on the stream.
Definition: oob.h:134
describes an exception that can live in a stream.
Definition: oob.h:80
Holds information for a single packet type in a Das2 stream.
Definition: packet.h:138
A set of callbacks used for input and output stream processing.
Definition: processor.h:119