Initial import to Gerrit.
[profile/ivi/festival.git] / src / modules / base / module_support.cc
1  /************************************************************************/
2  /*                                                                      */
3  /*                Centre for Speech Technology Research                 */
4  /*                     University of Edinburgh, UK                      */
5  /*                       Copyright (c) 1996,1997                        */
6  /*                        All Rights Reserved.                          */
7  /*                                                                      */
8  /*  Permission is hereby granted, free of charge, to use and distribute */
9  /*  this software and its documentation without restriction, including  */
10  /*  without limitation the rights to use, copy, modify, merge, publish, */
11  /*  distribute, sublicense, and/or sell copies of this work, and to     */
12  /*  permit persons to whom this work is furnished to do so, subject to  */
13  /*  the following conditions:                                           */
14  /*   1. The code must retain the above copyright notice, this list of   */
15  /*      conditions and the following disclaimer.                        */
16  /*   2. Any modifications must be clearly marked as such.               */
17  /*   3. Original authors' names are not deleted.                        */
18  /*   4. The authors' names are not used to endorse or promote products  */
19  /*      derived from this software without specific prior written       */
20  /*      permission.                                                     */
21  /*                                                                      */
22  /*  THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK       */
23  /*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING     */
24  /*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT  */
25  /*  SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE    */
26  /*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES   */
27  /*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN  */
28  /*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,         */
29  /*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF      */
30  /*  THIS SOFTWARE.                                                      */
31  /*                                                                      */
32  /*************************************************************************/
33  /*                                                                       */
34  /*                 Author: Richard Caley (rjc@cstr.ed.ac.uk)             */
35  /*                   Date: Tue Jul 29 1997                               */
36  /* --------------------------------------------------------------------  */
37  /* Some things useful in modules.                                        */
38  /*                                                                       */
39  /*************************************************************************/
40
41 #include "module_support.h"
42
43 #define CAR6(x) CAR(CDR5(x))
44 #define CDR6(x) CDR(CDR5(x))
45 #define CAR7(x) CAR(CDR6(x))
46 #define CDR7(x) CDR(CDR6(x))
47
48 #define CDR_to1(X) ((X!=NIL)&&CDR1(X))
49 #define CDR_to2(X) (CDR_to1(X)&&CDR2(X))
50 #define CDR_to3(X) (CDR_to2(X)&&CDR3(X))
51 #define CDR_to4(X) (CDR_to3(X)&&CDR4(X))
52 #define CDR_to5(X) (CDR_to4(X)&&CDR5(X))
53 #define CDR_to6(X) (CDR_to5(X)&&CDR6(X))
54 #define CDR_to7(X) (CDR_to6(X)&&CDR7(X))
55
56
57 void unpack_multiple_args(LISP args, LISP &v1, LISP &v2, LISP &v3, LISP &v4)
58 {
59   if (args)
60     {
61       v1 = CAR1(args);
62       if (CDR1(args))
63         {
64           v2 = CAR2(args);
65           if (CDR2(args))
66             {
67               v3 = CAR3(args);
68               if (CDR3(args))
69                 v4 = CAR4(args);
70             }
71         }
72     }
73 }
74
75 void unpack_multiple_args(LISP args, LISP &v1, LISP &v2, LISP &v3, LISP &v4, LISP &v5)
76 {
77   unpack_multiple_args(args, v1, v2, v3, v4);
78
79   if (CDR4(args))
80     v5 = CAR5(args);
81 }
82
83 void unpack_relation_arg(EST_Utterance *utt, 
84                          LISP lrel_name, 
85                          EST_String &relation_name,
86                          EST_Relation *&relation, 
87                          RelArgType type)
88 {
89     if (lrel_name)
90         relation_name = get_c_string(lrel_name);
91
92     if(utt->relation(relation_name))
93         relation = utt->relation(relation_name);
94
95     if (type==sat_existing)
96     {
97         if(!relation)
98             err("no relation", relation_name);  
99     }
100     else if (type==sat_new || type==sat_replace)
101     {
102         if (relation)
103             if (type==sat_new)
104                 err("relation exists", relation_name);
105         utt->create_relation(relation_name);
106
107         relation = &(*(utt->relation(relation_name)));
108     }
109 }
110
111 void unpack_module_args(LISP args, EST_Utterance *&utt)
112 {
113   if (args)
114     {
115       LISP lutt = CAR1(args);
116
117       utt = get_c_utt(lutt);
118       return;
119     }
120   err("no utterance given", NIL);
121 }
122
123
124 void unpack_module_args(LISP args, 
125                         EST_Utterance *&utt,
126                         EST_String &relation1_name, EST_Relation *&relation1, RelArgType type1)
127 {
128   unpack_module_args(args, utt);
129
130   unpack_relation_arg(utt, CDR_to1(args)?CAR2(args):NIL, relation1_name, relation1, type1);
131 }
132
133 void unpack_module_args(LISP args, 
134                         EST_Utterance *&utt,
135                         EST_String &relation1_name, EST_Relation *&relation1, RelArgType type1,
136                         EST_String &relation2_name, EST_Relation *&relation2, RelArgType type2
137                         )
138 {
139   unpack_module_args(args, utt);
140
141   unpack_relation_arg(utt, CDR_to1(args)?CAR2(args):NIL, relation1_name, relation1, type1);
142   unpack_relation_arg(utt, CDR_to2(args)?CAR3(args):NIL, relation2_name, relation2, type2);
143 }
144
145 void unpack_module_args(LISP args, 
146                         EST_Utterance *&utt,
147                         EST_String &relation1_name, EST_Relation *&relation1, RelArgType type1,
148                         EST_String &relation2_name, EST_Relation *&relation2, RelArgType type2,
149                         EST_String &relation3_name, EST_Relation *&relation3, RelArgType type3
150                         )
151 {
152   unpack_module_args(args, utt);
153
154   unpack_relation_arg(utt, CDR_to1(args)?CAR2(args):NIL, relation1_name, relation1, type1);
155   unpack_relation_arg(utt, CDR_to2(args)?CAR3(args):NIL, relation2_name, relation2, type2);
156   unpack_relation_arg(utt, CDR_to3(args)?CAR4(args):NIL, relation3_name, relation3, type3);
157 }
158
159 void unpack_module_args(LISP args, 
160                         EST_Utterance *&utt,
161                         EST_String &relation1_name, EST_Relation *&relation1, RelArgType type1,
162                         EST_String &relation2_name, EST_Relation *&relation2, RelArgType type2,
163                         EST_String &relation3_name, EST_Relation *&relation3, RelArgType type3,
164                         EST_String &relation4_name, EST_Relation *&relation4, RelArgType type4
165                         )
166 {
167   unpack_module_args(args, utt);
168
169   unpack_relation_arg(utt, CDR_to1(args)?CAR2(args):NIL, relation1_name, relation1, type1);
170   unpack_relation_arg(utt, CDR_to2(args)?CAR3(args):NIL, relation2_name, relation2, type2);
171   unpack_relation_arg(utt, CDR_to3(args)?CAR4(args):NIL, relation3_name, relation3, type3);
172   unpack_relation_arg(utt, CDR_to4(args)?CAR5(args):NIL, relation4_name, relation4, type4);
173 }
174
175 void unpack_module_args(LISP args, 
176                         EST_Utterance *&utt,
177                         EST_String &relation1_name, EST_Relation *&relation1, RelArgType type1,
178                         EST_String &relation2_name, EST_Relation *&relation2, RelArgType type2,
179                         EST_String &relation3_name, EST_Relation *&relation3, RelArgType type3,
180                         EST_String &relation4_name, EST_Relation *&relation4, RelArgType type4,
181                         EST_String &relation5_name, EST_Relation *&relation5, RelArgType type5
182                         )
183 {
184   unpack_module_args(args, utt);
185
186   unpack_relation_arg(utt, CDR_to1(args)?CAR2(args):NIL, relation1_name, relation1, type1);
187   unpack_relation_arg(utt, CDR_to2(args)?CAR3(args):NIL, relation2_name, relation2, type2);
188   unpack_relation_arg(utt, CDR_to3(args)?CAR4(args):NIL, relation3_name, relation3, type3);
189   unpack_relation_arg(utt, CDR_to4(args)?CAR5(args):NIL, relation4_name, relation4, type4);
190   unpack_relation_arg(utt, CDR_to5(args)?CAR6(args):NIL, relation5_name, relation5, type5);
191 }
192