src/work/akos/loader.h
changeset 96 e2e18eb0fd10
equal deleted inserted replaced
-1:000000000000 0:4c0371e12dce
       
     1 #ifndef LOADER_H
       
     2 #define LOADER_H
       
     3 
       
     4 #include <map>
       
     5 
       
     6 #define LINE_LEN 1024
       
     7 
       
     8 template<typename Graph>
       
     9 void LoadGraph(Graph &G, char *filename) {
       
    10   FILE *file;
       
    11   if ((file = fopen(filename, "r")) == NULL) {
       
    12     printf("Could not open %s\n", filename);
       
    13     return;
       
    14   }
       
    15   int i, n1, n2;
       
    16   std::map<int, typename Graph::NodeIt> nmap;
       
    17   char line[LINE_LEN];
       
    18   while (fgets(line, LINE_LEN, file)) {
       
    19     switch (line[0]) {
       
    20       case 'n':
       
    21         nmap[atoi(line + 1)] = G.addNode();
       
    22         break;
       
    23       case 'a':
       
    24         n1 = atoi(line + 1);
       
    25         i = 1;
       
    26         while (isspace(line[i])) i++;
       
    27         while (isdigit(line[i])) i++;
       
    28         n2 = atoi(line + i);
       
    29         if (nmap.find(n1) == nmap.end()) {
       
    30           nmap[n1] = G.addNode();
       
    31         }
       
    32         if (nmap.find(n2) == nmap.end()) {
       
    33           nmap[n2] = G.addNode();
       
    34         }
       
    35         G.addEdge(nmap[n1], nmap[n2]);
       
    36         break;
       
    37     }
       
    38   }
       
    39 }
       
    40 
       
    41 #endif