@@ -231,6 +231,35 @@ get_codec(const std::string &path, const std::string &ext=".g3")
231231 log_fatal (" Invalid filename %s" , path.c_str ());
232232}
233233
234+ static Codec
235+ check_input_path (const std::string &path, const std::string &ext)
236+ {
237+ std::filesystem::path fpath (path);
238+ if (!std::filesystem::exists (fpath) ||
239+ !std::filesystem::is_regular_file (fpath))
240+ log_fatal (" Could not find file %s" , path.c_str ());
241+
242+ return get_codec (path, ext);
243+ }
244+
245+ static Codec
246+ check_output_path (const std::string &path, const std::string &ext)
247+ {
248+ std::filesystem::path fpath (path);
249+
250+ if (fpath.empty ())
251+ log_fatal (" Empty file path" );
252+
253+ if (fpath.has_parent_path ()) {
254+ auto ppath = fpath.parent_path ();
255+ if (!std::filesystem::exists (ppath))
256+ log_fatal (" Parent path does not exist: %s" ,
257+ ppath.string ().c_str ());
258+ }
259+
260+ return get_codec (path, ext);
261+ }
262+
234263class InputFileStreamCounter : public std ::streambuf {
235264public:
236265 InputFileStreamCounter (const std::string& path, size_t size)
@@ -286,57 +315,6 @@ class InputFileStreamCounter : public std::streambuf {
286315 size_t bytes_;
287316};
288317
289- void
290- g3_istream_from_path (std::istream &stream, const std::string &path, float timeout,
291- size_t buffersize, const std::string &ext)
292- {
293- g3_stream_close (stream);
294-
295- std::streambuf *sbuf = nullptr ;
296-
297- // Figure out what kind of ultimate data source this is
298- if (path.find (" tcp://" ) == 0 ) {
299- sbuf = new RemoteInputStreamBuffer (path, timeout, buffersize);
300- } else {
301- // Simple file case
302- switch (get_codec (path, ext)) {
303- #ifdef ZLIB_FOUND
304- case GZ:
305- sbuf = new GZipDecoder (path, buffersize);
306- break ;
307- #endif
308- #ifdef BZIP2_FOUND
309- case BZIP2:
310- sbuf = new BZip2Decoder (path, buffersize);
311- break ;
312- #endif
313- #ifdef LZMA_FOUND
314- case LZMA:
315- sbuf = new LZMADecoder (path, buffersize);
316- break ;
317- #endif
318- default :
319- // Read buffer
320- sbuf = new InputFileStreamCounter (path, buffersize);
321- break ;
322- }
323- }
324-
325- stream.rdbuf (sbuf);
326- }
327-
328- int
329- g3_istream_handle (std::istream &stream)
330- {
331- std::streambuf* sbuf = stream.rdbuf ();
332- if (!sbuf)
333- return -1 ;
334- RemoteInputStreamBuffer* rbuf = dynamic_cast <RemoteInputStreamBuffer*>(sbuf);
335- if (!rbuf)
336- return -1 ;
337- return rbuf->fd ();
338- }
339-
340318class OutputFileStreamCounter : public std ::streambuf {
341319public:
342320 OutputFileStreamCounter (const std::string& path, size_t size, bool append)
@@ -395,6 +373,60 @@ class OutputFileStreamCounter : public std::streambuf {
395373 size_t bytes_;
396374};
397375
376+
377+ void
378+ g3_istream_from_path (std::istream &stream, const std::string &path, float timeout,
379+ size_t buffersize, const std::string &ext)
380+ {
381+ g3_stream_close (stream);
382+
383+ std::streambuf *sbuf = nullptr ;
384+
385+ // Figure out what kind of ultimate data source this is
386+ if (path.find (" tcp://" ) == 0 ) {
387+ sbuf = new RemoteInputStreamBuffer (path, timeout, buffersize);
388+ goto done;
389+ }
390+
391+ // Simple file case
392+ switch (check_input_path (path, ext)) {
393+ #ifdef ZLIB_FOUND
394+ case GZ:
395+ sbuf = new GZipDecoder (path, buffersize);
396+ break ;
397+ #endif
398+ #ifdef BZIP2_FOUND
399+ case BZIP2:
400+ sbuf = new BZip2Decoder (path, buffersize);
401+ break ;
402+ #endif
403+ #ifdef LZMA_FOUND
404+ case LZMA:
405+ sbuf = new LZMADecoder (path, buffersize);
406+ break ;
407+ #endif
408+ default :
409+ // Read buffer
410+ sbuf = new InputFileStreamCounter (path, buffersize);
411+ break ;
412+ }
413+
414+ done:
415+ stream.rdbuf (sbuf);
416+ }
417+
418+ int
419+ g3_istream_handle (std::istream &stream)
420+ {
421+ std::streambuf* sbuf = stream.rdbuf ();
422+ if (!sbuf)
423+ return -1 ;
424+ RemoteInputStreamBuffer* rbuf = dynamic_cast <RemoteInputStreamBuffer*>(sbuf);
425+ if (!rbuf)
426+ return -1 ;
427+ return rbuf->fd ();
428+ }
429+
398430void
399431g3_ostream_to_path (std::ostream &stream, const std::string &path, bool append,
400432 size_t buffersize, const std::string &ext)
@@ -403,7 +435,7 @@ g3_ostream_to_path(std::ostream &stream, const std::string &path, bool append,
403435
404436 std::streambuf *sbuf = nullptr ;
405437
406- Codec codec = get_codec (path, ext);
438+ Codec codec = check_output_path (path, ext);
407439 if (append && codec != NONE)
408440 log_fatal (" Cannot append to compressed file." );
409441
@@ -439,31 +471,3 @@ g3_stream_close(std::ios &stream)
439471 delete sbuf;
440472 stream.rdbuf (nullptr );
441473}
442-
443- void
444- g3_check_input_path (const std::string &path)
445- {
446- if (path.find (" ://" ) != path.npos )
447- return ;
448-
449- std::filesystem::path fpath (path);
450- if (!std::filesystem::exists (fpath) ||
451- !std::filesystem::is_regular_file (fpath))
452- log_fatal (" Could not find file %s" , path.c_str ());
453- }
454-
455- void
456- g3_check_output_path (const std::string &path)
457- {
458- std::filesystem::path fpath (path);
459-
460- if (fpath.empty ())
461- log_fatal (" Empty file path" );
462-
463- if (!fpath.has_parent_path ())
464- return ;
465-
466- if (!std::filesystem::exists (fpath.parent_path ()))
467- log_fatal (" Parent path does not exist: %s" ,
468- fpath.parent_path ().string ().c_str ());
469- }
0 commit comments