void ROICollection::load(const std::string& filename) throw (SerializationException) { // open file for reading std::fstream fileStream(filename.c_str(), std::ios_base::in); if (fileStream.fail()) { throw SerializationException("Failed to open ROICollection file '" + tgt::FileSystem::absolutePath(filename) + "' for reading."); } // read data stream into deserializer XmlDeserializer d(FileSys.dirName(filename)); d.setUseAttributes(true); try { d.read(fileStream); } catch (SerializationException& e) { throw SerializationException("Failed to read serialization data stream from ROICollection file '" + filename + "': " + e.what()); } catch (...) { throw SerializationException("Failed to read serialization data stream from ROICollection file '" + filename + "' (unknown exception)."); } // deserialize ROICollection from data stream try { d.deserialize("ROICollection", *this); } catch (std::exception& e) { throw SerializationException("Deserialization from ROICollection file '" + filename + "' failed: " + e.what()); } catch (...) { throw SerializationException("Deserialization from ROICollection file '" + filename + "' failed (unknown exception)."); } }
std::string CommandManager::Unescape(const std::string& escapedText) throw(SerializationException) { // defer initialization of a StringBuffer until we know we need one std::string buffer; for (std::string::const_iterator itr = escapedText.begin(); itr != escapedText.end(); ++itr) { std::string::value_type c = *itr; if (c != ESCAPE_CHAR) { // normal unescaped character if (!buffer.empty()) { buffer += c; } } else { if (buffer.empty()) { buffer.assign(escapedText.begin(), itr); } if (++itr != escapedText.end()) { c = *itr; if (c == PARAMETER_START_CHAR || c == PARAMETER_END_CHAR || c == ID_VALUE_CHAR || c == PARAMETER_SEPARATOR_CHAR || c == ESCAPE_CHAR) { buffer += c; } else { std::string msg("Invalid character '"); msg += c; msg += "' in escape sequence"; throw SerializationException(msg); } } else { throw SerializationException( "Unexpected termination of escape sequence"); //$NON-NLS-1$ } } } if (buffer.empty()) { return escapedText; } return buffer; }
/** * Loads only tuple data, not schema, from the serialized tile. * Used for initial data loading. * @param allow_export if false, export enabled is overriden for this load. */ void Tile::DeserializeTuplesFrom(SerializeInput &input, type::AbstractPool *pool) { /* * Directly receives a Tile buffer. * [00 01] [02 03] [04 .. 0x] * rowstart colcount colcount * 1 byte (column types) * * [0x+1 .. 0y] * colcount * strings (column names) * * [0y+1 0y+2 0y+3 0y+4] * rowcount * * [0y+5 .. end] * rowdata */ input.ReadInt(); // rowstart input.ReadByte(); oid_t column_count = input.ReadShort(); PL_ASSERT(column_count > 0); // Store the following information so that we can provide them to the user on // failure type::Type::TypeId types[column_count]; std::vector<std::string> names; // Skip the column types for (oid_t column_itr = 0; column_itr < column_count; ++column_itr) { types[column_itr] = (type::Type::TypeId)input.ReadEnumInSingleByte(); } // Skip the column names for (oid_t column_itr = 0; column_itr < column_count; ++column_itr) { names.push_back(input.ReadTextString()); } // Check if the column count matches what the temp table is expecting if (column_count != schema.GetColumnCount()) { std::stringstream message(std::stringstream::in | std::stringstream::out); message << "Column count mismatch. Expecting " << schema.GetColumnCount() << ", but " << column_count << " given" << std::endl; message << "Expecting the following columns:" << std::endl; message << schema.GetColumnCount() << std::endl; message << "The following columns are given:" << std::endl; for (oid_t column_itr = 0; column_itr < column_count; column_itr++) { message << "column " << column_itr << ": " << names[column_itr] << ", type = " << types[column_itr] << std::endl; } throw SerializationException(message.str()); } // Use the deserialization routine skipping header DeserializeTuplesFromWithoutHeader(input, pool); }
void Serializer::writeFile() { try { refDataContainer_.setReferenceAttributes(); doc_.SaveFile(getFileName()); } catch (TxException& e) { throw SerializationException(e.what(), IvwContext); } }
void Serializer::writeFile(std::ostream& stream) { try { refDataContainer_.setReferenceAttributes(); stream << doc_; } catch (TxException& e) { throw SerializationException(e.what(), IvwContext); } }
void Workspace::load(const std::string& filename, const std::string& workDir) throw (SerializationException) { // open file for reading std::fstream fileStream(filename.c_str(), std::ios_base::in); if (fileStream.fail()) { //LERROR("Failed to open file '" << tgt::FileSystem::absolutePath(filename) << "' for reading."); throw SerializationException("Failed to open workspace file '" + tgt::FileSystem::absolutePath(filename) + "' for reading."); } std::string documentPath; if (!workDir.empty()) documentPath = workDir; else documentPath = filename; // read data stream into deserializer XmlDeserializer d(documentPath); d.setUseAttributes(true); NetworkSerializer ser; try { d.read(fileStream, &ser); } catch (SerializationException& e) { throw SerializationException("Failed to read serialization data stream from workspace file '" + filename + "': " + e.what()); } catch (...) { throw SerializationException("Failed to read serialization data stream from workspace file '" + filename + "' (unknown exception)."); } // deserialize workspace from data stream try { d.deserialize("Workspace", *this); errorList_ = d.getErrors(); setFilename(filename); } catch (std::exception& e) { throw SerializationException("Deserialization from workspace file '" + filename + "' failed: " + e.what()); } catch (...) { throw SerializationException("Deserialization from workspace file '" + filename + "' failed (unknown exception)."); } }
rapidjson::GenericValue<rapidjson::UTF8<>>& JsonFullReader::GetElementForNumber() { rapidjson::GenericValue<rapidjson::UTF8<>>& element = GetElement(); if( !element.IsNumber() ) { throw SerializationException( __FILE__, __LINE__, __FUNCTION__, "Trying to get a number value from something that is not a number." ); } return element; }
void GeometryPort::loadData(const std::string& path) throw (VoreenException) { tgtAssert(!path.empty(), "empty path"); // append .xml if no extension specified std::string filename = path; if (tgt::FileSystem::fileExtension(filename).empty()) filename += ".xml"; // open file for reading std::fstream fileStream(filename.c_str(), std::ios_base::in); if (fileStream.fail()) { throw SerializationException("Failed to open file '" + tgt::FileSystem::absolutePath(filename) + "' for reading."); } // read data stream into deserializer XmlDeserializer d(filename); d.setUseAttributes(true); try { d.read(fileStream); } catch (SerializationException& e) { throw SerializationException("Failed to read serialization data stream from file '" + filename + "': " + e.what()); } catch (...) { throw SerializationException("Failed to read serialization data stream from file '" + filename + "' (unknown exception)."); } // deserialize workspace from data stream try { Geometry* geometry = 0; d.deserialize("Geometry", geometry); setData(geometry); } catch (std::exception& e) { throw SerializationException("Deserialization from file '" + filename + "' failed: " + e.what()); } catch (...) { throw SerializationException("Deserialization from file '" + filename + "' failed (unknown exception)."); } }
cv::Mat RadiometricResponse::load(const std::string& filename) { std::ifstream file(filename); if (!file.is_open()) BOOST_THROW_EXCEPTION(SerializationException("Failed to open radiometric response file") << SerializationException::Filename(filename)); cv::Mat response(1, 256, CV_32FC3); for (size_t i = 0; i < 3; ++i) { auto v = response.begin<cv::Vec3f>(); for (size_t j = 0; j < 256; ++j) { file >> (*v++)[order_ == ChannelOrder::BGR ? i : 2 - i]; if (file.fail()) BOOST_THROW_EXCEPTION(SerializationException("Radiometric response file contains invalid data") << SerializationException::Filename(filename)); } } file.close(); return response; }
rapidjson::GenericValue<rapidjson::UTF8<>>& JsonFullReader::GetElement() { if( isObject ) { release_assert( label.size() > 0 ); if( (*m_json).HasMember( label.c_str() ) ) { return (*m_json)[label.c_str()]; } else { std::stringstream ss; ss << "The '" << label << "' element is not in this object." ; throw SerializationException( __FILE__, __LINE__, __FUNCTION__, ss.str().c_str() ); } } else { if( (*m_json).IsArray() ) { if( (*m_json).Size() > m_index ) { return (*m_json)[m_index++]; } else { std::stringstream ss; ss << "Tried to get the " << m_index << " element when the array only has " << (*m_json).Size() << " elements."; throw SerializationException( __FILE__, __LINE__, __FUNCTION__, ss.str().c_str() ); } } else { throw SerializationException( __FILE__, __LINE__, __FUNCTION__, "The element is expected to be an array and is not." ); } } }
void Serializer::initialize() { try { auto decl = util::make_unique<TxDeclaration>(SerializeConstants::XmlVersion, "", ""); doc_.LinkEndChild(decl.get()); rootElement_ = new TxElement(SerializeConstants::InviwoTreedata); rootElement_->SetAttribute(SerializeConstants::VersionAttribute, SerializeConstants::InviwoVersion); doc_.LinkEndChild(rootElement_); auto comment = util::make_unique<TxComment>(); comment->SetValue(SerializeConstants::EditComment.c_str()); rootElement_->LinkEndChild(comment.get()); } catch (TxException& e) { throw SerializationException(e.what(), IvwContext); } }
void GeometryPort::saveData(const std::string& path) const throw (VoreenException) { if (!hasData()) throw VoreenException("Port is empty"); tgtAssert(!path.empty(), "empty path"); // append .xml if no extension specified std::string filename = path; if (tgt::FileSystem::fileExtension(filename).empty()) filename += ".xml"; // serialize workspace XmlSerializer s(filename); s.setUseAttributes(true); s.serialize("Geometry", getData()); // write serialization data to temporary string stream std::ostringstream textStream; try { s.write(textStream); if (textStream.fail()) throw SerializationException("Failed to write serialization data to string stream."); } catch (std::exception& e) { throw SerializationException("Failed to write serialization data to string stream: " + std::string(e.what())); } catch (...) { throw SerializationException("Failed to write serialization data to string stream (unknown exception)."); } // Now we have a valid StringStream containing the serialization data. // => Open output file and write it to the file. std::fstream fileStream(filename.c_str(), std::ios_base::out); if (fileStream.fail()) throw SerializationException("Failed to open file '" + filename + "' for writing."); try { fileStream << textStream.str(); } catch (std::exception& e) { throw SerializationException("Failed to write serialization data stream to file '" + filename + "': " + std::string(e.what())); } catch (...) { throw SerializationException("Failed to write serialization data stream to file '" + filename + "' (unknown exception)."); } fileStream.close(); }
void VolumeOctreeBase::deserialize(XmlDeserializer& s) { tgt::ivec3 iVolumeDim; s.deserialize("volumeDim", iVolumeDim); dimensions_ = static_cast<tgt::svec3>(iVolumeDim); numVoxels_ = tgt::hmul(dimensions_); tgt::ivec3 iOctreeDim; s.deserialize("octreeDim", iOctreeDim); octreeDim_ = static_cast<tgt::svec3>(iOctreeDim); if (!isCubicAndPot(octreeDim_) || !tgt::hand(tgt::greaterThanEqual(octreeDim_, getVolumeDim()))) throw SerializationException("Invalid octree dimensions: " + genericToString(octreeDim_)); s.deserialize("bytesPerVoxel", bytesPerVoxel_); s.deserialize("numLevels", numLevels_); tgt::ivec3 iBrickDim; s.deserialize("brickDim", iBrickDim); brickDim_ = static_cast<tgt::svec3>(iBrickDim); s.deserialize("numChannels", numChannels_); }
void ISerializable::serialize(IArchive& ar, ISerializable*& obj) { static std::string nullptr_string( "nullptr" ); if ( ar.IsWriter() && ( obj == nullptr ) ) { ar.startClass( nullptr_string ); ar.endClass(); return; } std::string class_name = ar.IsWriter() ? obj->GetClassName() : "__UNK__"; ar.startClass(class_name); if ( ar.IsReader() && (class_name == nullptr_string) ) { ar.endClass(); obj = nullptr; return; } auto serialize_function = SerializationRegistrar::_get_serializer(class_name); if (!ar.IsWriter()) { auto constructor_function = SerializationRegistrar::_get_constructor(class_name); if( constructor_function == nullptr ) { std::stringstream msg; msg << "Could not find constructor for class_name='" << class_name << "'"; throw SerializationException( __FILE__, __LINE__, __FUNCTION__, msg.str().c_str() ); } obj = constructor_function(); } serialize_function(ar, obj); ar.endClass(); }
IArchive& JsonFullReader::startObject() { if (label.size() > 0) { m_value_stack.push(m_json); if (isObject) { m_json = &(*m_json)[label.c_str()]; } else { m_json = &(*m_json)[m_index++]; isObject = true; } } if( !m_json->IsObject() ) { throw SerializationException( __FILE__, __LINE__, __FUNCTION__, "Expected to starting reading an object and it is not an object." ); } return *this; }
SmartPointer<ParameterizedCommand> CommandManager::Deserialize( const std::string& serializedParameterizedCommand) throw(NotDefinedException, SerializationException) { const int lparenPosition = (int) this->UnescapedIndexOf( serializedParameterizedCommand, PARAMETER_START_CHAR); std::string commandIdEscaped; std::string serializedParameters; if (lparenPosition == -1) { commandIdEscaped = serializedParameterizedCommand; } else { commandIdEscaped = serializedParameterizedCommand.substr(0, lparenPosition); if (serializedParameterizedCommand .at(serializedParameterizedCommand.size() - 1) != PARAMETER_END_CHAR) { throw SerializationException( "Parentheses must be balanced in serialized ParameterizedCommand"); //$NON-NLS-1$ } serializedParameters = serializedParameterizedCommand.substr( lparenPosition + 1, // skip PARAMETER_START_CHAR serializedParameterizedCommand.size() - 1); // skip // PARAMETER_END_CHAR } const std::string commandId(this->Unescape(commandIdEscaped)); Command::Pointer command(this->GetCommand(commandId)); const std::vector<IParameter::Pointer> parameters(command->GetParameters()); const std::vector<Parameterization>parameterizations(this->GetParameterizations( serializedParameters, parameters)); ParameterizedCommand::Pointer pCmd(new ParameterizedCommand(command, parameterizations)); return pCmd; }
void Workspace::save(const std::string& filename, bool overwrite, const std::string& workDir) throw (SerializationException) { // check if file is already present if (!overwrite && tgt::FileSystem::fileExists(filename)) throw SerializationException("File '" + filename + "' already exists."); std::string documentPath; if (!workDir.empty()) documentPath = workDir; else documentPath = filename; // serialize workspace XmlSerializer s(documentPath); s.setUseAttributes(true); s.serialize("Workspace", *this); errorList_ = s.getErrors(); // write serialization data to temporary string stream std::ostringstream textStream; try { s.write(textStream); if (textStream.fail()) throw SerializationException("Failed to write serialization data to string stream."); } catch (std::exception& e) { throw SerializationException("Failed to write serialization data to string stream: " + std::string(e.what())); } catch (...) { throw SerializationException("Failed to write serialization data to string stream (unknown exception)."); } // Now we have a valid StringStream containing the serialization data. // => Open output file and write it to the file. // For added data security we write to a temporary file and afterwards move it into place // (which should be an atomic operation). const std::string tmpfilename = filename + ".tmp"; std::fstream fileStream(tmpfilename.c_str(), std::ios_base::out); if (fileStream.fail()) throw SerializationException("Failed to open file '" + tmpfilename + "' for writing."); try { fileStream << textStream.str(); } catch (std::exception& e) { throw SerializationException("Failed to write serialization data stream to file '" + tmpfilename + "': " + std::string(e.what())); } catch (...) { throw SerializationException("Failed to write serialization data stream to file '" + tmpfilename + "' (unknown exception)."); } fileStream.close(); // Finally move the temporary file into place. It is important that this happens in-place, // without deleting the old file first. bool success; #ifdef WIN32 // rename() does not replace existing files on Windows, so we have to use this success = (MoveFileEx(tmpfilename.c_str(), filename.c_str(), MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED) != 0); #else // atomic replace success = (rename(tmpfilename.c_str(), filename.c_str()) == 0); #endif if (!success) { #ifdef WIN32 _unlink(tmpfilename.c_str()); // ignore failure here #else unlink(tmpfilename.c_str()); // ignore failure here #endif throw SerializationException("Failed to rename temporary file '" + tmpfilename + "' to '" + filename + "'"); } // saving successful setFilename(filename); }
void Receive(Shuttler& shuttle, IntegerSequence<>, int which, Variant& value) { BOOST_THROW_EXCEPTION(SerializationException("Invalid variant.")); }