nidas  v1.2-1520
XMLFdBinInputStream.h
Go to the documentation of this file.
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2 // vim: set shiftwidth=4 softtabstop=4 expandtab:
3 /*
4  ********************************************************************
5  ** NIDAS: NCAR In-situ Data Acquistion Software
6  **
7  ** 2005, Copyright University Corporation for Atmospheric Research
8  **
9  ** This program is free software; you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation; either version 2 of the License, or
12  ** (at your option) any later version.
13  **
14  ** This program is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** The LICENSE.txt file accompanying this software contains
20  ** a copy of the GNU General Public License. If it is not found,
21  ** write to the Free Software Foundation, Inc.,
22  ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  **
24  ********************************************************************
25 */
26 
27 #ifndef NIDAS_CORE_XMLFDBININPUTSTREAM_H
28 #define NIDAS_CORE_XMLFDBININPUTSTREAM_H
29 
30 #include <xercesc/util/XercesDefs.hpp>
31 #include <xercesc/util/BinInputStream.hpp>
32 #include <nidas/util/IOException.h>
33 
34 #include <unistd.h>
35 
36 #include <iostream>
37 
38 namespace nidas { namespace core {
39 
44 class XMLFdBinInputStream: public xercesc::BinInputStream {
45 public:
46 
52  XMLFdBinInputStream(const std::string& n,int f) : name(n),fd(f),curpos(0),_eof(false) {}
54  {
55  // std::cerr << "~XMLFdBinInputStream" << std::endl;
56  }
57 
58 #if XERCES_VERSION_MAJOR < 3
59  unsigned int
60 #else
61  XMLFilePos
62 #endif
63  curPos() const { return curpos; }
64 
68 #if XERCES_VERSION_MAJOR < 3
69  unsigned int
70 #else
71  XMLSize_t
72 #endif
73  readBytes(XMLByte* const toFill,
74 #if XERCES_VERSION_MAJOR < 3
75  const unsigned int maxToRead
76 #else
77  const XMLSize_t maxToRead
78 #endif
79  )
81  {
82  if (_eof) return 0;
83  // std::cerr << "XMLFdBinInputStream reading " << maxToRead << std::endl;
84  ssize_t l = ::read(fd,toFill,maxToRead);
85  if (l < 0) throw nidas::util::IOException(name,"read",errno);
86  for (int i = 0; i < l; i++)
87  if (toFill[i] == '\x04') {
88  l = i;
89  _eof = true;
90  }
91  curpos += l;
92  // std::cerr << "XMLFdBinInputStream read " << std::string((char*)toFill,0,l < 20 ? l : 20) << std::endl;
93  // std::cerr << "XMLFdBinInputStream read " << std::string((char*)toFill,0,l) << std::endl;
94  // std::cerr << "XMLFdBinInputStream read " << l << std::endl;
95  // toFill[l] = 0;
96  // toFill[l+1] = 0;
97  return l;
98  }
99 
100 #if XERCES_VERSION_MAJOR >= 3
101  const XMLCh* getContentType() const
102  {
103  return 0;
104  }
105 #endif
106 
107 protected:
108 
109  std::string name;
110 
111  int fd;
112 #if XERCES_VERSION_MAJOR < 3
113  unsigned int curpos;
114 #else
115  XMLFilePos curpos;
116 #endif
117 
118  bool _eof;
119 
120 };
121 
122 }} // namespace nidas namespace core
123 
124 #endif
125 
unsigned int readBytes(XMLByte *const toFill, const unsigned int maxToRead)
return number of bytes read, or 0 on EOF.
Definition: XMLFdBinInputStream.h:73
int fd
Definition: XMLFdBinInputStream.h:111
bool _eof
Definition: XMLFdBinInputStream.h:118
unsigned int curpos
Definition: XMLFdBinInputStream.h:113
XMLFdBinInputStream(const std::string &n, int f)
Constructor.
Definition: XMLFdBinInputStream.h:52
unsigned int curPos() const
Definition: XMLFdBinInputStream.h:63
Definition: IOException.h:37
std::string name
Definition: XMLFdBinInputStream.h:109
~XMLFdBinInputStream()
Definition: XMLFdBinInputStream.h:53
Implemenation of xercesc::BinInputStream, which reads from a unix file descriptor.
Definition: XMLFdBinInputStream.h:44