uint my_b_gets(IO_CACHE *info, char *to, uint max_length) { char *start = to; uint length; max_length--; /* Save place for end \0 */ /* Calculate number of characters in buffer */ if (!(length= my_b_bytes_in_cache(info)) && !(length= my_b_fill(info))) return 0; for (;;) { char *pos,*end; if (length > max_length) length=max_length; for (pos=info->read_pos,end=pos+length ; pos < end ;) { if ((*to++ = *pos++) == '\n') { info->read_pos=pos; *to='\0'; return (uint) (to-start); } } if (!(max_length-=length)) { /* Found enough charcters; Return found string */ info->read_pos=pos; *to='\0'; return (uint) (to-start); } if (!(length=my_b_fill(info))) return 0; } }
/* Copy contents of an IO_CACHE to a file. SYNOPSIS my_b_copy_to_file() cache IO_CACHE to copy from file File to copy to DESCRIPTION Copy the contents of the cache to the file. The cache will be re-inited to a read cache and will read from the beginning of the cache. If a failure to write fully occurs, the cache is only copied partially. TODO Make this function solid by handling partial reads from the cache in a correct manner: it should be atomic. RETURN VALUE 0 All OK 1 An error occured */ int my_b_copy_to_file(IO_CACHE *cache, FILE *file) { size_t bytes_in_cache; DBUG_ENTER("my_b_copy_to_file"); /* Reinit the cache to read from the beginning of the cache */ if (reinit_io_cache(cache, READ_CACHE, 0L, FALSE, FALSE)) DBUG_RETURN(1); bytes_in_cache= my_b_bytes_in_cache(cache); do { if (my_fwrite(file, cache->read_pos, bytes_in_cache, MYF(MY_WME | MY_NABP)) == (size_t) -1) DBUG_RETURN(1); cache->read_pos= cache->read_end; } while ((bytes_in_cache= my_b_fill(cache))); DBUG_RETURN(0); }
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; }