1
/* Copyright (C) 2000 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
More functions to be used with IO_CACHE files
20
#include "mysys/mysys_priv.h"
21
#include <mystrings/m_string.h>
23
#include <mystrings/m_ctype.h>
24
#include <mysys/iocache.h>
30
Copy contents of an IO_CACHE to a file.
34
cache IO_CACHE to copy from
38
Copy the contents of the cache to the file. The cache will be
39
re-inited to a read cache and will read from the beginning of the
42
If a failure to write fully occurs, the cache is only copied
46
Make this function solid by handling partial reads from the cache
47
in a correct manner: it should be atomic.
54
my_b_copy_to_file(IO_CACHE *cache, FILE *file)
56
size_t bytes_in_cache;
58
/* Reinit the cache to read from the beginning of the cache */
59
if (reinit_io_cache(cache, READ_CACHE, 0L, false, false))
61
bytes_in_cache= my_b_bytes_in_cache(cache);
64
if (fwrite(cache->read_pos, 1, bytes_in_cache, file)
67
cache->read_pos= cache->read_end;
68
} while ((bytes_in_cache= my_b_fill(cache)));
73
my_off_t my_b_append_tell(IO_CACHE* info)
76
Prevent optimizer from putting res in a register when debugging
77
we need this to be able to see the value of res when the assert fails
79
volatile my_off_t res;
82
We need to lock the append buffer mutex to keep flush_io_cache()
83
from messing with the variables that we need in order to provide the
84
answer to the question.
86
pthread_mutex_lock(&info->append_buffer_lock);
87
res = info->end_of_file + (info->write_pos-info->append_read_pos);
88
pthread_mutex_unlock(&info->append_buffer_lock);
92
my_off_t my_b_safe_tell(IO_CACHE *info)
94
if (unlikely(info->type == SEQ_READ_APPEND))
95
return my_b_append_tell(info);
96
return my_b_tell(info);
100
Make next read happen at the given position
101
For write cache, make next write happen at the given position
104
void my_b_seek(IO_CACHE *info,my_off_t pos)
110
Verify that it is OK to do seek in the non-append
111
area in SEQ_READ_APPEND cache
112
a) see if this always works
113
b) see if there is a better way to make it work
115
if (info->type == SEQ_READ_APPEND)
116
flush_io_cache(info);
118
offset=(pos - info->pos_in_file);
120
if (info->type == READ_CACHE || info->type == SEQ_READ_APPEND)
122
/* TODO: explain why this works if pos < info->pos_in_file */
123
if ((uint64_t) offset < (uint64_t) (info->read_end - info->buffer))
125
/* The read is in the current buffer; Reuse it */
126
info->read_pos = info->buffer + offset;
131
/* Force a new read on next my_b_read */
132
info->read_pos=info->read_end=info->buffer;
135
else if (info->type == WRITE_CACHE)
137
/* If write is in current buffer, reuse it */
138
if ((uint64_t) offset <
139
(uint64_t) (info->write_end - info->write_buffer))
141
info->write_pos = info->write_buffer + offset;
144
flush_io_cache(info);
145
/* Correct buffer end so that we write in increments of IO_SIZE */
146
info->write_end=(info->write_buffer+info->buffer_length-
147
(pos & (IO_SIZE-1)));
149
info->pos_in_file=pos;
150
info->seek_not_done=1;
156
Fill buffer of the cache.
159
This assumes that you have already used all characters in the CACHE,
160
independent of the read_pos value!
163
0 On error or EOF (info->error = -1 on error)
164
# Number of characters
168
size_t my_b_fill(IO_CACHE *info)
170
my_off_t pos_in_file=(info->pos_in_file+
171
(size_t) (info->read_end - info->buffer));
172
size_t diff_length, length, max_length;
174
if (info->seek_not_done)
175
{ /* File touched, do seek */
176
if (lseek(info->file,pos_in_file,SEEK_SET) ==
182
info->seek_not_done=0;
184
diff_length=(size_t) (pos_in_file & (IO_SIZE-1));
185
max_length=(info->read_length-diff_length);
186
if (max_length >= (info->end_of_file - pos_in_file))
187
max_length= (size_t) (info->end_of_file - pos_in_file);
194
if ((length= my_read(info->file,info->buffer,max_length,
195
info->myflags)) == (size_t) -1)
200
info->read_pos=info->buffer;
201
info->read_end=info->buffer+length;
202
info->pos_in_file=pos_in_file;
208
Read a string ended by '\n' into a buffer of 'max_length' size.
209
Returns number of characters read, 0 on error.
210
last byte is set to '\0'
211
If buffer is full then to[max_length-1] will be set to \0.
214
size_t my_b_gets(IO_CACHE *info, char *to, size_t max_length)
218
max_length--; /* Save place for end \0 */
220
/* Calculate number of characters in buffer */
221
if (!(length= my_b_bytes_in_cache(info)) &&
222
!(length= my_b_fill(info)))
227
unsigned char *pos, *end;
228
if (length > max_length)
230
for (pos=info->read_pos,end=pos+length ; pos < end ;)
232
if ((*to++ = *pos++) == '\n')
236
return (size_t) (to-start);
239
if (!(max_length-=length))
241
/* Found enough charcters; Return found string */
244
return (size_t) (to-start);
246
if (!(length=my_b_fill(info)))
252
my_off_t my_b_filelength(IO_CACHE *info)
254
if (info->type == WRITE_CACHE)
255
return my_b_tell(info);
257
info->seek_not_done= 1;
258
return lseek(info->file, 0L, SEEK_END);
263
Simple printf version. Supports '%s', '%d', '%u', "%ld" and "%lu"
264
Used for logging in MySQL
265
returns number of written character, or (size_t) -1 on error
268
size_t my_b_printf(IO_CACHE *info, const char* fmt, ...)
273
result=my_b_vprintf(info, fmt, args);
279
size_t my_b_vprintf(IO_CACHE *info, const char* fmt, va_list args)
281
size_t out_length= 0;
282
int32_t minimum_width; /* as yet unimplemented */
283
int32_t minimum_width_sign;
284
uint32_t precision; /* as yet unimplemented for anything but %b */
286
basic_string<unsigned char> buffz;
289
Store the location of the beginning of a format directive, for the
290
case where we learn we shouldn't have been parsing a format string
291
at all, and we don't want to lose the flag/precision/width/size
294
const char* backtrack;
296
for (; *fmt != '\0'; fmt++)
298
/* Copy everything until '%' or end of string */
299
const char *start=fmt;
302
for (; (*fmt != '\0') && (*fmt != '%'); fmt++) ;
304
length= (size_t) (fmt - start);
306
if (my_b_write(info, (const unsigned char*) start, length))
309
if (*fmt == '\0') /* End of format */
313
By this point, *fmt must be a percent; Keep track of this location and
314
skip over the percent character.
320
is_zero_padded= false;
321
minimum_width_sign= 1;
324
/* Skip if max size is used (to be compatible with printf) */
330
minimum_width_sign= -1; fmt++; goto process_flags;
332
is_zero_padded= true; fmt++; goto process_flags;
334
/** @todo Implement "#" conversion flag. */ fmt++; goto process_flags;
336
/** @todo Implement " " conversion flag. */ fmt++; goto process_flags;
338
/** @todo Implement "+" conversion flag. */ fmt++; goto process_flags;
343
precision= (int) va_arg(args, int);
348
while (my_isdigit(&my_charset_utf8_general_ci, *fmt)) {
349
minimum_width=(minimum_width * 10) + (*fmt - '0');
353
minimum_width*= minimum_width_sign;
359
precision= (int) va_arg(args, int);
364
while (my_isdigit(&my_charset_utf8_general_ci, *fmt)) {
365
precision=(precision * 10) + (*fmt - '0');
371
if (*fmt == 's') /* String parameter */
373
register char *par = va_arg(args, char *);
374
size_t length2 = strlen(par);
375
/* TODO: implement precision */
376
out_length+= length2;
377
if (my_b_write(info, (const unsigned char*) par, length2))
380
else if (*fmt == 'b') /* Sized buffer parameter, only precision makes sense */
382
char *par = va_arg(args, char *);
383
out_length+= precision;
384
if (my_b_write(info, (const unsigned char*) par, precision))
387
else if (*fmt == 'd' || *fmt == 'u') /* Integer parameter */
393
iarg = va_arg(args, int);
395
length2= (size_t) (int10_to_str((long) iarg,buff, -10) - buff);
397
length2= (uint32_t) (int10_to_str((long) (uint32_t) iarg,buff,10)- buff);
399
/* minimum width padding */
400
if (minimum_width > length2)
402
const size_t buflen = minimum_width - length2;
404
buffz.reserve(buflen);
405
buffz.append(buflen, is_zero_padded ? '0' : ' ');
406
my_b_write(info, buffz.data(), buflen);
409
out_length+= length2;
410
if (my_b_write(info, (const unsigned char *)buff, length2))
413
else if ((*fmt == 'l' && fmt[1] == 'd') || fmt[1] == 'u')
420
iarg = va_arg(args, long);
422
length2= (size_t) (int10_to_str(iarg,buff, -10) - buff);
424
length2= (size_t) (int10_to_str(iarg,buff,10)- buff);
425
out_length+= length2;
426
if (my_b_write(info, (unsigned char*) buff, length2))
431
/* %% or unknown code */
432
if (my_b_write(info, (const unsigned char*) backtrack, (size_t) (fmt-backtrack)))
434
out_length+= fmt-backtrack;