1 /******************************************************************************
2 * Copyright (c) 2011, Michael P. Gerlek (mpg@flaxen.com)
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. or Flaxen Geo Consulting 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 #include <pdal/StageFactory.hpp>
36 #include <pdal/PluginManager.hpp>
37 #include <pdal/util/FileUtils.hpp>
38 
39 #include <algorithm>
40 #include <sstream>
41 #include <string>
42 
43 namespace pdal
44 {
45 
46 /**
47   Find the default reader for a file.
48 
49   \param filename  Filename for which to infer a reader.
50   \return  Name of the reader driver associated with the file.
51 */
inferReaderDriver(const std::string & filename)52 std::string StageFactory::inferReaderDriver(const std::string& filename)
53 {
54     std::string ext;
55 
56     if (Utils::endsWith(filename, "ept.json") || Utils::startsWith(filename, "ept://"))
57         return "readers.ept";
58     if (Utils::startsWith(filename, "i3s://"))
59         return "readers.i3s";
60 
61     ext = FileUtils::extension(filename);
62     // Strip off '.' and make lowercase.
63     if (ext.length() > 1)
64         ext = Utils::tolower(ext.substr(1));
65 
66     return PluginManager<Stage>::extensions().defaultReader(ext);
67 }
68 
69 
70 /**
71   Find the default writer for a file.
72 
73   \param filename  Filename for which to infer a writer.
74   \return  Name of the writer driver associated with the file.
75 */
inferWriterDriver(const std::string & filename)76 std::string StageFactory::inferWriterDriver(const std::string& filename)
77 {
78     std::string lFilename = Utils::tolower(filename);
79     if (lFilename == "devnull" || lFilename == "/dev/null")
80         return "writers.null";
81 
82     std::string ext;
83     if (lFilename == "stdout")
84         ext = ".txt";
85     else
86         ext = Utils::tolower(FileUtils::extension(lFilename));
87     // Strip off '.' and make lowercase.
88     if (ext.length() > 1)
89         ext = Utils::tolower(ext.substr(1));
90 
91     return PluginManager<Stage>::extensions().defaultWriter(ext);
92 }
93 
94 
StageFactory(bool)95 StageFactory::StageFactory(bool /* legacy */)
96 {
97 }
98 
99 
createStage(std::string const & stage_name)100 Stage *StageFactory::createStage(std::string const& stage_name)
101 {
102     static_assert(0 < sizeof(Stage), "");
103     Stage *s = PluginManager<Stage>::createObject(stage_name);
104     if (s)
105     {
106         std::lock_guard<std::mutex> lock(m_mutex);
107         m_ownedStages.push_back(std::unique_ptr<Stage>(s));
108     }
109     return s;
110 }
111 
112 
destroyStage(Stage * s)113 void StageFactory::destroyStage(Stage *s)
114 {
115     std::lock_guard<std::mutex> lock(m_mutex);
116     for (auto it = m_ownedStages.begin(); it != m_ownedStages.end(); ++it)
117     {
118         if (s == it->get())
119         {
120             m_ownedStages.erase(it);
121             break;
122         }
123     }
124 }
125 
126 } // namespace pdal
127