Fix bug with gcc<5 and minor changes to filenames

This commit is contained in:
Nikos Karamolegkos 2017-08-02 17:31:26 +03:00
parent 4e5eec817f
commit fe1b612010
2 changed files with 79 additions and 70 deletions

View File

@ -34,23 +34,25 @@ namespace gr
frame_file_sink::make (const std::string& prefix_name, int output_type) frame_file_sink::make (const std::string& prefix_name, int output_type)
{ {
return gnuradio::get_initial_sptr ( return gnuradio::get_initial_sptr (
new frame_file_sink_impl (prefix_name, output_type)); new frame_file_sink_impl (prefix_name, output_type));
} }
/* /*
* The private constructor * The private constructor
*/ */
frame_file_sink_impl::frame_file_sink_impl (const std::string& prefix_name, frame_file_sink_impl::frame_file_sink_impl (const std::string& prefix_name,
int output_type) : int output_type) :
gr::block ("frame_file_sink", gr::io_signature::make (0, 0, 0), gr::block ("frame_file_sink", gr::io_signature::make (0, 0, 0),
gr::io_signature::make (0, 0, 0)), gr::io_signature::make (0, 0, 0)),
d_prefix_name (prefix_name), d_prefix_name (prefix_name),
d_output_type (output_type) d_output_type (output_type),
d_filename_prev(""),
d_counter(0)
{ {
message_port_register_in (pmt::mp ("frame")); message_port_register_in (pmt::mp ("frame"));
set_msg_handler ( set_msg_handler (
pmt::mp ("frame"), pmt::mp ("frame"),
boost::bind (&frame_file_sink_impl::msg_handler_frame, this, _1)); boost::bind (&frame_file_sink_impl::msg_handler_frame, this, _1));
} }
@ -66,77 +68,79 @@ namespace gr
{ {
/* check for the current UTC time */ /* check for the current UTC time */
std::stringstream tmp_time; std::chrono::system_clock::time_point p2 =
std::time_t t = std::time (nullptr); std::chrono::system_clock::now ();
tmp_time << std::put_time (std::gmtime (&t), "%FT%H-%M-%S");
std::string time2string = tmp_time.str (); char buffer[30];
std::time_t t2 = std::chrono::system_clock::to_time_t (p2);
struct tm * timeinfo;
timeinfo = std::gmtime (&t2);
std::strftime (buffer, 30, "%FT%H-%M-%S", timeinfo);
//puts (buffer);
/* create name of the file according prefix and timestamp */ /* create name of the file according prefix and timestamp */
std::string filename; std::string filename;
filename.append (d_prefix_name); filename.append (d_prefix_name);
filename.append ("_"); filename.append ("_");
filename.append (time2string); filename.append (buffer);
if (!filename.compare(d_filename_prev))
{
filename.append ("_");
d_counter++;
filename.append(std::to_string(d_counter));
d_filename_prev.assign(filename);
}
else
{
d_filename_prev.assign(filename);
d_counter=0;
}
uint8_t *su; uint8_t *su;
switch (d_output_type) switch (d_output_type)
{ {
case 0: case 0:
{ {
/* add .txt to filename */ /* add .txt to filename */
filename.append (".txt"); filename.append (".txt");
FILE *fd = fopen (filename.c_str (), "w"); std::ofstream fd (filename.c_str ());
if (fd == NULL) { fd.write ((const char *) pmt::blob_data (msg),
throw std::invalid_argument ("Error opening file"); pmt::blob_length (msg));
fd.close ();
} break;
fwrite ((const char *) pmt::blob_data (msg), pmt::blob_length (msg), }
1, fd); case 1:
fclose (fd); {
break; /* add .hex to filename */
} filename.append (".hex");
case 1: std::ofstream fd (filename.c_str ());
{ su = (uint8_t *) pmt::blob_data (msg);
/* add .hex to filename */ for (size_t i = 0; i < pmt::blob_length (msg); i++) {
filename.append (".hex"); fd << std::hex << std::showbase << std::setw (4)
FILE *fd = fopen (filename.c_str (), "w"); << (uint32_t) su[i] << " ";
if (fd == NULL) { }
throw std::invalid_argument ("Error opening file"); fd.close ();
break;
} }
su = (uint8_t *) pmt::blob_data (msg); case 2:
std::stringstream tmp_hex; {
for (size_t i = 0; i < pmt::blob_length (msg); i++) { /* add .bin to filename */
tmp_hex << std::hex << std::showbase << std::setw (4) filename.append (".bin");
<< (uint32_t) su[i] << " "; std::ofstream fd (filename.c_str ());
} su = (uint8_t *) pmt::blob_data (msg);
std::string str_hex = tmp_hex.str (); for (size_t i = 0; i < pmt::blob_length (msg); i++) {
fprintf (fd, "%s", str_hex.c_str ()); fd << "0b" << std::bitset<8> (su[i]) << " ";
fclose (fd); }
break; fd.close ();
} break;
case 2: }
{ default:
/* add .bin to filename */ throw std::invalid_argument ("Invalid format");
filename.append (".bin"); }
FILE *fd = fopen (filename.c_str (), "w");
if (fd == NULL) {
throw std::invalid_argument ("Error opening file");
}
su = (uint8_t *) pmt::blob_data (msg);
std::stringstream tmp_bin;
for (size_t i = 0; i < pmt::blob_length (msg); i++) {
tmp_bin << "0b" << std::bitset<8> (su[i]) << " ";
}
std::string str_bin = tmp_bin.str ();
fprintf (fd, "%s", str_bin.c_str ());
fclose (fd);
break;
}
default:
throw std::invalid_argument ("Invalid format");
}
} }

View File

@ -22,6 +22,9 @@
#define INCLUDED_SATNOGS_FRAME_FILE_SINK_IMPL_H #define INCLUDED_SATNOGS_FRAME_FILE_SINK_IMPL_H
#include <satnogs/frame_file_sink.h> #include <satnogs/frame_file_sink.h>
#include <chrono>
#include <fstream>
namespace gr namespace gr
{ {
@ -33,6 +36,8 @@ namespace gr
private: private:
const std::string d_prefix_name; const std::string d_prefix_name;
int d_output_type; int d_output_type;
std::string d_filename_prev;
int d_counter;
public: public:
frame_file_sink_impl (const std::string& prefix_name, int output_type); frame_file_sink_impl (const std::string& prefix_name, int output_type);