]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
Issue 4550 (2/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / lily-guile.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2015 Jan Nieuwenhuizen <janneke@gnu.org>
5   Han-Wen Nienhuys <hanwen@xs4all.nl>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "lily-guile.hh"
22
23 #include <cstdio>
24 #include <cstdlib>
25 #include <cstring> /* strdup, strchr */
26 #include <cctype>
27
28
29 #include "dimensions.hh"
30 #include "direction.hh"
31 #include "file-path.hh"
32 #include "international.hh"
33 #include "libc-extension.hh"
34 #include "main.hh"
35 #include "misc.hh"
36 #include "offset.hh"
37 #include "pitch.hh"
38 #include "string-convert.hh"
39 #include "source-file.hh"
40 #include "version.hh"
41 #include "warn.hh"
42 #include "lily-imports.hh"
43
44 using std::string;
45 using std::vector;
46
47 /*
48   symbols/strings.
49  */
50 string
51 ly_scm_write_string (SCM s)
52 {
53   SCM port = scm_mkstrport (SCM_INUM0,
54                             scm_make_string (SCM_INUM0, SCM_UNDEFINED),
55                             SCM_OPN | SCM_WRTNG,
56                             "ly_write2string");
57   //  SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
58   SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
59
60   // scm_apply (write, port, SCM_EOL);
61   scm_call_2 (write, s, port);
62   return ly_scm2string (scm_strport_to_string (port));
63 }
64
65 SCM
66 ly_quote_scm (SCM s)
67 {
68   return scm_list_2 (ly_symbol2scm ("quote"), s);
69 }
70
71 string
72 ly_symbol2string (SCM s)
73 {
74   /*
75     Ugh. this is not very efficient.
76   */
77   return ly_scm2string (scm_symbol_to_string (s));
78 }
79
80 string
81 robust_symbol2string (SCM sym, const string &str)
82 {
83   return scm_is_symbol (sym) ? ly_symbol2string (sym) : str;
84 }
85
86 string
87 gulp_file_to_string (const string &fn, bool must_exist, int size)
88 {
89   string s = global_path.find (fn);
90   if (s == "")
91     {
92       if (must_exist)
93         {
94           string e = _f ("cannot find file: `%s'", fn);
95           e += " ";
96           e += _f ("(load path: `%s')", global_path.to_string ());
97           error (e);
98           /* unreachable */
99         }
100       return s;
101     }
102
103   debug_output ("[" + s, true);
104
105   vector<char> chars = gulp_file (s, size);
106   string result (&chars[0], chars.size ());
107
108   debug_output ("]\n", false);
109
110   return result;
111 }
112
113 extern "C" {
114   // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
115   void
116   ly_display_scm (SCM s)
117   {
118     scm_display (s, scm_current_output_port ());
119     scm_newline (scm_current_output_port ());
120   }
121 };
122
123 /*
124   STRINGS
125  */
126 string
127 ly_scm2string (SCM str)
128 {
129   assert (scm_is_string (str));
130   string result;
131   size_t len = scm_c_string_length (str);
132   if (len)
133     {
134       result.resize (len);
135       scm_to_locale_stringbuf (str, &result.at (0), len);
136     }
137   return result;
138 }
139
140 SCM
141 ly_string2scm (string const &str)
142 {
143   return scm_from_locale_stringn (str.c_str (),
144                                   str.length ());
145 }
146
147 char *
148 ly_scm2str0 (SCM str)
149 {
150   return scm_to_utf8_string (str);
151 }
152
153 /*
154   PAIRS
155 */
156 SCM
157 index_get_cell (SCM s, Direction d)
158 {
159   assert (d);
160   return (d == LEFT) ? scm_car (s) : scm_cdr (s);
161 }
162
163 SCM
164 index_set_cell (SCM s, Direction d, SCM v)
165 {
166   if (d == LEFT)
167     scm_set_car_x (s, v);
168   else if (d == RIGHT)
169     scm_set_cdr_x (s, v);
170   return s;
171 }
172
173 bool
174 is_number_pair (SCM p)
175 {
176   return scm_is_pair (p)
177          && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
178 }
179
180 unsigned int
181 ly_scm_hash (SCM s)
182 {
183   return scm_ihashv (s, ~1u);
184 }
185
186 bool
187 is_axis (SCM s)
188 {
189   if (scm_is_integer (s))
190     {
191       int i = scm_to_int (s);
192       return i == 0 || i == 1;
193     }
194   return false;
195 }
196
197 bool
198 to_boolean (SCM s)
199 {
200   return scm_is_bool (s) && ly_scm2bool (s);
201 }
202
203 /*
204   DIRECTIONS
205  */
206 Direction
207 to_dir (SCM s)
208 {
209   return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
210 }
211
212 Direction
213 robust_scm2dir (SCM d, Direction def)
214 {
215   if (is_direction (d))
216     def = to_dir (d);
217   return def;
218 }
219
220 bool
221 is_direction (SCM s)
222 {
223   if (scm_is_number (s))
224     {
225       int i = scm_to_int (s);
226       return i >= -1 && i <= 1;
227     }
228   return false;
229 }
230
231 /*
232   INTERVALS
233  */
234 Interval
235 ly_scm2interval (SCM p)
236 {
237   return Interval (scm_to_double (scm_car (p)),
238                    scm_to_double (scm_cdr (p)));
239 }
240
241 Drul_array<Real>
242 ly_scm2realdrul (SCM p)
243 {
244   return Drul_array<Real> (scm_to_double (scm_car (p)),
245                            scm_to_double (scm_cdr (p)));
246 }
247
248 SCM
249 ly_interval2scm (Drul_array<Real> i)
250 {
251   return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
252 }
253
254 Interval
255 robust_scm2interval (SCM k, Drul_array<Real> v)
256 {
257   Interval i;
258   i[LEFT] = v[LEFT];
259   i[RIGHT] = v[RIGHT];
260   if (is_number_pair (k))
261     i = ly_scm2interval (k);
262   return i;
263 }
264
265 Drul_array<Real>
266 robust_scm2drul (SCM k, Drul_array<Real> v)
267 {
268   if (is_number_pair (k))
269     v = ly_scm2interval (k);
270   return v;
271 }
272
273 Drul_array<bool>
274 robust_scm2booldrul (SCM k, Drul_array<bool> def)
275 {
276   if (scm_is_pair (k))
277     {
278       def[LEFT] = to_boolean (scm_car (k));
279       def[RIGHT] = to_boolean (scm_cdr (k));
280     }
281   return def;
282 }
283
284 /*
285   OFFSET
286 */
287 SCM
288 ly_offset2scm (Offset o)
289 {
290   return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
291 }
292
293 Offset
294 ly_scm2offset (SCM s)
295 {
296   return Offset (scm_to_double (scm_car (s)),
297                  scm_to_double (scm_cdr (s)));
298 }
299
300 Offset
301 robust_scm2offset (SCM k, Offset o)
302 {
303   if (is_number_pair (k))
304     o = ly_scm2offset (k);
305   return o;
306 }
307 SCM
308 ly_offsets2scm (vector<Offset> os)
309 {
310   SCM l = SCM_EOL;
311   SCM *tail = &l;
312   for (vsize i = 0; i < os.size (); i++)
313     {
314       *tail = scm_cons (ly_offset2scm (os[i]), SCM_EOL);
315       tail = SCM_CDRLOC (*tail);
316     }
317   return l;
318 }
319
320 vector<Offset>
321 ly_scm2offsets (SCM s)
322 {
323   vector<Offset> os;
324   for (; scm_is_pair (s); s = scm_cdr (s))
325     os.push_back (ly_scm2offset (scm_car (s)));
326   return os;
327 }
328
329 /*
330   ALIST
331 */
332 SCM
333 ly_alist_vals (SCM alist)
334 {
335   SCM x = SCM_EOL;
336   for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
337     x = scm_cons (scm_cdar (p), x);
338   return x;
339 }
340
341 /*
342   LISTS
343  */
344
345 /* Return I-th element, or last elt L. If I < 0, then we take the first
346    element.
347
348    PRE: length (L) > 0  */
349 SCM
350 robust_list_ref (int i, SCM l)
351 {
352   while (i-- > 0 && scm_is_pair (scm_cdr (l)))
353     l = scm_cdr (l);
354   return scm_car (l);
355 }
356
357 SCM
358 ly_deep_copy (SCM src)
359 {
360   if (scm_is_pair (src))
361     {
362       SCM res = SCM_EOL;
363       do
364         {
365           res = scm_cons (ly_deep_copy (scm_car (src)), res);
366           src = scm_cdr (src);
367         }
368       while (scm_is_pair (src));
369       // Oh, come on, GUILE.  Why do you require the second argument
370       // of scm_reverse_x to be a proper list?  That makes no sense.
371       // return scm_reverse_x (res, ly_deep_copy (src));
372       SCM last_cons = res;
373       res = scm_reverse_x (res, SCM_EOL);
374       scm_set_cdr_x (last_cons, ly_deep_copy (src));
375       return res;
376     }
377   if (scm_is_vector (src))
378     {
379       int len = scm_c_vector_length (src);
380       SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
381       for (int i = 0; i < len; i++)
382         {
383           SCM si = scm_from_int (i);
384           scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
385         }
386       return nv;
387     }
388   return src;
389 }
390
391 string
392 print_scm_val (SCM val)
393 {
394   string realval = ly_scm_write_string (val);
395   if (realval.length () > 200)
396     realval = realval.substr (0, 100)
397               + "\n :\n :\n"
398               + realval.substr (realval.length () - 100);
399   return realval;
400 }
401
402 bool
403 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
404 {
405
406   // If undefined, some internal function caused it...should never happen.
407   assert (!SCM_UNBNDP (val));
408   if (!scm_is_symbol (sym))
409     return false;
410
411   SCM type = scm_object_property (sym, type_symbol);
412
413   if (!scm_is_null (type) && !ly_is_procedure (type))
414     {
415       warning (_f ("cannot find property type-check for `%s' (%s).",
416                    ly_symbol2string (sym).c_str (),
417                    ly_symbol2string (type_symbol).c_str ())
418                + "  " + _ ("perhaps a typing error?"));
419
420       /* Be strict when being anal :) */
421       if (do_internal_type_checking_global)
422         scm_throw (ly_symbol2scm ("ly-file-failed"), scm_list_3 (ly_symbol2scm ("typecheck"),
423                                                                  sym, val));
424
425       warning (_ ("skipping assignment"));
426       return false;
427     }
428
429   /*
430     Always succeeds.
431
432
433     TODO: should remove #f from allowed vals?
434   */
435   if (scm_is_null (val) || scm_is_false (val))
436     return true;
437
438   if (!scm_is_null (val)
439       && ly_is_procedure (type)
440       && scm_is_false (scm_call_1 (type, val)))
441     {
442       SCM type_name = Lily::type_name (type);
443
444       warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
445                    ly_symbol2string (sym).c_str (),
446                    print_scm_val (val),
447                    ly_scm2string (type_name).c_str ()));
448       progress_indication ("\n");
449       return false;
450     }
451   return true;
452 }
453
454 void
455 ly_wrong_smob_arg (bool pred (SCM), SCM var, int number, const char *fun)
456 {
457   string type = predicate_to_typename ((void *) pred);
458   if (pred (var))
459     {
460       // Uh oh.  unsmob<T> delivered 0, yet
461       // unsmob<T> delivers true.  This means that unsmob<T> is a
462       // matching check from a base class of T, but var is of an
463       // incompatible derived type.
464       type = string (_ ("Wrong kind of ")).append (type);
465     }
466   scm_wrong_type_arg_msg (mangle_cxx_identifier (fun).c_str (),
467                           number, var, type.c_str ());
468 }
469
470
471 /* some SCM abbrevs
472
473 zijn deze nou handig?
474 zijn ze er al in scheme, maar heten ze anders? */
475
476 /* Remove doubles from (sorted) list */
477 SCM
478 ly_unique (SCM list)
479 {
480   SCM unique = SCM_EOL;
481   for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
482     {
483       if (!scm_is_pair (scm_cdr (i))
484           || !ly_is_equal (scm_car (i), scm_cadr (i)))
485         unique = scm_cons (scm_car (i), unique);
486     }
487   return scm_reverse_x (unique, SCM_EOL);
488 }
489
490 /* Split list at member s, removing s.
491    Return (BEFORE . AFTER)  */
492 SCM
493 ly_split_list (SCM s, SCM list)
494 {
495   SCM before = SCM_EOL;
496   SCM after = list;
497   for (; scm_is_pair (after);)
498     {
499       SCM i = scm_car (after);
500       after = scm_cdr (after);
501       if (ly_is_equal (i, s))
502         break;
503       before = scm_cons (i, before);
504     }
505   return scm_cons (scm_reverse_x (before, SCM_EOL), after);
506 }
507
508 void
509 taint (SCM *)
510 {
511   /*
512     nop.
513   */
514 }
515
516 /*
517   display stuff without using stack
518 */
519 SCM
520 display_list (SCM s)
521 {
522   SCM p = scm_current_output_port ();
523
524   scm_puts ("(", p);
525   for (; scm_is_pair (s); s = scm_cdr (s))
526     {
527       scm_display (scm_car (s), p);
528       scm_puts (" ", p);
529     }
530   scm_puts (")", p);
531   return SCM_UNSPECIFIED;
532 }
533
534 Slice
535 int_list_to_slice (SCM l)
536 {
537   Slice s;
538   s.set_empty ();
539   for (; scm_is_pair (l); l = scm_cdr (l))
540     if (scm_is_number (scm_car (l)))
541       s.add_point (scm_to_int (scm_car (l)));
542   return s;
543 }
544
545 Real
546 robust_scm2double (SCM k, double x)
547 {
548   if (scm_is_number (k))
549     x = scm_to_double (k);
550   return x;
551 }
552
553 vector<Real>
554 ly_scm2floatvector (SCM l)
555 {
556   vector<Real> floats;
557   for (SCM s = l; scm_is_pair (s); s = scm_cdr (s))
558     floats.push_back (robust_scm2double (scm_car (s), 0.0));
559   return floats;
560 }
561
562 SCM
563 ly_floatvector2scm (vector<Real> v)
564 {
565   SCM l = SCM_EOL;
566   SCM *tail = &l;
567   for (vsize i = 0; i < v.size (); i++)
568     {
569       *tail = scm_cons (scm_from_double (v[i]), SCM_EOL);
570       tail = SCM_CDRLOC (*tail);
571     }
572   return l;
573 }
574
575 string
576 robust_scm2string (SCM k, const string &s)
577 {
578   if (scm_is_string (k))
579     return ly_scm2string (k);
580   return s;
581 }
582
583 int
584 robust_scm2int (SCM k, int o)
585 {
586   if (scm_is_integer (k))
587     o = scm_to_int (k);
588   return o;
589 }
590
591 vsize
592 robust_scm2vsize (SCM k, vsize o)
593 {
594   if (scm_is_integer (k))
595     {
596       int i = scm_to_int (k);
597       if (i >= 0)
598         return (vsize) i;
599     }
600   return o;
601 }
602
603 SCM
604 ly_rational2scm (Rational r)
605 {
606   if (r.is_infinity ())
607     {
608       if (r > Rational (0))
609         return scm_inf ();
610
611       return scm_difference (scm_inf (), SCM_UNDEFINED);
612     }
613
614   return scm_divide (scm_from_int64 (r.numerator ()),
615                      scm_from_int64 (r.denominator ()));
616 }
617
618 Rational
619 ly_scm2rational (SCM r)
620 {
621   if (scm_is_true (scm_inf_p (r)))
622     {
623       if (scm_is_true (scm_positive_p (r)))
624         {
625           Rational r;
626           r.set_infinite (1);
627           return r;
628         }
629       else
630         {
631           Rational r;
632           r.set_infinite (-1);
633           return r;
634         }
635     }
636
637   return Rational (scm_to_int64 (scm_numerator (r)),
638                    scm_to_int64 (scm_denominator (r)));
639 }
640
641 Rational
642 robust_scm2rational (SCM n, Rational rat)
643 {
644   if (ly_is_rational (n))
645     return ly_scm2rational (n);
646   else
647     return rat;
648 }
649
650 bool
651 ly_is_rational (SCM n)
652 {
653   return (scm_is_real (n)
654           && (scm_is_true (scm_exact_p (n))
655               || scm_is_true (scm_inf_p (n))));
656 }
657
658 SCM
659 alist_to_hashq (SCM alist)
660 {
661   int i = scm_ilength (alist);
662   if (i < 0)
663     return scm_c_make_hash_table (0);
664
665   SCM tab = scm_c_make_hash_table (i);
666   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
667     {
668       SCM pt = scm_cdar (s);
669       scm_hashq_set_x (tab, scm_caar (s), pt);
670     }
671   return tab;
672 }
673
674 SCM
675 ly_hash2alist (SCM tab)
676 {
677   return Lily::hash_table_to_alist (tab);
678 }
679
680 /*
681   C++ interfacing.
682  */
683
684 string
685 mangle_cxx_identifier (string cxx_id)
686 {
687   if (cxx_id.substr (0, 3) == "ly_")
688     cxx_id = cxx_id.replace (0, 3, "ly:");
689   else
690     {
691       cxx_id = String_convert::to_lower (cxx_id);
692       cxx_id = "ly:" + cxx_id;
693     }
694   if (cxx_id.substr (cxx_id.length () - 2) == "_p")
695     cxx_id = cxx_id.replace (cxx_id.length () - 2, 2, "?");
696   else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
697     cxx_id = cxx_id.replace (cxx_id.length () - 2, 2, "!");
698
699   replace_all (&cxx_id, "_less?", "<?");
700   replace_all (&cxx_id, "_2_", "->");
701   replace_all (&cxx_id, "__", "::");
702   replace_all (&cxx_id, '_', '-');
703
704   return cxx_id;
705 }
706
707 SCM
708 ly_string_array_to_scm (vector<string> a)
709 {
710   SCM s = SCM_EOL;
711   for (vsize i = a.size (); i; i--)
712     s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
713   return s;
714 }
715
716 /* SYMBOLS is a whitespace separated list.  */
717 SCM
718 parse_symbol_list (char const *symbols)
719 {
720   while (isspace (*symbols))
721     symbols++;
722   string s = symbols;
723   replace_all (&s, '\n', ' ');
724   replace_all (&s, '\t', ' ');
725   replace_all (&s, "  ", " ");
726   return ly_string_array_to_scm (string_split (s, ' '));
727 }
728
729 /* GDB debugging. */
730 struct ly_t_double_cell
731 {
732   SCM a;
733   SCM b;
734   SCM c;
735   SCM d;
736 };
737
738 /* inserts at front, removing duplicates */
739 SCM ly_assoc_prepend_x (SCM alist, SCM key, SCM val)
740 {
741   return scm_acons (key, val, scm_assoc_remove_x (alist, key));
742 }
743