parse_top.cc 1.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Parse chris' ".top" file format into a LEDA graph
 */

#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <LEDA/graph_alg.h>
#include <LEDA/graphwin.h>
#include <LEDA/dictionary.h>
#include <LEDA/map.h>
#include <LEDA/graph_iterator.h>
13
#include <LEDA/node_pq.h>
14 15 16 17

#include "common.h"
#include "virtual.h"

18 19 20 21 22
extern tb_vgraph G;

extern node_pq<int> unassigned_nodes;

int parse_top(tb_vgraph &G, istream& i)
23 24 25 26 27 28
{
  dictionary<string, node> nmap;
  node no1;
  string s1, s2;
  char inbuf[255];
  char n1[32], n2[32], type[32];
29
  char lname[32];
30
  int num_nodes = 0;
31 32
  int bw;
  int r;
33
  
34 35 36 37 38 39 40 41 42 43 44
  while (!i.eof()) {
    char *ret;
    i.getline(inbuf, 254);
    ret = strchr(inbuf, '\n');
    if (ret) *ret = 0;
    if (strlen(inbuf) == 0) { continue; }
	    
    if (!strncmp(inbuf, "node", 4)) {
      if (sscanf(inbuf, "node %s %s", n1, type) < 1) {
	fprintf(stderr, "bad node line: %s\n", inbuf);
      } else {
45
	num_nodes++;
46 47
	string s1(n1);
	no1 = G.new_node();
48
	unassigned_nodes.insert(no1,random());
49 50 51 52
	G[no1].name=strdup(n1);
	G[no1].posistion = 0;
	G[no1].no_connections=0;
	nmap.insert(s1, no1);
53
	G[no1].type=string(type);
54
      }
55
    } else if (!strncmp(inbuf, "link", 4)) {
56
      r=sscanf(inbuf, "link %s %s %s %d", lname, n1, n2,&bw);
57
      if (r < 2) {
58 59
	fprintf(stderr, "bad link line: %s\n", inbuf);
      } else {
60
	if (r == 2) bw = 10;
61 62 63 64 65 66
	string s1(n1);
	string s2(n2);
	edge e;
	node node1 = nmap.access(s1);
	node node2 = nmap.access(s2);
	e = G.new_edge(node1, node2);
67
	G[e].bandwidth = bw;
68 69 70
	G[e].type = tb_vlink::LINK_UNKNOWN;
	G[e].plink = NULL;
	G[e].plink_two = NULL;
71
	G[e].name = strdup(lname);
72 73 74 75 76 77
      }
    }
    else {
      fprintf(stderr, "unknown directive: %s\n", inbuf);
    }
  }
78
  return num_nodes;
79
}