static void Main() { var lib = GraphLibrary <object, object, object, object> .LoadFile("../../../../../demo.fgn"); Console.WriteLine(lib.Save()); Example1(); Example2(); }
public static GraphLibrary <T, G, N, E> Load(string fileData) { GraphLibrary <T, G, N, E> library = new GraphLibrary <T, G, N, E>(); Active active = Active.None; Graph <G, N, E> graph = null; int nodeId = -1; int edgeId = -1; int currPos = 0; string line = GraphString.NextLine(fileData, ref currPos)?.Trim(); while (line != null) { if (line.Length == 0) { continue; } if (line[0] == '-') { if (line.Length < 3 || (line[2] != ' ' && line[2] != '\t')) { continue; } string args = line.Substring(3); switch (line[1]) { case 'g': library.Add(args); graph = library.Get(args); active = Active.Graph; break; case 'n': nodeId = graph.AddNodeId(args); active = Active.Node; break; case 'e': string[] nodes = args.Split(','); edgeId = graph.AddEdgeId( nodes[0].Trim(), nodes[1].Trim()); active = Active.Edge; break; } } else if (line[0] == '#') { } else { int sep = line.IndexOf(' '); string key = line.Substring(0, sep); string val = line.Substring(sep + 1); val = GraphString.Unescape(val); switch (active) { case Active.Graph: graph.data [key] = val; break; case Active.Node: graph.nodes[nodeId].data[key] = val; break; case Active.Edge: graph.edges[edgeId].data[key] = val; break; case Active.None: library.data [key] = val; break; } } line = GraphString.NextLine(fileData, ref currPos)?.Trim(); } return(library); }