mmap.cc 1.88 KB
Newer Older
phlo's avatar
phlo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "mmap.hh"

#include <sstream>
#include <iostream>

#include "parser.hh"

namespace ConcuBinE {

//==============================================================================
// MMap
//==============================================================================

//------------------------------------------------------------------------------
phlo's avatar
phlo committed
15
// public constructors
phlo's avatar
phlo committed
16 17
//------------------------------------------------------------------------------

phlo's avatar
phlo committed
18
MMap::MMap (std::istream & f, const std::string & p) : path(p)
phlo's avatar
phlo committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
{
  size_t line_num = 1;
  for (std::string line_buf; std::getline(f, line_buf); line_num++)
    {
      // skip empty lines
      if (line_buf.empty())
        continue;

      std::istringstream line(line_buf);

      // skip comments
      if (line_buf[line_buf.find_first_not_of(" \t")] == '#')
        continue;

      word_t address;

      if (!(line >> address))
        {
          line.clear();

          std::string token;

          if (!(line >> token))
            parser_error(path, line_num, "missing address");

          parser_error(path, line_num, "illegal address [" + token + "]");
        }

      word_t value;

      if (!(line >> value))
        {
          line.clear();

          std::string token;

          if (!(line >> token))
            parser_error(path, line_num, "missing value");

          parser_error(path, line_num, "illegal value [" + token + "]");
        }

      emplace(address, value);
    }
}

//------------------------------------------------------------------------------
phlo's avatar
phlo committed
66
// public member functions
phlo's avatar
phlo committed
67 68 69 70 71 72 73 74 75
//------------------------------------------------------------------------------

// MMap::print -----------------------------------------------------------------

std::string MMap::print () const
{
  std::ostringstream ss;

  for (const auto & [adr, val] : *this)
phlo's avatar
phlo committed
76
    ss << adr << ' ' << val << eol;
phlo's avatar
phlo committed
77 78 79 80 81

  return ss.str();
}

} // namespace ConcuBinE