Addressed code review comments Signed-off-by: Maciej Kurc <mkurc@antmicro.com>
diff --git a/ql-iob-plugin/pcf_parser.cc b/ql-iob-plugin/pcf_parser.cc index ea93762..c7a9309 100644 --- a/ql-iob-plugin/pcf_parser.cc +++ b/ql-iob-plugin/pcf_parser.cc
@@ -19,7 +19,6 @@ */ #include "pcf_parser.hh" -#include <fstream> #include <regex> // ============================================================================ @@ -27,11 +26,10 @@ bool PcfParser::parse (const std::string& a_FileName) { // Open the file - std::fstream file(a_FileName.c_str(), std::ifstream::in); + std::ifstream file(a_FileName.c_str()); // Parse it - std::istream* stream = &file; - return parse(stream); + return parse(file); } const std::vector<PcfParser::Constraint> PcfParser::getConstraints () const { @@ -41,9 +39,9 @@ // ============================================================================ -bool PcfParser::parse (std::istream*& a_Stream) { +bool PcfParser::parse (std::ifstream& a_Stream) { - if (a_Stream == nullptr) { + if (!a_Stream.good()) { return false; } @@ -53,9 +51,9 @@ // Parse PCF lines std::regex re("^\\s*set_io\\s+([^#\\s]+)\\s+([^#\\s]+)(?:\\s+#(.*))?"); - while (a_Stream->good()) { + while (a_Stream.good()) { std::string line; - std::getline(*a_Stream, line); + std::getline(a_Stream, line); // Match against regex std::cmatch cm;
diff --git a/ql-iob-plugin/pcf_parser.hh b/ql-iob-plugin/pcf_parser.hh index 22263fd..8a4a920 100644 --- a/ql-iob-plugin/pcf_parser.hh +++ b/ql-iob-plugin/pcf_parser.hh
@@ -20,7 +20,7 @@ #ifndef PCF_PARSER_HH #define PCF_PARSER_HH -#include <istream> +#include <fstream> #include <string> #include <vector> @@ -51,12 +51,12 @@ /// Parses a PCF file and stores constraint within the class instance. /// Returns false in case of error bool parse (const std::string& a_FileName); - bool parse (std::istream*& a_Stream); + bool parse (std::ifstream& a_Stream); /// Returns the constraint list const std::vector<Constraint> getConstraints () const; -protected: +private: /// A list of constraints std::vector<Constraint> m_Constraints;
diff --git a/ql-iob-plugin/pinmap_parser.cc b/ql-iob-plugin/pinmap_parser.cc index 89a8da8..77e3662 100644 --- a/ql-iob-plugin/pinmap_parser.cc +++ b/ql-iob-plugin/pinmap_parser.cc
@@ -19,7 +19,6 @@ */ #include "pinmap_parser.hh" -#include <fstream> #include <sstream> // ============================================================================ @@ -27,11 +26,10 @@ bool PinmapParser::parse (const std::string& a_FileName) { // Open the file - std::fstream file(a_FileName.c_str(), std::ifstream::in); + std::ifstream file(a_FileName.c_str()); // Parse it - std::istream* stream = &file; - return parse(stream); + return parse(file); } const std::vector<PinmapParser::Entry> PinmapParser::getEntries() const { @@ -55,23 +53,27 @@ return fields; } -bool PinmapParser::parseHeader (std::istream*& a_Stream) { +bool PinmapParser::parseHeader (std::ifstream& a_Stream) { // Get the header line std::string header; - std::getline(*a_Stream, header); + std::getline(a_Stream, header); // Parse fields m_Fields = getFields(header); + if (m_Fields.empty()) { + return false; + } + return true; } -bool PinmapParser::parseData (std::istream*& a_Stream) { +bool PinmapParser::parseData (std::ifstream& a_Stream) { // Parse lines as they come - while (a_Stream->good()) { + while (a_Stream.good()) { std::string line; - std::getline(*a_Stream, line); + std::getline(a_Stream, line); if (line.empty()) { continue; @@ -83,6 +85,11 @@ // Assign data fields to columns Entry entry; for (size_t i=0; i<data.size(); ++i) { + + if (i >= m_Fields.size()) { + return false; + } + entry[m_Fields[i]] = data[i]; } @@ -92,9 +99,9 @@ return true; } -bool PinmapParser::parse (std::istream*& a_Stream) { +bool PinmapParser::parse (std::ifstream& a_Stream) { - if (a_Stream == nullptr) { + if (!a_Stream.good()) { return false; }
diff --git a/ql-iob-plugin/pinmap_parser.hh b/ql-iob-plugin/pinmap_parser.hh index 6d729be..5139244 100644 --- a/ql-iob-plugin/pinmap_parser.hh +++ b/ql-iob-plugin/pinmap_parser.hh
@@ -20,7 +20,7 @@ #ifndef PINMAP_PARSER_HH #define PINMAP_PARSER_HH -#include <istream> +#include <fstream> #include <string> #include <vector> #include <map> @@ -38,21 +38,21 @@ /// Parses a pinmap CSV file bool parse (const std::string& a_FileName); - bool parse (std::istream*& a_Stream); + bool parse (std::ifstream& a_Stream); /// Returns a vector of entries const std::vector<Entry> getEntries() const; -protected: +private: /// Splits the input string into a vector of fields. Fields are comma /// separated. static std::vector<std::string> getFields (const std::string& a_String); /// Parses the header - bool parseHeader (std::istream*& a_Stream); + bool parseHeader (std::ifstream& a_Stream); /// Parses the data - bool parseData (std::istream*& a_Stream); + bool parseData (std::ifstream& a_Stream); /// Header fields std::vector<std::string> m_Fields;