/* osc/message.cpp Copyright (c) 2022 Kevin Matz (kevin.matz@gmail.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include "message.h" namespace OSC { /** * @brief Message::type_tag * @return */ std::string Message::type_tag() const { std::string types = ","; for (const auto & argument: arguments) types += argument->type(); return types; } /** * @brief Message::createArguments * @param types * @return */ std::vector> Message::createArguments(std::string types) { auto args = std::vector>(); for (std::string::size_type pos = 0; pos < types.size(); pos++) { switch (types.at(pos)) { case 'i': args.emplace_back(std::make_shared()); break; case 'f': args.emplace_back(std::make_shared()); break; case 's': case 'S': args.emplace_back(std::make_shared()); break; case 'b': args.emplace_back(std::make_shared()); break; case 'h': args.emplace_back(std::make_shared()); break; case 't': args.emplace_back(std::make_shared()); break; case 'd': args.emplace_back(std::make_shared()); break; case 'c': args.emplace_back(std::make_shared()); break; case 'r': args.emplace_back(std::make_shared()); break; case 'm': args.emplace_back(std::make_shared()); break; case 'T': args.emplace_back(std::make_shared()); break; case 'F': args.emplace_back(std::make_shared()); break; case 'N': args.emplace_back(std::make_shared()); break; case 'I': args.emplace_back(std::make_shared()); break; case '[': { args.emplace_back(std::make_shared()); auto arg = std::static_pointer_cast(args.back()); std::string::size_type close = pos; int nests = 1; while (nests > 0) { close = types.find_first_of("[]", close); if (close == std::string::npos) // no closing brace, use all the remaining types { close = types.size()-1; break; } if (types.at(close) == '[') // nested braces, keep going { nests++; continue; } if (types.at(close) == ']') // closed this level of nesting { nests--; } } arg->setTypes(types.substr(pos+1, close-(pos+1))); pos = close; } break; default: continue; } } return args; } size_t Message::streamSize() const { size_t address_size = address_pattern.size(); // character count address_size += 1; // null terminated address_size += address_size % 4 ? 4 - (address_size % 4) : 0; // padding bytes assert(0 == address_size % 4); // 32 bit aligned size_t type_size = type_tag().size(); // character count type_size += 1; // null terminated type_size += type_size % 4 ? 4 - (type_size % 4) : 0; // padding assert(0 == type_size % 4); // 32 bit aligned size_t argument_size = 0; for (const auto & argument: arguments) argument_size += argument->streamSize(); // length of all arguments assert(0 == argument_size % 4); // 32 bit aligned return address_size + type_size + argument_size; } void Message::iStream(std::shared_ptr stream) { stream->readString(address_pattern); if (address_pattern.front() != '/') return stream->setstate(std::ios::failbit); for (uint i = 0; i < (address_pattern.size() + 1) % 4; i++) stream->readType(); std::string type_string; stream->readString(type_string); if (type_string.front() != ',') return stream->setstate(std::ios::failbit); for (uint i = 0; i < (type_string.size() + 1) % 4; i++) stream->readType(); arguments = createArguments(type_string); for (auto &argument: arguments) argument->iStream(stream); } void Message::oStream(std::shared_ptr stream) const { stream->writeString(address_pattern); auto address_pad = (address_pattern.size() + 1) % 4; address_pad = address_pad ? 4 - address_pad : 0; for (uint i = 0; i < address_pad; i++) stream->writeType(0x00); std::string type_string = type_tag(); stream->writeString(type_string); auto type_pad = (type_string.size() + 1) % 4; type_pad = type_pad ? 4 - type_pad : 0; for (uint i = 0; i < type_pad; i++) stream->writeType(0x00); for (const auto &argument: arguments) argument->oStream(stream); } } // namespace OSC