← Back to team overview

maria-developers team mailing list archive

Re: How to copy IO_CACHE into a string?

 

Hi,

Anybody can help me to review it? Thank you!

On Mon, Feb 25, 2013 at 2:17 PM, Lixun Peng <penglixun@xxxxxxxxx> wrote:
> Hi Guys,
>
> I want to copy the contents of the IO_CACHE into a string.
> But I'm not found a function can do it, but I found
> "my_b_copy_to_file" can do the similar thing, so I implement a
> function "my_b_copy_to_string" by imitating "my_b_copy_to_file", to do
> this thing.
>
> Could you please review my code?
>
>
> char *my_b_copy_to_string(IO_CACHE *cache, size_t *bytes_in_cache)
> {
>   char *buff;
>   char *tmp_buff;
>   size_t now_size;
>   size_t inc_size;
>
>   /* Reinit the cache to read from the beginning of the cache */
>   if (reinit_io_cache(cache, READ_CACHE, 0L, FALSE, FALSE))
>     return NULL;
>
>   now_size= my_b_bytes_in_cache(cache);
>   inc_size= 0;
>   buff= (char *) my_malloc(now_size + 1, MYF(0));
>   tmp_buff= buff;
>   do
>   {
>     now_size+= inc_size;
>     if(inc_size > 0)
>     {
>       buff= (char *) my_realloc(buff, now_size + 1, MYF(0));
>       tmp_buff= buff + (now_size - inc_size);
>       memcpy(tmp_buff, cache->read_pos, inc_size);
>     }
>     else
>     {
>       memcpy(tmp_buff, cache->read_pos, now_size);
>     }
>     cache->read_pos= cache->read_end;
>   } while ((inc_size= my_b_fill(cache)));
>   buff[now_size]= '\0';
>
>   reinit_io_cache(cache, WRITE_CACHE, 0, FALSE, TRUE);
>   *bytes_in_cache= now_size;
>   return buff;
> }
>


Thanks,
Lixun

-- 
Senior MySQL Developer @ Taobao.com

Mobile Phone: +86 18658156856 (Hangzhou)
Gtalk: penglixun(at)gmail.com
Twitter: http://www.twitter.com/plinux
Blog: http://www.penglixun.com


References