add buffer_mmapread
parent
f6f16b116c
commit
34df761a0a
@ -0,0 +1,13 @@
|
||||
.TH buffer_close 3
|
||||
.SH NAME
|
||||
buffer_close \- close buffer
|
||||
.SH SYNTAX
|
||||
.B #include <buffer.h>
|
||||
|
||||
void \fBbuffer_close\fP(buffer* \fIb\fR);
|
||||
.SH DESCRIPTION
|
||||
buffer_close close the file associated with the buffer and frees/unmaps
|
||||
the memory associated with the buffer if buffer_init_free or
|
||||
buffer_mmapread were used.
|
||||
.SH "SEE ALSO"
|
||||
buffer_init(3), buffer_init_free(3), buffer_mmapread(3)
|
@ -0,0 +1,13 @@
|
||||
#include <buffer.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
void buffer_close(buffer* b) {
|
||||
if (b->fd != -1) close(b->fd);
|
||||
switch (b->todo) {
|
||||
case FREE: free(b->x); break;
|
||||
case MUNMAP: munmap(b->x,b->a); break;
|
||||
default: ;
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
.TH buffer_init_free 3
|
||||
.SH NAME
|
||||
buffer_init_free \- initialize buffer structure
|
||||
.SH SYNTAX
|
||||
.B #include <buffer.h>
|
||||
|
||||
int \fBbuffer_init_free\fR(buffer &\fIb\fR,
|
||||
int (*\fIop\fR)(int,char*,unsigned int),
|
||||
int \fIfd\fR, unsigned char* \fIy\fR, unsigned long int \fIylen\fR);
|
||||
.SH DESCRIPTION
|
||||
buffer_init_free is like buffer_init except that the memory (\fIy\fR is
|
||||
marked to be freed by buffer_close().
|
||||
.SH "SEE ALSO"
|
||||
buffer_init(3), buffer_mmap_read(3), buffer(3)
|
@ -0,0 +1,7 @@
|
||||
#include "buffer.h"
|
||||
|
||||
void buffer_init_free(buffer* b,int (*op)(),int fd,
|
||||
unsigned char* y,unsigned long int ylen) {
|
||||
buffer_init(b,op,fd,y,ylen);
|
||||
b->todo=FREE;
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
.TH buffer_mmapread 3
|
||||
.SH NAME
|
||||
buffer_mmapread \- create read-only memory-mapped file buffer
|
||||
.SH SYNTAX
|
||||
.B #include <buffer.h>
|
||||
|
||||
int \fBbuffer_mmapread\fR(buffer &\fIb\fR,const char* \fIfilename\fR);
|
||||
.SH DESCRIPTION
|
||||
buffer_mmapread opens \fIfilename\fR for reading and fills \fIb\fR so
|
||||
that the contents of the file can be read from it. Using mmap is more
|
||||
efficient than reading through a real buffer, but you have to call
|
||||
buffer_close to unmap the memory in the end.
|
||||
.SH EXAMPLE
|
||||
#include <buffer.h>
|
||||
#include <open.h>
|
||||
|
||||
buffer input;
|
||||
char x;
|
||||
|
||||
buffer_mmapread(&input,"/etc/passwd");
|
||||
while (buffer_get(&input,&x,1)==1) {
|
||||
buffer_put(buffer_1,&x,1);
|
||||
if (x=='\\n') break;
|
||||
}
|
||||
buffer_flush(buffer_1);
|
||||
buffer_close(&input);
|
||||
|
||||
.SH "SEE ALSO"
|
||||
buffer_flush(3), buffer(3)
|
@ -0,0 +1,15 @@
|
||||
#include <buffer.h>
|
||||
#include <mmap.h>
|
||||
|
||||
static int op() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int buffer_mmapread(buffer* b,const char* filename) {
|
||||
if (!(b->x=mmap_read(filename,&b->n))) return -1;
|
||||
b->p=0; b->a=b->n;
|
||||
b->fd=-1;
|
||||
b->op=op;
|
||||
b->todo=MUNMAP;
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
#include <buffer.h>
|
||||
|
||||
int main() {
|
||||
buffer input;
|
||||
char x;
|
||||
buffer_mmapread(&input,"/etc/passwd");
|
||||
while (buffer_get(&input,&x,1)==1) {
|
||||
buffer_put(buffer_1,&x,1);
|
||||
if (x=='\n') break;
|
||||
}
|
||||
buffer_flush(buffer_1);
|
||||
buffer_close(&input);
|
||||
}
|
Loading…
Reference in New Issue