1
/******************************************************************************
4
* Project: libLAS - http://liblas.org - A BSD library for LAS format data.
5
* Purpose: A composite class to handle read/write operations
6
* Author: Mateusz Loskot, mateusz@loskot.net
8
******************************************************************************
9
* Copyright (c) 2008, Mateusz Loskot
10
* Copyright (c) 2008, Howard Butler
12
* All rights reserved.
14
* Redistribution and use in source and binary forms, with or without
15
* modification, are permitted provided that the following
18
* * Redistributions of source code must retain the above copyright
19
* notice, this list of conditions and the following disclaimer.
20
* * Redistributions in binary form must reproduce the above copyright
21
* notice, this list of conditions and the following disclaimer in
22
* the documentation and/or other materials provided
23
* with the distribution.
24
* * Neither the name of the Martin Isenburg or Iowa Department
25
* of Natural Resources nor the names of its contributors may be
26
* used to endorse or promote products derived from this software
27
* without specific prior written permission.
29
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
32
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
33
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
34
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
35
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
36
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
37
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
38
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
39
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
41
****************************************************************************/
43
#include <liblas/detail/file.hpp>
44
#include <liblas/lasreader.hpp>
45
#include <liblas/laswriter.hpp>
52
namespace liblas { namespace detail {
54
FileImpl::FileImpl(std::string const& filename)
55
: m_mode(0), m_filename(filename),
56
m_istrm(0), m_ostrm(0), m_reader(0), m_writer(0)
58
if (filename == "stdin")
64
std::ios::openmode const mode = std::ios::in | std::ios::binary;
65
m_istrm = new std::ifstream(m_filename.c_str(), mode);
70
throw_no_file_error();
76
m_reader = new LASReader(*m_istrm);
77
m_header = m_reader->GetHeader();
82
FileImpl::FileImpl(std::string const& filename, LASHeader const& header, int mode)
83
: m_mode(mode), m_filename(filename),
84
m_istrm(0), m_ostrm(0), m_reader(0), m_writer(0), m_header(header)
86
if (filename == "stdout")
93
if ( (mode > 2) || (mode < 1)) {
94
throw std::runtime_error("File mode must be eWrite or eAppend");
99
m = std::ios::out | std::ios::in | std::ios::binary | std::ios::ate;
103
m = std::ios::out | std::ios::binary | std::ios::ate;
105
m_ostrm = new std::ofstream(m_filename.c_str(), m);
107
if (!m_ostrm->good())
110
throw_no_file_error();
115
assert(0 != m_ostrm);
116
m_writer = new LASWriter(*m_ostrm, m_header);
118
assert(0 == m_istrm);
121
FileImpl::~FileImpl()
123
// NOTE: The order of destruction is very important.
124
// reader/writer first, then streams.
125
// In other words, kill clients first, suppliers afterwards.
127
if (m_istrm != &std::cin && 0 != m_istrm)
129
assert(0 == m_writer);
130
assert(0 == m_ostrm);
137
if (m_ostrm != &std::cout && 0 != m_ostrm)
139
assert(0 == m_reader);
140
assert(0 == m_istrm);
148
std::string FileImpl::GetName() const
153
int FileImpl::GetMode() const
158
LASHeader const& FileImpl::GetHeader() const
163
LASReader& FileImpl::GetReader()
167
// TODO: Define specialized exception type for this error
168
std::string msg("Reader is file write-only: " + m_filename);
169
throw std::runtime_error(msg);
175
LASWriter& FileImpl::GetWriter()
179
// TODO: Define specialized exception type for this error
180
std::string msg("Writer is file read-only: " + m_filename);
181
throw std::runtime_error(msg);
187
void FileImpl::throw_no_file_error() const
189
// TODO: Define specialized exception type for this error
190
// for example: liblas::open_error or liblas::no_file_error
191
std::string msg("can not open file " + m_filename);
192
throw std::runtime_error(msg);
195
}} // namespace liblas::detail