PIPS
util.c
Go to the documentation of this file.
1 /*
2 
3  $Id: util.c 23065 2016-03-02 09:05:50Z 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 /*
28  * The motivation of text is to delay string concatenation by dealing with
29  * lists of strings instead as more as possible.
30  */
31 
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <string.h>
35 
36 #include "genC.h"
37 #include "linear.h"
38 #include "misc.h"
39 #include "text-util.h"
40 
41 char *
42 int2a(int i)
43 {
44  char *buffer;
45  asprintf(&buffer,"%d", i);
46  return buffer;
47 }
48 
49 
50 char *
51 f2a(float f)
52 {
53  char *buffer;
54  asprintf(&buffer, "%f", f);
55  return buffer;
56 }
57 
58 void
60  string a_format, ...)
61 {
62  /* beurk */
63  char *buffer;
64  va_list some_arguments;
65  va_start(some_arguments, a_format);
66  int ret = vasprintf(&buffer, a_format, some_arguments);
67  pips_assert("vasprintf is ok", ret >= 0);
69  buffer));
70  va_end(some_arguments);
71 }
72 
73 
74 /* Return the first word of a sentence: */
75 string
77 {
78  string begin = string_undefined;
79 
80  if (sentence_formatted_p(s))
81  /* The sentence is seen as a big word: */
82  begin = sentence_formatted(s);
83  else if (sentence_unformatted_p(s)) {
85  /* From the first word to the last one. Label should is
86  skipped: */
87  begin = STRING(CAR(l));
88  }
89  else {
90  pips_assert("s should be formatted or unformatted...", 0);
91  // redundant... to avoid a warning
92  abort();
93  }
94 
95  return begin;
96 }
97 
98 
99 /* Return the last word of a sentence: */
100 string
102 {
103  string end = string_undefined;
104 
105  if (sentence_formatted_p(s))
106  /* The sentence is seen as a big word: */
107  end = sentence_formatted(s);
108  else if (sentence_unformatted_p(s)) {
110  /* From the first word to the last one. Label should is
111  skipped: */
112  end = STRING(CAR(gen_last(l)));
113  }
114  else {
115  pips_assert("s should be formatted or unformatted...", 0);
116  // redundant... to avoid a warning
117  abort();
118  }
119 
120  return end;
121 }
122 
123 /******************************************************** LINE MANAGEMENT */
124 
125 #define LINE_SUFFIX "\n"
126 
127 /* returns a possible index where to cut the string. 0 if none.
128  */
129 static int
131 {
132  int last = 0, i;
133  for (i=strlen(s)-1; i>0 && !last; i--)
134  if (s[i] == ',' || s[i]==')')
135  last = i;
136  return last;
137 }
138 
139 void
141  string buffer, /* current line being processed */
142  const char* append, /* string to add to this line */
143  string continuation, /* prefix when breaking a line */
144  text txt /* where to append completed lines */)
145 {
146  bool divide;
147  char tmp[MAX_LINE_LENGTH];
148  int last_cut;
149  int lappend;
150  int lbuffer = strlen(buffer);
151  bool comment = false;
152  char stmp;
153  /* special case: appends a sole "," on end of line... */
154  if (same_string_p(append, ", ") && lbuffer+3==MAX_LINE_LENGTH)
155  append = ",";
156 
157  lappend = strlen(append);
158 
159 
160  if (lbuffer + lappend + 2 > MAX_LINE_LENGTH) /* 2 = strlen("\n\0") */
161  {
162  /* cannot append the string. must go next line.
163  */
164  last_cut = last_comma_or_clopar(buffer);
165 
166  divide = (last_cut > 0)
167  && (last_cut != lbuffer-1)
168  && (lbuffer - last_cut + lappend +strlen(continuation)
169  < MAX_LINE_LENGTH - 2);
170 
171  if (divide)
172  {
173  int nl_cut = last_cut;
174  while (buffer[nl_cut+1]==' ') nl_cut++;
175  strcpy(tmp, buffer+nl_cut+1); /* save the end of the line */
176  buffer[last_cut+1] = '\0'; /* trunc! */
177  }
178 
179  /* happend new line. */
180  strcat(buffer, LINE_SUFFIX);
183 
184  /* now regenerate the beginning of the line */
185  strcpy(buffer, continuation);
186 
187  if (divide) {
188  strcat(buffer, tmp); /* get back saved part */
189 
190  if (strlen(buffer) + lappend + 2 > MAX_LINE_LENGTH
192  /* current line + new line are too large. Try to append the
193  buffer alone, before trying to add the new line alone */
194  strcat(buffer, LINE_SUFFIX);
197  strcpy(buffer, continuation);
198  }
199  }
200 
201  }
202 
203  /* Append the new line */
204  lbuffer = strlen(buffer);
205  stmp = continuation[0];
206  comment = (stmp == 'c'|| stmp == 'C' || stmp == '!'|| stmp == '*'
207  || (continuation[0] == '/' && continuation[1] == '/')
208  || (continuation[0] == '/' && continuation[1] == '*'));
209 
210  if (strlen(buffer) + lappend + 2 > MAX_LINE_LENGTH) {
211  /* this shouldn't happen.
212  * it can occur if lappend+lcontinuation is too large.
213  */
214  if (comment) {
215  /* Cut the comment */
216  int coupure = MAX_LINE_LENGTH-2 -lbuffer;
217  char tmp2[2*MAX_LINE_LENGTH];
218  strcpy(tmp2,append+coupure);
219  /*SG: warning: I removed the const modifier here */
220  ((char*)append)[coupure]='\0';
221 
224 
225  }
226  else
227  pips_internal_error("line code too large...");
228  }
229  else if (! same_string_p(append, " ")
231  strcat(buffer, append);
232 }
233 
234 void
236  string buffer,
237  text txt,
238  string continuation)
239 {
240  if (strlen(buffer)!=0) /* do not append an empty line to text */ {
241  int lbuffer=0;
242  char stmp = continuation[0];
243  char stmp1 = continuation[1];
244  bool comment = stmp == 'c'|| stmp == 'C'
245  || stmp == '!'|| stmp == '*'
246  || (stmp == '/' && stmp1 == '*') || (stmp == '/' && stmp1 == '/');
247 
248  if ((lbuffer=strlen(buffer))+2>MAX_LINE_LENGTH) {
249  if (comment) {
250  int coupure = MAX_LINE_LENGTH-2;
251  char tmp2[2*MAX_LINE_LENGTH];
252  strcpy(tmp2,buffer+coupure);
253  buffer[coupure]='\0';
254 
258  }
259  else
260  pips_assert("buffer is too large",
261  strlen(buffer)+1<MAX_LINE_LENGTH);
262  }
263  else {
264  strcat(buffer, LINE_SUFFIX);
267  buffer[0] = '\0';
268  }
269  }
270 }
271 
272 /* Add the word list wl to the end of the last sentence of text t */
274 {
275  list sl = text_sentences(t);
276 
277  if(ENDP(sl)) {
278  pips_internal_error("what kind of sentence to make?");
279  }
280  else {
281  sentence s = SENTENCE(CAR(gen_last(sl)));
282  if(sentence_formatted_p(s)) {
283  pips_internal_error("Not implemented");
284  }
285  else {
288  }
289  }
290  pips_assert("t is consistent", text_consistent_p(t));
291 }
bool text_consistent_p(text p)
Definition: text.c:80
sentence make_sentence(enum sentence_utype tag, void *val)
Definition: text.c:59
#define append(s)
text text_region_no_action(effect reg) input : a region output : a text consisting of several lines o...
Definition: prettyprint.c:77
#define divide(a, b)
#define continuation
Definition: prettyprint.c:102
#define ret(why, what)
true if not a remapping for old.
Definition: dynamic.c:986
static void comment(string_buffer code, spoc_hardware_type hw, dagvtx v, int stage, int side, bool flip)
Definition: freia_spoc.c:52
#define STRING(x)
Definition: genC.h:87
#define ENDP(l)
Test if a list is empty.
Definition: newgen_list.h:66
list gen_nconc(list cp1, list cp2)
physically concatenates CP1 and CP2 but do not duplicates the elements
Definition: list.c:344
#define CAR(pcons)
Get the value of the first element of a list.
Definition: newgen_list.h:92
list gen_last(list l)
Return the last element of a list.
Definition: list.c:578
char end
Definition: gtk_status.c:82
#define asprintf
Definition: misc-local.h:225
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
#define pips_internal_error
Definition: misc-local.h:149
#define abort()
Definition: misc-local.h:53
#define same_string_p(s1, s2)
#define string_undefined
Definition: newgen_types.h:40
int f(int off1, int off2, int n, float r[n], float a[n], float b[n])
Definition: offsets.c:15
char * strdup()
static string buffer
Definition: string.c:113
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
#define ADD_SENTENCE_TO_TEXT(t, p)
#define MAX_LINE_LENGTH
maximum length of a line when prettyprinting...
void add_to_current_line(string buffer, const char *append, string continuation, text txt)
Definition: util.c:140
char * int2a(int i)
util.c
Definition: util.c:42
void add_words_to_text(text t, list wl)
Add the word list wl to the end of the last sentence of text t.
Definition: util.c:273
void add_one_unformated_printf_to_text(text r, string a_format,...)
Definition: util.c:59
static int last_comma_or_clopar(string s)
returns a possible index where to cut the string.
Definition: util.c:130
string last_word_of_sentence(sentence s)
Return the last word of a sentence:
Definition: util.c:101
void close_current_line(string buffer, text txt, string continuation)
Definition: util.c:235
char * f2a(float f)
Definition: util.c:51
string first_word_of_sentence(sentence s)
Return the first word of a sentence:
Definition: util.c:76
#define LINE_SUFFIX
Definition: util.c:125
#define sentence_unformatted_p(x)
Definition: text.h:79
#define SENTENCE(x)
newgen_unformatted_domain_defined
Definition: text.h:36
#define sentence_unformatted(x)
Definition: text.h:81
#define sentence_formatted(x)
Definition: text.h:78
#define text_sentences(x)
Definition: text.h:113
#define unformatted_words(x)
Definition: text.h:155
@ is_sentence_formatted
Definition: text.h:57
#define sentence_formatted_p(x)
Definition: text.h:76
int vasprintf(char **resultp, const char *format, va_list args)
Formatted output to strings.
Definition: vasprintf.c:33