~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/readline.c

  • Committer: Monty Taylor
  • Date: 2008-08-01 22:33:44 UTC
  • mto: (236.1.42 codestyle)
  • mto: This revision was merged to the branch mainline in revision 261.
  • Revision ID: monty@inaugust.com-20080801223344-vzhlflfmtijp1imv
First pass at gettexizing the error messages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
/* readline for batch mode */
17
17
 
18
 
#include <config.h>
19
 
#include <drizzled/internal/my_sys.h>
20
 
#include <client/linebuffer.h>
21
 
#include <boost/version.hpp>
22
 
 
23
 
#include <vector>
24
 
 
25
 
using namespace std;
26
 
using namespace drizzled;
27
 
 
28
 
LineBuffer::LineBuffer(uint32_t my_max_size,FILE *my_file)
29
 
  :
30
 
    file(my_file),
31
 
    max_size(my_max_size)
32
 
{
33
 
  if (my_file)
34
 
 
35
 
  /*
36
 
    if here beacuse the old way of using file_descriptor is deprecated in boost
37
 
    1.44.  There is a #define to re-enable the function but this is broken in
38
 
    Fedora 14. See https://bugzilla.redhat.com/show_bug.cgi?id=654480
39
 
  */
40
 
#if BOOST_VERSION < 104400
41
 
    file_stream = new boost::iostreams::stream<boost::iostreams::file_descriptor>(fileno(my_file), true);
42
 
#else
43
 
    file_stream = new boost::iostreams::stream<boost::iostreams::file_descriptor>(fileno(my_file), boost::iostreams::never_close_handle);
44
 
#endif
45
 
  else
46
 
    file_stream = new std::stringstream;
47
 
  line.reserve(max_size);
48
 
}
49
 
 
50
 
void LineBuffer::addString(const string &str)
51
 
{
52
 
  (*file_stream) << str << endl;
53
 
}
54
 
 
55
 
char *LineBuffer::readline()
56
 
{
57
 
  file_stream->getline(&line[0], max_size);
58
 
 
59
 
  if (file_stream->fail())
60
 
    return 0;
61
 
  else
62
 
    return &line[0];
63
 
}
64
 
 
 
18
#include "client_priv.h"
 
19
#include <mysys/my_sys.h>
 
20
#include <mystrings/m_string.h>
 
21
#include "my_readline.h"
 
22
 
 
23
static bool init_line_buffer(LINE_BUFFER *buffer,File file,ulong size,
 
24
                            ulong max_size);
 
25
static bool init_line_buffer_from_string(LINE_BUFFER *buffer,char * str);
 
26
static size_t fill_buffer(LINE_BUFFER *buffer);
 
27
static char *intern_read_line(LINE_BUFFER *buffer,ulong *out_length);
 
28
 
 
29
 
 
30
LINE_BUFFER *batch_readline_init(ulong max_size,FILE *file)
 
31
{
 
32
  LINE_BUFFER *line_buff;
 
33
  if (!(line_buff=(LINE_BUFFER*)
 
34
        my_malloc(sizeof(*line_buff),MYF(MY_WME | MY_ZEROFILL))))
 
35
    return 0;
 
36
  if (init_line_buffer(line_buff,fileno(file),IO_SIZE,max_size))
 
37
  {
 
38
    my_free(line_buff,MYF(0));
 
39
    return 0;
 
40
  }
 
41
  return line_buff;
 
42
}
 
43
 
 
44
 
 
45
char *batch_readline(LINE_BUFFER *line_buff)
 
46
{
 
47
  char *pos;
 
48
  ulong out_length;
 
49
 
 
50
  if (!(pos=intern_read_line(line_buff,&out_length)))
 
51
    return 0;
 
52
  if (out_length && pos[out_length-1] == '\n')
 
53
    if (--out_length && pos[out_length-1] == '\r')  /* Remove '\n' */
 
54
      out_length--;                                 /* Remove '\r' */
 
55
  line_buff->read_length=out_length;
 
56
  pos[out_length]=0;
 
57
  return pos;
 
58
}
 
59
 
 
60
 
 
61
void batch_readline_end(LINE_BUFFER *line_buff)
 
62
{
 
63
  if (line_buff)
 
64
  {
 
65
    my_free(line_buff->buffer,MYF(MY_ALLOW_ZERO_PTR));
 
66
    my_free(line_buff,MYF(0));
 
67
  }
 
68
}
 
69
 
 
70
 
 
71
LINE_BUFFER *batch_readline_command(LINE_BUFFER *line_buff, char * str)
 
72
{
 
73
  if (!line_buff)
 
74
    if (!(line_buff=(LINE_BUFFER*)
 
75
          my_malloc(sizeof(*line_buff),MYF(MY_WME | MY_ZEROFILL))))
 
76
      return 0;
 
77
  if (init_line_buffer_from_string(line_buff,str))
 
78
  {
 
79
    my_free(line_buff,MYF(0));
 
80
    return 0;
 
81
  }
 
82
  return line_buff;
 
83
}
 
84
 
 
85
 
 
86
/*****************************************************************************
 
87
      Functions to handle buffered readings of lines from a stream
 
88
******************************************************************************/
 
89
 
 
90
static bool
 
91
init_line_buffer(LINE_BUFFER *buffer,File file,ulong size,ulong max_buffer)
 
