~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/linebuffer.cc

  • Committer: Andrew Hutchings
  • Date: 2010-11-07 12:27:27 UTC
  • mto: (1912.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 1913.
  • Revision ID: andrew@linuxjedi.co.uk-20101107122727-r9govyzy73qcyvx5
Re-write of linebuffer to stop buffer overrrun and hopefully improve performance
Error if max_line_length hit instead of eating into RAM

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
LineBuffer::LineBuffer(uint32_t my_max_size,FILE *my_file)
28
28
  :
29
29
    file(my_file),
30
 
    line(),
31
 
    max_size(my_max_size),
32
 
    eof(false)
 
30
    max_size(my_max_size)
33
31
{
 
32
  if (my_file)
 
33
    file_stream = new boost::iostreams::stream<boost::iostreams::file_descriptor>(fileno(my_file), true);
 
34
  else
 
35
    file_stream = new std::stringstream;
34
36
  line.reserve(max_size);
35
37
}
36
38
 
37
39
void LineBuffer::addString(const string &str)
38
40
{
39
 
  buffer << str << endl;
 
41
  (*file_stream) << str << endl;
40
42
}
41
43
 
42
44
char *LineBuffer::readline()
43
45
{
44
 
  uint32_t read_count;
45
 
 
46
 
  if (file && !eof)
47
 
  {
48
 
    if ((read_count= read(fileno(file), (&line[0]), max_size-1)))
49
 
    {
50
 
      line[read_count+1]= '\0';
51
 
      buffer << &line[0];
52
 
    }
53
 
    else
54
 
      eof= true;
55
 
  }
56
 
 
57
 
  buffer.getline(&line[0],max_size);
58
 
 
59
 
  if (buffer.eof())
 
46
  file_stream->getline(&line[0], max_size);
 
47
 
 
48
  if (file_stream->fail())
60
49
    return 0;
61
50
  else
62
51
    return &line[0];