~ubuntu-branches/ubuntu/wily/liblas/wily

« back to all changes in this revision

Viewing changes to src/detail/file.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Francesco Paolo Lovergine
  • Date: 2009-10-02 12:36:21 UTC
  • Revision ID: james.westby@ubuntu.com-20091002123621-xrf0hhzxbwloga43
Tags: upstream-1.2.1
ImportĀ upstreamĀ versionĀ 1.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 * $Id$
 
3
 *
 
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
 
7
 *
 
8
 ******************************************************************************
 
9
 * Copyright (c) 2008, Mateusz Loskot
 
10
 * Copyright (c) 2008, Howard Butler
 
11
 *
 
12
 * All rights reserved.
 
13
 * 
 
14
 * Redistribution and use in source and binary forms, with or without 
 
15
 * modification, are permitted provided that the following 
 
16
 * conditions are met:
 
17
 * 
 
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.
 
28
 * 
 
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 
 
40
 * OF SUCH DAMAGE.
 
41
 ****************************************************************************/
 
42
 
 
43
#include <liblas/detail/file.hpp>
 
44
#include <liblas/lasreader.hpp>
 
45
#include <liblas/laswriter.hpp>
 
46
#include <string>
 
47
#include <iostream>
 
48
#include <fstream>
 
49
#include <stdexcept>
 
50
#include <cassert>
 
51
 
 
52
namespace liblas { namespace detail {
 
53
 
 
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)
 
57
{
 
58
    if (filename == "stdin")
 
59
    {
 
60
        m_istrm = &std::cin;
 
61
    }
 
62
    else
 
63
    {
 
64
        std::ios::openmode const mode = std::ios::in | std::ios::binary;
 
65
        m_istrm = new std::ifstream(m_filename.c_str(), mode);
 
66
        
 
67
        if (!m_istrm->good())
 
68
        {
 
69
            delete m_istrm;
 
70
            throw_no_file_error();
 
71
        }
 
72
    }
 
73
 
 
74
    // TODO: apply RAII
 
75
    assert(0 != m_istrm);
 
76
    m_reader = new LASReader(*m_istrm);
 
77
    m_header = m_reader->GetHeader();
 
78
 
 
79
    assert(0 == m_ostrm);
 
80
}
 
81
 
 
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)
 
85
{
 
86
    if (filename == "stdout")
 
87
    {
 
88
        m_ostrm = &std::cout;
 
89
    }
 
90
    else
 
91
    {
 
92
        std::ios::openmode m;
 
93
        if ( (mode > 2) || (mode < 1)) {
 
94
            throw std::runtime_error("File mode must be eWrite or eAppend");
 
95
        }
 
96
        
 
97
        // append mode 
 
98
        if (mode == 2) {
 
99
            m = std::ios::out | std::ios::in | std::ios::binary | std::ios::ate;
 
100
        }
 
101
        // write mode
 
102
        else {
 
103
            m = std::ios::out | std::ios::binary | std::ios::ate;
 
104
        }
 
105
        m_ostrm = new std::ofstream(m_filename.c_str(), m);
 
106
 
 
107
        if (!m_ostrm->good())
 
108
        {
 
109
            delete m_ostrm;
 
110
            throw_no_file_error();
 
111
        }
 
112
    }
 
113
 
 
114
    // TODO: apply RAII
 
115
    assert(0 != m_ostrm);
 
116
    m_writer = new LASWriter(*m_ostrm, m_header);
 
117
 
 
118
    assert(0 == m_istrm);
 
119
}
 
120
 
 
121
FileImpl::~FileImpl()
 
122
{
 
123
    // NOTE: The order of destruction is very important.
 
124
    // reader/writer first, then streams.
 
125
    // In other words, kill clients first, suppliers afterwards.
 
126
 
 
127
    if (m_istrm != &std::cin && 0 != m_istrm)
 
128
    {
 
129
        assert(0 == m_writer);
 
130
        assert(0 == m_ostrm);
 
131
        delete m_reader;
 
132
        m_reader = 0;
 
133
        delete m_istrm;
 
134
        m_istrm = 0;
 
135
    }
 
136
 
 
137
    if (m_ostrm != &std::cout && 0 != m_ostrm)
 
138
    {
 
139
        assert(0 == m_reader);
 
140
        assert(0 == m_istrm);
 
141
        delete m_writer;
 
142
        m_writer = 0;
 
143
        delete m_ostrm;
 
144
        m_ostrm = 0;
 
145
    }
 
146
}
 
147
 
 
148
std::string FileImpl::GetName() const
 
149
{
 
150
    return m_filename;
 
151
}
 
152
 
 
153
int FileImpl::GetMode() const
 
154
{
 
155
    return m_mode;
 
156
}
 
157
 
 
158
LASHeader const& FileImpl::GetHeader() const
 
159
{
 
160
    return m_header;
 
161
}
 
162
 
 
163
LASReader& FileImpl::GetReader()
 
164
{
 
165
    if (0 == m_reader)
 
166
    {
 
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);
 
170
    }
 
171
 
 
172
    return (*m_reader);
 
173
}
 
174
 
 
175
LASWriter& FileImpl::GetWriter()
 
176
{
 
177
    if (0 == m_writer)
 
178
    {
 
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);
 
182
    }
 
183
 
 
184
    return (*m_writer);
 
185
}
 
186
 
 
187
void FileImpl::throw_no_file_error() const
 
188
{
 
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);
 
193
}
 
194
 
 
195
}} // namespace liblas::detail