comparison framework/OSCScript.h @ 658:94f518af106c single-point

Support reading OSC script from stdin; make it possible to run OSC script even without external OSC port
author Chris Cannam
date Thu, 28 Mar 2019 13:37:40 +0000
parents 4f53620962d9
children 2915cc467ebf
comparison
equal deleted inserted replaced
657:029c224384d1 658:94f518af106c
36 m_queue(queue), 36 m_queue(queue),
37 m_abandoning(false) { 37 m_abandoning(false) {
38 } 38 }
39 39
40 void run() override { 40 void run() override {
41 41
42 QFile f(m_filename);
43 if (!f.open(QFile::ReadOnly | QFile::Text)) {
44 SVCERR << "OSCScript: Failed to open script file \""
45 << m_filename << "\" for reading" << endl;
46 throw std::runtime_error("OSC script file not found");
47 }
48
49 if (!m_queue) { 42 if (!m_queue) {
50 SVCERR << "OSCScript: No OSC queue available" << endl; 43 SVCERR << "OSCScript: No OSC queue available" << endl;
51 throw std::runtime_error("OSC queue not running"); 44 throw std::runtime_error("OSC queue not running");
52 } 45 }
46
47 QFile f;
48 QString reportedFilename;
49
50 if (m_filename == "-") {
51 f.open(stdin, QFile::ReadOnly | QFile::Text);
52 reportedFilename = "<stdin>";
53 } else {
54 f.setFileName(m_filename);
55 if (!f.open(QFile::ReadOnly | QFile::Text)) {
56 SVCERR << "OSCScript: Failed to open script file \""
57 << m_filename << "\" for reading" << endl;
58 throw std::runtime_error("OSC script file not found");
59 }
60 reportedFilename = m_filename;
61 }
53 62
63 QTextStream str(&f);
54 int lineno = 0; 64 int lineno = 0;
55 QTextStream str(&f); 65
56
57 while (!str.atEnd() && !m_abandoning) { 66 while (!str.atEnd() && !m_abandoning) {
58 67
59 ++lineno; 68 ++lineno;
60 69
61 QString line = str.readLine().trimmed(); 70 QString line = str.readLine().trimmed();
66 75
67 } else if (line[0].isDigit()) { 76 } else if (line[0].isDigit()) {
68 bool ok = false; 77 bool ok = false;
69 float pause = line.toFloat(&ok); 78 float pause = line.toFloat(&ok);
70 if (ok) { 79 if (ok) {
71 SVCERR << "OSCScript: " << m_filename << ":" << lineno 80 SVCERR << "OSCScript: "
81 << reportedFilename << ":" << lineno
72 << ": pausing for " << pause << " sec" << endl; 82 << ": pausing for " << pause << " sec" << endl;
73 msleep(unsigned(round(pause * 1000.0f))); 83 msleep(unsigned(round(pause * 1000.0f)));
74 continue; 84 continue;
75 } else { 85 } else {
76 SVCERR << "OSCScript: " << m_filename << ":" << lineno 86 SVCERR << "OSCScript: "
77 << ": error: failed to parse sleep time, giving up" 87 << reportedFilename << ":" << lineno
88 << ": warning: failed to parse sleep time, ignoring"
78 << endl; 89 << endl;
79 throw std::runtime_error("OSC script parse error"); 90 continue;
80 } 91 }
81 92
82 } else if (line[0] == '/' && line.size() > 1) { 93 } else if (line[0] == '/' && line.size() > 1) {
83 QStringList parts = StringBits::splitQuoted(line, ' '); 94 QStringList parts = StringBits::splitQuoted(line, ' ');
84 if (parts.empty()) { 95 if (parts.empty()) {
85 SVCERR << "OSCScript: " << m_filename << ":" << lineno 96 SVCERR << "OSCScript: "
97 << reportedFilename << ":" << lineno
86 << ": warning: empty command spec, ignoring" 98 << ": warning: empty command spec, ignoring"
87 << endl; 99 << endl;
88 continue; 100 continue;
89 } 101 }
90 OSCMessage message; 102 OSCMessage message;
91 message.setMethod(parts[0].mid(1)); 103 message.setMethod(parts[0].mid(1));
92 for (int i = 1; i < parts.size(); ++i) { 104 for (int i = 1; i < parts.size(); ++i) {
93 message.addArg(parts[i]); 105 message.addArg(parts[i]);
94 } 106 }
95 SVCERR << "OSCScript: " << m_filename << ":" << lineno 107 SVCERR << "OSCScript: " << reportedFilename << ":" << lineno
96 << ": invoking: \"" << parts[0] << "\"" << endl; 108 << ": invoking: \"" << parts[0] << "\"" << endl;
97 m_queue->postMessage(message); 109 m_queue->postMessage(message);
98 110
99 } else { 111 } else {
100 SVCERR << "OSCScript: " << m_filename << ":" << lineno 112 SVCERR << "OSCScript: " << reportedFilename << ":" << lineno
101 << ": error: message expected" << endl; 113 << ": warning: message expected, ignoring" << endl;
102 throw std::runtime_error("OSC script parse error");
103 } 114 }
104 } 115 }
105 116
106 SVCERR << "OSCScript: " << m_filename << ": finished" << endl; 117 SVCERR << "OSCScript: " << reportedFilename << ": finished" << endl;
107 } 118 }
108 119
109 void abandon() { 120 void abandon() {
110 m_abandoning = true; 121 m_abandoning = true;
111 } 122 }