]> git.donarmstrong.com Git - lilypond.git/blob - lily/input-smob.cc
4d446ab6c014ff5ea320d3d03d91e6420ba2a1d3
[lilypond.git] / lily / input-smob.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "input.hh"
21 #include "source-file.hh"
22 #include "std-string.hh"
23
24 #include "ly-smobs.icc"
25
26 /* Dummy input location for use if real one is missing.  */
27 Input dummy_input_global;
28
29 static long input_tag;
30
31 static SCM
32 mark_smob (SCM s)
33 {
34   Input *sc = (Input *) SCM_CELL_WORD_1 (s);
35
36   if (Source_file *sf = sc->get_source_file ())
37     return sf->self_scm ();
38
39   return SCM_EOL;
40 }
41
42 static int
43 print_smob (SCM s, SCM port, scm_print_state *)
44 {
45   string str = "#<location " + unsmob_input (s)->location_string () + ">";
46   scm_puts (str.c_str (), port);
47   return 1;
48 }
49
50 static size_t
51 free_smob (SCM s)
52 {
53   delete unsmob_input (s);
54   return 0;
55 }
56
57 static SCM
58 equal_smob (SCM sa, SCM sb)
59 {
60   Input *a = (Input *) SCM_CELL_WORD_1 (sa);
61   Input *b = (Input *) SCM_CELL_WORD_1 (sb);
62   if (a->get_source_file () == b->get_source_file ()
63       && a->start () == b->start ()
64       && a->end () == b->end ())
65     return SCM_BOOL_T;
66   else
67     return SCM_BOOL_F;
68 }
69
70 static void
71 start_input_smobs ()
72 {
73   input_tag = scm_make_smob_type ("input", 0);
74   scm_set_smob_mark (input_tag, mark_smob);
75   scm_set_smob_free (input_tag, free_smob);
76   scm_set_smob_print (input_tag, print_smob);
77   scm_set_smob_equalp (input_tag, equal_smob);
78 }
79
80 SCM
81 make_input (Input ip)
82 {
83   Input *nip = new Input (ip);
84   SCM z;
85
86   SCM_NEWSMOB (z, input_tag, nip);
87   return z;
88 }
89
90 Input *
91 unsmob_input (SCM s)
92 {
93   if (SCM_IMP (s))
94     return 0;
95   if (SCM_CAR (s) == (SCM)input_tag) // ugh.
96     return (Input *) SCM_CDR (s);
97   else
98     return 0;
99 }
100
101 ADD_SCM_INIT_FUNC (input, start_input_smobs);
102