InputStream.cpp
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:2k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2003 MySQL AB
  2.    This program is free software; you can redistribute it and/or modify
  3.    it under the terms of the GNU General Public License as published by
  4.    the Free Software Foundation; either version 2 of the License, or
  5.    (at your option) any later version.
  6.    This program is distributed in the hope that it will be useful,
  7.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9.    GNU General Public License for more details.
  10.    You should have received a copy of the GNU General Public License
  11.    along with this program; if not, write to the Free Software
  12.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  13. #include <ndb_global.h>
  14. #include "InputStream.hpp"
  15. #include <socket_io.h>
  16. FileInputStream Stdin(stdin);
  17. FileInputStream::FileInputStream(FILE * file)
  18.   : f(file) {
  19. }
  20. char* 
  21. FileInputStream::gets(char * buf, int bufLen){ 
  22.   if(!feof(f)){
  23.     return fgets(buf, bufLen, f);
  24.   }
  25.   return 0;
  26. }
  27. SocketInputStream::SocketInputStream(NDB_SOCKET_TYPE socket, 
  28.      unsigned readTimeout) 
  29.   : m_socket(socket) { 
  30.   m_timeout = readTimeout; 
  31. }
  32. char* 
  33. SocketInputStream::gets(char * buf, int bufLen) {
  34.   buf[0] = 77;
  35.   assert(bufLen >= 2);
  36.   int res = readln_socket(m_socket, m_timeout, buf, bufLen - 1);
  37.   if(res == -1)
  38.     return 0;
  39.   if(res == 0 && buf[0] == 77){ // select return 0
  40.     buf[0] = 0;
  41.   } else if(res == 0 && buf[0] == 0){ // only newline
  42.     buf[0] = 'n';
  43.     buf[1] = 0;
  44.   } else {
  45.     int len = strlen(buf);
  46.     buf[len + 1] = '';
  47.     buf[len] = 'n';
  48.   }
  49.   return buf;
  50. }