// Helper function to write 'matrix' of strings to a CSV file. static void writeCSV(const StringMatrix& string_matrix, const std::string& filename) { CHECK_GE(string_matrix.size(), 1) << "Provided matrix of strings had no entries."; std::ofstream out_file_stream; out_file_stream.open(filename.c_str()); // Iterate over the rows of the string matrix and write comma-separated fields. for (StringMatrix::const_iterator it = string_matrix.begin(); it != string_matrix.end(); ++it) { CHECK_GE(it->size(), 1) << "String matrix row has no entries."; out_file_stream << it->at(0u); for (size_t i = 1u; i < it->size(); ++i) { out_file_stream << "," << it->at(i); } out_file_stream << std::endl; } out_file_stream.close(); }
MatLabUdmTruthTable::StringVectorManager MatLabUdmTruthTable::getStringVectorManager( std::string matrix ) { static boost::regex scPattern( "(?<!&#x[0-9A-Fa-f]{2});", boost::regex_constants::perl ); static boost::regex scCodePattern( "[Bb];", boost::regex_constants::perl ); static boost::regex cmPattern( ",", boost::regex_constants::perl ); static boost::regex cmCodePattern( "[Cc];", boost::regex_constants::perl ); static boost::regex labeledStringPattern( "\\s*(\\S+)\\s*:\\s*(.*?)\\s*\\Z", boost::regex_constants::perl | boost::regex_constants::mod_s); matrix = matrix.substr( 1, matrix.size() - 2 ); // GET RID OF ENCLOSING {} StringMatrix stringMatrix; bool scMatch = true; boost::match_results< std::string::const_iterator > results; while ( scMatch && ( boost::regex_search( matrix, results, scPattern ) || !(scMatch = false) ) ) { std::string row; if ( scMatch ) { row = results.prefix(); matrix = results.suffix(); } else { row = matrix; } row = boost::regex_replace( row, scCodePattern, ";" ); stringMatrix.push_back( StringVectorSP( new StringVector ) ); StringVector &stringVector = *stringMatrix.back(); bool cmMatch = true; while( cmMatch && ( boost::regex_search( row, results, cmPattern ) || !( cmMatch = false ) ) ) { std::string cell; if ( cmMatch ) { cell = results.prefix(); row = results.suffix(); } else { cell = row; } cell = boost::regex_replace( cell, cmCodePattern, "," ); stringVector.push_back( cell ); } } StringVectorMap stringVectorMap; for( StringMatrix::iterator stmItr = stringMatrix.begin() ; stmItr != stringMatrix.end() ; ++stmItr ) { StringVector &stringVector = **stmItr; std::string &labeledString = stringVector[1]; labeledString = RegexCommon::eliminateContinuations( labeledString ); if ( boost::regex_search( labeledString, results, labeledStringPattern ) ) { std::string key( results[1] ); std::string condition( results[2] ); stringVectorMap.insert( std::make_pair( key, *stmItr ) ); labeledString = condition; } } return StringVectorManager( stringMatrix, stringVectorMap ); }