File: ImportTGF.cpp

package info (click to toggle)
tulip 6.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 196,224 kB
  • sloc: cpp: 571,851; ansic: 13,983; python: 4,105; sh: 1,555; yacc: 522; xml: 484; makefile: 168; pascal: 148; lex: 55
file content (189 lines) | stat: -rw-r--r-- 5,302 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
 *
 * This file is part of Tulip (https://tulip.labri.fr)
 *
 * Authors: David Auber and the Tulip development Team
 * from LaBRI, University of Bordeaux
 *
 * Tulip is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * Tulip is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 */

#include <algorithm>
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <tulip/TulipPluginHeaders.h>

/** \file
 *  \brief - Import a graph file in Trivial Graph format
 *  This plugin imports a graph from a file in Trivial Graph input format,</br>
 *  as it is described https://en.wikipedia.org/wiki/Trivial_Graph_Format
 *
 *
 */
using namespace std;
using namespace tlp;

bool getUnsignedInt(unsigned int &i, const string &str) {
  const char *ptr = str.c_str();
  char *endPtr;
  long int value = strtol(ptr, &endPtr, 10);
  i = uint(value);
  return (value >= 0) && (*endPtr == 0);
}

bool nextToken(const string &str, string &token, string::size_type &pos, bool lastToken) {
  token.clear();
  auto size = str.size();
  if (pos == size)
    return false;

  // look for the beginning of the next token
  pos = str.find_first_not_of(' ', pos);
  if (pos == string::npos)
    return false;

  // look for the end of the next token
  auto endPos = lastToken ? str.find_last_not_of(' ') : str.find(' ', pos);
  if (endPos == string::npos)
    endPos = size;
  else if (lastToken)
    ++endPos;

  // copy found token into token
  token.reserve(endPos - pos);
  token.append(str.data() + pos, endPos - pos);

  // advance pos for the next lookup
  pos = endPos;

  return true;
}

bool tokenize(const string &str, vector<string> &tokens, unsigned int nbMax) {
  if (str.empty())
    return true;

  tokens.clear();
  tokens.reserve(nbMax);
  string token;
  string::size_type pos = 0;

  // collect nbMax tokens
  while (nbMax && nextToken(str, token, pos, nbMax == 1)) {
    --nbMax;
    tokens.push_back(token);
  }

  return tokens.size() > 0;
}

class ImportTGF : public ImportFileModule {

public:
  PLUGININFORMATION(
      "TGF", "Patrick Mary", "30/11/2023",
      "<p>File extension: tgf</p><p>Imports a new graph from a text file in Trivial Graph Format<br/>as it is described in <a href=\"https://en.wikipedia.org/wiki/Trivial_Graph_Format\">https://en.wikipedia.org/wiki/Trivial_Graph_Format</a></p>",
      "1.0", "File")

  ImportTGF(const tlp::PluginContext *context) : ImportFileModule(context, {"tgf"}) {}

  ~ImportTGF() override {}

  bool importFile() override {
    std::istream *in = tlp::getInputFileStream(filename);
    // check for open stream failure
    if (in->fail()) {
      std::stringstream ess;
      ess << filename << ": " << tlp::getStrError();
      pluginProgress->setError(ess.str());
      delete in;
      return false;
    }

    stringstream errors;
    size_t lineNumber = 0;

    if (pluginProgress)
      pluginProgress->showPreview(false);

    tlp::StringProperty *labels = graph->getProperty<StringProperty>("viewLabel");
    tlp_hash_map<unsigned int, tlp::node> nodes;
    vector<std::string> tokens;
    std::string line;
    bool ok = true, inNodes = true;

    while (!in->eof() && std::getline(*in, line)) {
      ++lineNumber;
      ok = tokenize(line, tokens, inNodes ? 2 : 3);
      if (!ok) {
        errors << "empty line";
        break;
      }

      if (inNodes) {
        if (tokens.size() == 1 && tokens[0] == "#") {
          // found separator between nodes and edges declaration
          inNodes = false;
          continue;
        }
        unsigned int id;
        // get node id
        if (!getUnsignedInt(id, tokens[0]) || (nodes.find(id) != nodes.end())) {
          errors << "invalid node id";
          ok = false;
          break;
        }
        // add node
        node n = graph->addNode();
        nodes.emplace(id, n);
        // assign it a label if any
        if (tokens.size() == 2)
          labels->setNodeValue(n, tokens[1]);
      } else {
        if (tokens.size() < 2) {
          errors << "invalid node id";
          ok = false;
          break;
        }
        unsigned int sid, tid;
        // get source and target nodes
        if (!getUnsignedInt(sid, tokens[0]) || (nodes.find(sid) == nodes.end()) ||
            !getUnsignedInt(tid, tokens[1]) || (nodes.find(tid) == nodes.end())) {
          errors << "invalid node id";
          ok = false;
          break;
        }
        // add edge
        edge e = graph->addEdge(nodes[sid], nodes[tid]);
        // assign it a label if any
        if (tokens.size() == 3)
          labels->setEdgeValue(e, tokens[2]);
      }
    }
    if (!ok && pluginProgress) {
      errors << " found while parsing file: " << filename << endl;
      errors << "at line " << lineNumber << endl;

      if (pluginProgress) {
        pluginProgress->setError(errors.str());
      }
    }

    delete in;
    return ok;
  }
};

PLUGIN(ImportTGF)