Example #1
0
File: Main.cpp Project: vascofg/cal
/**
 * Carrega o grafo de um ficheiro.
 * @param: Grafo.
 * @return: Retorna true se o grafo for carregado com sucesso, false em caso contrĂ¡rio
 */
bool loadGraph(Graph<int>* graph) {
	ifstream ifs;
	string line;
	ifs.open(FILE_NAME, ios::in);
	if (!ifs.is_open())
		return false;
	getline(ifs, line); //vehicle capacity
	if (line == "")
		return false;
	graph->vehicle_capacity = atoi(line.c_str());
	getline(ifs, line); //blank line
	if (line != "")
		return false;
	getline(ifs, line); //first vertex
	if (line == "")
		return false;
	while (line != "") {
		size_t current = 0;
		size_t next = -1;
		next = line.find_first_of(" ", current);
		int vid = atoi(line.substr(current, next).c_str());
		graph->addVertex(vid);
		Vertex<int> *v = graph->getVertex(vid);
		if (next != string::npos) {
			current = next + 1;
			next = line.find_first_of(" ", current);
			char ntype = line.substr(current, next)[0];
			if (ntype == 's')
				v->isShelter = true;
			else if (next != string::npos) {
				current = next + 1;
				next = line.find_first_of(" ", current);
				unsigned int val = atoi(line.substr(current, next).c_str());
				if (ntype == 'v')
					v->addVehicle(Vehicle(val));
				else if (ntype == 'p')
					v->addPeople(val);
			}
		}
		getline(ifs, line);
	}
	getline(ifs, line); //first edge
	int eid = 0;
	while (line != "") {
		size_t current = 0;
		size_t next = -1;
		next = line.find_first_of(" ", current);
		eid = atoi(line.substr(current, next).c_str());
		current = next + 1;
		next = line.find_first_of(" ", current);
		int source = atoi(line.substr(current, next).c_str());
		current = next + 1;
		next = line.find_first_of(" ", current);
		int dest = atoi(line.substr(current, next).c_str());
		current = next + 1;
		next = line.find_first_of(" ", current);
		int weight = atoi(line.substr(current, next).c_str());
		graph->addEdge(source, dest, weight, eid);
		getline(ifs, line);
	}
	if (eid != 0)
		graph->numEdge = eid; //set numedge to the last edge on file
	return true;
}