1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* ShowLoader.cpp
* A class that reads OLA show files
* Copyright (C) 2011 Simon Newton
*
* The data file is in the form:
* universe-number channel1,channel2,channel3
* delay-in-ms
* universe-number channel1,channel2,channel3
*/
#include <errno.h>
#include <string.h>
#include <ola/DmxBuffer.h>
#include <ola/Logging.h>
#include <ola/StringUtils.h>
#include <fstream>
#include <ios>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "examples/ShowLoader.h"
using std::vector;
using std::string;
using ola::DmxBuffer;
const char ShowLoader::OLA_SHOW_HEADER[] = "OLA Show";
ShowLoader::ShowLoader(const string &filename)
: m_filename(filename),
m_line(0) {
}
ShowLoader::~ShowLoader() {
if (m_show_file.is_open()) {
m_show_file.close();
}
}
/**
* Load the show file.
* @returns true if we could open the file, false otherwise.
*/
bool ShowLoader::Load() {
m_show_file.open(m_filename.data());
if (!m_show_file.is_open()) {
OLA_FATAL << "Can't open " << m_filename << ": " << strerror(errno);
return false;
}
string line;
ReadLine(&line);
if (line != OLA_SHOW_HEADER) {
OLA_WARN << "Invalid show file, expecting " << OLA_SHOW_HEADER << " got "
<< line;
return false;
}
return true;
}
/**
* Reset to the start of the show
*/
void ShowLoader::Reset() {
m_show_file.clear();
m_show_file.seekg(0, std::ios::beg);
// skip over the first line
string line;
ReadLine(&line);
}
/**
* Get the next time offset
* @param timeout a pointer to the timeout in ms
*/
ShowLoader::State ShowLoader::NextTimeout(unsigned int *timeout) {
string line;
ReadLine(&line);
if (line.empty()) {
return END_OF_FILE;
}
if (!ola::StringToInt(line, timeout, true)) {
OLA_WARN << "Line " << m_line << ": Invalid timeout: " << line;
return INVALID_LINE;
}
return OK;
}
/**
* Read the next DMX frame.
* @param universe the universe to send on
* @param data the DMX data
*/
ShowLoader::State ShowLoader::NextFrame(unsigned int *universe,
DmxBuffer *data) {
string line;
ReadLine(&line);
if (line.empty()) {
return END_OF_FILE;
}
vector<string> inputs;
ola::StringSplit(line, &inputs);
if (inputs.size() != 2) {
OLA_WARN << "Line " << m_line << " invalid: " << line;
return INVALID_LINE;
}
if (!ola::StringToInt(inputs[0], universe, true)) {
OLA_WARN << "Line " << m_line << " invalid: " << line;
return INVALID_LINE;
}
return (data->SetFromString(inputs[1]) ? OK : INVALID_LINE);
}
void ShowLoader::ReadLine(string *line) {
getline(m_show_file, *line);
ola::StripSuffix(line, "\r");
m_line++;
}
|