-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinebuffer.c
More file actions
130 lines (104 loc) · 2.54 KB
/
linebuffer.c
File metadata and controls
130 lines (104 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "linebuffer.h"
LineBuffer *buf_new(int descriptor, const char *linesep) {
LineBuffer *neu = calloc(1, sizeof(LineBuffer));
neu->descriptor = descriptor;
neu->linesep = linesep;
neu->lineseplen = strlen(linesep);
neu->bytesread = 0;
neu->here = 0;
return neu;
}
void buf_dispose(LineBuffer *b) {
close(b->descriptor);
free(b);
}
void bufferStatus(LineBuffer *buf) {
printf("=> [Buffer] bytesread %d, here: %d, end: %d, buf_where(): %d\n", buf->bytesread, buf->here, buf->end, buf_where(buf));
}
int buf_readline(LineBuffer *b, char *line, int linemax){
int lesen;
int sepPos;
int oset=0;
if(b->here >= b->end) {
lesen = read(b->descriptor, b->buffer, LINEBUFFERSIZE);
if(lesen == 0){
return -1;
}
if(lesen < 0){
exit(2);
}
b->here = 0;
b->end = lesen;
b->bytesread = b->bytesread + strlen(b->buffer);
}
sepPos = strstr(b->buffer + b->here, b->linesep) - b->buffer;
if(sepPos >= 0){
strncpy(line, b->buffer + b->here, sepPos - b->here);
line[sepPos - b->here] = 0;
} else {
strncpy(line, b->buffer + b->here, b->end - b->here);
line[b->end - b->here] = 0;
oset = strlen(line);
lesen = read(b->descriptor, b->buffer, LINEBUFFERSIZE);
if(lesen == 0){
return -1;
}
if(lesen < 0){
exit(2);
}
b->here = 0;
b->end = lesen;
b->bytesread = b->bytesread + lesen;
sepPos = strstr(b->buffer + b->here, b->linesep) - b->buffer;
strncpy(line+oset, b->buffer + b->here, sepPos);
line[sepPos - b->here + oset] = 0;
}
line[linemax-1] = 0;
b->here = sepPos + b->lineseplen;
return buf_where(b) - 1;
}
int buf_where(LineBuffer *b){
return b->bytesread - b->end + b->here;
}
int buf_seek(LineBuffer *b, int seekpos){
int set;
set = lseek(b->descriptor, seekpos, SEEK_SET);
if(set < 1) {
perror("Fehler beim Setzen");
exit(3);
}
b->bytesread = seekpos;
b->here = 0;
b->end = 0;
return set;
}
/*
int main(void) {
int datei;
int readLine = 0;
LineBuffer *lbuffer = NULL;
char *befuellMich = NULL;
datei = open("joendhard.mbox", O_RDONLY, 0644);
if(datei<0) {
perror("Oeffnen fehlgeschlagen!");
exit(1);
}
lbuffer = buf_new(datei, "\n");
befuellMich = calloc(1, sizeof(char *) * 10);
bufferStatus(lbuffer);
while(readLine = buf_readline(lbuffer, befuellMich, 100) > 0) {
printf("Befuellt: %s Pos: %d\n", befuellMich, lbuffer->here);
memset(befuellMich, 0, 80);
}
buf_dispose(lbuffer);
return 0;
}
*/