1
0
mirror of https://gitlab.os-k.eu/os-k-team/os-k.git synced 2023-08-25 14:03:10 +02:00
os-k/include/extras/buf.h
2019-04-21 21:18:13 +02:00

117 lines
4.4 KiB
C

//----------------------------------------------------------------------------//
// 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/>. //
//----------------------------------------------------------------------------//
#ifndef _KALEXTRAS_LOCKS_H
#include <extras/locks.h>
#endif
#ifndef _KALEXTRAS_BUF_H
#define _KALEXTRAS_BUF_H
//
// 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
};
typedef struct Buffer_t Buffer_t;
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
uchar *buf; // Beginning of buffer
uchar *rp; // Read pointer
uchar *wp; // Write pointer
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;
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);
void BLockBuf(Buffer_t *);
void BUnlockBuf(Buffer_t *);
bool BTrylockBuf(Buffer_t *);
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);
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);
#endif