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