Skip to content

Commit

Permalink
mkrufky#5 Added format sequence detection and handling for output fil…
Browse files Browse the repository at this point in the history
…e name
  • Loading branch information
pashamesh committed Aug 4, 2015
1 parent 7dddae0 commit c7e7e07
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
60 changes: 57 additions & 3 deletions libdvbtee/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,21 @@ void output_stream::close_file()
**/
int output_stream::change_file(char* target_file)
{
std::stringstream tmp_stream;
std::string new_name;

tmp_stream << target_file << "_" << name_index;
tmp_stream >> new_name;
if (detect_printf_seq(target_file)) {

This comment has been minimized.

Copy link
@mkrufky

mkrufky Aug 11, 2015

passing a char * into a function that takes a const std::string & feels questionable to me. looks like it could be prone to error

This comment has been minimized.

Copy link
@pashamesh

pashamesh Aug 12, 2015

Author Owner

I will check it

dprintf("sequence detected");

char buff[100];

This comment has been minimized.

Copy link
@mkrufky

mkrufky Aug 11, 2015

This char buff[] is going to contain a file name or maybe a full path name. Better to use a size of 256 to match the size of char filename[] in main() of dvbtee.cpp

This comment has been minimized.

Copy link
@pashamesh

pashamesh Aug 12, 2015

Author Owner

will change to 256

snprintf(buff, sizeof(buff), target_file, name_index);
new_name = buff;
} else {
dprintf("sequence not detected");

std::stringstream tmp_stream;
tmp_stream << target_file << "_" << name_index;
tmp_stream >> new_name;
}

dprintf("sock: %d, old: %s, new: %s", sock, target_file, new_name.c_str());

Expand All @@ -586,6 +596,50 @@ int output_stream::change_file(char* target_file)
return 0;
}

/**
Search for printf format sequence in string.
@param char* str target string.

This comment has been minimized.

Copy link
@mkrufky

mkrufky Aug 11, 2015

the comment doesn't match the actual parameter that the function takes as str

@return bool
**/
bool output_stream::detect_printf_seq(const std::string& str) {
std::string::size_type last_pos = 0;

do {
last_pos = str.find('%', last_pos);

if (last_pos == std::string::npos)
break; // Not found anythin.

if (last_pos == str.length() - 1)
break; // Found stray '%' at the end of the string.

char ch = str[last_pos + 1];

if (ch == '%') // double percent -> escaped %. Go on for next.
{
last_pos += 2;
continue;
}

while (
last_pos < str.length()
&& std::string("0123456789").find(ch) != std::string::npos
) {
last_pos++;
ch = str[last_pos+1];
}

if (ch == 'd')
{
return true;
}
last_pos++;
} while (last_pos < str.length());

return false;
}

int output_stream::add(void* priv, stream_callback callback, map_pidtype &pids)
{
stream_cb = callback;
Expand Down
1 change: 1 addition & 0 deletions libdvbtee/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class output_stream
void stop();
inline void stop_after_drain() { if (drain()) stop(); }
int change_file(char*);
bool detect_printf_seq(const std::string&);
void close_file();

bool push(uint8_t*, int);
Expand Down

1 comment on commit c7e7e07

@mkrufky
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this - very cool idea :-) ... and it addresses my concern about zero-padding from the previous patch

Please sign in to comment.