2020-03-02 09:19:44 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2020-03-02 09:19:44 +00:00
|
|
|
/**
|
|
|
|
* Miscellaneous utilities for use in yul-phaser.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-03-02 09:20:28 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
2020-02-21 23:48:35 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2020-03-02 09:20:28 +00:00
|
|
|
|
2020-03-02 09:19:44 +00:00
|
|
|
namespace solidity::phaser
|
|
|
|
{
|
|
|
|
|
2020-02-21 23:48:35 +00:00
|
|
|
/// Loads the whole file into memory, splits the content into lines, strips newlines and
|
|
|
|
/// returns the result as a list of strings.
|
|
|
|
///
|
|
|
|
/// Throws FileOpenError if the file does not exist or cannot be opened for reading.
|
|
|
|
/// Throws FileReadError if any read operation fails during the whole process.
|
|
|
|
std::vector<std::string> readLinesFromFile(std::string const& _path);
|
|
|
|
|
2020-03-02 09:20:28 +00:00
|
|
|
/// Reads a token from the input stream and translates it to a string using a map.
|
|
|
|
/// Sets the failbit in the stream if there's no matching value in the map.
|
|
|
|
template <typename C>
|
|
|
|
std::istream& deserializeChoice(
|
|
|
|
std::istream& _inputStream,
|
|
|
|
C& _choice,
|
|
|
|
std::map<std::string, C> const& _stringToValueMap
|
|
|
|
)
|
|
|
|
{
|
|
|
|
std::string deserializedValue;
|
|
|
|
_inputStream >> deserializedValue;
|
|
|
|
|
|
|
|
auto const& pair = _stringToValueMap.find(deserializedValue);
|
|
|
|
if (pair != _stringToValueMap.end())
|
|
|
|
_choice = pair->second;
|
|
|
|
else
|
|
|
|
_inputStream.setstate(std::ios_base::failbit);
|
|
|
|
|
|
|
|
return _inputStream;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Translates a value to a string using a map and prints it to the output stream.
|
|
|
|
/// Sets the failbit if the value is not in the map.
|
|
|
|
template <typename C>
|
|
|
|
std::ostream& serializeChoice(
|
|
|
|
std::ostream& _outputStream,
|
|
|
|
C const& _choice,
|
|
|
|
std::map<C, std::string> const& _valueToStringMap
|
|
|
|
)
|
|
|
|
{
|
|
|
|
auto const& pair = _valueToStringMap.find(_choice);
|
|
|
|
if (pair != _valueToStringMap.end())
|
|
|
|
_outputStream << pair->second;
|
|
|
|
else
|
|
|
|
_outputStream.setstate(std::ios_base::failbit);
|
|
|
|
|
|
|
|
return _outputStream;
|
|
|
|
}
|
|
|
|
|
2020-03-02 09:19:44 +00:00
|
|
|
}
|