Line data Source code
1 : /*************************************************************************
2 : *
3 : * Copyright (c) 2012 Kohei Yoshida
4 : *
5 : * Permission is hereby granted, free of charge, to any person
6 : * obtaining a copy of this software and associated documentation
7 : * files (the "Software"), to deal in the Software without
8 : * restriction, including without limitation the rights to use,
9 : * copy, modify, merge, publish, distribute, sublicense, and/or sell
10 : * copies of the Software, and to permit persons to whom the
11 : * Software is furnished to do so, subject to the following
12 : * conditions:
13 : *
14 : * The above copyright notice and this permission notice shall be
15 : * included in all copies or substantial portions of the Software.
16 : *
17 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 : * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 : * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 : * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 : * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 : * OTHER DEALINGS IN THE SOFTWARE.
25 : *
26 : ************************************************************************/
27 :
28 : #ifndef __ORCUS_CELL_BUFFER_HPP__
29 : #define __ORCUS_CELL_BUFFER_HPP__
30 :
31 : #include <string>
32 :
33 : #define ORCUS_DEBUG_CELL_BUFFER 0
34 :
35 : #if ORCUS_DEBUG_CELL_BUFFER
36 : #include <iostream>
37 : using std::cout;
38 : using std::endl;
39 : #endif
40 :
41 : namespace orcus {
42 :
43 : /**
44 : * Temporary cell buffer used to convert cell values when needed. This is
45 : * used in the sax and csv parsers.
46 : */
47 64 : class cell_buffer
48 : {
49 : std::string m_buffer;
50 : size_t m_buf_size; /// Logical buffer size. May differ from the actual buffer size.
51 : public:
52 64 : cell_buffer() : m_buf_size(0) {}
53 :
54 332 : void append(const char* p, size_t len)
55 : {
56 332 : if (!len)
57 340 : return;
58 :
59 : #if ORCUS_DEBUG_CELL_BUFFER
60 : cout << "cell_buffer::append: '" << std::string(p, len) << "'" << endl;
61 : #endif
62 :
63 324 : size_t size_needed = m_buf_size + len;
64 324 : if (m_buffer.size() < size_needed)
65 38 : m_buffer.resize(size_needed);
66 :
67 324 : char* p_dest = &m_buffer[m_buf_size];
68 324 : std::strncpy(p_dest, p, len);
69 324 : m_buf_size += len;
70 : }
71 :
72 104 : void reset() { m_buf_size = 0; }
73 104 : const char* get() const { return &m_buffer[0]; }
74 104 : size_t size() const { return m_buf_size; }
75 0 : bool empty() const { return m_buf_size == 0; }
76 : };
77 :
78 : }
79 :
80 : #endif
|