114 lines
4.3 KiB
C
114 lines
4.3 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 _KALKERN_BASE_H
|
||
|
#include <kernel/base.h>
|
||
|
#endif
|
||
|
|
||
|
#ifndef _KALKERN_BUF_H
|
||
|
#define _KALKERN_BUF_H
|
||
|
|
||
|
typedef struct Buffer_t Buffer_t;
|
||
|
|
||
|
//
|
||
|
// 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 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
|
||
|
|
||
|
char *buf; // Beginning of buffer
|
||
|
char *rp; // Read pointer
|
||
|
char *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, BStdErr;
|
||
|
|
||
|
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 *buf);
|
||
|
int BGetState(Buffer_t *buf);
|
||
|
int BGetLineLen(Buffer_t *buf);
|
||
|
void BSetLineLen(Buffer_t *buf, int len);
|
||
|
|
||
|
void BLockBuf(Buffer_t *buf);
|
||
|
void BUnlockBuf(Buffer_t *buf);
|
||
|
bool BTrylockBuf(Buffer_t *buf);
|
||
|
|
||
|
error_t BFlushBuf(Buffer_t *buf);
|
||
|
error_t BPutOnBuf(Buffer_t *buf, uchar ch);
|
||
|
error_t BPrintOnBuf(Buffer_t *buf, size_t n, const char *fmt, ...);
|
||
|
error_t BPrintOnBufV(Buffer_t *buf, size_t n, const char *fmt, va_list ap);
|
||
|
|
||
|
#endif
|