92
{
 
93
  buffer->file=file;
 
94
  buffer->bufread=size;
 
95
  buffer->max_size=max_buffer;
 
96
  if (!(buffer->buffer = (char*) my_malloc(buffer->bufread+1,
 
97
                                           MYF(MY_WME | MY_FAE))))
 
98
    return 1;
 
99
  buffer->end_of_line=buffer->end=buffer->buffer;
 
100
  buffer->buffer[0]=0;                          /* For easy start test */
 
101
  return 0;
 
102
}
 
103
 
 
104
/*
 
105
  init_line_buffer_from_string can be called on the same buffer
 
106
  several times. the resulting buffer will contain a
 
107
  concatenation of all strings separated by spaces
 
108
*/
 
109
static bool init_line_buffer_from_string(LINE_BUFFER *buffer,char * str)
 
110
{
 
111
  uint old_length=(uint)(buffer->end - buffer->buffer);
 
112
  uint length= (uint) strlen(str);
 
113
  if (!(buffer->buffer= buffer->start_of_line= buffer->end_of_line=
 
114
        (char*) my_realloc((uchar*) buffer->buffer, old_length+length+2,
 
115
                           MYF(MY_FAE|MY_ALLOW_ZERO_PTR))))
 
116
    return 1;
 
117
  buffer->end= buffer->buffer + old_length;
 
118
  if (old_length)
 
119
    buffer->end[-1]=' ';
 
120
  memcpy(buffer->end, str, length);
 
121
  buffer->end[length]= '\n';
 
122
  buffer->end[length+1]= 0;
 
123
  buffer->end+= length+1;
 
124
  buffer->eof=1;
 
125
  buffer->max_size=1;
 
126
  return 0;
 
127
}
 
128
 
 
129
 
 
130
/*
 
131
  Fill the buffer retaining the last n bytes at the beginning of the
 
132
  newly filled buffer (for backward context).   Returns the number of new
 
133
  bytes read from disk.
 
134
*/
 
135
 
 
136
static size_t fill_buffer(LINE_BUFFER *buffer)
 
137
{
 
138
  size_t read_count;
 
139
  uint bufbytes= (uint) (buffer->end - buffer->start_of_line);
 
140
 
 
141
  if (buffer->eof)
 
142
    return 0;                                   /* Everything read */
 
143
 
 
144
  /* See if we need to grow the buffer. */
 
145
 
 
146
  for (;;)
 
147
  {
 
148
    uint start_offset=(uint) (buffer->start_of_line - buffer->buffer);
 
149
    read_count=(buffer->bufread - bufbytes)/IO_SIZE;
 
150
    if ((read_count*=IO_SIZE))
 
151
      break;
 
152
    buffer->bufread *= 2;
 
153
    if (!(buffer->buffer = (char*) my_realloc(buffer->buffer,
 
154
                                              buffer->bufread+1,
 
155
                                              MYF(MY_WME | MY_FAE))))
 
156
      return (uint) -1;
 
157
    buffer->start_of_line=buffer->buffer+start_offset;
 
158
    buffer->end=buffer->buffer+bufbytes;
 
159
  }
 
160
 
 
161
  /* Shift stuff down. */
 
162
  if (buffer->start_of_line != buffer->buffer)
 
163
  {
 
164
    memcpy(buffer->buffer, buffer->start_of_line, (uint) bufbytes);
 
165
    buffer->end=buffer->buffer+bufbytes;
 
166
  }
 
167
 
 
168
  /* Read in new stuff. */
 
169
  if ((read_count= my_read(buffer->file, (uchar*) buffer->end, read_count,
 
170
                           MYF(MY_WME))) == MY_FILE_ERROR)
 
171
    return (size_t) -1;
 
172
 
 
173
  /* Kludge to pretend every nonempty file ends with a newline. */
 
174
  if (!read_count && bufbytes && buffer->end[-1] != '\n')
 
175
  {
 
176
    buffer->eof = read_count = 1;
 
177
    *buffer->end = '\n';
 
178
  }
 
179
  buffer->end_of_line=(buffer->start_of_line=buffer->buffer)+bufbytes;
 
180
  buffer->end+=read_count;
 
181
  *buffer->end=0;                               /* Sentinel */
 
182
  return read_count;
 
183
}
 
184
 
 
185
 
 
186
 
 
187
char *intern_read_line(LINE_BUFFER *buffer,ulong *out_length)
 
188
{
 
189
  char *pos;
 
190
  size_t length;
 
191
 
 
192
 
 
193
  buffer->start_of_line=buffer->end_of_line;
 
194
  for (;;)
 
195
  {
 
196
    pos=buffer->end_of_line;
 
197
    while (*pos != '\n' && *pos)
 
198
      pos++;
 
199
    if (pos == buffer->end)
 
200
    {
 
201
      if ((uint) (pos - buffer->start_of_line) < buffer->max_size)
 
202
      {
 
203
        if (!(length=fill_buffer(buffer)) || length == (size_t) -1)
 
204
          return(0);
 
205
        continue;
 
206
      }
 
207
      pos--;                                    /* break line here */
 
208
    }
 
209
    buffer->end_of_line=pos+1;
 
210
    *out_length=(ulong) (pos + 1 - buffer->eof - buffer->start_of_line);
 
211
    return(buffer->start_of_line);
 
212
  }
 
213
}