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