os-k/include/extras/buf.h

117 lines
4.4 KiB
C
Raw Normal View History

2019-03-24 22:51:16 +01:00
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Desc: Buffer library //
// //
// //
// Copyright © 2018-2019 The OS/K Team //
// //
// This file is part of OS/K. //
// //
// OS/K is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// any later version. //
// //
// OS/K is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY//without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
2019-04-06 07:53:58 +02:00
#ifndef _KALEXTRAS_LOCKS_H
#include <extras/locks.h>
2019-03-24 22:51:16 +01:00
#endif
2019-04-06 07:53:58 +02:00
#ifndef _KALEXTRAS_BUF_H
#define _KALEXTRAS_BUF_H
2019-03-24 22:51:16 +01:00
//
// Buffer flags
//
enum
{
BF_LINE = 1, // Line-buffered
BF_FILE = 2, // Linked to a file
BF_ALLOC = 4, // Buffer was malloc'd
BF_FONCLOSE = 8, // Don't flush on close
};
//
// Buffer states
//
enum
{
BS_CLOSED = 0, // Buffer closed (need allocation)
BS_EOF, // Buffer closed, or end of file reached
BS_ERR, // Buffer not ready, an error happened
// Buffer ready for...
BS_RDWR, // Both reading and writing
BS_RDONLY, // Only for reading
BS_WRONLY, // Only for writing
};
2019-04-06 07:53:58 +02:00
typedef struct Buffer_t Buffer_t;
2019-03-24 22:51:16 +01:00
typedef error_t (*BFlusher_t)(Buffer_t *);
struct Buffer_t
{
uint initDone;
Lock_t lock;
int flags; // Buffer flags
int state; // Buffer state
size_t size; // Current size
2019-03-25 17:33:51 +01:00
uchar *buf; // Beginning of buffer
uchar *rp; // Read pointer
uchar *wp; // Write pointer
2019-03-24 22:51:16 +01:00
BFlusher_t flusher; // Called for flushing
// The following parameters are only used in line buf mode
int lastLF; // Characters since beginning of line
int lineLen; // Line length (for line buffers)
int nLines; // Number of lines in "true" buffer
// size/(lineLen*nLines) - 1 playback buffers
};
extern Buffer_t *BStdOut, *BStdDbg;
2019-03-24 22:51:16 +01:00
error_t BCloseBuf(Buffer_t *buf);
void BFlushOnClose(Buffer_t *buf);
Buffer_t *BOpenPureBuf(char *source, size_t size);
// Creates a lined buffer of pbCount*nLines lines each
// of lineLen length (pb = playback buffer, for scrolling up)
// If source==NULL, malloc the buffer
Buffer_t *BOpenLineBuf(char *source, int mode, int lineLen,
int nLines, int pbCount, BFlusher_t flusher);
int BGetFlags(Buffer_t *);
int BGetState(Buffer_t *);
int BGetLineLen(Buffer_t *);
void BSetLineLen(Buffer_t *, int);
2019-03-24 22:51:16 +01:00
void BLockBuf(Buffer_t *);
void BUnlockBuf(Buffer_t *);
bool BTrylockBuf(Buffer_t *);
2019-03-24 22:51:16 +01:00
error_t BFlushBuf(Buffer_t *);
error_t BPutOnBuf(Buffer_t *, uchar);
error_t BPrintOnBuf(Buffer_t *, const char *fmt, ...);
error_t BPrintOnBufV(Buffer_t *, const char *fmt, va_list);
2019-03-24 22:51:16 +01:00
error_t bputc(Buffer_t *buf, uchar ch);
error_t bprintf(Buffer_t *buf, const char *fmt, ...);
error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap);
2019-03-24 22:51:16 +01:00
#endif