20 #include "mysys_priv.h"
52 size_t bytes_in_cache;
53 DBUG_ENTER(
"my_b_copy_to_file");
56 if (reinit_io_cache(cache, READ_CACHE, 0L, FALSE, FALSE))
58 bytes_in_cache= my_b_bytes_in_cache(cache);
61 if (my_fwrite(file, cache->read_pos, bytes_in_cache,
62 MYF(MY_WME | MY_NABP)) == (
size_t) -1)
64 cache->read_pos= cache->read_end;
65 }
while ((bytes_in_cache= my_b_fill(cache)));
66 if(cache->error == -1)
72 my_off_t my_b_append_tell(
IO_CACHE* info)
79 # define dbug_volatile volatile
81 # define dbug_volatile
88 dbug_volatile my_off_t res;
104 volatile my_off_t save_pos;
105 save_pos = my_tell(info->file,MYF(0));
106 my_seek(info->file,(my_off_t)0,MY_SEEK_END,MYF(0));
110 DBUG_ASSERT(info->end_of_file - (info->append_read_pos-info->write_buffer)
111 == (res=my_tell(info->file,MYF(0))));
112 my_seek(info->file,save_pos,MY_SEEK_SET,MYF(0));
115 res = info->end_of_file + (info->write_pos-info->append_read_pos);
120 my_off_t my_b_safe_tell(
IO_CACHE *info)
122 if (unlikely(info->type == SEQ_READ_APPEND))
123 return my_b_append_tell(info);
124 return my_b_tell(info);
132 void my_b_seek(
IO_CACHE *info,my_off_t pos)
135 DBUG_ENTER(
"my_b_seek");
136 DBUG_PRINT(
"enter",(
"pos: %lu", (ulong) pos));
145 if (info->type == SEQ_READ_APPEND)
146 (void) flush_io_cache(info);
148 offset=(pos - info->pos_in_file);
150 if (info->type == READ_CACHE || info->type == SEQ_READ_APPEND)
153 if ((ulonglong) offset < (ulonglong) (info->read_end - info->buffer))
156 info->read_pos = info->buffer +
offset;
162 info->read_pos=info->read_end=info->buffer;
165 else if (info->type == WRITE_CACHE)
168 if ((ulonglong) offset <
169 (ulonglong) (info->write_end - info->write_buffer))
171 info->write_pos = info->write_buffer +
offset;
174 (void) flush_io_cache(info);
176 info->write_end=(info->write_buffer+info->buffer_length-
177 (pos & (IO_SIZE-1)));
179 info->pos_in_file=pos;
180 info->seek_not_done=1;
200 my_off_t pos_in_file=(info->pos_in_file+
201 (size_t) (info->read_end - info->buffer));
202 size_t diff_length, length, max_length;
204 if (info->seek_not_done)
206 if (my_seek(info->file,pos_in_file,MY_SEEK_SET,MYF(0)) ==
212 info->seek_not_done=0;
214 diff_length=(size_t) (pos_in_file & (IO_SIZE-1));
215 max_length=(info->read_length-diff_length);
216 if (max_length >= (info->end_of_file - pos_in_file))
217 max_length= (
size_t) (info->end_of_file - pos_in_file);
224 DBUG_EXECUTE_IF (
"simulate_my_b_fill_error",
225 {DBUG_SET(
"+d,simulate_file_read_error");});
226 if ((length= my_read(info->file,info->buffer,max_length,
227 info->myflags)) == (size_t) -1)
232 info->read_pos=info->buffer;
233 info->read_end=info->buffer+length;
234 info->pos_in_file=pos_in_file;
246 size_t my_b_gets(
IO_CACHE *info,
char *
to,
size_t max_length)
253 if (!(length= my_b_bytes_in_cache(info)) &&
254 !(length= my_b_fill(info)))
260 if (length > max_length)
262 for (pos=info->read_pos,end=pos+length ; pos < end ;)
264 if ((*to++ = *pos++) ==
'\n')
268 return (
size_t) (to-start);
271 if (!(max_length-=length))
276 return (
size_t) (to-start);
278 if (!(length=my_b_fill(info)))
284 my_off_t my_b_filelength(
IO_CACHE *info)
286 if (info->type == WRITE_CACHE)
287 return my_b_tell(info);
289 info->seek_not_done= 1;
290 return my_seek(info->file, 0L, MY_SEEK_END, MYF(0));
300 size_t my_b_printf(
IO_CACHE *info,
const char*
fmt, ...)
305 result=my_b_vprintf(info, fmt, args);
311 size_t my_b_vprintf(
IO_CACHE *info,
const char*
fmt, va_list args)
313 size_t out_length= 0;
315 uint minimum_width_sign;
317 my_bool is_zero_padded;
325 const char* backtrack;
327 for (; *fmt !=
'\0'; fmt++)
330 const char *start=
fmt;
333 for (; (*fmt !=
'\0') && (*fmt !=
'%'); fmt++) ;
335 length= (size_t) (fmt - start);
337 if (my_b_write(info, (
const uchar*) start, length))
347 DBUG_ASSERT(*fmt ==
'%');
351 is_zero_padded= FALSE;
352 minimum_width_sign= 1;
361 minimum_width_sign= -1; fmt++;
goto process_flags;
363 is_zero_padded= TRUE; fmt++;
goto process_flags;
364 case '#': fmt++;
goto process_flags;
366 case ' ': fmt++;
goto process_flags;
368 case '+': fmt++;
goto process_flags;
374 precision= (int) va_arg(args,
int);
379 while (my_isdigit(&my_charset_latin1, *fmt)) {
380 minimum_width=(minimum_width * 10) + (*fmt -
'0');
384 minimum_width*= minimum_width_sign;
390 precision= (int) va_arg(args,
int);
395 while (my_isdigit(&my_charset_latin1, *fmt)) {
396 precision=(precision * 10) + (*fmt -
'0');
404 reg2
char *par = va_arg(args,
char *);
405 size_t length2 = strlen(par);
407 out_length+= length2;
408 if (my_b_write(info, (uchar*) par, length2))
411 else if (*fmt ==
'b')
413 char *par = va_arg(args,
char *);
414 out_length+= precision;
415 if (my_b_write(info, (uchar*) par, precision))
418 else if (*fmt ==
'd' || *fmt ==
'u')
424 iarg = va_arg(args,
int);
426 length2= (size_t) (int10_to_str((
long) iarg,buff, -10) - buff);
428 length2= (uint) (int10_to_str((
long) (uint) iarg,buff,10)- buff);
431 if (minimum_width > length2)
435 buffz= my_alloca(minimum_width - length2);
437 memset(buffz,
'0', minimum_width - length2);
439 memset(buffz,
' ', minimum_width - length2);
440 if (my_b_write(info, buffz, minimum_width - length2))
448 out_length+= length2;
449 if (my_b_write(info, (uchar*) buff, length2))
452 else if ((*fmt ==
'l' && fmt[1] ==
'd') || fmt[1] ==
'u')
459 iarg = va_arg(args,
long);
461 length2= (size_t) (int10_to_str(iarg,buff, -10) - buff);
463 length2= (size_t) (int10_to_str(iarg,buff,10)- buff);
464 out_length+= length2;
465 if (my_b_write(info, (uchar*) buff, length2))
468 else if (fmt[0] ==
'l' && fmt[1] ==
'l' && fmt[2] ==
'u')
474 iarg = va_arg(args, ulonglong);
475 length2= (size_t) (longlong10_to_str(iarg, buff, 10) - buff);
476 out_length+= length2;
478 if (my_b_write(info, (uchar *) buff, length2))
484 if (my_b_write(info, (uchar*) backtrack, (
size_t) (fmt-backtrack)))
486 out_length+= fmt-backtrack;