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