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