diff options
| -rw-r--r-- | src/dabOutput/dabOutput.h | 20 | ||||
| -rw-r--r-- | src/dabOutput/dabOutputFifo.cpp | 69 | ||||
| -rw-r--r-- | src/dabOutput/dabOutputFile.cpp | 96 | 
3 files changed, 86 insertions, 99 deletions
diff --git a/src/dabOutput/dabOutput.h b/src/dabOutput/dabOutput.h index b911880..eaa623f 100644 --- a/src/dabOutput/dabOutput.h +++ b/src/dabOutput/dabOutput.h @@ -110,17 +110,6 @@ class DabOutputFile : public DabOutput              type_ = ETI_FILE_TYPE_FRAMED;          } -        DabOutputFile(const DabOutputFile& other) -        { -            filename_ = other.filename_; -            file_ = other.file_; -            nbFrames_ = other.nbFrames_; -            type_ = other.type_; -        } - -        ~DabOutputFile() {} -        const DabOutputFile& operator=(const DabOutputFile& other) = delete; -          int Open(const char* filename);          int Write(void* buffer, int size);          int Close(); @@ -130,6 +119,11 @@ class DabOutputFile : public DabOutput          }      protected: +        /* Set ETI type according to filename, and return +         * filename without the &type=foo part +         */ +        std::string SetEtiType(const std::string& filename); +          std::string filename_;          int file_;          EtiFileType type_; @@ -137,12 +131,10 @@ class DabOutputFile : public DabOutput  };  // ---------- FIFO output ------------ -// only write is different for the FIFO output  class DabOutputFifo : public DabOutputFile  {      public:          DabOutputFifo() : DabOutputFile() {} -        ~DabOutputFifo() {}          int Open(const char* filename);          int Write(void* buffer, int size); @@ -176,7 +168,7 @@ class DabOutputRaw : public DabOutput              delete[] buffer_;          } -        const DabOutputRaw operator=(const DabOutputRaw& other); +        const DabOutputRaw operator=(const DabOutputRaw& other) = delete;          int Open(const char* name);          int Write(void* buffer, int size); diff --git a/src/dabOutput/dabOutputFifo.cpp b/src/dabOutput/dabOutputFifo.cpp index decd443..f466199 100644 --- a/src/dabOutput/dabOutputFifo.cpp +++ b/src/dabOutput/dabOutputFifo.cpp @@ -2,7 +2,7 @@     Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Her Majesty the Queen in     Right of Canada (Communications Research Center Canada) -   Copyright (C) 2013 Matthias P. Braendli +   Copyright (C) 2016 Matthias P. Braendli     http://mpb.li     Fifo output is very similar to file, except it doesn't seek @@ -27,56 +27,39 @@  #include <cstring>  #include <fcntl.h>  #include <limits.h> -#include <sys/types.h>  // mkfifo -#include <sys/stat.h>   // mkfifo +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <errno.h>  #include "dabOutput.h"  int DabOutputFifo::Open(const char* filename)  { -    char* token = strchr((char*)filename, '?'); -    if (token != NULL) { -        *(token++) = 0; -        char* nextPair; -        char* key; -        char* value; -        // Go through all the &stuff=foo pairs -        // Only the key "type" is supported -        do { -            nextPair = strchr(token, '&'); -            if (nextPair != NULL) { -                *nextPair = 0; -            } -            key = token; -            value = strchr(token, '='); -            if (value != NULL) { -                *(value++) = 0; -                if (strcmp(key, "type") == 0) { -                    if (strcmp(value, "raw") == 0) { -                        this->type_ = ETI_FILE_TYPE_RAW; -                        break; -                    } else if (strcmp(value, "framed") == 0) { -                        this->type_ = ETI_FILE_TYPE_FRAMED; -                        break; -                    } else if (strcmp(value, "streamed") == 0) { -                        this->type_ = ETI_FILE_TYPE_STREAMED; -                        break; -                    } else { -                        etiLog.log(error, -                                "File type '%s' is not supported.\n", value); -                        return -1; -                    } -                } -                else { -                    etiLog.log(warn, "Parameter '%s' unknown\n", key); -                } +    filename_ = SetEtiType(filename); + +    // Create the fifo if it does not already exist +    struct stat s = {0}; +    int ret = stat(filename_.c_str(), &s); + +    if (ret == -1) { +        if (errno == ENOENT) { +            ret = mkfifo(filename_.c_str(), 0666); +            if (ret == -1) { +                etiLog.level(error) << "Could not create fifo " << filename_ << " : " << +                    strerror(errno); +                return -1;              } -        } while (nextPair != NULL); +        } +        else { +            etiLog.level(error) << "Could not stat fifo " << filename_ << " : " << +                strerror(errno); +            return -1; +        }      } -    this->file_ = mkfifo(filename, 0666); -    this->file_ = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666); +    this->file_ = open(filename_.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);      if (this->file_ == -1) { -        perror(filename); +        perror(filename_.c_str());          return -1;      }      return 0; diff --git a/src/dabOutput/dabOutputFile.cpp b/src/dabOutput/dabOutputFile.cpp index 5b7543c..b8e9e70 100644 --- a/src/dabOutput/dabOutputFile.cpp +++ b/src/dabOutput/dabOutputFile.cpp @@ -2,7 +2,7 @@     Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Her Majesty the Queen in     Right of Canada (Communications Research Center Canada) -   Copyright (C) 2013 Matthias P. Braendli +   Copyright (C) 2016 Matthias P. Braendli     http://mpb.li     File output @@ -23,6 +23,7 @@     You should have received a copy of the GNU General Public License     along with ODR-DabMux.  If not, see <http://www.gnu.org/licenses/>.     */ +#include <string>  #include <cstdio>  #include <cstring>  #include <fcntl.h> @@ -31,49 +32,11 @@  int DabOutputFile::Open(const char* filename)  { -    char* token = strchr((char*)filename, '?'); -    if (token != NULL) { -        *(token++) = 0; -        char* nextPair; -        char* key; -        char* value; -        // Go through all the &stuff=foo pairs -        // Only the key "type" is supported -        do { -            nextPair = strchr(token, '&'); -            if (nextPair != NULL) { -                *nextPair = 0; -            } -            key = token; -            value = strchr(token, '='); -            if (value != NULL) { -                *(value++) = 0; -                if (strcmp(key, "type") == 0) { -                    if (strcmp(value, "raw") == 0) { -                        this->type_ = ETI_FILE_TYPE_RAW; -                        break; -                    } else if (strcmp(value, "framed") == 0) { -                        this->type_ = ETI_FILE_TYPE_FRAMED; -                        break; -                    } else if (strcmp(value, "streamed") == 0) { -                        this->type_ = ETI_FILE_TYPE_STREAMED; -                        break; -                    } else { -                        etiLog.log(error, -                                "File type '%s' is not supported.\n", value); -                        return -1; -                    } -                } -                else { -                    etiLog.log(warn, "Parameter '%s' unknown\n", key); -                } -            } -        } while (nextPair != NULL); -    } +    filename_ = SetEtiType(filename); -    this->file_ = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666); +    this->file_ = open(filename_.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);      if (this->file_ == -1) { -        perror(filename); +        perror(filename_.c_str());          return -1;      }      return 0; @@ -136,3 +99,52 @@ int DabOutputFile::Close()      return -1;  } +std::string DabOutputFile::SetEtiType(const std::string& filename) +{ +    size_t ix = filename.find('?'); +    const std::string filename_before_q = filename.substr(0, ix); + +    if (ix != std::string::npos) { + +        do { +            const size_t ix_key = ix + 1; + +            const size_t ix_eq = filename.find('=', ix); +            if (ix_eq == std::string::npos) { +                // no equals sign, not a valid query. Return up to the question mark +                break; +            } + +            const size_t ix_val = ix_eq + 1; + +            const std::string key = filename.substr(ix_key, ix_eq - ix_key); + +            ix = filename.find('&', ix); +            const size_t len_value = (ix == std::string::npos) ? std::string::npos : ix - ix_val; + +            if (key == "type") { +                const std::string value = filename.substr(ix_val, len_value); + +                if (value == "raw") { +                    this->type_ = ETI_FILE_TYPE_RAW; +                    break; +                } else if (value == "framed") { +                    this->type_ = ETI_FILE_TYPE_FRAMED; +                    break; +                } else if (value == "streamed") { +                    this->type_ = ETI_FILE_TYPE_STREAMED; +                    break; +                } else { +                    std::stringstream ss; +                    ss << "File type '" << value << "' is not supported."; +                    throw std::runtime_error(ss.str()); +                } +            } + +        } +        while (ix != std::string::npos); +    } + +    return filename_before_q; +} +  | 
