diff options
author | Tom Rondeau | 2012-12-14 10:28:05 -0500 |
---|---|---|
committer | Tom Rondeau | 2012-12-14 10:28:05 -0500 |
commit | d12b7b7511c9751af973d1f3cd8fc8020000f4ac (patch) | |
tree | 829389085072ad6d19917b7aaa59e76fbc5584e0 /gnuradio-core/src | |
parent | 996e5996d64f90c85da8681e7df3086e20bcb530 (diff) | |
download | gnuradio-d12b7b7511c9751af973d1f3cd8fc8020000f4ac.tar.gz gnuradio-d12b7b7511c9751af973d1f3cd8fc8020000f4ac.tar.bz2 gnuradio-d12b7b7511c9751af973d1f3cd8fc8020000f4ac.zip |
core: updated file metadata source to support detached header files.
Diffstat (limited to 'gnuradio-core/src')
-rw-r--r-- | gnuradio-core/src/lib/io/gr_file_meta_source.cc | 42 | ||||
-rw-r--r-- | gnuradio-core/src/lib/io/gr_file_meta_source.h | 17 | ||||
-rw-r--r-- | gnuradio-core/src/lib/io/gr_file_meta_source.i | 11 |
3 files changed, 43 insertions, 27 deletions
diff --git a/gnuradio-core/src/lib/io/gr_file_meta_source.cc b/gnuradio-core/src/lib/io/gr_file_meta_source.cc index 02955e7b9..669c5d477 100644 --- a/gnuradio-core/src/lib/io/gr_file_meta_source.cc +++ b/gnuradio-core/src/lib/io/gr_file_meta_source.cc @@ -50,10 +50,10 @@ #endif gr_file_meta_source_sptr -gr_make_file_meta_source(const char *filename, +gr_make_file_meta_source(const std::string &filename, bool repeat, bool detached_header, - const char *hdr_filename) + const std::string &hdr_filename) { return gnuradio::get_initial_sptr (new gr_file_meta_source(filename, @@ -62,10 +62,10 @@ gr_make_file_meta_source(const char *filename, hdr_filename)); } -gr_file_meta_source::gr_file_meta_source(const char *filename, +gr_file_meta_source::gr_file_meta_source(const std::string &filename, bool repeat, bool detached_header, - const char *hdr_filename) + const std::string &hdr_filename) : gr_sync_block("file_meta_source", gr_make_io_signature(0, 0, 0), gr_make_io_signature(1, 1, 1)), @@ -78,12 +78,13 @@ gr_file_meta_source::gr_file_meta_source(const char *filename, d_hdr_fp = 0; d_new_hdr_fp = 0; - if(detached_header == true) + if(detached_header == true) { d_state = STATE_DETACHED; + } else d_state = STATE_INLINE; - if(!open(filename)) + if(!open(filename, hdr_filename)) throw std::runtime_error("file_meta_source: can't open file\n"); do_update(); @@ -120,15 +121,22 @@ gr_file_meta_source::~gr_file_meta_source() bool gr_file_meta_source::read_header(pmt_t &hdr, pmt_t &extras) { + // Select which file handle to read from. + FILE *fp; + if(d_state == STATE_DETACHED) + fp = d_hdr_fp; + else + fp = d_fp; + size_t ret; size_t size = 0; std::string str; char *hdr_buffer = new char[METADATA_HEADER_SIZE]; while(size < METADATA_HEADER_SIZE) { - ret = fread(&hdr_buffer[size], sizeof(char), METADATA_HEADER_SIZE-size, d_fp); + ret = fread(&hdr_buffer[size], sizeof(char), METADATA_HEADER_SIZE-size, fp); if(ret == 0) { delete [] hdr_buffer; - if(feof(d_fp)) + if(feof(fp)) return false; else { std::stringstream s; @@ -157,10 +165,10 @@ gr_file_meta_source::read_header(pmt_t &hdr, pmt_t &extras) size = 0; hdr_buffer = new char[extra_len]; while(size < extra_len) { - ret = fread(&hdr_buffer[size], sizeof(char), extra_len-size, d_fp); + ret = fread(&hdr_buffer[size], sizeof(char), extra_len-size, fp); if(ret == 0) { delete [] hdr_buffer; - if(feof(d_fp)) + if(feof(fp)) return false; else { std::stringstream s; @@ -283,16 +291,20 @@ gr_file_meta_source::parse_extras(pmt_t extras, uint64_t offset, } bool -gr_file_meta_source::open(const char *filename) +gr_file_meta_source::open(const std::string &filename, + const std::string &hdr_filename) { bool ret = true; if(d_state == STATE_DETACHED) { - std::stringstream s; - s << filename << ".hdr"; - ret = _open(&d_new_hdr_fp, s.str().c_str()); + std::string s; + if(hdr_filename == "") + s = filename + ".hdr"; + else + s = hdr_filename; + ret = _open(&d_new_hdr_fp, s.c_str()); } - ret = ret && _open(&d_new_fp, filename); + ret = ret && _open(&d_new_fp, filename.c_str()); d_updated = true; return ret; } diff --git a/gnuradio-core/src/lib/io/gr_file_meta_source.h b/gnuradio-core/src/lib/io/gr_file_meta_source.h index cb34e11d9..95e466936 100644 --- a/gnuradio-core/src/lib/io/gr_file_meta_source.h +++ b/gnuradio-core/src/lib/io/gr_file_meta_source.h @@ -36,10 +36,10 @@ class gr_file_meta_source; typedef boost::shared_ptr<gr_file_meta_source> gr_file_meta_source_sptr; GR_CORE_API gr_file_meta_source_sptr -gr_make_file_meta_source(const char *filename, +gr_make_file_meta_source(const std::string &filename, bool repeat=false, bool detached_header=false, - const char *hdr_filename=""); + const std::string &hdr_filename=""); /*! * \brief Reads stream from file with meta-data headers. Headers are @@ -68,12 +68,14 @@ class GR_CORE_API gr_file_meta_source : public gr_sync_block * \param detached_header (bool): Set to true if header * info is stored in a separate file (usually named filename.hdr) * \param hdr_filename (string): Name of detached header file if used. + * Defaults to 'filename.hdr' if detached_header is true but this + * field is an empty string. */ friend GR_CORE_API gr_file_meta_source_sptr - gr_make_file_meta_source(const char *filename, + gr_make_file_meta_source(const std::string &filename, bool repeat, bool detached_header, - const char *hdr_filename); + const std::string &hdr_filename); private: enum meta_state_t { @@ -96,10 +98,10 @@ class GR_CORE_API gr_file_meta_source : public gr_sync_block std::vector<gr_tag_t> d_tags; protected: - gr_file_meta_source(const char *filename, + gr_file_meta_source(const std::string &filename, bool repeat=false, bool detached_header=false, - const char *hdr_filename=""); + const std::string &hdr_filename=""); bool _open(FILE **fp, const char *filename); bool read_header(pmt_t &hdr, pmt_t &extras); @@ -111,7 +113,8 @@ class GR_CORE_API gr_file_meta_source : public gr_sync_block public: ~gr_file_meta_source(); - bool open(const char *filename); + bool open(const std::string &filename, + const std::string &hdr_filename=""); void close(); void do_update(); diff --git a/gnuradio-core/src/lib/io/gr_file_meta_source.i b/gnuradio-core/src/lib/io/gr_file_meta_source.i index 6cf089da1..cb1281036 100644 --- a/gnuradio-core/src/lib/io/gr_file_meta_source.i +++ b/gnuradio-core/src/lib/io/gr_file_meta_source.i @@ -23,22 +23,23 @@ GR_SWIG_BLOCK_MAGIC(gr,file_meta_source) gr_file_meta_source_sptr -gr_make_file_meta_source(const char *filename, +gr_make_file_meta_source(const std::string &filename, bool repeat=false, bool detached_header=false, - const char *hdr_filename=""); + const std::string &hdr_filename=""); class gr_file_meta_source : public gr_sync_block { protected: - gr_file_meta_source(const char *filename, + gr_file_meta_source(const std::string &filename, bool repeat, bool detached_header, - const char *hdr_filename); + const std::string &hdr_filename); public: ~gr_file_meta_source(); - bool open(const char *filename); + bool open(const std::string &filename, + const std::string &hdr_filename=""); void close(); }; |