MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
InputStream.cpp
1 /*
2  Copyright (C) 2003-2007 MySQL AB
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 
20 #include <ndb_global.h>
21 
22 #include "InputStream.hpp"
23 #include <socket_io.h>
24 
25 FileInputStream Stdin(stdin);
26 
27 FileInputStream::FileInputStream(FILE * file)
28  : f(file) {
29 }
30 
31 char*
32 FileInputStream::gets(char * buf, int bufLen){
33  if(!feof(f)){
34  return fgets(buf, bufLen, f);
35  }
36  return 0;
37 }
38 
39 SocketInputStream::SocketInputStream(NDB_SOCKET_TYPE socket,
40  unsigned read_timeout_ms)
41  : m_socket(socket) {
42  m_startover= true;
43  m_timeout_remain= m_timeout_ms = read_timeout_ms;
44 
45  m_timedout= false;
46 }
47 
48 char*
49 SocketInputStream::gets(char * buf, int bufLen) {
50  if(timedout())
51  return 0;
52  assert(bufLen >= 2);
53  int offset= 0;
54  if(m_startover)
55  {
56  buf[0]= '\0';
57  m_startover= false;
58  }
59  else
60  offset= strlen(buf);
61 
62  int time= 0;
63  int res = readln_socket(m_socket, m_timeout_remain, &time,
64  buf+offset, bufLen-offset, m_mutex);
65 
66  if(res >= 0)
67  m_timeout_remain-=time;
68  if(res == 0 || m_timeout_remain<=0)
69  {
70  m_timedout= true;
71  buf[0]=0;
72  return buf;
73  }
74 
75  m_startover= true;
76 
77  if(res == -1)
78  {
79  return 0;
80  }
81 
82  return buf;
83 }