/*************************************************************************** * * * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * * Copyright (C) 2005, 2006 Christian Schoenebeck * * * * 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 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., 59 Temple Place, Suite 330, Boston, * * MA 02111-1307 USA * ***************************************************************************/ #include #include #include "DeviceParameter.h" namespace LinuxSampler { // if string is encapsulated into apostrophes or quotation marks, then remove those apostrophes / quotation marks static void __eliminate_quotation(String& s) { if (s.size()) { const char cBegin = s[0]; const char cEnd = s[s.size() - 1]; if ( (cBegin == '\'' && cEnd == '\'') || (cBegin == '\"' && cEnd == '\"') ) { s = s.substr(1, s.size() - 2); } } } static bool __parse_bool(String val) throw (Exception) { __eliminate_quotation(val); int b; if (val == "1" || !strcasecmp(val.c_str(),"true")) b = true; else if (val == "0" || !strcasecmp(val.c_str(),"false")) b = false; else throw Exception("Invalid value for boolean Device parameter"); return b; } static int __parse_int(String val) throw (Exception) { __eliminate_quotation(val); return atoi(val.c_str()); // TODO: format check is missing } static float __parse_float(String val) throw (Exception) { __eliminate_quotation(val); return atof(val.c_str()); // TODO: format check is missing } static String __parse_string(String val) { __eliminate_quotation(val); return val; } static std::vector __parse_strings(String val) throw (Exception) { std::vector vS; // checking for empty list if (val.length() == 0) return vS; // if there's only a single value, then we also allow to give it without being encapsulated into apostrophes if (val.find("\'") == String::npos && val.find("\"") == String::npos) { vS.push_back(val); } else { // if multiple strings or a string encapsulated into apostrophes char* pStart = (char*) val.c_str(); char* pC = pStart; while (true) { if (*pC != '\'' && *pC != '\"') throw Exception("Invalid form, all individual strings should be encapsulated into apostrophes, separated by commas"); // search for token end char* pTokenStart = pC + 1; do { pC++; if (*pC == '\0') throw Exception("Invalid form, all individual strings should be encapsulated into apostrophes, separated by commas"); } while (*pC != '\'' && *pC != '\"'); String token = val.substr((int)(pTokenStart - pStart), (int)(pC - pTokenStart)); vS.push_back(token); // we found the token's end // now there should be either a comma or the end of the total string if (*(++pC) == '\0') break; if (*pC != ',') throw Exception("Invalid form, all individual strings should be encapsulated into apostrophes, separated by commas"); pC++; } } return vS; } // *************** DeviceRuntimeParameterBool *************** // * DeviceRuntimeParameterBool::DeviceRuntimeParameterBool(bool bVal) { this->bVal = bVal; } String DeviceRuntimeParameterBool::Type() { return "BOOL"; } bool DeviceRuntimeParameterBool::Multiplicity() { return false; } optional DeviceRuntimeParameterBool::RangeMin() { return optional::nothing; } optional DeviceRuntimeParameterBool::RangeMax() { return optional::nothing; } optional DeviceRuntimeParameterBool::Possibilities() { return optional::nothing; } String DeviceRuntimeParameterBool::Value() { return (ValueAsBool()) ? "true" : "false"; } void DeviceRuntimeParameterBool::SetValue(String val) throw (Exception) { if (Fix()) throw Exception("Device parameter is read only"); int b = __parse_bool(val); SetValue(b); } bool DeviceRuntimeParameterBool::ValueAsBool() { return bVal; } void DeviceRuntimeParameterBool::SetValue(bool b) throw (Exception) { if (Fix()) throw Exception("Device parameter is read only"); OnSetValue(b); bVal = b; } // *************** DeviceRuntimeParameterInt *************** // * DeviceRuntimeParameterInt::DeviceRuntimeParameterInt(int iVal) { this->iVal = iVal; } String DeviceRuntimeParameterInt::Type() { return "INT"; } bool DeviceRuntimeParameterInt::Multiplicity() { return false; } optional DeviceRuntimeParameterInt::RangeMin() { optional rangemin = RangeMinAsInt(); if (!rangemin) return optional::nothing; return ToString(*rangemin); } optional DeviceRuntimeParameterInt::RangeMax() { optional rangemax = RangeMaxAsInt(); if (!rangemax) return optional::nothing; return ToString(*rangemax); } optional DeviceRuntimeParameterInt::Possibilities() { std::vector possibilities = PossibilitiesAsInt(); if (possibilities.empty()) return optional::nothing; std::stringstream ss; std::vector::iterator iter = possibilities.begin(); while (iter != possibilities.end()) { if (ss.str() != "") ss << ","; ss << *iter; iter++; } return ss.str(); } String DeviceRuntimeParameterInt::Value() { return ToString(ValueAsInt()); } void DeviceRuntimeParameterInt::SetValue(String val) throw (Exception) { if (Fix()) throw Exception("Device parameter is read only"); int i = __parse_int(val); if (RangeMinAsInt() && i < *RangeMinAsInt()) throw Exception("Invalid device parameter value: too small"); if (RangeMaxAsInt() && i > *RangeMaxAsInt()) throw Exception("Invalid device parameter value: too big"); std::vector possibilities = PossibilitiesAsInt(); if (possibilities.size()) { bool valid = false; std::vector::iterator iter = possibilities.begin(); while (iter != possibilities.end()) { if (i == *iter) { valid = true; break; } iter++; } if (!valid) throw Exception("Invalid device parameter value: not in set of possible values"); } SetValue(i); } int DeviceRuntimeParameterInt::ValueAsInt() { return iVal; } void DeviceRuntimeParameterInt::SetValue(int i) throw (Exception) { if (Fix()) throw Exception("Device parameter is read only"); OnSetValue(i); iVal = i; } // *************** DeviceRuntimeParameterFloat *************** // * DeviceRuntimeParameterFloat::DeviceRuntimeParameterFloat(float fVal) { this->fVal = fVal; } String DeviceRuntimeParameterFloat::Type() { return "FLOAT"; } bool DeviceRuntimeParameterFloat::Multiplicity() { return false; } optional DeviceRuntimeParameterFloat::RangeMin() { optional rangemin = RangeMinAsFloat(); if (!rangemin) return optional::nothing; return ToString(*rangemin); } optional DeviceRuntimeParameterFloat::RangeMax() { optional rangemax = RangeMaxAsFloat(); if (!rangemax) return optional::nothing; return ToString(*rangemax); } optional DeviceRuntimeParameterFloat::Possibilities() { std::vector possibilities = PossibilitiesAsFloat(); if (possibilities.empty()) return optional::nothing; std::stringstream ss; std::vector::iterator iter = possibilities.begin(); while (iter != possibilities.end()) { if (ss.str() != "") ss << ","; ss << *iter; iter++; } return ss.str(); } String DeviceRuntimeParameterFloat::Value() { return ToString(ValueAsFloat()); } void DeviceRuntimeParameterFloat::SetValue(String val) throw (Exception) { if (Fix()) throw Exception("Device parameter is read only"); float f = __parse_float(val); if (RangeMinAsFloat() && f < *RangeMinAsFloat()) throw Exception("Invalid device parameter value: too small"); if (RangeMaxAsFloat() && f > *RangeMaxAsFloat()) throw Exception("Invalid device parameter value: too big"); std::vector possibilities = PossibilitiesAsFloat(); if (possibilities.size()) { bool valid = false; std::vector::iterator iter = possibilities.begin(); while (iter != possibilities.end()) { if (f == *iter) { valid = true; break; } iter++; } if (!valid) throw Exception("Invalid device parameter value: not in set of possible values"); } SetValue(f); } float DeviceRuntimeParameterFloat::ValueAsFloat() { return fVal; } void DeviceRuntimeParameterFloat::SetValue(float f) throw (Exception) { if (Fix()) throw Exception("Device parameter is read only"); OnSetValue(f); fVal = f; } // *************** DeviceRuntimeParameterString *************** // * DeviceRuntimeParameterString::DeviceRuntimeParameterString(String sVal) { this->sVal = sVal; } String DeviceRuntimeParameterString::Type() { return "STRING"; } bool DeviceRuntimeParameterString::Multiplicity() { return false; } optional DeviceRuntimeParameterString::RangeMin() { return optional::nothing; } optional DeviceRuntimeParameterString::RangeMax() { return optional::nothing; } optional DeviceRuntimeParameterString::Possibilities() { std::vector possibilities = PossibilitiesAsString(); if (possibilities.empty()) return optional::nothing; std::stringstream ss; std::vector::iterator iter = possibilities.begin(); while (iter != possibilities.end()) { if (ss.str() != "") ss << ","; ss << "'" << *iter << "'"; iter++; } return ss.str(); } String DeviceRuntimeParameterString::Value() { return "\'" + ValueAsString() + "\'"; } void DeviceRuntimeParameterString::SetValue(String val) throw (Exception) { if (Fix()) throw Exception("Device parameter is read only"); SetValueAsString(__parse_string(val)); } String DeviceRuntimeParameterString::ValueAsString() { return sVal; } void DeviceRuntimeParameterString::SetValueAsString(String val) throw (Exception) { if (Fix()) throw Exception("Device parameter is read only"); if (val.find("\'") != String::npos) throw Exception("Character -> \' <- not allowed"); if (val.find("\"") != String::npos) throw Exception("Character -> \" <- not allowed"); OnSetValue(val); sVal = val; } // *************** DeviceRuntimeParameterStrings *************** // * DeviceRuntimeParameterStrings::DeviceRuntimeParameterStrings(std::vector vS) { this->sVals = vS; } String DeviceRuntimeParameterStrings::Type() { return "STRING"; } bool DeviceRuntimeParameterStrings::Multiplicity() { return true; } optional DeviceRuntimeParameterStrings::RangeMin() { return optional::nothing; } optional DeviceRuntimeParameterStrings::RangeMax() { return optional::nothing; } optional DeviceRuntimeParameterStrings::Possibilities() { std::vector possibilities = PossibilitiesAsString(); if (possibilities.empty()) return optional::nothing; std::stringstream ss; std::vector::iterator iter = possibilities.begin(); while (iter != possibilities.end()) { if (ss.str() != "") ss << ","; ss << "'" << *iter << "'"; iter++; } return ss.str(); } String DeviceRuntimeParameterStrings::Value() { String result; std::vector::iterator iter = this->sVals.begin(); while (iter != this->sVals.end()) { if (result != "") result += ","; result += "'" + *iter + "'"; iter++; } return result; } void DeviceRuntimeParameterStrings::SetValue(String val) throw (Exception) { if (Fix()) throw Exception("Device parameter is read only"); std::vector vS = __parse_strings(val); SetValue(vS); } std::vector DeviceRuntimeParameterStrings::ValueAsStrings() { return sVals; } void DeviceRuntimeParameterStrings::SetValue(std::vector vS) throw (Exception) { if (Fix()) throw Exception("Device parameter is read only"); OnSetValue(vS); sVals = vS; } // *************** DeviceCreationParameter *************** // * optional DeviceCreationParameter::Depends() { std::map dependencies = DependsAsParameters(); if (!dependencies.size()) return optional::nothing; std::map::iterator iter = dependencies.begin(); String s; for (; iter != dependencies.end(); iter++) { if (s != "") s += ","; s += iter->first; } return s; } optional DeviceCreationParameter::Default() { std::map Parameters; // empty parameters vector return Default(Parameters); } optional DeviceCreationParameter::RangeMin() { std::map Parameters; // empty parameters vector return RangeMin(Parameters); } optional DeviceCreationParameter::RangeMax() { std::map Parameters; // empty parameters vector return RangeMax(Parameters); } optional DeviceCreationParameter::Possibilities() { std::map Parameters; // empty parameters vector return Possibilities(Parameters); } // *************** DeviceCreationParameterBool *************** // * DeviceCreationParameterBool::DeviceCreationParameterBool(bool bVal) : DeviceCreationParameter() { this->bVal = bVal; } DeviceCreationParameterBool::DeviceCreationParameterBool(String val) throw (Exception) { this->bVal = __parse_bool(val); } void DeviceCreationParameterBool::InitWithDefault() { std::map Parameters; // empty parameters vector optional defaultval = DefaultAsBool(Parameters); this->bVal = (defaultval) ? *defaultval : false; } String DeviceCreationParameterBool::Type() { return "BOOL"; } bool DeviceCreationParameterBool::Multiplicity() { return false; } optional DeviceCreationParameterBool::Default(std::map Parameters) { optional defaultval = DefaultAsBool(Parameters); if (!defaultval) return optional::nothing; return (*defaultval) ? "true" : "false"; } optional DeviceCreationParameterBool::RangeMin(std::map Parameters) { return optional::nothing; } optional DeviceCreationParameterBool::RangeMax(std::map Parameters) { return optional::nothing; } optional DeviceCreationParameterBool::Possibilities(std::map Parameters) { return optional::nothing; } String DeviceCreationParameterBool::Value() { return (ValueAsBool()) ? "true" : "false"; } void DeviceCreationParameterBool::SetValue(String val) throw (Exception) { if (Fix()) throw Exception("Device parameter is read only"); int b = __parse_bool(val); SetValue(b); } bool DeviceCreationParameterBool::ValueAsBool() { return bVal; } void DeviceCreationParameterBool::SetValue(bool b) throw (Exception) { if (Fix()) throw Exception("Device parameter is read only"); OnSetValue(b); bVal = b; } // *************** DeviceCreationParameterInt *************** // * DeviceCreationParameterInt::DeviceCreationParameterInt(int iVal) : DeviceCreationParameter() { this->iVal = iVal; } DeviceCreationParameterInt::DeviceCreationParameterInt(String val) throw (Exception) { this->iVal = __parse_int(val); } void DeviceCreationParameterInt::InitWithDefault() { std::map Parameters; // empty parameters vector optional i = DefaultAsInt(Parameters); this->iVal = (i) ? *i : 0; } String DeviceCreationParameterInt::Type() { return "INT"; } bool DeviceCreationParameterInt::Multiplicity() { return false; } optional DeviceCreationParameterInt::Default(std::map Parameters) { optional defaultval = DefaultAsInt(Parameters); if (!defaultval) return optional::nothing; return ToString(*defaultval); } optional DeviceCreationParameterInt::RangeMin(std::map Parameters) { optional rangemin = RangeMinAsInt(Parameters); if (!rangemin) return optional::nothing; return ToString(*rangemin); } optional DeviceCreationParameterInt::RangeMax(std::map Parameters) { optional rangemax = RangeMaxAsInt(Parameters); if (!rangemax) return optional::nothing; return ToString(*rangemax); } optional DeviceCreationParameterInt::Possibilities(std::map Parameters) { std::vector possibilities = PossibilitiesAsInt(Parameters); if (possibilities.empty()) return optional::nothing; std::vector::iterator iter = possibilities.begin(); std::stringstream ss; while (iter != possibilities.end()) { if (ss.str() != "") ss << ","; ss << *iter; iter++; } return ss.str(); } String DeviceCreationParameterInt::Value() { return ToString(ValueAsInt()); } void DeviceCreationParameterInt::SetValue(String val) throw (Exception) { if (Fix()) throw Exception("Device parameter is read only"); int i = __parse_int(val); //if (RangeMinAsInt() && i < *RangeMinAsInt()) throw Exception("Invalid device parameter value: too small"); //if (RangeMaxAsInt() && i > *RangeMaxAsInt()) throw Exception("Invalid device parameter value: too big"); /*if (PossibilitiesAsInt()) { bool valid = false; std::vector* pPossibilities = PossibilitiesAsInt(); std::vector::iterator iter = pPossibilities->begin(); while (iter != pPossibilities->end()) { if (i == *iter) { valid = true; break; } iter++; } if (!valid) throw Exception("Invalid Device parameter value: not in set of possible values"); }*/ SetValue(i); } int DeviceCreationParameterInt::ValueAsInt() { return iVal; } void DeviceCreationParameterInt::SetValue(int i) throw (Exception) { if (Fix()) throw Exception("Device parameter is read only"); OnSetValue(i); iVal = i; } // *************** DeviceCreationParameterFloat *************** // * DeviceCreationParameterFloat::DeviceCreationParameterFloat(float fVal) : DeviceCreationParameter() { this->fVal = fVal; } DeviceCreationParameterFloat::DeviceCreationParameterFloat(String val) throw (Exception) { this->fVal = __parse_float(val); } void DeviceCreationParameterFloat::InitWithDefault() { std::map Parameters; // empty parameters vector optional f = DefaultAsFloat(Parameters); this->fVal = (f) ? *f : 0.0f; } String DeviceCreationParameterFloat::Type() { return "FLOAT"; } bool DeviceCreationParameterFloat::Multiplicity() { return false; } optional DeviceCreationParameterFloat::Default(std::map Parameters) { optional defaultval = DefaultAsFloat(Parameters); if (!defaultval) return optional::nothing; return ToString(*defaultval); } optional DeviceCreationParameterFloat::RangeMin(std::map Parameters) { optional rangemin = RangeMinAsFloat(Parameters); if (!rangemin) return optional::nothing; return ToString(*rangemin); } optional DeviceCreationParameterFloat::RangeMax(std::map Parameters) { optional rangemax = RangeMaxAsFloat(Parameters); if (!rangemax) return optional::nothing; return ToString(*rangemax); } optional DeviceCreationParameterFloat::Possibilities(std::map Parameters) { std::vector possibilities = PossibilitiesAsFloat(Parameters); if (possibilities.empty()) return optional::nothing; std::vector::iterator iter = possibilities.begin(); std::stringstream ss; while (iter != possibilities.end()) { if (ss.str() != "") ss << ","; ss << *iter; iter++; } return ss.str(); } String DeviceCreationParameterFloat::Value() { return ToString(ValueAsFloat()); } void DeviceCreationParameterFloat::SetValue(String val) throw (Exception) { if (Fix()) throw Exception("Device parameter is read only"); float f = __parse_float(val); //if (RangeMinAsFloat() && i < *RangeMinAsFloat()) throw Exception("Invalid device parameter value: too small"); //if (RangeMaxAsFloat() && i > *RangeMaxAsFloat()) throw Exception("Invalid device parameter value: too big"); /*if (PossibilitiesAsFloat()) { bool valid = false; std::vector* pPossibilities = PossibilitiesAsFloat(); std::vector::iterator iter = pPossibilities->begin(); while (iter != pPossibilities->end()) { if (f == *iter) { valid = true; break; } iter++; } if (!valid) throw Exception("Invalid Device parameter value: not in set of possible values"); }*/ SetValue(f); } float DeviceCreationParameterFloat::ValueAsFloat() { return fVal; } void DeviceCreationParameterFloat::SetValue(float f) throw (Exception) { if (Fix()) throw Exception("Device parameter is read only"); OnSetValue(f); fVal = f; } // *************** DeviceCreationParameterString *************** // * DeviceCreationParameterString::DeviceCreationParameterString(String sVal) : DeviceCreationParameter() { this->sVal = __parse_string(sVal); } void DeviceCreationParameterString::InitWithDefault() { std::map Parameters; // empty parameters vector optional defaulval = DefaultAsString(Parameters); if (defaulval) this->sVal = *defaulval; else this->sVal = ""; } String DeviceCreationParameterString::Type() { return "STRING"; } bool DeviceCreationParameterString::Multiplicity() { return false; } optional DeviceCreationParameterString::Default(std::map Parameters) { optional defaultval = DefaultAsString(Parameters); if (!defaultval) return optional::nothing; return "'" + *defaultval + "'"; } optional DeviceCreationParameterString::RangeMin(std::map Parameters) { return optional::nothing; } optional DeviceCreationParameterString::RangeMax(std::map Parameters) { return optional::nothing; } optional DeviceCreationParameterString::Possibilities(std::map Parameters) { std::vector possibilities = PossibilitiesAsString(Parameters); if (possibilities.empty()) return optional::nothing; std::stringstream ss; std::vector::iterator iter = possibilities.begin(); while (iter != possibilities.end()) { if (ss.str() != "") ss << ","; ss << "'" << *iter << "'"; iter++; } return ss.str(); } String DeviceCreationParameterString::Value() { return "\'" + ValueAsString() + "\'"; } void DeviceCreationParameterString::SetValue(String val) throw (Exception) { if (Fix()) throw Exception("Device parameter is read only"); SetValueAsString(__parse_string(val)); } String DeviceCreationParameterString::ValueAsString() { return sVal; } void DeviceCreationParameterString::SetValueAsString(String val) throw (Exception) { if (val.find("\'") != String::npos) throw Exception("Character -> \' <- not allowed"); if (val.find("\"") != String::npos) throw Exception("Character -> \" <- not allowed"); OnSetValue(val); sVal = val; } // *************** DeviceCreationParameterStrings *************** // * DeviceCreationParameterStrings::DeviceCreationParameterStrings(std::vector sVals) : DeviceCreationParameter() { this->sVals = sVals; } DeviceCreationParameterStrings::DeviceCreationParameterStrings(String val) throw (Exception) { this->sVals = __parse_strings(val); } void DeviceCreationParameterStrings::InitWithDefault() { std::map Parameters; // empty parameters vector optional > defaultval = DefaultAsStrings(Parameters); this->sVals = (defaultval) ? *defaultval : std::vector(); } String DeviceCreationParameterStrings::Type() { return "STRING"; } bool DeviceCreationParameterStrings::Multiplicity() { return true; } optional DeviceCreationParameterStrings::Default(std::map Parameters) { std::vector defaultval = DefaultAsStrings(Parameters); if (defaultval.empty()) return optional::nothing; String result; std::vector::iterator iter = defaultval.begin(); for (; iter != defaultval.end(); iter++) { if (result != "") result += ","; result += ("'" + *iter + "'"); } return result; } optional DeviceCreationParameterStrings::RangeMin(std::map Parameters) { return optional::nothing; } optional DeviceCreationParameterStrings::RangeMax(std::map Parameters) { return optional::nothing; } optional DeviceCreationParameterStrings::Possibilities(std::map Parameters) { std::vector possibilities = PossibilitiesAsString(Parameters); if (possibilities.empty()) return optional::nothing; std::stringstream ss; std::vector::iterator iter = possibilities.begin(); while (iter != possibilities.end()) { if (ss.str() != "") ss << ","; ss << "'" << *iter << "'"; iter++; } return ss.str(); } String DeviceCreationParameterStrings::Value() { String result; std::vector::iterator iter = this->sVals.begin(); while (iter != this->sVals.end()) { if (result != "") result += ","; result += "'" + *iter + "'"; iter++; } return result; } void DeviceCreationParameterStrings::SetValue(String val) throw (Exception) { if (Fix()) throw Exception("Device parameter is read only"); std::vector vS = __parse_strings(val); SetValue(vS); } std::vector DeviceCreationParameterStrings::ValueAsStrings() { return sVals; } void DeviceCreationParameterStrings::SetValue(std::vector vS) throw (Exception) { if (Fix()) throw Exception("Device parameter is read only"); OnSetValue(vS); sVals = vS; } } // namespace LinuxSampler