Exemple #1
0
//============================================================================================
// FileReader::read
//
// Input:
//  path: path to the file containing details about the graph to be used with Dijkstra's Algorithm.
//
// Output:
//  InputReaderResult object.
//
// Read the details about the graph that will be used with Dijkstra's Algorithm from the specified
// file.
//============================================================================================
InputReaderResult *FileReader::read(std::string path){
    FileReader::lineNo = 0;
    std::ifstream stream;
    unsigned long from, to;
    
    stream.open(path);
    
    if(!stream.is_open()){
        InputReader::throwException("Problem opening file at %s", path.c_str());
    }
    
    std::map<unsigned long, City *>cities = FileReader::readCities(stream);
    printf("\n");
    
    //read existing roads
    readRoads(stream, cities, false);
    printf("\n");
    
    //read potential roads
    std::vector<Road *> optRoads = readRoads(stream, cities, true);
    printf("\n");
    
    from = FileReader::readUL(stream, true);
    printf("Starting city: %lu\n", from);
    
    to = FileReader::readUL(stream, true);
    printf("Ending city: %lu\n", to);
    
    stream.close();
    
    return new InputReaderResult(to, from, cities, optRoads);
}
Exemple #2
0
/**
 * Reads the edges from a file and loads them into the graph
 */
void readEdges(Graph<Node, Road> & g, GraphViewer *gv) {
	ifstream inFile;

	//Ler o ficheiro subroads.txt
	inFile.open("database/subroadsDemo.txt");

	if (!inFile) {
		cerr << "Unable to open file subroadsDemo.txt";
		exit(1);   // call system to stop
	}

	std::string line;

	unsigned long roadID;
	unsigned long node1ID, node2ID;

	while (std::getline(inFile, line)) {
		std::stringstream linestream(line);
		std::string data;

		linestream >> roadID;

		std::getline(linestream, data, ';'); // read up-to the first ; (discard ;).
		linestream >> node1ID;
		std::getline(linestream, data, ';'); // read up-to the first ; (discard ;).
		linestream >> node2ID;

		float weight = calcWeight(findNode(g, node1ID), findNode(g, node2ID));
		gv->addEdge(roadID, node1ID, node2ID, EdgeType::UNDIRECTED);
		Road r = readRoads(roadID, gv);
		if (r.is_two_way())
			g.addEdge1(findNode(g, node2ID), findNode(g, node1ID), weight, r);

		g.addEdge1(findNode(g, node1ID), findNode(g, node2ID), weight, r);

	}

	inFile.close();

}
Exemple #3
0
void RoadManager::read()
{
    readRoads("data/roads", roadMap, true, false);
}