18
18
#include <drizzled/global.h>
19
19
#include <mysys/my_sys.h>
20
20
#include <mystrings/m_string.h>
21
#include "client/my_readline.h"
21
#include "client/linebuffer.h"
23
25
using namespace std;
25
static bool init_line_buffer(LINE_BUFFER *buffer,File file,uint32_t size,
27
static bool init_line_buffer_from_string(LINE_BUFFER *buffer,char * str);
28
static size_t fill_buffer(LINE_BUFFER *buffer);
29
static char *intern_read_line(LINE_BUFFER *buffer,uint32_t *out_length);
32
LINE_BUFFER *batch_readline_init(uint32_t max_size,FILE *file)
34
LINE_BUFFER *line_buff;
35
if (!(line_buff=(LINE_BUFFER*) malloc(sizeof(*line_buff))))
37
memset(line_buff, 0, sizeof(*line_buff));
38
if (init_line_buffer(line_buff,fileno(file),IO_SIZE,max_size))
47
char *batch_readline(LINE_BUFFER *line_buff)
27
LineBuffer::LineBuffer(uint32_t my_max_size,FILE *my_file)
30
file= fileno(my_file);
34
max_size= my_max_size;
35
buffer= (char *) malloc(bufread+1);
37
throw runtime_error("malloc failed");
38
start_of_line= end_of_line= end= buffer;
44
LineBuffer::~LineBuffer()
49
void LineBuffer::add_string(const char *str)
51
uint32_t old_length= (uint32_t) (end - buffer);
52
uint32_t length= (uint32_t) strlen(str);
53
char *tmpptr= (char *) realloc(buffer,old_length+length+2);
55
throw runtime_error("malloc failed");
57
buffer= start_of_line= end_of_line= tmpptr;
58
end= buffer + old_length;
61
memcpy(end, str, length);
69
char *LineBuffer::readline()
50
72
uint32_t out_length;
52
if (!(pos=intern_read_line(line_buff,&out_length)))
74
if (!(pos=internal_readline(&out_length)))
54
76
if (out_length && pos[out_length-1] == '\n')
55
77
if (--out_length && pos[out_length-1] == '\r') /* Remove '\n' */
56
78
out_length--; /* Remove '\r' */
57
line_buff->read_length=out_length;
79
read_length=out_length;
63
void batch_readline_end(LINE_BUFFER *line_buff)
67
free(line_buff->buffer);
73
LINE_BUFFER *batch_readline_command(LINE_BUFFER *line_buff, char * str)
77
if (!(line_buff=(LINE_BUFFER*) malloc(sizeof(*line_buff))))
79
memset(line_buff, 0, sizeof(*line_buff));
81
if (init_line_buffer_from_string(line_buff,str))
90
/*****************************************************************************
91
Functions to handle buffered readings of lines from a stream
92
******************************************************************************/
95
init_line_buffer(LINE_BUFFER *buffer,File file,uint32_t size,uint32_t max_buffer)
99
buffer->max_size=max_buffer;
100
if (!(buffer->buffer = (char*) malloc(buffer->bufread+1)))
102
buffer->end_of_line=buffer->end=buffer->buffer;
103
buffer->buffer[0]=0; /* For easy start test */
108
init_line_buffer_from_string can be called on the same buffer
109
several times. the resulting buffer will contain a
110
concatenation of all strings separated by spaces
112
static bool init_line_buffer_from_string(LINE_BUFFER *buffer,char * str)
114
uint32_t old_length=(uint32_t)(buffer->end - buffer->buffer);
115
uint32_t length= (uint32_t) strlen(str);
116
char * tmpptr= (char*)realloc(buffer->buffer, old_length+length+2);
120
buffer->buffer= buffer->start_of_line= buffer->end_of_line= tmpptr;
121
buffer->end= buffer->buffer + old_length;
124
memcpy(buffer->end, str, length);
125
buffer->end[length]= '\n';
126
buffer->end[length+1]= 0;
127
buffer->end+= length+1;
135
Fill the buffer retaining the last n bytes at the beginning of the
136
newly filled buffer (for backward context). Returns the number of new
137
bytes read from disk.
140
static size_t fill_buffer(LINE_BUFFER *buffer)
84
char *LineBuffer::internal_readline(uint32_t *out_length)
89
start_of_line=end_of_line;
93
while (*pos != '\n' && *pos)
97
if ((uint32_t) (pos - start_of_line) < max_size)
99
if (!(length=fill_buffer()) || length == (size_t) -1)
103
pos--; /* break line here */
106
*out_length= (uint32_t) (pos + 1 - eof - start_of_line);
107
return(start_of_line);
111
size_t LineBuffer::fill_buffer()
142
113
size_t read_count;
143
uint32_t bufbytes= (uint32_t) (buffer->end - buffer->start_of_line);
114
uint32_t bufbytes= (uint32_t) (end - start_of_line);
146
117
return 0; /* Everything read */
148
119
/* See if we need to grow the buffer. */
152
uint32_t start_offset=(uint32_t) (buffer->start_of_line - buffer->buffer);
153
read_count=(buffer->bufread - bufbytes)/IO_SIZE;
123
uint32_t start_offset=(uint32_t) (start_of_line - buffer);
124
read_count=(bufread - bufbytes)/IO_SIZE;
154
125
if ((read_count*=IO_SIZE))
156
buffer->bufread *= 2;
157
if (!(buffer->buffer = (char*) realloc(buffer->buffer,
128
if (!(buffer = (char*) realloc(buffer, bufread+1)))
159
129
return (uint32_t) -1;
160
buffer->start_of_line=buffer->buffer+start_offset;
161
buffer->end=buffer->buffer+bufbytes;
130
start_of_line= buffer+start_offset;
131
end= buffer+bufbytes;
164
134
/* Shift stuff down. */
165
if (buffer->start_of_line != buffer->buffer)
135
if (start_of_line != buffer)
167
memmove(buffer->buffer, buffer->start_of_line, (uint32_t) bufbytes);
168
buffer->end=buffer->buffer+bufbytes;
137
memmove(buffer, start_of_line, (uint32_t) bufbytes);
138
end= buffer+bufbytes;
171
141
/* Read in new stuff. */
172
if ((read_count= my_read(buffer->file, (unsigned char*) buffer->end, read_count,
142
if ((read_count= my_read(file, (unsigned char*) end, read_count,
173
143
MYF(MY_WME))) == MY_FILE_ERROR)
174
144
return (size_t) -1;
176
146
/* Kludge to pretend every nonempty file ends with a newline. */
177
if (!read_count && bufbytes && buffer->end[-1] != '\n')
147
if (!read_count && bufbytes && end[-1] != '\n')
179
buffer->eof = read_count = 1;
182
buffer->end_of_line=(buffer->start_of_line=buffer->buffer)+bufbytes;
183
buffer->end+=read_count;
184
*buffer->end=0; /* Sentinel */
152
end_of_line= (start_of_line=buffer)+bufbytes;
154
*end= 0; /* Sentinel */
185
155
return read_count;
190
char *intern_read_line(LINE_BUFFER *buffer,uint32_t *out_length)
196
buffer->start_of_line=buffer->end_of_line;
199
pos=buffer->end_of_line;
200
while (*pos != '\n' && *pos)
202
if (pos == buffer->end)
204
if ((uint32_t) (pos - buffer->start_of_line) < buffer->max_size)
206
if (!(length=fill_buffer(buffer)) || length == (size_t) -1)
210
pos--; /* break line here */
212
buffer->end_of_line=pos+1;
213
*out_length=(uint32_t) (pos + 1 - buffer->eof - buffer->start_of_line);
214
return(buffer->start_of_line);