20 #include <my_global.h>
24 #include "my_readline.h"
28 static bool init_line_buffer_from_string(
LINE_BUFFER *buffer,
char * str);
30 static char *intern_read_line(
LINE_BUFFER *buffer, ulong *out_length);
36 MY_STAT input_file_stat;
39 if (my_fstat(fileno(file), &input_file_stat, MYF(MY_WME)) ||
40 MY_S_ISDIR(input_file_stat.st_mode) ||
41 MY_S_ISBLK(input_file_stat.st_mode))
46 my_malloc(
sizeof(*line_buff),MYF(MY_WME | MY_ZEROFILL))))
48 if (init_line_buffer(line_buff,my_fileno(file),IO_SIZE,max_size))
57 char *batch_readline(
LINE_BUFFER *line_buff,
bool binary_mode)
62 if (!(pos=intern_read_line(line_buff, &out_length)))
64 if (out_length && pos[out_length-1] ==
'\n')
73 if (--out_length && pos[out_length-1] ==
'\r')
83 if (--out_length && !binary_mode && pos[out_length-1] ==
'\r')
88 line_buff->read_length=out_length;
90 DBUG_DUMP(
"Query: ", (
unsigned char *) pos, out_length);
99 my_free(line_buff->buffer);
109 my_malloc(
sizeof(*line_buff),MYF(MY_WME | MY_ZEROFILL))))
111 if (init_line_buffer_from_string(line_buff,str))
125 init_line_buffer(
LINE_BUFFER *buffer,File file,ulong
size,ulong max_buffer)
128 buffer->bufread=
size;
129 buffer->max_size=max_buffer;
130 if (!(buffer->buffer = (
char*) my_malloc(buffer->bufread+1,
131 MYF(MY_WME | MY_FAE))))
133 buffer->end_of_line=buffer->end=buffer->buffer;
143 static bool init_line_buffer_from_string(
LINE_BUFFER *buffer,
char * str)
145 uint old_length=(uint)(buffer->end - buffer->buffer);
146 uint length= (uint) strlen(str);
147 if (!(buffer->buffer= buffer->start_of_line= buffer->end_of_line=
148 (
char*) my_realloc((uchar*) buffer->buffer, old_length+length+2,
149 MYF(MY_FAE|MY_ALLOW_ZERO_PTR))))
151 buffer->end= buffer->buffer + old_length;
154 memcpy(buffer->end, str, length);
155 buffer->end[length]=
'\n';
156 buffer->end[length+1]= 0;
157 buffer->end+= length+1;
173 uint bufbytes= (uint) (buffer->end - buffer->start_of_line);
182 uint start_offset=(uint) (buffer->start_of_line - buffer->buffer);
183 read_count=(buffer->bufread - bufbytes)/IO_SIZE;
184 if ((read_count*=IO_SIZE))
186 if (buffer->bufread * 2 > buffer->max_size)
194 buffer->bufread *= 2;
195 if (!(buffer->buffer = (
char*) my_realloc(buffer->buffer,
197 MYF(MY_WME | MY_FAE))))
199 buffer->error= my_errno;
202 buffer->start_of_line=buffer->buffer+start_offset;
203 buffer->end=buffer->buffer+bufbytes;
207 if (buffer->start_of_line != buffer->buffer)
209 bmove(buffer->buffer,buffer->start_of_line,(uint) bufbytes);
210 buffer->end=buffer->buffer+bufbytes;
214 if ((read_count= my_read(buffer->file, (uchar*) buffer->end, read_count,
215 MYF(MY_WME))) == MY_FILE_ERROR)
217 buffer->error= my_errno;
221 DBUG_PRINT(
"fill_buff", (
"Got %lu bytes", (ulong) read_count));
227 if (bufbytes && buffer->end[-1] !=
'\n')
233 buffer->end_of_line=(buffer->start_of_line=buffer->buffer)+bufbytes;
234 buffer->end+=read_count;
240 char *intern_read_line(
LINE_BUFFER *buffer, ulong *out_length)
244 DBUG_ENTER(
"intern_read_line");
246 buffer->start_of_line=buffer->end_of_line;
249 pos=buffer->end_of_line;
250 while (*pos !=
'\n' && pos != buffer->end)
252 if (pos == buffer->end)
260 if (!(length= fill_buffer(buffer)))
265 else if (length == (
size_t) -1)
270 buffer->truncated= 1;
273 buffer->truncated= 0;
274 buffer->end_of_line=pos+1;
275 *out_length=(ulong) (pos + 1 - buffer->eof - buffer->start_of_line);
277 DBUG_DUMP(
"Query: ", (
unsigned char *) buffer->start_of_line, *out_length);
278 DBUG_RETURN(buffer->start_of_line);