IddFile IddFile::catchallIddFile() { IddFile result; result.addObject(IddObject()); return result; }
bool ImfFile::m_load(std::istream& is) { // keep track of line number in the Idf int lineNum = 0; // number of object in the Idf, 1 is first object int objectNum = 0; // temp string to read file std::string line; // this will contain matches to regular expressions boost::smatch matches; // keep running comment std::string comment; // keep track of the current section std::string section = ""; // get CommentOnly IddObject OptionalIddObject commentOnlyIddObject; commentOnlyIddObject = IddFactory::instance().getObject(IddObjectType::CommentOnly); OS_ASSERT(commentOnlyIddObject); // Use a boost filter to make sure that no matter what line endings come in, // they are converted to what is expected by the current os boost::iostreams::filtering_istream filt; //#ifdef _POSIX_VERSION filt.push(boost::iostreams::newline_filter(boost::iostreams::newline::posix)); //#else // filt.push(boost::iostreams::newline_filter(boost::iostreams::newline::dos)); //#endif filt.push(is); // read the rest of the file line by line // todo, do this by regex while (getline(filt, line)) { ++lineNum; if (boost::regex_match(line, idfRegex::commentOnlyLine())){ // continue comment comment += (line + idfRegex::newLinestring()); continue; } else if (boost::regex_match(line, commentRegex::whitespaceOnlyLine())){ // end comment boost::trim(comment); if (!comment.empty()){ // make a comment only object to hold the comment OptionalIdfObject commentOnlyObject; commentOnlyObject = IdfObject::load(commentOnlyIddObject->name() + ";" + comment, *commentOnlyIddObject); OS_ASSERT(commentOnlyObject); // put it in the object list m_sectionMap[section].push_back(*commentOnlyObject); } //clear out comment comment = ""; continue; } else if (boost::regex_search(line, matches, idfRegex::imfSection())){ // end comment boost::trim(comment); if (!comment.empty()){ // make a comment only object to hold the comment OptionalIddObject commentOnlyIddObject = IddFactory::instance().getObject(IddObjectType::CommentOnly); OS_ASSERT(commentOnlyIddObject); OptionalIdfObject commentOnlyObject; commentOnlyObject = IdfObject::load(commentOnlyIddObject->name() + ";" + comment, *commentOnlyIddObject); OS_ASSERT(commentOnlyObject); // put it in the object list m_sectionMap[section].push_back(*commentOnlyObject); } //clear out comment comment = ""; // get the new imf section section = std::string(matches[1].first, matches[1].second); boost::trim(section); } else if (boost::regex_search(line, matches, idfRegex::imfSectionEnd())) { // end comment boost::trim(comment); if (!comment.empty()){ // make a comment only object to hold the comment OptionalIdfObject commentOnlyObject; commentOnlyObject = IdfObject::load(commentOnlyIddObject->name() + ";" + comment, *commentOnlyIddObject); OS_ASSERT(commentOnlyObject); // put it in the object list m_sectionMap[section].push_back(*commentOnlyObject); } //clear out comment comment = ""; } else { // IdfObject bool foundEndLine(false); // a valid Idf object to parse ++objectNum; // peek at the object type and name for indexing in map std::string objectType; std::string objectName; //bool objectHasName(false); if (boost::regex_search(line, matches, idfRegex::objectTypeAndName())){ objectType = std::string(matches[1].first, matches[1].second); boost::trim(objectType); objectName = std::string(matches[2].first, matches[2].second); boost::trim(objectType); } else if (boost::regex_search(line, matches, idfRegex::line())){ // doesn't match name, just type objectType = std::string(matches[1].first, matches[1].second); boost::trim(objectType); } else{ // can't figure out the object's type LOG(Warn, "Unrecognizable object type '" + line + "'. Defaulting to 'Catchall'."); objectType = "Catchall"; } // get the corresponding idd object entry OptionalIddObject iddObject = m_iddFileAndFactoryWrapper.getObject(objectType); if (!iddObject){ LOG(Warn, "Cannot find object type '" + objectType + "' in Idd. Placing data in Catchall object."); iddObject = IddObject(); objectType = "Catchall"; } // put the text for this object in a new string with a newline std::string text(line + idfRegex::newLinestring()); // check if this line also matches closing line object if (boost::regex_match(line, idfRegex::objectEnd())){ foundEndLine = true; } // continue reading until we have seen the entire object // last line will be thrown away, requires empty line between objects in Idf while((!foundEndLine) && (std::getline(filt, line))){ ++lineNum; // add line to text, include newline separator text += (line + idfRegex::newLinestring()); // check if we have found the last field if (boost::regex_match(line, idfRegex::objectEnd())){ foundEndLine = true; } } // construct the object OptionalIdfObject object = IdfObject::load(text,*iddObject); if (!object) { LOG(Error,"Unable to construct IdfObject from text: " << std::endl << text << std::endl << "Throwing this object out and parsing the remainder of the file."); continue; } // put it in the object list m_sectionMap[section].push_back(*object); } } return true; }