Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge ../simgrid
[simgrid.git] / src / xbt / graphxml_parse.c
1 /* Copyright (c) 2006, 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "xbt/misc.h"
8 #include "xbt/sysdep.h"
9 #include "xbt/log.h"
10 #include "xbt/asserts.h"
11
12 #include "xbt/dynar.h"
13 #include "xbt/graphxml_parse.h"
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(graphxml_parse, xbt,
16                                 "Logging specific to the graphxml parsing  module");
17
18 #undef CLEANUP
19 #include "graphxml.c"
20
21 static xbt_dynar_t xbt_graph_input_buffer_stack = NULL;
22 static xbt_dynar_t xbt_graph_file_to_parse_stack = NULL;
23
24 static void nil_function(void)
25 {
26   return;
27 }
28
29 void_f_void_t STag_graphxml_graph_fun = nil_function;
30 void_f_void_t ETag_graphxml_graph_fun = nil_function;
31 void_f_void_t STag_graphxml_node_fun = nil_function;
32 void_f_void_t ETag_graphxml_node_fun = nil_function;
33 void_f_void_t STag_graphxml_edge_fun = nil_function;
34 void_f_void_t ETag_graphxml_edge_fun = nil_function;
35
36 YY_BUFFER_STATE xbt_graph_input_buffer;
37 FILE *xbt_graph_file_to_parse;
38
39 void xbt_graph_parse_reset_parser(void)
40 {
41   STag_graphxml_graph_fun = nil_function;
42   ETag_graphxml_graph_fun = nil_function;
43   STag_graphxml_node_fun = nil_function;
44   ETag_graphxml_node_fun = nil_function;
45   STag_graphxml_edge_fun = nil_function;
46   ETag_graphxml_edge_fun = nil_function;
47 }
48
49 void STag_graphxml_graph(void)
50 {
51   STag_graphxml_graph_fun();
52 }
53
54 void ETag_graphxml_graph(void)
55 {
56   ETag_graphxml_graph_fun();
57 }
58
59
60 void STag_graphxml_node(void)
61 {
62   STag_graphxml_node_fun();
63 }
64
65 void ETag_graphxml_node(void)
66 {
67   ETag_graphxml_node_fun();
68 }
69
70
71 void STag_graphxml_edge(void)
72 {
73   STag_graphxml_edge_fun();
74 }
75
76 void ETag_graphxml_edge(void)
77 {
78   ETag_graphxml_edge_fun();
79 }
80
81
82
83 void xbt_graph_parse_open(const char *file)
84 {
85   if (!file) {
86     XBT_WARN
87         ("I hope you know what you're doing... you just gave me a NULL pointer!");
88     return;
89   }
90   if (!xbt_graph_input_buffer_stack)
91     xbt_graph_input_buffer_stack =
92         xbt_dynar_new(sizeof(YY_BUFFER_STATE), NULL);
93   if (!xbt_graph_file_to_parse_stack)
94     xbt_graph_file_to_parse_stack = xbt_dynar_new(sizeof(FILE *), NULL);
95
96   xbt_graph_file_to_parse = fopen(file, "r");   /* FIXME should use something like surf_fopen */
97   xbt_assert((xbt_graph_file_to_parse), "Unable to open \"%s\"\n", file);
98   xbt_graph_input_buffer =
99       xbt_graph_parse__create_buffer(xbt_graph_file_to_parse, 10);
100   xbt_graph_parse__switch_to_buffer(xbt_graph_input_buffer);
101   xbt_graph_parse_lineno = 1;
102 }
103
104 void xbt_graph_parse_close(void)
105 {
106   xbt_dynar_free(&xbt_graph_input_buffer_stack);
107   xbt_dynar_free(&xbt_graph_file_to_parse_stack);
108
109   if (xbt_graph_file_to_parse) {
110     xbt_graph_parse__delete_buffer(xbt_graph_input_buffer);
111     fclose(xbt_graph_file_to_parse);
112   }
113 }
114
115
116 static int _xbt_graph_parse(void)
117 {
118   return xbt_graph_parse_lex();
119 }
120
121 int_f_void_t xbt_graph_parse = _xbt_graph_parse;
122
123 double xbt_graph_parse_get_double(const char *string)
124 {
125   double result;
126   _XBT_GNUC_UNUSED int ret = 0;
127
128   ret = sscanf(string, "%lg", &result);
129   xbt_assert((ret == 1), "Parse error line %d : %s not a number",
130               xbt_graph_parse_lineno, string);
131   return result;
132 }