degate  0.1.2
Importer.h
Go to the documentation of this file.
00001 /* -*-c++-*-
00002 
00003  This file is part of the IC reverse engineering tool degate.
00004 
00005  Copyright 2008, 2009, 2010 by Martin Schobert
00006 
00007  Degate is free software: you can redistribute it and/or modify
00008  it under the terms of the GNU General Public License as published by
00009  the Free Software Foundation, either version 3 of the License, or
00010  any later version.
00011 
00012  Degate is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with degate. If not, see <http://www.gnu.org/licenses/>.
00019 
00020  */
00021 
00022 #ifndef __IMPORTER_H__
00023 #define __IMPORTER_H__
00024 
00025 #include "globals.h"
00026 
00027 #include <stdexcept>
00028 #include <sstream>
00029 #include <boost/format.hpp>
00030 
00031 namespace degate {
00032 
00033   /**
00034    * The base class for importers that can parse text files.
00035    */
00036 
00037   class Importer {
00038 
00039   protected:
00040 
00041     /**
00042      * Check if the file exists and can be read.
00043      * @return Returns RET_OK if the file can be read.
00044      * @todo Change the return type tool bool.
00045      */
00046     virtual ret_t check_file(std::string const& filename) const;
00047 
00048     /**
00049      * Parse a string that represents a boolean value, that is "true" or "false".
00050      * @return Returns a C++ bool, depending on the parsed string.
00051      * @throw std::invalid_argument The exception is thrown, if the string can not be parsed.
00052      */
00053     virtual bool parse_bool(std::string const& str) const;
00054 
00055     /**
00056      * Parse a string that represents a number.
00057      * @throw XMLAttributeParseException This exception is thrown, if the string can't be parsed.
00058      */
00059     template <typename T> T parse_number(std::string const& str) const {
00060       std::stringstream strm(str);
00061       T v;
00062       strm >> v;
00063       if(!strm) {
00064         boost::format f("Can't parse number in Importer::parse_number(). Value is %1%");
00065         f % str;
00066         throw XMLAttributeParseException(f.str());
00067       }
00068       else return v;
00069     }
00070 
00071   public:
00072 
00073     /**
00074      * Create a new text importer object.
00075      */
00076     Importer() {}
00077 
00078     /**
00079      * Destroy a text importer object.
00080      */
00081     virtual ~Importer() {}
00082   };
00083 
00084 }
00085 
00086 #endif