1ac3d0c65SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3ac3d0c65SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4ac3d0c65SAndrew Rist * or more contributor license agreements. See the NOTICE file
5ac3d0c65SAndrew Rist * distributed with this work for additional information
6ac3d0c65SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7ac3d0c65SAndrew Rist * to you under the Apache License, Version 2.0 (the
8ac3d0c65SAndrew Rist * "License"); you may not use this file except in compliance
9ac3d0c65SAndrew Rist * with the License. You may obtain a copy of the License at
10ac3d0c65SAndrew Rist *
11ac3d0c65SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12ac3d0c65SAndrew Rist *
13ac3d0c65SAndrew Rist * Unless required by applicable law or agreed to in writing,
14ac3d0c65SAndrew Rist * software distributed under the License is distributed on an
15ac3d0c65SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ac3d0c65SAndrew Rist * KIND, either express or implied. See the License for the
17ac3d0c65SAndrew Rist * specific language governing permissions and limitations
18ac3d0c65SAndrew Rist * under the License.
19ac3d0c65SAndrew Rist *
20ac3d0c65SAndrew Rist *************************************************************/
21ac3d0c65SAndrew Rist
22ac3d0c65SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #ifndef CSV_BSTREAM_HXX
25cdf0e10cSrcweir #define CSV_BSTREAM_HXX
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <string.h>
28cdf0e10cSrcweir #include <cosv/string.hxx>
29cdf0e10cSrcweir
30cdf0e10cSrcweir
31cdf0e10cSrcweir namespace csv
32cdf0e10cSrcweir {
33cdf0e10cSrcweir
34cdf0e10cSrcweir
35cdf0e10cSrcweir enum seek_dir
36cdf0e10cSrcweir {
37cdf0e10cSrcweir beg = 0,
38cdf0e10cSrcweir cur = 1,
39cdf0e10cSrcweir end = 2
40cdf0e10cSrcweir };
41cdf0e10cSrcweir
42cdf0e10cSrcweir
43cdf0e10cSrcweir class bistream
44cdf0e10cSrcweir {
45cdf0e10cSrcweir public:
46cdf0e10cSrcweir // LIFECYCLE
~bistream()47cdf0e10cSrcweir virtual ~bistream() {}
48cdf0e10cSrcweir
49cdf0e10cSrcweir // OPERATIONS
50cdf0e10cSrcweir /// @return Number of actually read bytes.
51cdf0e10cSrcweir uintt read(
52cdf0e10cSrcweir void * out_pDest,
53cdf0e10cSrcweir uintt i_nNrofBytes);
54cdf0e10cSrcweir // INQUIRY
55cdf0e10cSrcweir /** @return True, if already one try to read had failed.
56cdf0e10cSrcweir There is no guarantee, that it returns true, if end of data
57cdf0e10cSrcweir is just reached.
58cdf0e10cSrcweir Though it will return false, if there is still somemething
59cdf0e10cSrcweir to read.
60cdf0e10cSrcweir */
61cdf0e10cSrcweir bool eod() const;
62cdf0e10cSrcweir
63cdf0e10cSrcweir private:
64cdf0e10cSrcweir virtual uintt do_read(
65cdf0e10cSrcweir void * out_pDest,
66cdf0e10cSrcweir uintt i_nNrofBytes) = 0;
67cdf0e10cSrcweir virtual bool inq_eod() const = 0;
68cdf0e10cSrcweir };
69cdf0e10cSrcweir
70cdf0e10cSrcweir
71cdf0e10cSrcweir class bostream
72cdf0e10cSrcweir {
73cdf0e10cSrcweir public:
74cdf0e10cSrcweir // LIFECYCLE
~bostream()75cdf0e10cSrcweir virtual ~bostream() {}
76cdf0e10cSrcweir
77cdf0e10cSrcweir // OPERATIONS
78cdf0e10cSrcweir /// @return Number of actually written bytes.
79cdf0e10cSrcweir uintt write(
80cdf0e10cSrcweir const void * i_pSrc,
81cdf0e10cSrcweir uintt i_nNrofBytes);
82cdf0e10cSrcweir /// @return Number of actually written bytes.
83cdf0e10cSrcweir uintt write(
84cdf0e10cSrcweir const char * i_pSrc );
85cdf0e10cSrcweir /// @return Number of actually written bytes.
86cdf0e10cSrcweir uintt write(
87cdf0e10cSrcweir const String & i_pSrc );
88cdf0e10cSrcweir private:
89cdf0e10cSrcweir virtual uintt do_write(
90cdf0e10cSrcweir const void * i_pSrc,
91cdf0e10cSrcweir uintt i_nNrofBytes) = 0;
92cdf0e10cSrcweir };
93cdf0e10cSrcweir
94cdf0e10cSrcweir
95cdf0e10cSrcweir class bstream : public bistream,
96cdf0e10cSrcweir public bostream
97cdf0e10cSrcweir {
98cdf0e10cSrcweir public:
99cdf0e10cSrcweir uintt seek(
100cdf0e10cSrcweir intt i_nDistanceFromBegin,
101cdf0e10cSrcweir seek_dir i_eStartPoint = ::csv::beg );
102cdf0e10cSrcweir uintt position() const;
103cdf0e10cSrcweir
104cdf0e10cSrcweir private:
105cdf0e10cSrcweir virtual uintt do_seek(
106cdf0e10cSrcweir intt i_nDistance,
107cdf0e10cSrcweir seek_dir i_eStartPoint = ::csv::beg ) = 0;
108cdf0e10cSrcweir virtual uintt inq_position() const = 0;
109cdf0e10cSrcweir };
110cdf0e10cSrcweir
111cdf0e10cSrcweir
112cdf0e10cSrcweir // IMPLEMENTATION
113cdf0e10cSrcweir inline uintt
read(void * o_pDest,uintt i_nNrofBytes)114cdf0e10cSrcweir bistream::read( void * o_pDest,
115cdf0e10cSrcweir uintt i_nNrofBytes)
116cdf0e10cSrcweir { return do_read(o_pDest, i_nNrofBytes); }
117cdf0e10cSrcweir inline bool
eod() const118cdf0e10cSrcweir bistream::eod() const
119cdf0e10cSrcweir { return inq_eod(); }
120cdf0e10cSrcweir
121cdf0e10cSrcweir inline uintt
write(const void * i_pSrc,uintt i_nNrofBytes)122cdf0e10cSrcweir bostream::write( const void * i_pSrc,
123cdf0e10cSrcweir uintt i_nNrofBytes)
124cdf0e10cSrcweir { return do_write( i_pSrc, i_nNrofBytes ); }
125cdf0e10cSrcweir inline uintt
write(const char * i_sSrc)126cdf0e10cSrcweir bostream::write( const char * i_sSrc )
127cdf0e10cSrcweir { return write( i_sSrc, strlen(i_sSrc) ); }
128cdf0e10cSrcweir inline uintt
write(const String & i_sSrc)129cdf0e10cSrcweir bostream::write( const String & i_sSrc )
130cdf0e10cSrcweir { return write( i_sSrc.c_str(), i_sSrc.length() ); }
131cdf0e10cSrcweir
132cdf0e10cSrcweir inline uintt
seek(intt i_nDistance,seek_dir i_eStartPoint)133cdf0e10cSrcweir bstream::seek( intt i_nDistance,
134cdf0e10cSrcweir seek_dir i_eStartPoint )
135cdf0e10cSrcweir { return do_seek( i_nDistance, i_eStartPoint ); }
136cdf0e10cSrcweir inline uintt
position() const137cdf0e10cSrcweir bstream::position() const
138cdf0e10cSrcweir { return inq_position(); }
139cdf0e10cSrcweir
140cdf0e10cSrcweir
141cdf0e10cSrcweir
142cdf0e10cSrcweir } // namespace csv
143cdf0e10cSrcweir
144cdf0e10cSrcweir
145cdf0e10cSrcweir #endif
146cdf0e10cSrcweir
147