📄 buffers.cpp
字号:
#include "win_and_sock.h"
#include "buffers.h"
#include <stdlib.h>
#include <memory.h>
// returns allocated size.
int alloc_rb(ringbuf * buf,int size)
{
buf->p = (BYTE *)malloc(size);
if(buf->p == NULL) // malloc failed
size = 0;
buf->ep = buf->sp = 0;
buf->wp = buf->ep;
buf->rp = buf->sp;
buf->len = size;
return buf->len;
}
// Writes to ring buffer. Returns 0 or srclen
int write_rb(ringbuf * buf, BYTE * src, int srclen)
{
int tmp1 = buf->sp - buf->wp - 1;
if(tmp1<0) // right order
{
if(tmp1 + buf->len - srclen < 0) // not enough free space
{
srclen = 0;
}
else
{
// now enough free space
size_t w = buf->len - buf->wp; // free bytes straight
if(w > srclen)
w = srclen;
// now w = part to write straight
memcpy(buf->p + buf->wp, src, w);
if(srclen-w>0)
{
memcpy(buf->p, src+w, srclen-w); // write rest
}
}
}
else // back order
{
// now tmp1 = free space
if(tmp1 - srclen < 0) // not enough free space
{
srclen = 0;
}
else
{
// can write srclen bytes straight
memcpy(buf->p + buf->wp, src, srclen);
}
}
buf->wp += srclen;
while(buf->wp>=buf->len)
buf->wp -= buf->len;
return srclen; // all written
}
// Reads from ring buffer. Returns amount of read bytes.
int read_rb(ringbuf * buf, BYTE * dest, int sizebuf)
{
int tmp1 = buf->ep-buf->rp;
if(tmp1>=0) // right order
{
// now tmp1 = total bytes
if(sizebuf>tmp1)
sizebuf = tmp1;
if(dest)
{
// can write sizebuf bytes straight
memcpy(dest, buf->p + buf->rp, sizebuf);
}
}
else // back order
{
tmp1 = buf->len + tmp1; // now tmp1 = total bytes
if(sizebuf>tmp1)
sizebuf = tmp1; // now sizebuf - how we can and shall read
size_t w = buf->len - buf->rp; // w = bytes straight
if(sizebuf<w)
w = sizebuf; // now w = part bytes to read straight
if(dest)
{
memcpy(dest, buf->p + buf->rp, w);
if(sizebuf-w > 0)
{
memcpy(dest+w, buf->p, sizebuf - w);
}
}
}
buf->rp += sizebuf;
while(buf->rp>=buf->len)
buf->rp -= buf->len;
return sizebuf;
}
// commit or undo writing based on bool predicate. return amount of written data
int wr_done_rb(ringbuf * buf, bool pred)
{
if(pred)
{
int tocommit; // amount of bytes to commit
if(buf->wp-buf->ep>=0)
tocommit = buf->wp-buf->ep;
else
tocommit = buf->len+(buf->wp-buf->ep);
wrcom_rb(buf);
return tocommit;
}
else
{
wr_undo_rb(buf);
return 0;
}
}
WORD read_rb_W(ringbuf * buf)
{
WORD res = 0;
read_rb(buf, (BYTE*)&res, 2);
return res;
}
int read_rb_I(ringbuf * buf)
{
int res = 0;
read_rb(buf, (BYTE*)&res, 4);
return res;
}
BYTE read_rb_B(ringbuf * buf)
{
BYTE res = 0;
read_rb(buf, (BYTE*)&res, 1);
return res;
}
/********* Stack Buffer *********/
// returns allocated size.
int alloc_sb(stackbuf * buf,int size)
{
buf->p = (BYTE *)malloc(size);
if(buf->p == NULL) // malloc failed
size = 0;
buf->rp = buf->wp = 0;
buf->sz = size;
return buf->sz;
}
int write_sb(stackbuf * buf, BYTE * src, int len)
{
if(buf->sz < buf->wp + len) // checking free space
return 0;
if(len<=0)
return 0;
memcpy(buf->p + buf->wp, src, len);
buf->wp += len;
return len;
}
int fill_sp(stackbuf * buf, BYTE c, int len)
{
if(buf->sz < buf->wp + len) // checking free space
return 0;
if(len<=0)
return 0;
memset(buf->p + buf->wp, c, len);
buf->wp += len;
return len;
}
// todo: implement as macros
int write_sb_at(stackbuf * buf, int atp, BYTE * src, int len)
{
if(buf->sz < buf->wp + atp + len) // checking free space
return 0;
memcpy(buf->p + buf->wp + atp, src, len);
return len;
}
// todo: implement as macros
int fill_sp_at(stackbuf * buf, int atp, BYTE c, int len)
{
if(buf->sz < buf->wp + atp + len) // checking free space
return 0;
memset(buf->p + buf->wp + atp, c, len);
return len;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -