degate  0.1.2
ExternalMatching.cc
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 #include <ExternalMatching.h>
00023 #include <BoundingBox.h>
00024 #include <ImageHelper.h>
00025 #include <DegateHelper.h>
00026 #include <stdlib.h>
00027 #include <iostream>
00028 #include <fstream>
00029 
00030 using namespace degate;
00031 
00032 ExternalMatching::ExternalMatching() {}
00033 
00034 
00035 void ExternalMatching::init(BoundingBox const& bounding_box, Project_shptr project) {
00036 
00037   this->bounding_box = bounding_box;
00038 
00039   if(project == NULL)
00040     throw InvalidPointerException("Invalid pointer for parameter project.");
00041 
00042   lmodel = project->get_logic_model();
00043   assert(lmodel != NULL); // always has a logic model
00044 
00045   layer = lmodel->get_current_layer();
00046   if(layer == NULL) throw DegateRuntimeException("No current layer in project.");
00047 
00048 
00049   ScalingManager_shptr sm = layer->get_scaling_manager();
00050   assert(sm != NULL);
00051 
00052   img = sm->get_image(1).second;
00053   assert(img != NULL);
00054 }
00055 
00056 
00057 void ExternalMatching::set_command(std::string const& cmd) {
00058   this->cmd = cmd;
00059 }
00060 
00061 std::string ExternalMatching::get_command() const {
00062   return cmd;
00063 }
00064 
00065 void ExternalMatching::run() {
00066 
00067   // create a temp dir
00068   std::string dir = create_temp_directory();
00069   assert(is_directory(dir));
00070 
00071   std::string image_file = dir;
00072   image_file.append("/image.tiff");
00073 
00074   std::string results_file = dir;
00075   results_file.append("/results.dat");
00076 
00077   save_part_of_image(image_file, img, bounding_box);
00078 
00079   boost::format f("%1% --image %2% --results %3% "
00080                   "--start-x %4% --start-y %5% --width %6% --height %7%");
00081   f % cmd
00082     % image_file
00083     % results_file
00084     % bounding_box.get_min_x()
00085     % bounding_box.get_min_y()
00086     % bounding_box.get_width()
00087     % bounding_box.get_height();
00088 
00089 
00090   debug(TM, "start external command: %s", f.str().c_str());
00091   exit_code = system(f.str().c_str());
00092   if(exit_code == -1) {
00093     debug(TM, "system() failed");
00094   }
00095   else {
00096     BOOST_FOREACH(PlacedLogicModelObject_shptr plo,
00097                   parse_file(results_file)) {
00098       lmodel->add_object(layer, plo);
00099     }
00100   }
00101 
00102   // cleanup
00103   remove_directory(dir);
00104 }
00105 
00106 int ExternalMatching::get_exit_code() const {
00107   return WEXITSTATUS(exit_code);
00108 }
00109 
00110 
00111 std::list<PlacedLogicModelObject_shptr> ExternalMatching::parse_file(std::string const& filename) const {
00112 
00113   std::list<PlacedLogicModelObject_shptr> list;
00114   std::string line;
00115   std::ifstream file(filename.c_str());
00116 
00117   if(file.is_open()) {
00118 
00119     while(!file.eof()) {
00120 
00121       getline(file, line);
00122 
00123       PlacedLogicModelObject_shptr plo = parse_line(line);
00124       if(plo != NULL) list.push_back(plo);
00125     }
00126     file.close();
00127   }
00128   return list;
00129 }
00130 
00131 PlacedLogicModelObject_shptr ExternalMatching::parse_line(std::string const& line) const {
00132 
00133   std::vector<std::string> tokens = tokenize(line);
00134 
00135   PlacedLogicModelObject_shptr plo;
00136 
00137   if(tokens.size() == 0) return plo;
00138   else if(tokens[0].at(0) == '#') return plo;
00139   else if(tokens[0] == "wire" and tokens.size() >= 6) {
00140 
00141     int
00142       x1 = boost::lexical_cast<int>(tokens[1]),
00143       y1 = boost::lexical_cast<int>(tokens[2]),
00144       x2 = boost::lexical_cast<int>(tokens[3]),
00145       y2 = boost::lexical_cast<int>(tokens[4]),
00146       diameter = boost::lexical_cast<unsigned int>(tokens[5]);
00147 
00148     return Wire_shptr(new Wire(x1, y1, x2, y2, diameter));
00149   }
00150   else if(tokens[0] == "via" and tokens.size() >= 6) {
00151 
00152     int
00153       x = boost::lexical_cast<int>(tokens[1]),
00154       y = boost::lexical_cast<int>(tokens[2]),
00155       diameter = boost::lexical_cast<unsigned int>(tokens[3]);
00156 
00157     Via::DIRECTION dir = tokens[4] == "up" ?
00158       Via::DIRECTION_UP : Via::DIRECTION_DOWN;
00159 
00160     return Via_shptr(new Via(x, y, diameter, dir));
00161   }
00162 
00163   else {
00164     std::string err("Can't parse line: ");
00165     throw DegateRuntimeException(err + line);
00166   }
00167 }
00168 
00169