1 /******************************************************************************
2 * Copyright (c) 2016, Hobu Inc. (info@hobu.co)
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following
8 * conditions are met:
9 *
10 *     * Redistributions of source code must retain the above copyright
11 *       notice, this list of conditions and the following disclaimer.
12 *     * Redistributions in binary form must reproduce the above copyright
13 *       notice, this list of conditions and the following disclaimer in
14 *       the documentation and/or other materials provided
15 *       with the distribution.
16 *     * Neither the name of Hobu, Inc. nor the
17 *       names of its contributors may be used to endorse or promote
18 *       products derived from this software without specific prior
19 *       written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 * OF SUCH DAMAGE.
33 ****************************************************************************/
34 
35 #pragma once
36 
37 #include <istream>
38 
39 #include <pdal/Reader.hpp>
40 #include <pdal/Streamable.hpp>
41 
42 namespace pdal
43 {
44 
45 class PDAL_DLL TextReader : public Reader, public Streamable
46 {
47 public:
48     std::string getName() const;
49 
TextReader()50     TextReader() : m_istream(NULL)
51     {}
52 
53 private:
54     /**
55       Retrieve summary information for the file. NOTE - entire file must
56       be read to retrieve summary for text files.
57 
58       \param table  Point table being initialized.
59     */
60     virtual QuickInfo inspect();
61 
62     /**
63       Initialize the reader by opening the file and reading the header line.
64       Closes the file on completion.
65 
66       \param table  Point table being initialized.
67     */
68     virtual void initialize(PointTableRef table);
69 
70     /**
71       Add arguments to those accepted at the command line.
72       \param args  Argument list to modify.
73     */
74     virtual void addArgs(ProgramArgs& args);
75 
76     /**
77       Add dimensions found in the header line to the layout.
78 
79       \param layout  Layout to which the dimenions are added.
80     */
81     virtual void addDimensions(PointLayoutPtr layout);
82 
83     /**
84       Reopen the file in preparation for reading.
85 
86       \param table  Point table to make ready.
87     */
88     virtual void ready(PointTableRef table);
89 
90     /**
91       Read up to numPts points into the \ref view.
92 
93       \param view  PointView in which to insert point data.
94       \param numPts  Maximum number of points to read.
95       \return  Number of points read.
96     */
97     virtual point_count_t read(PointViewPtr view, point_count_t numPts);
98 
99     /**
100       Close input file.
101 
102       \param table  PointTable we're done with.
103     */
104     virtual void done(PointTableRef table);
105 
106     /**
107       Read a single point from the input.
108 
109       \param point  Reference to point to fill with data.
110       \return  False if no point could be read.
111     */
112     virtual bool processOne(PointRef& point);
113 
114     bool fillFields();
115 
116     /**
117       Parse a header line into a list of dimension names.
118 
119       \param header  Header line to parse.
120     */
121     void parseHeader(const std::string& header);
122 
123     /**
124       Parse a header line that starts with a quote.
125 
126       \param header Header line to parse.
127     */
128     void parseQuotedHeader(const std::string& header);
129 
130     /**
131       Parse a header line that doesn't start with a quote.
132 
133       \param header Header line to parse.
134     */
135     void parseUnquotedHeader(const std::string& header);
136 
137     /**
138       Check a header line to see if it appears header-like.  Display a
139       warning if it doesn't look like a header.
140 
141       \param header  Header string to test.
142     */
143     void checkHeader(const std::string& header);
144 
145 private:
146     char m_separator;
147     Arg *m_separatorArg;
148     std::istream *m_istream;
149     StringList m_dimNames;
150     Dimension::IdList m_dims;
151     StringList m_fields;
152     size_t m_line;
153     std::string m_header;
154     size_t m_skip;
155 };
156 
157 } // namespace pdal
158