]> git.donarmstrong.com Git - lilypond.git/blob - guile18/libguile/keywords.c
Import guile-1.8 as multiple upstream tarball component
[lilypond.git] / guile18 / libguile / keywords.c
1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2003, 2004, 2006, 2008 Free Software Foundation, Inc.
2  * 
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17
18
19 \f
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <string.h>
25
26 #include "libguile/_scm.h"
27 #include "libguile/async.h"
28 #include "libguile/ports.h"
29 #include "libguile/root.h"
30 #include "libguile/smob.h"
31 #include "libguile/hashtab.h"
32
33 #include "libguile/validate.h"
34 #include "libguile/keywords.h"
35 #include "libguile/strings.h"
36
37 \f
38
39 scm_t_bits scm_tc16_keyword;
40
41 #define KEYWORDP(X)     (SCM_SMOB_PREDICATE (scm_tc16_keyword, (X)))
42 #define KEYWORDSYM(X)   (SCM_SMOB_OBJECT (X))
43
44 static int
45 keyword_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED)
46 {
47   scm_puts ("#:", port);
48   scm_display (KEYWORDSYM (exp), port);
49   return 1;
50 }
51
52 SCM_DEFINE (scm_keyword_p, "keyword?", 1, 0, 0, 
53             (SCM obj),
54             "Return @code{#t} if the argument @var{obj} is a keyword, else\n"
55             "@code{#f}.")
56 #define FUNC_NAME s_scm_keyword_p
57 {
58   return scm_from_bool (KEYWORDP (obj));
59 }
60 #undef FUNC_NAME
61
62 SCM_DEFINE (scm_symbol_to_keyword, "symbol->keyword", 1, 0, 0,
63             (SCM symbol),
64             "Return the keyword with the same name as @var{symbol}.")
65 #define FUNC_NAME s_scm_symbol_to_keyword
66 {
67   SCM keyword;
68
69   SCM_ASSERT_TYPE (scm_is_symbol (symbol), symbol, 0, NULL, "symbol");
70
71   SCM_CRITICAL_SECTION_START;
72   /* njrev: NEWSMOB and hashq_set_x can raise errors */
73   keyword = scm_hashq_ref (scm_keyword_obarray, symbol, SCM_BOOL_F);
74   if (scm_is_false (keyword))
75     {
76       SCM_NEWSMOB (keyword, scm_tc16_keyword, SCM_UNPACK (symbol));
77       scm_hashq_set_x (scm_keyword_obarray, symbol, keyword);
78     }
79   SCM_CRITICAL_SECTION_END;
80   return keyword;
81 }
82 #undef FUNC_NAME
83
84 SCM_DEFINE (scm_keyword_to_symbol, "keyword->symbol", 1, 0, 0,
85             (SCM keyword),
86             "Return the symbol with the same name as @var{keyword}.")
87 #define FUNC_NAME s_scm_keyword_to_symbol
88 {
89   scm_assert_smob_type (scm_tc16_keyword, keyword);
90   return KEYWORDSYM (keyword);
91 }
92 #undef FUNC_NAME
93
94 int
95 scm_is_keyword (SCM val)
96 {
97   return KEYWORDP (val);
98 }
99
100 SCM
101 scm_from_locale_keyword (const char *str)
102 {
103   return scm_symbol_to_keyword (scm_from_locale_symbol (str));
104 }
105
106 SCM
107 scm_from_locale_keywordn (const char *str, size_t len)
108 {
109   return scm_symbol_to_keyword (scm_from_locale_symboln (str, len));
110 }
111
112 /* njrev: critical sections reviewed so far up to here */
113 void
114 scm_init_keywords ()
115 {
116   scm_tc16_keyword = scm_make_smob_type ("keyword", 0);
117   scm_set_smob_mark (scm_tc16_keyword, scm_markcdr);
118   scm_set_smob_print (scm_tc16_keyword, keyword_print);
119
120   scm_keyword_obarray = scm_c_make_hash_table (0);
121 #include "libguile/keywords.x"
122 }
123
124
125 /*
126   Local Variables:
127   c-file-style: "gnu"
128   End:
129 */