PIPS
util.c
Go to the documentation of this file.
1 /*
2 
3  $Id: util.c 23493 2018-10-23 13:15:05Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 #ifdef HAVE_CONFIG_H
25  #include "pips_config.h"
26 #endif
27 #include <stdio.h>
28 #include <string.h>
29 
30 #include "genC.h"
31 #include "linear.h"
32 
33 #include "properties.h"
34 #include "misc.h"
35 #include "pipsdbm.h"
36 
37 #include "ri.h"
38 #include "ri-util.h"
39 #include "text-util.h"
40 
41 #include "icfg.h"
42 
43 void safe_free_vertex(vertex ver, list l_of_vers)
44 {
45  if (!gen_in_list_p(ver, l_of_vers))
46  free_vertex(ver);
47  return;
48 }
49 
51 {
52  if (!gen_in_list_p(ver, l_of_vers))
53  ADD_ELEMENT_TO_LIST(l_of_vers, VERTEX, ver);
54  return l_of_vers;
55 }
56 
58 {
59  MAP(SUCCESSOR, succ, {
60  vertex ver_child = successor_vertex(succ);
61  l_of_vers = safe_add_vertex_to_list(ver_child, l_of_vers);
62  l_of_vers = list_of_connected_nodes(ver_child, l_of_vers);
63  }, vertex_successors(ver));
64  return l_of_vers;
65 }
66 
67 static string convert_string_for_daVinci_graph (string s)
68 {
69  string r;
70  int l = strlen(s);
71  if (l == 0) /* if empty, do nothing */
72  return strdup(s);
73  else {
74  int len;
75  r = (string) malloc(l + 3);
76  memset(r, 0, l + 3);
77  if (strstr(s, "C <") == s) {/* effects lines */
78  r[0] = ' '; /* delete the comment of effects */
79  strcpy(r + 1, s + 1);
80  len = l+1;
81  } else {
82  strcpy(r, s);
83  len = l;
84  }
85  // append a \n if needed
86  if (r[len-1] != '\n')
87  r[len] = '\n';
88  }
89  return r;
90 }
91 
92 vertex get_vertex_by_string(const char* str_name, list l_of_vers)
93 {
94  MAP(VERTEX, ver, {
95  text ver_label = (text)vertex_vertex_label(ver);
96  list sens = text_sentences(ver_label);
97  sentence ver_first_sen = SENTENCE(CAR(sens));
98  string ver_name = first_word_of_sentence(ver_first_sen);
99  string ver_real_name = remove_newline_of_string(ver_name);
100  if (same_string_p(ver_real_name, str_name) || same_string_p(ver_name, str_name)) {
101  free(ver_real_name);
102  return ver;
103  } else
104  free(ver_real_name);
105  }, l_of_vers);
106  return vertex_undefined;
107 }
108 
109 
110 list safe_make_successor(vertex ver_parent, vertex ver_child, list l_of_vers)
111 {
112  successor succ;
113  sentence ver_child_first_sen = SENTENCE(CAR(text_sentences((text)vertex_vertex_label(ver_child))));
114  string ver_child_name = first_word_of_sentence(ver_child_first_sen);
115 
116  /* search if it was made yet, do not make */
117  MAP(SUCCESSOR, succ, {
118  vertex ver = successor_vertex(succ);
119  sentence ver_first_sen = SENTENCE(CAR(text_sentences((text)vertex_vertex_label(ver))));
120  string ver_name = first_word_of_sentence(ver_first_sen);
121  if (same_string_p(ver_name, ver_child_name)) return l_of_vers;
122  }, vertex_successors(ver_parent));
123 
124  /* if it was not made, do it */
125  succ = make_successor(NULL, ver_child);
126  ADD_ELEMENT_TO_LIST(vertex_successors(ver_parent), SUCCESSOR, succ);
127  /* add vertex parent to the list of vertex */
128  l_of_vers = safe_add_vertex_to_list(ver_parent, l_of_vers);
129 
130  return l_of_vers;
131 }
132 
133 
134 void print_graph_of_text_to_daVinci(FILE * f_out, list l_of_vers)
135 {
136  bool first_node_parent = true;
137  fprintf(f_out, "[\n");
138 
139  FOREACH (VERTEX, ver_parent, l_of_vers) {
140  bool first_node_child = true;
141  text node_parent_text = (text)vertex_vertex_label(ver_parent);
142  bool first_sentence = true;
143 
144  if (first_node_parent)
145  first_node_parent = false;
146  else
147  fprintf(f_out, ",\n");
148 
149  FOREACH (SENTENCE, sen, text_sentences(node_parent_text)){
150  string s = sentence_to_string(sen);
151  if (first_sentence) {
152  string tmp = remove_newline_of_string (s);
153  fprintf(f_out, "l(\"%s\",n(\"\",[a(\"OBJECT\",\"", tmp);
154  first_sentence = false;
155  free (tmp);
156  }
157  if (strstr(s, CALL_MARK)) {
158  /*fprintf(f_out, convert_string_for_daVinci_graph(s + strlen(CALL_MARK)));*/
159  } else {
160  string r = convert_string_for_daVinci_graph(s);
161  fputs(r, f_out);
162  free(r);
163  }
164  }
165 
166  fprintf(f_out, "\")],[\n");
167 
168  FOREACH (SUCCESSOR, succ, vertex_successors(ver_parent)){
169  vertex ver_child = successor_vertex(succ);
170  text node_child_text = (text)vertex_vertex_label(ver_child);
171  sentence node_child_sen = SENTENCE(CAR(text_sentences(node_child_text)));
172  string node_name_child = remove_newline_of_string(first_word_of_sentence(node_child_sen));
173 
174  if (first_node_child)
175  first_node_child = false;
176  else
177  fprintf(f_out, ",\n");
178  fprintf(f_out, " l(\"\",e(\"\",[],r(\"%s\")))", node_name_child);
179  free(node_name_child);
180  }
181 
182  fprintf(f_out, "]))");
183  }
184 
185  fprintf(f_out, "\n]");
186 
187  return;
188 }
189 
190 void print_graph_daVinci_from_starting_node(FILE * f_out, vertex start_ver)
191 {
192  list l = NIL;
193  ADD_ELEMENT_TO_LIST(l, VERTEX, start_ver);
194  l = list_of_connected_nodes(start_ver, l);
196  gen_free_list(l);
197  return;
198 }
199 
200 void print_marged_text_from_starting_node(FILE *fd, int margin, vertex start_ver, list l_of_vers)
201 {
202  if (!vertex_undefined_p(start_ver)) {
203  text txt = (text)vertex_vertex_label(start_ver);
204  MAP(SENTENCE, sen, {
205  string s = sentence_to_string(sen);
206  string call_mark = strstr(s, CALL_MARK);
207  if(call_mark) {
208  vertex ver_child = get_vertex_by_string(call_mark + strlen(CALL_MARK), l_of_vers);
209  print_marged_text_from_starting_node(fd, margin + (call_mark - s), ver_child, l_of_vers);
210  } else if (strlen(s)) { /* if s in not empty, ok write out */
211  string tmp = remove_newline_of_string(s);
212  fprintf(fd, "%*s%s\n", margin, "", tmp);
213  free (tmp);
214  }
215  }, text_sentences(txt));
216  }
217  return;
218 }
219 
221 (const char* mod_name, string res_name, string file_ext, vertex start_ver, list l_of_vers, bool res_text_type)
222 {
223  string filename, localfilename, dir;
224  FILE *fd;
225 
226  localfilename = db_build_file_resource_name(res_name, mod_name, file_ext);
228  filename = strdup(concatenate(dir, "/", localfilename, NULL));
229  /*free(dir);*/ /* this line may cause a problem of bus errone*/
230 
231  fd = safe_fopen(filename, "w");
232  if (!vertex_undefined_p(start_ver)) {
233  if (res_text_type) {
234  print_marged_text_from_starting_node(fd, 0, start_ver, l_of_vers);
235  safe_fclose(fd, filename);
236  write_an_attachment_file(filename);
237  } else {
239  safe_fclose(fd, filename);
240  }
241  }
242  DB_PUT_FILE_RESOURCE(res_name, mod_name, localfilename);
243  free(filename); /* this line must be after DB_PUT_FILE_RESOURCE for the reason of memory liberation */
244 
245  return true;
246 }
void free_vertex(vertex p)
Definition: graph.c:107
successor make_successor(arc_label a1, vertex a2)
Definition: graph.c:98
void write_an_attachment_file(string file_name)
Add the attachment in Emacs mode by creating a twin file that is decorated with Emacs properties:
FILE * safe_fopen(const char *filename, const char *what)
Definition: file.c:67
int safe_fclose(FILE *stream, const char *filename)
Definition: file.c:77
void * malloc(YYSIZE_T)
void free(void *)
#define successor_vertex(x)
Definition: graph.h:118
#define vertex_undefined
Definition: graph.h:128
#define vertex_vertex_label(x)
Definition: graph.h:152
#define vertex_successors(x)
Definition: graph.h:154
#define vertex_undefined_p(x)
Definition: graph.h:129
#define SUCCESSOR(x)
SUCCESSOR.
Definition: graph.h:86
#define VERTEX(x)
VERTEX.
Definition: graph.h:122
#define NIL
The empty list (nil in Lisp)
Definition: newgen_list.h:47
#define CAR(pcons)
Get the value of the first element of a list.
Definition: newgen_list.h:92
void gen_free_list(list l)
free the spine of the list
Definition: list.c:327
bool gen_in_list_p(const void *vo, const list lx)
tell whether vo belongs to lx
Definition: list.c:734
#define FOREACH(_fe_CASTER, _fe_item, _fe_list)
Apply/map an instruction block on all the elements of a list.
Definition: newgen_list.h:179
#define MAP(_map_CASTER, _map_item, _map_code, _map_list)
Apply/map an instruction block on all the elements of a list (old fashioned)
Definition: newgen_list.h:226
#define DB_PUT_FILE_RESOURCE
Put a file resource into the current workspace database.
Definition: pipsdbm-local.h:85
#define CALL_MARK
Definition: icfg-local.h:49
#define ADD_ELEMENT_TO_LIST(_list, _type, _element)
Definition: icfg-local.h:50
list safe_make_successor(vertex ver_parent, vertex ver_child, list l_of_vers)
Definition: util.c:110
void print_graph_of_text_to_daVinci(FILE *f_out, list l_of_vers)
Definition: util.c:134
vertex get_vertex_by_string(const char *str_name, list l_of_vers)
Definition: util.c:92
list list_of_connected_nodes(vertex ver, list l_of_vers)
Definition: util.c:57
list safe_add_vertex_to_list(vertex ver, list l_of_vers)
Definition: util.c:50
void print_graph_daVinci_from_starting_node(FILE *f_out, vertex start_ver)
Definition: util.c:190
bool make_resource_from_starting_node(const char *mod_name, string res_name, string file_ext, vertex start_ver, list l_of_vers, bool res_text_type)
Definition: util.c:221
static string convert_string_for_daVinci_graph(string s)
Definition: util.c:67
void safe_free_vertex(vertex ver, list l_of_vers)
util.c
Definition: util.c:43
void print_marged_text_from_starting_node(FILE *fd, int margin, vertex start_ver, list l_of_vers)
Definition: util.c:200
string db_build_file_resource_name(const char *rname, const char *oname, const char *suffix)
returns an allocated file name for a file resource.
Definition: lowlevel.c:169
void * memset(void *str, int c, size_t len)
memset.c – set an area of memory to a given value Copyright (C) 1991, 2003, 2009-2011 Free Software F...
Definition: memset.c:23
string remove_newline_of_string(string)
Definition: string.c:309
string concatenate(const char *,...)
Return the concatenation of the given strings.
Definition: string.c:183
#define same_string_p(s1, s2)
char * string
STRING.
Definition: newgen_types.h:39
string db_get_current_workspace_directory(void)
Definition: workspace.c:96
int sens
facteur multiplicatif suivant qu'on analyse un terme
Definition: sc_gram.c:100
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
char * strdup()
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
string sentence_to_string(sentence sen)
SG: moved here from icfdg.
Definition: print.c:230
string first_word_of_sentence(sentence s)
Return the first word of a sentence:
Definition: util.c:76
struct _newgen_struct_text_ * text
Definition: text.h:23
#define SENTENCE(x)
newgen_unformatted_domain_defined
Definition: text.h:36
#define text_sentences(x)
Definition: text.h:113