fakemake.cpp

Go to the documentation of this file.
00001 
00003 #include <ctime>
00004 #include <iostream>
00005 #include <istream>
00006 #include <ostream>
00007 #include <sstream>
00008 #include <string>
00009 #include <utility>
00010 
00011 #include "artifact.hpp"
00012 #include "depgraph.hpp"
00013 #include "variables.hpp"
00014 
00016 std::map<std::string, artifact> artifacts;
00017 
00019 artifact& lookup_artifact(std::string const& name)
00020 {
00021   std::map<std::string, artifact>::iterator iter( artifacts.find(name) );
00022   if (iter != artifacts.end())
00023     return iter->second;
00024   else
00025   {
00026     artifacts.insert(std::make_pair(name, artifact(name)));
00027     return artifacts[name];
00028   }
00029 }
00030 
00034 void parse_graph(std::istream& in, dependency_graph& graph)
00035 {
00036   std::string line;
00037   while (std::getline(std::cin, line))
00038   {
00039     std::string target_name, dependency_name;
00040     std::istringstream stream(line);
00041     if (stream >> target_name >> dependency_name)
00042     {
00043       artifact& target(lookup_artifact(expand(target_name)));
00044       std::string::size_type equal(dependency_name.find('='));
00045       if (equal == std::string::npos)
00046       {
00047         // It's a dependency specification
00048         artifact& dependency(lookup_artifact(target.expand(dependency_name)));
00049         graph.store_dependency(target, dependency);
00050       }
00051       else
00052         // It's a target-specific variable
00053         target.store_variable(dependency_name.substr(0, equal-1),
00054                               dependency_name.substr(equal+1));
00055     }
00056     else if (not target_name.empty())
00057     {
00058       std::string::size_type equal(target_name.find('='));
00059       if (equal == std::string::npos)
00060         // Input line has a target with no dependency,
00061         // so report an error.
00062         std::cerr << "malformed input: target, " << target_name <<
00063                      ", must be followed by a dependency name\n";
00064       else
00065         global_variables[target_name.substr(0, equal)] =
00066                                           target_name.substr(equal+1);
00067     }
00068     // else ignore blank lines
00069   }
00070 
00071   // Don't need the parse map any more.
00072   artifacts.clear();
00073 }
00074 
00075 int main()
00076 {
00077   dependency_graph graph;
00078 
00079   parse_graph(std::cin, graph);
00080 
00081   try {
00082     // Get the sorted artifacts in reverse order.
00083     std::vector<artifact> sorted;
00084     graph.sort(std::back_inserter(sorted));
00085 
00086     // Then print the artifacts in the correct order.
00087     for (std::vector<artifact>::reverse_iterator it(sorted.rbegin());
00088          it != sorted.rend();
00089          ++it)
00090     {
00091       std::cout << it->name() << '\n';
00092     }
00093   } catch (std::runtime_error const& ex) {
00094     std::cerr << ex.what() << '\n';
00095     return EXIT_FAILURE;
00096   }
00097 }

Generated on Sun Nov 30 09:53:22 2008 for Exploring C++ - Final Forms of Key Examples by  doxygen 1.5.3