DictPtr ParseDict(const JSONValue& doc) { // Required: type string type = GetStringProperty(doc, "type"); DictPtr dict; if (type == "group") { list<DictPtr> dicts; const JSONValue& docs = GetArrayProperty(doc, "dicts"); for (rapidjson::SizeType i = 0; i < docs.Size(); i++) { if (docs[i].IsObject()) { DictPtr dict = ParseDict(docs[i]); dicts.push_back(dict); } else { throw InvalidFormat("Element of the array must be an object"); } } return DictGroupPtr(new DictGroup(dicts)); } else { string fileName = GetStringProperty(doc, "file"); // Read from cache DictPtr& cache = dictCache[type][configDirectory][fileName]; if (cache != nullptr) { return cache; } if (type == "text") { dict = LoadDictWithPaths<TextDict>(fileName); } else if (type == "ocd") { dict = LoadDictWithPaths<DartsDict>(fileName); } else { throw InvalidFormat("Unknown dictionary type: " + type); } // Update Cache cache = dict; return dict; } }
DAT1::DAT1(VFS* vfs, const std::string& file) : VFSSource(vfs), m_datpath(file), m_data(vfs->open(file)) { FL_LOG(_log, LMsg("MFFalloutDAT1") << "loading: " << file << " filesize: " << m_data->getDataLength()); m_data->setIndex(0); const uint32_t dircount = m_data->read32Big(); m_data->moveIndex(4*3); FL_LOG(_log, LMsg("MFFalloutDAT1") << "number of directories " << dircount); // Sanity check. Each dir entry needs min. 16 bytes. if( dircount*16 > m_data->getDataLength() ) { throw InvalidFormat("directory count larger than filesize."); } std::list<std::string> dir_names; for (uint32_t i = 0; i < dircount; ++i) { std::string name = readString(); if (name == ".") { name = ""; } dir_names.push_back(name); } for(std::list<std::string>::iterator i= dir_names.begin(); i!= dir_names.end(); ++i) loadFileList(*i); }
const JSONValue& GetArrayProperty(const JSONValue& doc, const char* name) { const JSONValue& obj = GetProperty(doc, name); if (!obj.IsArray()) { throw InvalidFormat("Property must be an array: " + string(name)); } return obj; }
const char* GetStringProperty(const JSONValue& doc, const char* name) { const JSONValue& obj = GetProperty(doc, name); if (!obj.IsString()) { throw InvalidFormat("Property must be a string: " + string(name)); } return obj.GetString(); }
DAT2::DAT2(VFS* vfs, const std::string& file) : VFSSource(vfs), m_datpath(file), m_data(vfs->open(file)), m_filelist() { FL_LOG(_log, LMsg("MFFalloutDAT2") << "loading: " << file << " filesize: " << m_data->getDataLength()); m_data->setIndex(m_data->getDataLength() - 8); uint32_t fileListLength = m_data->read32Little(); uint32_t archiveSize = m_data->read32Little(); FL_LOG(_log, LMsg("MFFalloutDAT2") << "FileListLength: " << fileListLength << " ArchiveSize: " << archiveSize); if (archiveSize != m_data->getDataLength()) throw InvalidFormat("size mismatch"); m_data->setIndex( archiveSize - fileListLength - 8); m_filecount = m_data->read32Little(); m_currentIndex = m_data->getCurrentIndex(); FL_LOG(_log, LMsg("MFFalloutDAT2 FileCount: ") << m_filecount); // Do not read the complete file list at startup. // Instead read a chunk each frame. m_timer.setInterval(0); m_timer.setCallback( boost::bind( &DAT2::readFileEntry, this) ); m_timer.start(); }
void BmpReader::init(FILE* f, size_t& w, size_t& h) { my_file = f; my_pixel_offset = read_file_header(f); size_t bpp, table_num; read_image_header(f, w, h, bpp, table_num); if(bpp == 8) read_color_table(f, my_color_table, table_num); else if(bpp != 24) throw InvalidFormat("unexpected bits per pixel in BMP image header"); }
/// Constructs a compiler output handler. /// /// \param format %Output format to use. /// \param stream Stream to output to. /// /// \exception InvalidFormat Invalid format specified. /// CompilerOutput::CompilerOutput(const string& format, ostream& stream) : Output(), _format(format), _stream(stream) { int expr(0), file(0), line(0); for (string::size_type pos = 0; (pos = _format.find_first_of('%', pos)) != string::npos; ) { ++pos; if (check_format(_format, pos, "expr", expr)) ; else if (check_format(_format, pos, "file", file)) ; else if (check_format(_format, pos, "line", line)) ; else throw InvalidFormat(format); } if (!expr && !file && !line) throw InvalidFormat(format); }
SegmentationPtr ParseSegmentation(const JSONValue& doc) { SegmentationPtr segmentation; // Required: type string type = GetStringProperty(doc, "type"); if (type == "mmseg") { // Required: dict DictPtr dict = ParseDict(GetObjectProperty(doc, "dict")); segmentation = SegmentationPtr(new MaxMatchSegmentation(dict)); } else { throw InvalidFormat("Unknown segmentation type: " + type); } return segmentation; }
//! Encode object to text form. //! @return command in text form. std::string encode(void) const { std::ostringstream ss; ss << c_ssc_prefix << "," << m_version << "," << DUNE::Utils::String::str("%02X", m_flags) << "," << m_ttl << "," << m_priority << "," << m_src << "," << m_dsts.size() << ","; // Destinations. if (m_dsts.begin() == m_dsts.end()) throw InvalidFormat("no destinations"); for (std::set<unsigned>::const_iterator itr = m_dsts.begin(); itr != m_dsts.end(); ++itr) ss << *itr << ","; // Command name. ss << m_name << ","; // Command arguments. std::vector<std::string> args; size_t rv = encodeArgs(args); if (rv > 0) ss << DUNE::Utils::String::join(args.begin(), args.end(), ",") << ","; // CRC. std::string cmd(ss.str()); char crc[5] = {0}; std::sprintf(crc, "%04X", computeCRC(cmd, cmd.size())); cmd.append(crc); // Termination character. cmd.append(c_ssc_term); return cmd; }
//! Decode command in text form. //! @param[in] cmd command in text form. //! @return command object. static AbstractCommand* decode(const std::string& cmd) { // Validate CRC. if (AbstractCommand::getCRC(cmd) != AbstractCommand::computeCRC(cmd, cmd.size() - c_ssc_footer_size)) throw InvalidChecksum(); // Split command. std::vector<std::string> parts; DUNE::Utils::String::split(cmd, ",", parts); // Check minimum arguments. if (parts.size() < c_ssc_min_args) throw InvalidFormat("minimum arguments not present"); // Extract prefix. if (parts[0] != c_ssc_prefix) throw InvalidFormat("invalid prefix"); // Extract version. unsigned version = 0; if (std::sscanf(parts[AbstractCommand::OFFS_VERSION].c_str(), "%u", &version) != 1) throw InvalidVersion(); // Extract flags. unsigned flags = 0; if (std::sscanf(parts[AbstractCommand::OFFS_FLAGS].c_str(), "%02X", &flags) != 1) throw InvalidFormat("invalid flags"); // Extract TTL. unsigned ttl = 0; if (std::sscanf(parts[AbstractCommand::OFFS_TTL].c_str(), "%u", &ttl) != 1) throw InvalidFormat("invalid ttl"); // Extract priority. unsigned priority = 0; if (std::sscanf(parts[AbstractCommand::OFFS_PRIORITY].c_str(), "%u", &priority) != 1) throw InvalidFormat("invalid priority"); // Extract source. unsigned src = 0; if (std::sscanf(parts[AbstractCommand::OFFS_SRC].c_str(), "%u", &src) != 1) throw InvalidFormat("invalid source"); // Extract number of destinations. unsigned dst_count = 0; if (std::sscanf(parts[AbstractCommand::OFFS_DST_COUNT].c_str(), "%u", &dst_count) != 1) throw InvalidFormat("invalid destination count"); // Extract destinations. unsigned dst = 0; std::set<unsigned> dsts; for (unsigned i = 0; i < dst_count; ++i) { // FIXME: check size. if (std::sscanf(parts[AbstractCommand::OFFS_DST + i].c_str(), "%u", &dst) != 1) throw InvalidFormat("invalid destination"); dsts.insert(dst); } // Command name. std::string name = parts[AbstractCommand::OFFS_DST + dst_count]; AbstractCommand* cmd_obj = createByName(name); if (cmd_obj == NULL) throw InvalidValue(); cmd_obj->setName(name); cmd_obj->setSource(src); cmd_obj->setDestinations(dsts); cmd_obj->setVersion(version); cmd_obj->setPriority(priority); cmd_obj->setTTL(ttl); cmd_obj->setFlags(flags); size_t args_start = AbstractCommand::OFFS_DST + dst_count + 1; cmd_obj->decodeArgs(parts, args_start); return cmd_obj; }
const JSONValue& GetProperty(const JSONValue& doc, const char* name) { if (!doc.HasMember(name)) { throw InvalidFormat("Required property not found: " + string(name)); } return doc[name]; }