| 1 | /* |
| 2 | Copyright (C) 2008, 2009 Andres Cabrera |
| 3 | [email protected] |
| 4 | |
| 5 | This file is part of CsoundQt. |
| 6 | |
| 7 | CsoundQt is free software; you can redistribute it |
| 8 | and/or modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation; either |
| 10 | version 2.1 of the License, or (at your option) any later version. |
| 11 | |
| 12 | CsoundQt is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with Csound; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 20 | 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include "opentryparser.h" |
| 24 | #include "types.h" |
| 25 | #include "algorithm" |
| 26 | |
| 27 | #include <QFile> |
| 28 | |
| 29 | void OpEntryParser::parseOpcodesXml(QString opcodeFile) { |
| 30 | QDomDocument m_doc("opcodes"); |
| 31 | QFile file(opcodeFile); |
| 32 | if (!file.open(QIODevice::ReadOnly)) { |
| 33 | qDebug() << "OpEntryParser::OpEntryParser could not find opcode file:" << opcodeFile; |
| 34 | return; |
| 35 | } |
| 36 | if (!m_doc.setContent(&file)) { |
| 37 | qDebug() << "OpEntryParser::OpEntryParser set content"; |
| 38 | file.close(); |
| 39 | return; |
| 40 | } |
| 41 | file.close(); |
| 42 | excludedOpcodes << "|" << "||" << "^" << "+" << "*" << "-" << "/"; |
| 43 | QDomElement docElem = m_doc.documentElement(); |
| 44 | QList<Opcode> opcodesInCategoryList; |
| 45 | |
| 46 | QDomElement cat = docElem.firstChildElement("category"); |
| 47 | while(!cat.isNull()) { |
| 48 | QString catName = cat.attribute("name", "Miscellaneous"); |
| 49 | opcodesInCategoryList.clear(); |
| 50 | QDomElement opcode = cat.firstChildElement("opcode"); |
| 51 | while(!opcode.isNull()) { |
| 52 | QDomElement desc = opcode.firstChildElement("desc"); |
| 53 | QString description = desc.text(); |
| 54 | QDomElement synop = opcode.firstChildElement("synopsis"); |
| 55 | while(!synop.isNull()) { |
| 56 | Opcode op; |
| 57 | op.desc = description; |
| 58 | op.isInstalled = true; |
| 59 | QDomElement s = synop.toElement(); |
| 60 | QDomNode node = s.firstChild(); |
| 61 | QDomElement elem = node.toElement(); |
| 62 | if (elem.tagName()=="opcodename") { |
| 63 | op.opcodeName = elem.text().simplified(); |
| 64 | op.inArgs = node.nextSibling().toText().data().simplified(); |
| 65 | } |
| 66 | else { |
| 67 | op.outArgs = node.toText().data().simplified(); |
| 68 | node = node.nextSibling(); |
| 69 | op.opcodeName = node.toElement().text().simplified(); |
| 70 | node = node.nextSibling(); |
| 71 | if (!node.isNull()) |
| 72 | op.inArgs = node.toText().data().simplified(); |
| 73 | } |
| 74 | // check if several parenthesis ie description added to inArgs like "(MidiNoteNumber) (init- or control-rate args only)" |
| 75 | // remove, if existing |
| 76 | if (op.inArgs.count("(")>1) { |
| 77 | QString inArgs = op.inArgs; |
| 78 | int lastIndex = inArgs.lastIndexOf("("); |
| 79 | if (lastIndex>0) { |
| 80 | inArgs = inArgs.left(lastIndex-1); |
| 81 | op.inArgs = inArgs; |
| 82 | } |
| 83 | } |
| 84 | if (op.opcodeName != "" && excludedOpcodes.count(op.opcodeName) == 0 |
| 85 | && catName !="Utilities") { |
| 86 | addOpcode(op); |
| 87 | opcodesInCategoryList << op; |
| 88 | |
| 89 | } |
| 90 | synop = synop.nextSiblingElement("synopsis"); |
| 91 | } |
| 92 | opcode = opcode.nextSiblingElement("opcode"); |
| 93 | } |
| 94 | QPair<QString, QList<Opcode> > newCategory(catName, opcodesInCategoryList); |
| 95 | opcodeListCategory.append(opcodesInCategoryList); |
| 96 | categoryList.append(catName); |
| 97 | opcodeCategoryList.append(newCategory); |
| 98 | cat = cat.nextSiblingElement("category"); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | OpEntryParser::OpEntryParser(QString opcodeFile) |
| 103 | : m_opcodeFile(opcodeFile) |
| 104 | { |
| 105 | m_udosMap = nullptr; |
| 106 | parseOpcodesXml(opcodeFile); |
| 107 | addExtraOpcodes(); |
| 108 | } |
| 109 | |
| 110 | OpEntryParser::~OpEntryParser() |
| 111 | { |
| 112 | } |
| 113 | |
| 114 | void OpEntryParser::addExtraOpcodes() |
| 115 | { |
| 116 | addOpcode(Opcode("then")); |
| 117 | |
| 118 | addFlag("use-system-sr", "Use the samplerate defined by the realtime audio backend"); |
| 119 | addFlag("omacro", "--omacro:XXX=YYY set orchestra macro XXX to value YYY"); |
| 120 | addFlag("port", "--port=N. listen to UDP port N for instruments/orchestra code"); |
| 121 | |
| 122 | } |
| 123 | |
| 124 | void OpEntryParser::sortOpcodes() |
| 125 | { |
| 126 | std::sort(opcodeList.begin(), opcodeList.end(), |
| 127 | [](const Opcode &a, const Opcode &b) -> bool { return a.opcodeName < b.opcodeName; }); |
| 128 | } |
| 129 | |
| 130 | QStringList OpEntryParser::opcodeNameList(bool includeDisabled) |
| 131 | { |
| 132 | if(!m_opcodeNameListCache.isEmpty()) { |
| 133 | return m_opcodeNameListCache; |
| 134 | } |
| 135 | QStringList list; |
| 136 | includeDisabled = false; |
| 137 | for(auto opcode : opcodeList) { |
| 138 | if(includeDisabled || opcode.isInstalled) { |
| 139 | list.append(opcode.opcodeName); |
| 140 | } |
| 141 | } |
| 142 | m_opcodeNameListCache = list; |
| 143 | return list; |
| 144 | } |
| 145 | |
| 146 | void OpEntryParser::addOpcode(Opcode opcode) |
| 147 | { |
| 148 | opcodeList.append(opcode); |
| 149 | opcodeMap.insert(opcode.opcodeName, opcode); |
| 150 | } |
| 151 | |
| 152 | void OpEntryParser::addFlag(QString flag, QString desc) { |
| 153 | Opcode opcode(flag); |
| 154 | opcode.desc = desc; |
| 155 | opcode.isFlag = 1; |
| 156 | opcodeList.append(opcode); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | bool OpEntryParser::isKnownOpcode(QString name) { |
| 161 | return opcodeMap.contains(name) || m_udosMap->contains(name); |
| 162 | } |
| 163 | |
| 164 | Opcode OpEntryParser::findOpcode(QString opcodeName) { |
| 165 | if(opcodeMap.contains(opcodeName)) |
| 166 | return opcodeMap.value(opcodeName); |
| 167 | if(m_udosMap->contains(opcodeName)) { |
| 168 | return m_udosMap->value(opcodeName); |
| 169 | } |
| 170 | qDebug() << "OpEntryParser::findOpcode: opcode " << opcodeName << "not found"; |
| 171 | return Opcode(""); |
| 172 | } |
| 173 | |
| 174 | QString opcodeSyntax(Opcode opc) { |
| 175 | QString syntax = opc.outArgs.simplified(); |
| 176 | if (!opc.outArgs.isEmpty()) |
| 177 | syntax += " "; |
| 178 | syntax += opc.opcodeName.simplified(); |
| 179 | if (!opc.inArgs.isEmpty()) { |
| 180 | syntax += " " + opc.inArgs.simplified(); |
| 181 | } |
| 182 | if(!opc.desc.isEmpty()) { |
| 183 | syntax += "<br />[ " + opc.desc + " ]"; |
| 184 | } |
| 185 | return syntax; |
| 186 | } |
| 187 | |
| 188 | QString OpEntryParser::getSyntax(QString opcodeName) |
| 189 | { |
| 190 | if (opcodeName.size() < 2) { |
| 191 | return ""; |
| 192 | } |
| 193 | Opcode opc = findOpcode(opcodeName); |
| 194 | if(!opc.opcodeName.isEmpty()) { |
| 195 | QString syntax = opcodeSyntax(opc); |
| 196 | return syntax; |
| 197 | } |
| 198 | return ""; |
| 199 | } |
| 200 | |
| 201 | QVector<Opcode> OpEntryParser::getMatchingOpcodes(QString word, int maxsize) |
| 202 | { |
| 203 | // returns a vector of opcode definitions which start with <word> |
| 204 | // if maxsize is not 0, only a maximum of <maxsize> entries are included |
| 205 | int numopcodes = 0; |
| 206 | QVector<Opcode> out; |
| 207 | for (int i = 0; i < opcodeList.size(); i++) { |
| 208 | if (opcodeList[i].opcodeName.startsWith(word)) { |
| 209 | if(maxsize > 0 && numopcodes >= maxsize) |
| 210 | break; |
| 211 | out << opcodeList[i]; |
| 212 | numopcodes++; |
| 213 | } |
| 214 | } |
| 215 | auto it = m_udosMap->constBegin(); |
| 216 | while(it != m_udosMap->constEnd()) { |
| 217 | if(it->opcodeName.startsWith(word)) { |
| 218 | out << it.value(); |
| 219 | } |
| 220 | it++; |
| 221 | } |
| 222 | return out; |
| 223 | } |
| 224 | |
| 225 | QList< QPair<QString, QList<Opcode> > > OpEntryParser::getOpcodesByCategory() |
| 226 | { |
| 227 | return opcodeCategoryList; |
| 228 | } |
| 229 | |
| 230 | int OpEntryParser::getCategoryCount() |
| 231 | { |
| 232 | return categoryList.size(); |
| 233 | } |
| 234 | |
| 235 | QString OpEntryParser::getCategory(int index) |
| 236 | { |
| 237 | if (index < categoryList.size()) |
| 238 | return categoryList[index]; |
| 239 | else return QString(); |
| 240 | } |
| 241 | |
| 242 | QStringList OpEntryParser::getCategoryList() |
| 243 | { |
| 244 | return QStringList(categoryList); |
| 245 | } |
| 246 | |
| 247 | QList<Opcode> OpEntryParser::getOpcodeList(int index) |
| 248 | { |
| 249 | return opcodeListCategory[index]; |
| 250 | } |
| 251 | |
| 252 | bool OpEntryParser::isOpcode(QString opcodeName) |
| 253 | { |
| 254 | for (int i = 0; i < opcodeList.size();i++) { |
| 255 | if (opcodeName == opcodeList[i].opcodeName) { |
| 256 | return true; |
| 257 | } |
| 258 | } |
| 259 | if(this->m_udosMap->contains(opcodeName)) |
| 260 | return true; |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | |
| 265 | bool OpEntryParser::getOpcodeArgNames(Node &node) |
| 266 | { |
| 267 | QString opcodeName = node.getName(); |
| 268 | QVector<Port> inputs = node.getInputs(); |
| 269 | QVector<Port> outputs = node.getOutputs(); |
| 270 | int idx = -1; |
| 271 | for (int i = 0; i < opcodeList.size();i++) { |
| 272 | if (opcodeName == opcodeList[i].opcodeName) { |
| 273 | idx = i; |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | Opcode opcode; |
| 278 | if(idx >= 0) { |
| 279 | opcode = opcodeList[idx]; |
| 280 | } else { |
| 281 | if(m_udosMap->contains(opcodeName)) |
| 282 | opcode = m_udosMap->value(opcodeName); |
| 283 | else |
| 284 | return false; |
| 285 | } |
| 286 | QString inArgs = opcode.inArgs; |
| 287 | QString inArgsOpt = ""; |
| 288 | if (inArgs.contains("[")) |
| 289 | inArgsOpt = inArgs.mid(inArgs.indexOf("[")); |
| 290 | inArgs.remove(inArgsOpt); |
| 291 | QStringList args = inArgs.split(QRegExp("[,\\\"]+"), SKIP_EMPTY_PARTS); |
| 292 | for (int count = 0; count < args.size(); count ++) { |
| 293 | args[count] = args[count].trimmed(); |
| 294 | if (args[count] == "") |
| 295 | args.removeAt(count); |
| 296 | } |
| 297 | QStringList argsOpt = inArgsOpt.split(QRegExp("[,\\\\\\s\\[\\]]+"), SKIP_EMPTY_PARTS); |
| 298 | for (int j = 0; j < inputs.size(); j++) { |
| 299 | if (j < args.size()) { |
| 300 | inputs[j].argName = args[j]; |
| 301 | inputs[j].optional = false; |
| 302 | } |
| 303 | else { //optional parameter |
| 304 | int index = j - args.size(); |
| 305 | if (index < inArgsOpt.size()) { |
| 306 | if (index < argsOpt.size()) { |
| 307 | // in case the opcode contains an undefined number of optional arguments |
| 308 | inputs[j].argName = argsOpt[index]; |
| 309 | } |
| 310 | else { |
| 311 | inputs[j].argName = ""; |
| 312 | } |
| 313 | inputs[j].optional = true; |
| 314 | } |
| 315 | else { |
| 316 | qDebug("OpEntryParser::getOpcodeArgNames: Error too many inargs"); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | QString outArgs = opcode.outArgs; |
| 321 | QString outArgsOpt = ""; |
| 322 | if (outArgs.contains("[")) |
| 323 | outArgsOpt = outArgs.mid(outArgs.indexOf("[")); |
| 324 | outArgs.remove(outArgsOpt); |
| 325 | args = outArgs.split(QRegExp("[,\\s]+"), SKIP_EMPTY_PARTS); |
| 326 | argsOpt = outArgsOpt.split(QRegExp("[,\\\\\\s\\[\\]]"), SKIP_EMPTY_PARTS); |
| 327 | for (int j = 0; j < outputs.size(); j++) { |
| 328 | if (j < args.size()) { |
| 329 | outputs[j].argName = args[j]; |
| 330 | outputs[j].optional = false; |
| 331 | } |
| 332 | else { |
| 333 | //optional parameter |
| 334 | int index = j - args.size(); |
| 335 | if (index < inArgsOpt.size()) { |
| 336 | if (index < argsOpt.size()) { |
| 337 | // in case the opcode contains an undefined number of optional arguments |
| 338 | outputs[j].argName = argsOpt[index]; |
| 339 | } |
| 340 | else { |
| 341 | outputs[j].argName = ""; |
| 342 | } |
| 343 | outputs[j].optional = true; |
| 344 | } |
| 345 | else { |
| 346 | qDebug("OpEntryParser::getOpcodeArgNames: Error too many outargs"); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | node.setInputs(inputs); |
| 351 | node.setOutputs(outputs); |
| 352 | return true; |
| 353 | } |