3737#include < pdal/PipelineWriter.hpp>
3838#include < pdal/Stage.hpp>
3939
40- // TODO Address cause of std::min problems. See https://github.com/PDAL/CAPI/issues/4
4140#undef min
4241
4342namespace pdal
@@ -50,57 +49,55 @@ extern "C"
5049 struct Pipeline {
5150 public:
5251
53- std::unique_ptr<pdal::PipelineManager> manager = std::make_unique<pdal:: PipelineManager>();
52+ PipelineManagerPtr manager = std::make_unique<PipelineManager>();
5453
5554 bool m_executed = false ;
5655 std::stringstream logStream;
57- pdal::LogLevel logLevel;
5856 };
5957
6058 PDALPipelinePtr PDALCreatePipeline (const char * json)
6159 {
62- PDALPipelinePtr pipeline = new Pipeline ();
63- Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
64- PipelineManager *manager = ptr->manager .get ();
60+ Pipeline* pipeline = new Pipeline ();
6561 if (json && std::strlen (json) > 0 )
6662 {
6763 try
6864 {
69- LogPtr log (Log::makeLog (" capi pipeline" , &ptr->logStream ));
70- manager->setLog (log);
65+ std::stringstream* s = &pipeline->logStream ;
66+ LogPtr lptr (pdal::Log::makeLog (" pdal capi" , s, true ));
67+ pipeline->manager ->setLog (lptr);
7168
7269 std::stringstream strm;
7370 strm << json;
74- manager->readPipeline (strm);
71+ pipeline-> manager ->readPipeline (strm);
7572 }
7673 catch (const std::exception &e)
7774 {
7875 printf (" Could not create pipeline: %s\n %s\n " , e.what (), json);
76+ delete pipeline;
77+ return nullptr ;
7978 }
8079
81- if (manager)
80+ try
8281 {
83- try
84- {
85- manager->prepare ();
86- }
87- catch (const std::exception &e)
88- {
89- printf (" Error while validating pipeline: %s\n %s\n " , e.what (), json);
90- }
82+ pipeline->manager ->prepare ();
83+ }
84+ catch (const std::exception &e)
85+ {
86+ printf (" Error while validating pipeline: %s\n %s\n " , e.what (), json);
87+ delete pipeline;
88+ return nullptr ;
9189 }
92- }
9390
94- return pipeline;
91+ return pipeline;
92+ }
93+ return nullptr ;
9594 }
9695
9796 void PDALDisposePipeline (PDALPipelinePtr pipeline)
9897 {
9998 if (pipeline)
10099 {
101- Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
102- ptr->manager .reset ();
103- delete ptr;
100+ delete pipeline;
104101 }
105102 }
106103
@@ -111,58 +108,54 @@ extern "C"
111108 if (pipeline && buffer && size > 0 )
112109 {
113110 Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
114- PipelineManager *manager = ptr->manager .get ();
115111 buffer[0 ] = ' \0 ' ;
116112 buffer[size - 1 ] = ' \0 ' ;
117113
118- if (manager)
114+ try
115+ {
116+ if ( ! ptr->m_executed )
117+ throw pdal_error (" Pipeline has not been executed!" );
118+
119+ std::stringstream strm;
120+ pdal::PipelineWriter::writePipeline (ptr->manager ->getStage (), strm);
121+ std::strncpy (buffer, strm.str ().c_str (), size - 1 );
122+ result = std::min (strm.str ().length (), size);
123+ }
124+ catch (const std::exception &e)
119125 {
120- try
121- {
122- std::stringstream strm;
123- pdal::PipelineWriter::writePipeline (manager->getStage (), strm);
124- std::strncpy (buffer, strm.str ().c_str (), size - 1 );
125- result = std::min (strm.str ().length (), size);
126- }
127- catch (const std::exception &e)
128- {
129- printf (" Found error while retrieving pipeline's string representation: %s\n " , e.what ());
130- }
126+ printf (" Found error while retrieving pipeline's string representation: %s\n " , e.what ());
131127 }
132-
133128 }
134-
135129 return result;
136130 }
137131
138132 size_t PDALGetPipelineMetadata (PDALPipelinePtr pipeline, char *metadata, size_t size)
139133 {
134+ std::cout << " get metadata starts" << std::endl;
140135 size_t result = 0 ;
141136
142137 if (pipeline && metadata && size > 0 )
143138 {
144139 Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
145- PipelineManager *manager = ptr->manager .get ();
146140 metadata[0 ] = ' \0 ' ;
147141 metadata[size - 1 ] = ' \0 ' ;
148142
149- if (manager)
143+ try
144+ {
145+ if ( ! ptr->m_executed )
146+ throw pdal_error (" Pipeline has not been executed!" );
147+
148+ std::stringstream strm;
149+ MetadataNode root = ptr->manager ->getMetadata ().clone (" metadata" );
150+ pdal::Utils::toJSON (root, strm);
151+ std::strncpy (metadata, strm.str ().c_str (), size);
152+ result = std::min (strm.str ().length (), size);
153+ }
154+ catch (const std::exception &e)
150155 {
151- try
152- {
153- std::stringstream strm;
154- MetadataNode root = manager->getMetadata ().clone (" metadata" );
155- pdal::Utils::toJSON (root, strm);
156- std::strncpy (metadata, strm.str ().c_str (), size);
157- result = std::min (strm.str ().length (), size);
158- }
159- catch (const std::exception &e)
160- {
161- printf (" Found error while retrieving pipeline's metadata: %s\n " , e.what ());
162- }
156+ printf (" Found error while retrieving pipeline's metadata: %s\n " , e.what ());
163157 }
164158 }
165-
166159 return result;
167160 }
168161
@@ -173,27 +166,25 @@ extern "C"
173166 if (pipeline && schema && size > 0 )
174167 {
175168 Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
176- PipelineManager *manager = ptr-> manager . get ();
169+
177170 schema[0 ] = ' \0 ' ;
178171 schema[size - 1 ] = ' \0 ' ;
179172
180- if (manager)
173+ try
181174 {
182- try
183- {
184- std::stringstream strm;
185- MetadataNode root = manager->pointTable ().layout ()->toMetadata ().clone (" schema" );
186- pdal::Utils::toJSON (root, strm);
187- std::strncpy (schema, strm.str ().c_str (), size);
188- result = std::min (strm.str ().length (), size);
189- }
190- catch (const std::exception &e)
191- {
192- printf (" Found error while retrieving pipeline's schema: %s\n " , e.what ());
193- }
175+ std::stringstream strm;
176+ MetadataNode meta = ptr->manager ->pointTable ().layout ()->toMetadata ();
177+ MetadataNode root = meta.clone (" schema" );
178+ pdal::Utils::toJSON (root, strm);
179+ std::strncpy (schema, strm.str ().c_str (), size);
180+ result = std::min (strm.str ().length (), size);
181+ }
182+ catch (const std::exception &e)
183+ {
184+ printf (" Found error while retrieving pipeline's schema: %s\n " , e.what ());
194185 }
195- }
196186
187+ }
197188 return result;
198189 }
199190
@@ -204,22 +195,18 @@ extern "C"
204195 if (pipeline && log && size > 0 )
205196 {
206197 Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
207- PipelineManager *manager = ptr->manager .get ();
208198 log[0 ] = ' \0 ' ;
209199 log[size - 1 ] = ' \0 ' ;
210200
211- if (manager)
201+ try
212202 {
213- try
214- {
215- std::string s = ptr->logStream .str ();
216- std::strncpy (log, s.c_str (), size);
217- result = std::min (s.length (), size);
218- }
219- catch (const std::exception &e)
220- {
221- printf (" Found error while retrieving pipeline's log: %s\n " , e.what ());
222- }
203+ std::string s = ptr->logStream .str ();
204+ std::strncpy (log, s.c_str (), size);
205+ result = std::min (s.length (), size);
206+ }
207+ catch (const std::exception &e)
208+ {
209+ printf (" Found error while retrieving pipeline's log: %s\n " , e.what ());
223210 }
224211 }
225212
@@ -229,16 +216,14 @@ extern "C"
229216 void PDALSetPipelineLogLevel (PDALPipelinePtr pipeline, int level)
230217 {
231218 Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
232- PipelineManager *manager = ptr->manager .get ();
233219
234220 try
235221 {
236222 if (level < 0 || level > 8 )
237223 throw pdal_error (" log level must be between 0 and 8!" );
238224
239- ptr->logLevel = static_cast <pdal::LogLevel>(level);
240- pdal::LogPtr lptr = manager->log ();
241- lptr->setLevel (ptr->logLevel );
225+ pdal::LogPtr lptr = ptr->manager ->log ();
226+ lptr->setLevel (static_cast <pdal::LogLevel>(level));
242227 }
243228 catch (const std::exception &e)
244229 {
@@ -250,10 +235,16 @@ extern "C"
250235 int PDALGetPipelineLogLevel (PDALPipelinePtr pipeline)
251236 {
252237 Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
253- return (ptr && ptr->manager .get ())
254- ? static_cast <int >(
255- ptr->manager .get ()->log ()->getLevel ()
256- ) : 0 ;
238+ try
239+ {
240+ return (ptr)
241+ ? static_cast <int >(
242+ ptr->manager ->log ()->getLevel ()
243+ ) : 0 ;
244+ }
245+ catch (const std::exception &e)
246+ {printf (" Found error while getting log level: %s\n " , e.what ()); }
247+ return 0 ;
257248 }
258249
259250 int64_t PDALExecutePipeline (PDALPipelinePtr pipeline)
@@ -265,48 +256,47 @@ extern "C"
265256 {
266257 try
267258 {
268- result = ptr->manager .get ()->execute ();
259+ result = ptr->manager ->execute ();
260+ ptr->m_executed = true ;
269261 }
270262 catch (const std::exception &e)
271263 {
272264 printf (" Found error while executing pipeline: %s" , e.what ());
273265 }
274266 }
275-
276267 return result;
277268 }
278269
279270 bool PDALValidatePipeline (PDALPipelinePtr pipeline)
280271 {
281- int64_t result = 0 ;
282272 Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
283273
284274 if (ptr)
285275 {
286276 try
287277 {
288- ptr->manager .get ()->prepare ();
278+ ptr->manager ->prepare ();
279+ return true ;
289280 }
290281 catch (const std::exception &e)
291282 {
292283 printf (" Found error while validating pipeline: %s" , e.what ());
284+ return false ;
293285 }
294286 }
295-
296- return result;
287+ return false ;
297288 }
298289
299290 PDALPointViewIteratorPtr PDALGetPointViews (PDALPipelinePtr pipeline)
300291 {
301292 Pipeline *ptr = reinterpret_cast <Pipeline *>(pipeline);
302- PipelineManager *manager = ptr->manager .get ();
303293 pdal::capi::PointViewIterator *views = nullptr ;
304294
305295 if (ptr)
306296 {
307297 try
308298 {
309- views = new pdal::capi::PointViewIterator (manager->views ());
299+ views = new pdal::capi::PointViewIterator (ptr-> manager ->views ());
310300 }
311301 catch (const std::exception &e)
312302 {
0 commit comments