]> git.donarmstrong.com Git - lilypond.git/blob - lily/general-scheme.cc
b9fcf2540c594b1b9c26ddeb6a0dba9c43692288
[lilypond.git] / lily / general-scheme.cc
1 /*
2   lily-guile.cc -- implement assorted Guile bindings
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7                  Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9
10 #include "config.hh"
11
12 #include <math.h>   /* isinf */
13 #include <stdio.h>
14 #include <libintl.h>            // gettext on macos x
15
16 #include "libc-extension.hh"
17 #include "lily-guile.hh"
18 #include "string.hh"
19 #include "misc.hh"
20 #include "warn.hh"
21 #include "version.hh"
22 #include "dimensions.hh"
23 #include "main.hh"
24
25 /* MacOS S fix:
26    source-file.hh includes cmath which undefines isinf and isnan
27 */
28 #ifdef __APPLE__
29 inline int my_isinf (Real r) { return isinf (r); }
30 inline int my_isnan (Real r) { return isnan (r); }
31 #endif
32
33
34 /*
35   Ugh. Gulped file is copied twice. (maybe thrice if you count stdio
36   buffering.)
37  */
38 LY_DEFINE (ly_gulp_file, "ly:gulp-file",
39            1, 0, 0, (SCM name),
40            "Read the file @var{name}, and return its contents in a string.  "
41            "The file is looked up using the search path.")
42 {
43   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG1, __FUNCTION__, "string");
44   String contents = gulp_file_to_string (ly_scm2string (name), true);
45   return scm_from_locale_stringn (contents.get_str0 (), contents.length ());
46 }
47
48 LY_DEFINE (ly_warn, "ly:warn",
49            1, 0, 1, (SCM str, SCM rest),
50            "Scheme callable function to issue the warning @code{msg}. "
51            "The message is formatted with @code{format} and @code{rest}.")
52 {
53   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
54   progress_indication ("\n");
55
56   str = scm_simple_format (SCM_BOOL_F, str, rest);
57   warning (ly_scm2string (str));
58   return SCM_UNSPECIFIED;
59 }
60
61 LY_DEFINE (ly_programming_error, "ly:programming-error",
62            1, 0, 1, (SCM str, SCM rest),
63            "Scheme callable function to issue the warning @code{msg}. "
64            "The message is formatted with @code{format} and @code{rest}.")
65 {
66   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
67   progress_indication ("\n");
68
69   str = scm_simple_format (SCM_BOOL_F, str, rest);
70   programming_error (ly_scm2string (str));
71   return SCM_UNSPECIFIED;
72 }
73
74 LY_DEFINE (ly_dir_p, "ly:dir?",
75            1, 0, 0, (SCM s),
76           "type predicate. A direction is @code{-1}, @code{0} or "
77            "@code{1}, where @code{-1} represents "
78           "left or down and @code{1} represents right or up.")
79 {
80   if (scm_is_number (s))
81     {
82       int i = scm_to_int (s);
83       return (i>= -1 && i <= 1)  ? SCM_BOOL_T : SCM_BOOL_F; 
84     }
85   return SCM_BOOL_F;
86 }
87
88 LY_DEFINE (ly_assoc_get, "ly:assoc-get",
89            2, 1, 0,
90            (SCM key, SCM alist, SCM default_value),
91            "Return value if KEY in ALIST, else DEFAULT-VALUE "
92            "(or #f if not specified).")
93 {
94   SCM handle = scm_assoc (key, alist);
95
96   if (default_value == SCM_UNDEFINED)
97     default_value = SCM_BOOL_F;
98   
99   if (scm_is_pair (handle))
100     return scm_cdr (handle);
101   else
102     return default_value;
103 }
104
105
106 LY_DEFINE (ly_number2string, "ly:number->string",
107            1, 0, 0, (SCM s),
108            "Convert @var{num} to a string without generating many decimals.")
109 {
110   SCM_ASSERT_TYPE (scm_is_number (s), s, SCM_ARG1, __FUNCTION__, "number");
111
112   char str[400];                        // ugh.
113
114   if (scm_exact_p (s) == SCM_BOOL_F)
115     {
116       Real r (scm_to_double (s));
117 #ifdef __APPLE__
118       if (my_isinf (r) || my_isnan (r))
119 #else
120       if (isinf (r) || isnan (r))
121 #endif
122         {
123           programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
124           r = 0.0;
125         }
126
127       snprintf (str, sizeof (str), "%08.4f", r);
128     }
129   else
130     snprintf (str, sizeof (str), "%d", scm_to_int (s));
131
132   return scm_makfrom0str (str);
133 }
134
135
136
137 LY_DEFINE (ly_version,  "ly:version", 0, 0, 0, (),
138           "Return the current lilypond version as a list, e.g. @code{(1 3 127 uu1)}. ")
139 {
140   char const* vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " "  PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
141   
142   return scm_c_eval_string ((char*)vs);
143 }
144
145 LY_DEFINE (ly_unit,  "ly:unit", 0, 0, 0, (),
146           "Return the unit used for lengths as a string.")
147 {
148   return scm_makfrom0str (INTERNAL_UNIT);
149 }
150
151
152
153 LY_DEFINE (ly_dimension_p,  "ly:dimension?", 1, 0, 0, (SCM d),
154           "Return @var{d} is a number. Used to distinguish length "
155           "variables from normal numbers.")
156 {
157   return scm_number_p (d);
158 }
159
160 /*
161   Debugging mem leaks:
162  */
163 LY_DEFINE (ly_protects, "ly:protects",
164            0, 0, 0, (),
165           "Return hash of protected objects.")
166 {
167   return scm_protects;
168 }
169
170 LY_DEFINE (ly_gettext, "ly:gettext",
171            1, 0, 0, (SCM string),
172            "Gettext wrapper.")
173 {
174   SCM_ASSERT_TYPE (scm_is_string (string), string, SCM_ARG1,
175                    __FUNCTION__, "string");
176   return scm_makfrom0str (gettext (scm_i_string_chars (string)));
177 }
178
179
180
181
182 LY_DEFINE (ly_output_backend, "ly:output-backend",
183            0, 0, 0, (),
184            "Return name of output backend.")
185 {
186   return scm_makfrom0str (output_backend_global.to_str0 ());
187 }
188
189
190 LY_DEFINE (ly_output_formats, "ly:output-formats",
191            0, 0, 0, (),
192            "Formats passed to --format as a list of strings, "
193            "used for the output.")
194 {
195   Array<String> output_formats = split_string (output_format_global, ',');
196
197   SCM lst = SCM_EOL;
198   int output_formats_count = output_formats.size ();
199   for (int i = 0; i < output_formats_count; i ++)
200     lst = scm_cons (scm_makfrom0str (output_formats[i].to_str0 ()), lst);
201
202   return lst;
203 }
204
205 LY_DEFINE(ly_wchar_to_utf_8, "ly:wide-char->utf-8",
206           1, 0, 0, (SCM wc),
207           "Encode the Unicode codepoint @var{wc} as UTF-8")
208 {
209   char buf[100];
210
211   SCM_ASSERT_TYPE(scm_is_integer (wc), wc, SCM_ARG1, __FUNCTION__, "integer");
212   wchar_t wide_char = (wchar_t) scm_to_int (wc);
213
214   mbstate_t state;
215   memset (&state, '\0', sizeof (state));
216   memset (buf, '\0', sizeof (buf));
217
218   wcrtomb (buf, wide_char, &state);
219   
220   return scm_makfrom0str (buf);
221 }
222