1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2015 Ericsson Telecom AB
3 // All rights reserved. This program and the accompanying materials
4 // are made available under the terms of the Eclipse Public License v1.0
5 // which accompanies this distribution, and is available at
6 // http://www.eclipse.org/legal/epl-v10.html
7 ///////////////////////////////////////////////////////////////////////////////
11 #include "../common/memory.h"
12 #include "Parameters.h"
13 #include "Param_Types.hh"
20 #include "Charstring.hh"
21 #include "XmlReader.hh"
23 static const Token_Match
boolean_true_match("^(true).*$",TRUE
);
24 static const Token_Match
boolean_false_match("^(false).*$",TRUE
);
31 BOOLEAN::BOOLEAN(boolean other_value
)
34 boolean_value
= other_value
;
37 BOOLEAN::BOOLEAN(const BOOLEAN
& other_value
)
38 : Base_Type(other_value
)
40 other_value
.must_bound("Copying an unbound boolean value.");
42 boolean_value
= other_value
.boolean_value
;
45 BOOLEAN
& BOOLEAN::operator=(boolean other_value
)
48 boolean_value
= other_value
;
52 BOOLEAN
& BOOLEAN::operator=(const BOOLEAN
& other_value
)
54 other_value
.must_bound("Assignment of an unbound boolean value.");
56 boolean_value
= other_value
.boolean_value
;
60 boolean
BOOLEAN::operator!() const
62 must_bound("The operand of not operator is an unbound boolean value.");
63 return !boolean_value
;
66 boolean
BOOLEAN::operator&&(boolean other_value
) const
68 must_bound("The left operand of and operator is an unbound boolean value.");
69 return boolean_value
&& other_value
;
72 boolean
BOOLEAN::operator&&(const BOOLEAN
& other_value
) const
74 must_bound("The left operand of and operator is an unbound boolean value.");
75 if (!boolean_value
) return FALSE
;
76 other_value
.must_bound("The right operand of and operator is an unbound "
78 return other_value
.boolean_value
;
81 boolean
BOOLEAN::operator^(boolean other_value
) const
83 must_bound("The left operand of xor operator is an unbound boolean value.");
84 return boolean_value
!= other_value
;
87 boolean
BOOLEAN::operator^(const BOOLEAN
& other_value
) const
89 must_bound("The left operand of xor operator is an unbound boolean value.");
90 other_value
.must_bound("The right operand of xor operator is an unbound "
92 return boolean_value
!= other_value
.boolean_value
;
95 boolean
BOOLEAN::operator||(boolean other_value
) const
97 must_bound("The left operand of or operator is an unbound boolean value.");
98 return boolean_value
|| other_value
;
101 boolean
BOOLEAN::operator||(const BOOLEAN
& other_value
) const
103 must_bound("The left operand of or operator is an unbound boolean value.");
104 if (boolean_value
) return TRUE
;
105 other_value
.must_bound("The right operand of or operator is an unbound "
107 return other_value
.boolean_value
;
110 boolean
BOOLEAN::operator==(boolean other_value
) const
112 must_bound("The left operand of comparison is an unbound boolean value.");
113 return boolean_value
== other_value
;
116 boolean
BOOLEAN::operator==(const BOOLEAN
& other_value
) const
118 must_bound("The left operand of comparison is an unbound boolean value.");
119 other_value
.must_bound("The right operand of comparison is an unbound "
121 return boolean_value
== other_value
.boolean_value
;
124 BOOLEAN::operator boolean() const
126 must_bound("Using the value of an unbound boolean variable.");
127 return boolean_value
;
130 void BOOLEAN::clean_up()
135 void BOOLEAN::log() const
137 if (bound_flag
) TTCN_Logger::log_event_str(boolean_value
? "true" : "false");
138 else TTCN_Logger::log_event_unbound();
141 void BOOLEAN::encode_text(Text_Buf
& text_buf
) const
143 must_bound("Text encoder: Encoding an unbound boolean value.");
144 text_buf
.push_int(boolean_value
? 1 : 0);
147 void BOOLEAN::decode_text(Text_Buf
& text_buf
)
149 int int_value
= text_buf
.pull_int().get_val();
152 boolean_value
= FALSE
;
155 boolean_value
= TRUE
;
158 TTCN_error("Text decoder: An invalid boolean value (%d) was received.",
164 void BOOLEAN::set_param(Module_Param
& param
) {
165 param
.basic_check(Module_Param::BC_VALUE
, "boolean value");
166 Module_Param_Ptr mp
= ¶m
;
167 if (param
.get_type() == Module_Param::MP_Reference
) {
168 mp
= param
.get_referenced_param();
170 if (mp
->get_type()!=Module_Param::MP_Boolean
) param
.type_error("boolean value");
172 boolean_value
= mp
->get_boolean();
175 Module_Param
* BOOLEAN::get_param(Module_Param_Name
& /* param_name */) const
178 return new Module_Param_Unbound();
180 return new Module_Param_Boolean(boolean_value
);
183 void BOOLEAN::encode(const TTCN_Typedescriptor_t
& p_td
, TTCN_Buffer
& p_buf
,
184 TTCN_EncDec::coding_t p_coding
, ...) const
187 va_start(pvar
, p_coding
);
189 case TTCN_EncDec::CT_BER
: {
190 TTCN_EncDec_ErrorContext
ec("While BER-encoding type '%s': ", p_td
.name
);
191 unsigned BER_coding
=va_arg(pvar
, unsigned);
192 BER_encode_chk_coding(BER_coding
);
193 ASN_BER_TLV_t
*tlv
=BER_encode_TLV(p_td
, BER_coding
);
194 tlv
->put_in_buffer(p_buf
);
195 ASN_BER_TLV_t::destruct(tlv
);
197 case TTCN_EncDec::CT_RAW
: {
198 TTCN_EncDec_ErrorContext
ec("While RAW-encoding type '%s': ", p_td
.name
);
200 TTCN_EncDec_ErrorContext::error_internal
201 ("No RAW descriptor available for type '%s'.", p_td
.name
);
205 RAW_enc_tree
root(TRUE
,NULL
,&rp
,1,p_td
.raw
);
206 RAW_encode(p_td
, root
);
207 root
.put_to_buf(p_buf
);
209 case TTCN_EncDec::CT_TEXT
: {
210 TTCN_EncDec_ErrorContext
ec("While TEXT-encoding type '%s': ", p_td
.name
);
212 TTCN_EncDec_ErrorContext::error_internal
213 ("No TEXT descriptor available for type '%s'.", p_td
.name
);
214 TEXT_encode(p_td
,p_buf
);
216 case TTCN_EncDec::CT_XER
: {
217 TTCN_EncDec_ErrorContext
ec("While XER-encoding type '%s': ", p_td
.name
);
218 unsigned XER_coding
=va_arg(pvar
, unsigned);
219 XER_encode(*p_td
.xer
, p_buf
, XER_coding
, 0, 0);
221 case TTCN_EncDec::CT_JSON
: {
222 TTCN_EncDec_ErrorContext
ec("While JSON-encoding type '%s': ", p_td
.name
);
224 TTCN_EncDec_ErrorContext::error_internal
225 ("No JSON descriptor available for type '%s'.", p_td
.name
);
226 JSON_Tokenizer
tok(va_arg(pvar
, int) != 0);
227 JSON_encode(p_td
, tok
);
228 p_buf
.put_s(tok
.get_buffer_length(), (const unsigned char*)tok
.get_buffer());
231 TTCN_error("Unknown coding method requested to encode type '%s'",
237 void BOOLEAN::decode(const TTCN_Typedescriptor_t
& p_td
, TTCN_Buffer
& p_buf
,
238 TTCN_EncDec::coding_t p_coding
, ...)
241 va_start(pvar
, p_coding
);
243 case TTCN_EncDec::CT_BER
: {
244 TTCN_EncDec_ErrorContext
ec("While BER-decoding type '%s': ", p_td
.name
);
245 unsigned L_form
=va_arg(pvar
, unsigned);
247 BER_decode_str2TLV(p_buf
, tlv
, L_form
);
248 BER_decode_TLV(p_td
, tlv
, L_form
);
249 if(tlv
.isComplete
) p_buf
.increase_pos(tlv
.get_len());
251 case TTCN_EncDec::CT_RAW
: {
252 TTCN_EncDec_ErrorContext
ec("While RAW-decoding type '%s': ", p_td
.name
);
254 TTCN_EncDec_ErrorContext::error_internal
255 ("No RAW descriptor available for type '%s'.", p_td
.name
);
257 switch(p_td
.raw
->top_bit_order
){
265 if(RAW_decode(p_td
, p_buf
, p_buf
.get_len()*8, order
)<0)
266 ec
.error(TTCN_EncDec::ET_INCOMPL_MSG
,
267 "Can not decode type '%s', because invalid or incomplete"
268 " message was received"
271 case TTCN_EncDec::CT_TEXT
: {
272 Limit_Token_List limit
;
273 TTCN_EncDec_ErrorContext
ec("While TEXT-decoding type '%s': ", p_td
.name
);
275 TTCN_EncDec_ErrorContext::error_internal
276 ("No TEXT descriptor available for type '%s'.", p_td
.name
);
277 const unsigned char *b
=p_buf
.get_data();
278 if(b
[p_buf
.get_len()-1]!='\0'){
279 p_buf
.set_pos(p_buf
.get_len());
280 p_buf
.put_zero(8,ORDER_LSB
);
283 if(TEXT_decode(p_td
,p_buf
,limit
)<0)
284 ec
.error(TTCN_EncDec::ET_INCOMPL_MSG
,
285 "Can not decode type '%s', because invalid or incomplete"
286 " message was received"
289 case TTCN_EncDec::CT_XER
: {
290 TTCN_EncDec_ErrorContext
ec("While XER-decoding type '%s': ", p_td
.name
);
291 unsigned XER_coding
=va_arg(pvar
, unsigned);
292 XmlReaderWrap
reader(p_buf
);
293 for (int success
= reader
.Read(); success
==1; success
=reader
.Read()) {
294 int type
= reader
.NodeType();
295 if (type
==XML_READER_TYPE_ELEMENT
)
298 XER_decode(*p_td
.xer
, reader
, XER_coding
, 0);
299 size_t bytes
= reader
.ByteConsumed();
300 p_buf
.set_pos(bytes
);
302 case TTCN_EncDec::CT_JSON
: {
303 TTCN_EncDec_ErrorContext
ec("While JSON-decoding type '%s': ", p_td
.name
);
305 TTCN_EncDec_ErrorContext::error_internal
306 ("No JSON descriptor available for type '%s'.", p_td
.name
);
307 JSON_Tokenizer
tok((const char*)p_buf
.get_data(), p_buf
.get_len());
308 if(JSON_decode(p_td
, tok
, false)<0)
309 ec
.error(TTCN_EncDec::ET_INCOMPL_MSG
,
310 "Can not decode type '%s', because invalid or incomplete"
311 " message was received"
313 p_buf
.set_pos(tok
.get_buf_pos());
316 TTCN_error("Unknown coding method requested to decode type '%s'",
323 BOOLEAN::BER_encode_TLV(const TTCN_Typedescriptor_t
& p_td
,
324 unsigned p_coding
) const
327 ASN_BER_TLV_t
*new_tlv
=BER_encode_chk_bound(is_bound());
329 new_tlv
=ASN_BER_TLV_t::construct(1, NULL
);
330 new_tlv
->V
.str
.Vstr
[0]=boolean_value
==TRUE
?0xFF:0x00;
332 new_tlv
=ASN_BER_V2TLV(new_tlv
, p_td
, p_coding
);
336 boolean
BOOLEAN::BER_decode_TLV(const TTCN_Typedescriptor_t
& p_td
,
337 const ASN_BER_TLV_t
& p_tlv
,
342 ASN_BER_TLV_t stripped_tlv
;
343 BER_decode_strip_tags(*p_td
.ber
, p_tlv
, L_form
, stripped_tlv
);
344 TTCN_EncDec_ErrorContext
ec("While decoding BOOLEAN type: ");
345 stripped_tlv
.chk_constructed_flag(FALSE
);
346 if (!stripped_tlv
.isComplete
) return FALSE
;
347 if(stripped_tlv
.V
.str
.Vlen
!=1)
348 ec
.error(TTCN_EncDec::ET_INVAL_MSG
,
349 "Length of V-part is %lu (instead of 1).",
350 (unsigned long) stripped_tlv
.V
.str
.Vlen
);
351 if(stripped_tlv
.V
.str
.Vlen
>=1) {
352 switch(stripped_tlv
.V
.str
.Vstr
[0]) {
367 int BOOLEAN::TEXT_decode(const TTCN_Typedescriptor_t
& p_td
, TTCN_Buffer
& buff
,
368 Limit_Token_List
&, boolean no_err
, boolean
/*first_call*/)
371 int decoded_length
= 0;
373 if (p_td
.text
->begin_decode
) {
375 if ((tl
= p_td
.text
->begin_decode
->match_begin(buff
)) < 0) {
376 if (no_err
) return -1;
377 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_TOKEN_ERR
,
378 "The specified token '%s' not found for '%s': ",
379 (const char*) *(p_td
.text
->begin_decode
), p_td
.name
);
382 decoded_length
+= tl
;
383 buff
.increase_pos(tl
);
385 if (buff
.get_read_len() < 1 && no_err
) return -TTCN_EncDec::ET_LEN_ERR
;
387 boolean found
= FALSE
;
389 if ( p_td
.text
->val
.bool_values
390 && p_td
.text
->val
.bool_values
->true_decode_token
) {
392 if ((tl
= p_td
.text
->val
.bool_values
->true_decode_token
->match_begin(buff
)) > -1) {
395 boolean_value
= TRUE
;
400 if ((tl
= boolean_true_match
.match_begin(buff
)) >= 0) {
403 boolean_value
= TRUE
;
408 if ( p_td
.text
->val
.bool_values
409 && p_td
.text
->val
.bool_values
->false_decode_token
) {
411 if ((tl
= p_td
.text
->val
.bool_values
->false_decode_token
->match_begin(buff
)) > -1) {
414 boolean_value
= FALSE
;
419 if ((tl
= boolean_false_match
.match_begin(buff
)) >= 0) {
422 boolean_value
= FALSE
;
428 decoded_length
+= str_len
;
429 buff
.increase_pos(str_len
);
432 if (no_err
) return -1;
433 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_TOKEN_ERR
,
434 "No boolean token found for '%s': ", p_td
.name
);
435 return decoded_length
;
438 if (p_td
.text
->end_decode
) {
440 if ((tl
= p_td
.text
->end_decode
->match_begin(buff
)) < 0) {
441 if (no_err
) return -1;
442 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_TOKEN_ERR
,
443 "The specified token '%s' not found for '%s': ",
444 (const char*) *(p_td
.text
->end_decode
), p_td
.name
);
447 decoded_length
+= tl
;
448 buff
.increase_pos(tl
);
451 return decoded_length
;
455 int BOOLEAN::TEXT_encode(const TTCN_Typedescriptor_t
& p_td
,
456 TTCN_Buffer
& buff
) const{
457 int encoded_length
=0;
458 if(p_td
.text
->begin_encode
){
459 buff
.put_cs(*p_td
.text
->begin_encode
);
460 encoded_length
+=p_td
.text
->begin_encode
->lengthof();
463 TTCN_EncDec_ErrorContext::error
464 (TTCN_EncDec::ET_UNBOUND
, "Encoding an unbound value.");
465 if(p_td
.text
->end_encode
){
466 buff
.put_cs(*p_td
.text
->end_encode
);
467 encoded_length
+=p_td
.text
->end_encode
->lengthof();
469 return encoded_length
;
472 if(p_td
.text
->val
.bool_values
==NULL
){
474 buff
.put_s(4,(const unsigned char*)"true");
478 buff
.put_s(5,(const unsigned char*)"false");
483 if(p_td
.text
->val
.bool_values
->true_encode_token
){
484 buff
.put_cs(*p_td
.text
->val
.bool_values
->true_encode_token
);
485 encoded_length
+=p_td
.text
->
486 val
.bool_values
->true_encode_token
->lengthof();
488 buff
.put_s(4,(const unsigned char*)"true");
493 if(p_td
.text
->val
.bool_values
->false_encode_token
){
494 buff
.put_cs(*p_td
.text
->val
.bool_values
->false_encode_token
);
495 encoded_length
+=p_td
.text
->
496 val
.bool_values
->false_encode_token
->lengthof();
498 buff
.put_s(5,(const unsigned char*)"false");
504 if(p_td
.text
->end_encode
){
505 buff
.put_cs(*p_td
.text
->end_encode
);
506 encoded_length
+=p_td
.text
->end_encode
->lengthof();
508 return encoded_length
;
511 int BOOLEAN::RAW_encode(const TTCN_Typedescriptor_t
& p_td
, RAW_enc_tree
& myleaf
) const
514 int loc_length
= p_td
.raw
->fieldlength
? p_td
.raw
->fieldlength
: 1;
515 int length
= (loc_length
+ 7) / 8;
518 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND
,
519 "Encoding an unbound value.");
522 else tmp
= boolean_value
? 0xFF : 0x00;
523 // myleaf.ext_bit=EXT_BIT_NO;
524 if (myleaf
.must_free
) Free(myleaf
.body
.leaf
.data_ptr
);
525 if (length
> RAW_INT_ENC_LENGTH
) {
526 myleaf
.body
.leaf
.data_ptr
= bc
= (unsigned char*)Malloc(length
*sizeof(*bc
));
527 myleaf
.must_free
= TRUE
;
528 myleaf
.data_ptr_used
= TRUE
;
530 else bc
= myleaf
.body
.leaf
.data_array
;
532 memset(bc
, tmp
, length
* sizeof(*bc
));
533 if (boolean_value
&& loc_length
% 8 != 0) {
534 // remove the extra ones from the last octet
535 bc
[length
- 1] &= BitMaskTable
[loc_length
% 8];
537 return myleaf
.length
= loc_length
;
540 int BOOLEAN::RAW_decode(const TTCN_Typedescriptor_t
& p_td
, TTCN_Buffer
& buff
,
541 int limit
, raw_order_t top_bit_ord
, boolean no_err
, int /*sel_field*/,
542 boolean
/*first_call*/)
545 int prepaddlength
= buff
.increase_pos_padd(p_td
.raw
->prepadding
);
546 limit
-= prepaddlength
;
547 int decode_length
= p_td
.raw
->fieldlength
> 0 ? p_td
.raw
->fieldlength
: 1;
548 if (decode_length
> limit
) {
549 if (no_err
) return -TTCN_EncDec::ET_LEN_ERR
;
550 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_LEN_ERR
,
551 "There is not enough bits in the buffer to decode type %s (needed: %d, "
552 "found: %d).", p_td
.name
, decode_length
, limit
);
553 decode_length
= limit
;
555 int nof_unread_bits
= buff
.unread_len_bit();
556 if (decode_length
> nof_unread_bits
) {
557 if (no_err
) return -TTCN_EncDec::ET_INCOMPL_MSG
;
558 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_INCOMPL_MSG
,
559 "There is not enough bits in the buffer to decode type %s (needed: %d, "
560 "found: %d).", p_td
.name
, decode_length
, nof_unread_bits
);
561 decode_length
= nof_unread_bits
;
563 if (decode_length
< 0) return -1;
564 else if (decode_length
== 0) boolean_value
= FALSE
;
567 boolean orders
= FALSE
;
568 if (p_td
.raw
->bitorderinoctet
== ORDER_MSB
) orders
= TRUE
;
569 if (p_td
.raw
->bitorderinfield
== ORDER_MSB
) orders
= !orders
;
570 cp
.bitorder
= orders
? ORDER_MSB
: ORDER_LSB
;
572 if (p_td
.raw
->byteorder
== ORDER_MSB
) orders
= TRUE
;
573 if (p_td
.raw
->bitorderinfield
== ORDER_MSB
) orders
= !orders
;
574 cp
.byteorder
= orders
? ORDER_MSB
: ORDER_LSB
;
575 cp
.fieldorder
= p_td
.raw
->fieldorder
;
576 cp
.hexorder
= ORDER_LSB
;
577 int length
= (decode_length
+ 7) / 8;
578 unsigned char *data
= (unsigned char*)Malloc(length
*sizeof(unsigned char));
579 buff
.get_b((size_t)decode_length
, data
, cp
, top_bit_ord
);
580 if(decode_length
% 8){
581 data
[length
- 1] &= BitMaskTable
[decode_length
% 8];
583 unsigned char ch
= '\0';
584 for (int a
= 0; a
< length
; a
++) ch
|= data
[a
];
586 boolean_value
= ch
!= '\0';
589 decode_length
+= buff
.increase_pos_padd(p_td
.raw
->padding
);
590 return decode_length
+ prepaddlength
;
593 int BOOLEAN::XER_encode(const XERdescriptor_t
& p_td
,
594 TTCN_Buffer
& p_buf
, unsigned int flavor
, int indent
, embed_values_enc_struct_t
*) const
597 TTCN_EncDec_ErrorContext::error
598 (TTCN_EncDec::ET_UNBOUND
, "Encoding an unbound boolean value.");
600 int encoded_length
=(int)p_buf
.get_len();
602 int exer
= is_exer(flavor
);
604 flavor
|= (SIMPLE_TYPE
| BXER_EMPTY_ELEM
);
605 if (begin_xml(p_td
, p_buf
, flavor
, indent
, false) == -1) --encoded_length
;
608 if (p_td
.xer_bits
& XER_TEXT
) {
609 p_buf
.put_c(boolean_value
? '1' : '0');
612 if (boolean_value
) p_buf
.put_s(4, (cbyte
*)"true");
613 else p_buf
.put_s(5, (cbyte
*)"false");
617 if (boolean_value
) p_buf
.put_s(7, (cbyte
*)"<true/>");
618 else p_buf
.put_s(8, (cbyte
*)"<false/>");
621 end_xml(p_td
, p_buf
, flavor
, indent
, false);
623 return (int)p_buf
.get_len() - encoded_length
;
626 int BOOLEAN::XER_decode(const XERdescriptor_t
& p_td
, XmlReaderWrap
& reader
,
627 unsigned int flavor
, embed_values_dec_struct_t
*)
629 const boolean exer
= is_exer(flavor
);
630 int XMLValueList
= !exer
&& is_record_of(flavor
);
631 const boolean notag
= (exer
&& (p_td
.xer_bits
& (UNTAGGED
))) ||
632 is_exerlist(flavor
) || XMLValueList
;
633 int depth
= -1, success
, type
;
634 const char *value
= 0;
636 if (exer
&& (p_td
.xer_bits
& XER_ATTRIBUTE
)) {
637 verify_name(reader
, p_td
, exer
);
638 value
= (const char *)reader
.Value();
641 for (success
= reader
.Ok(); success
== 1; success
= reader
.Read()) {
642 type
= reader
.NodeType();
643 if (!notag
&& depth
== -1) {
644 if (XML_READER_TYPE_ELEMENT
== type
) {
645 verify_name(reader
, p_td
, exer
);
646 depth
= reader
.Depth();
648 if (exer
&& (p_td
.dfeValue
!= 0) && reader
.IsEmptyElement()) {
649 *this = *static_cast<const BOOLEAN
*>(p_td
.dfeValue
);
656 else { // found the enclosing tag already
657 if (!exer
&& XML_READER_TYPE_ELEMENT
== type
) {
658 // this must be EmptyElement Boolean
659 if (!reader
.IsEmptyElement()) TTCN_EncDec_ErrorContext::error(
660 TTCN_EncDec::ET_INVAL_MSG
, "Boolean must be empty element");
661 value
= (const char*)reader
.LocalName();
663 else if (XML_READER_TYPE_TEXT
== type
) {
665 value
= (const char*)reader
.Value();
668 // Must not modify the buffer when attempting to find the selected alternative for USE-UNION
669 if (!exer
|| !(flavor
& EXIT_ON_ERROR
)) reader
.Read();
673 } // if not attribute
675 if (value
!= 0 && *value
!= 0) {
677 if (value
[1]=='\0' && (*value
& 0x3E) == '0')
680 boolean_value
= *value
== '1';
682 else if (!strcmp(value
, "true")) {
683 boolean_value
= true;
686 else if (!strcmp(value
, "false")) {
687 boolean_value
= false;
693 if (exer
&& (p_td
.xer_bits
& XER_ATTRIBUTE
)) { // I am an attribute
694 // Let the caller do reader.AdvanceAttribute();
697 for (success
= reader
.Ok(); success
== 1; success
= reader
.Read()) {
698 type
= reader
.NodeType();
699 if (XML_READER_TYPE_END_ELEMENT
== type
) {
700 verify_end(reader
, p_td
, depth
, exer
);
701 reader
.Read(); // one last time
710 int BOOLEAN::JSON_encode(const TTCN_Typedescriptor_t
&, JSON_Tokenizer
& p_tok
) const
713 TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND
,
714 "Encoding an unbound boolean value.");
717 return p_tok
.put_next_token((boolean_value
) ? JSON_TOKEN_LITERAL_TRUE
: JSON_TOKEN_LITERAL_FALSE
, NULL
);
720 int BOOLEAN::JSON_decode(const TTCN_Typedescriptor_t
& p_td
, JSON_Tokenizer
& p_tok
, boolean p_silent
)
722 json_token_t token
= JSON_TOKEN_NONE
;
724 if (p_td
.json
->default_value
&& 0 == p_tok
.get_buffer_length()) {
725 // No JSON data in the buffer -> use default value
726 if (strcmp(p_td
.json
->default_value
, "true") == 0) {
727 token
= JSON_TOKEN_LITERAL_TRUE
;
730 token
= JSON_TOKEN_LITERAL_FALSE
;
733 dec_len
= p_tok
.get_next_token(&token
, NULL
, NULL
);
735 if (JSON_TOKEN_ERROR
== token
) {
736 JSON_ERROR(TTCN_EncDec::ET_INVAL_MSG
, JSON_DEC_BAD_TOKEN_ERROR
, "");
737 return JSON_ERROR_FATAL
;
739 else if (JSON_TOKEN_LITERAL_TRUE
== token
) {
741 boolean_value
= true;
743 else if (JSON_TOKEN_LITERAL_FALSE
== token
) {
745 boolean_value
= false;
749 return JSON_ERROR_INVALID_TOKEN
;
755 boolean
operator&&(boolean bool_value
, const BOOLEAN
& other_value
)
757 if (!bool_value
) return FALSE
;
758 other_value
.must_bound("The right operand of and operator is an unbound "
760 return other_value
.boolean_value
;
763 boolean
operator^(boolean bool_value
, const BOOLEAN
& other_value
)
765 other_value
.must_bound("The right operand of xor operator is an unbound "
767 return bool_value
!= other_value
.boolean_value
;
770 boolean
operator||(boolean bool_value
, const BOOLEAN
& other_value
)
772 if (bool_value
) return TRUE
;
773 other_value
.must_bound("The right operand of or operator is an unbound "
775 return other_value
.boolean_value
;
778 boolean
operator==(boolean bool_value
, const BOOLEAN
& other_value
)
780 other_value
.must_bound("The right operand of comparison is an unbound "
782 return bool_value
== other_value
.boolean_value
;
785 void BOOLEAN_template::clean_up()
787 if (template_selection
== VALUE_LIST
||
788 template_selection
== COMPLEMENTED_LIST
)
789 delete [] value_list
.list_value
;
790 template_selection
= UNINITIALIZED_TEMPLATE
;
793 void BOOLEAN_template::copy_template(const BOOLEAN_template
& other_value
)
795 switch (other_value
.template_selection
) {
797 single_value
= other_value
.single_value
;
804 case COMPLEMENTED_LIST
:
805 value_list
.n_values
= other_value
.value_list
.n_values
;
806 value_list
.list_value
= new BOOLEAN_template
[value_list
.n_values
];
807 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++)
808 value_list
.list_value
[i
].copy_template(
809 other_value
.value_list
.list_value
[i
]);
812 TTCN_error("Copying an uninitialized/unsupported boolean template.");
814 set_selection(other_value
);
817 BOOLEAN_template::BOOLEAN_template()
822 BOOLEAN_template::BOOLEAN_template(template_sel other_value
)
823 : Base_Template(other_value
)
825 check_single_selection(other_value
);
828 BOOLEAN_template::BOOLEAN_template(boolean other_value
)
829 : Base_Template(SPECIFIC_VALUE
)
831 single_value
= other_value
;
834 BOOLEAN_template::BOOLEAN_template(const BOOLEAN
& other_value
)
835 : Base_Template(SPECIFIC_VALUE
)
837 other_value
.must_bound("Creating a template from an unbound integer value.");
838 single_value
= other_value
.boolean_value
;
841 BOOLEAN_template::BOOLEAN_template(const OPTIONAL
<BOOLEAN
>& other_value
)
843 switch (other_value
.get_selection()) {
844 case OPTIONAL_PRESENT
:
845 set_selection(SPECIFIC_VALUE
);
846 single_value
= (boolean
)(const BOOLEAN
&)other_value
;
849 set_selection(OMIT_VALUE
);
852 TTCN_error("Creating a boolean template from an unbound optional field.");
856 BOOLEAN_template::BOOLEAN_template(const BOOLEAN_template
& other_value
)
859 copy_template(other_value
);
862 BOOLEAN_template::~BOOLEAN_template()
867 BOOLEAN_template
& BOOLEAN_template::operator=(template_sel other_value
)
869 check_single_selection(other_value
);
871 set_selection(other_value
);
875 BOOLEAN_template
& BOOLEAN_template::operator=(boolean other_value
)
878 set_selection(SPECIFIC_VALUE
);
879 single_value
= other_value
;
883 BOOLEAN_template
& BOOLEAN_template::operator=(const BOOLEAN
& other_value
)
885 other_value
.must_bound("Assignment of an unbound boolean value to a "
888 set_selection(SPECIFIC_VALUE
);
889 single_value
= other_value
.boolean_value
;
893 BOOLEAN_template
& BOOLEAN_template::operator=
894 (const OPTIONAL
<BOOLEAN
>& other_value
)
897 switch (other_value
.get_selection()) {
898 case OPTIONAL_PRESENT
:
899 set_selection(SPECIFIC_VALUE
);
900 single_value
= (boolean
)(const BOOLEAN
&)other_value
;
903 set_selection(OMIT_VALUE
);
906 TTCN_error("Assignment of an unbound optional field to a boolean "
912 BOOLEAN_template
& BOOLEAN_template::operator=
913 (const BOOLEAN_template
& other_value
)
915 if (&other_value
!= this) {
917 copy_template(other_value
);
922 boolean
BOOLEAN_template::match(boolean other_value
,
923 boolean
/* legacy */) const
925 switch (template_selection
) {
927 return single_value
== other_value
;
934 case COMPLEMENTED_LIST
:
935 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++)
936 if (value_list
.list_value
[i
].match(other_value
))
937 return template_selection
== VALUE_LIST
;
938 return template_selection
== COMPLEMENTED_LIST
;
940 TTCN_error("Matching with an uninitialized/unsupported boolean template.");
945 boolean
BOOLEAN_template::match(const BOOLEAN
& other_value
,
946 boolean
/* legacy */) const
948 if (!other_value
.is_bound()) return FALSE
;
949 return match(other_value
.boolean_value
);
952 boolean
BOOLEAN_template::valueof() const
954 if (template_selection
!= SPECIFIC_VALUE
|| is_ifpresent
)
955 TTCN_error("Performing valueof or "
956 "send operation on a non-specific boolean template.");
960 void BOOLEAN_template::set_type(template_sel template_type
,
961 unsigned int list_length
)
963 if (template_type
!= VALUE_LIST
&& template_type
!= COMPLEMENTED_LIST
)
964 TTCN_error("Setting an invalid list type for a boolean template.");
966 set_selection(template_type
);
967 value_list
.n_values
= list_length
;
968 value_list
.list_value
= new BOOLEAN_template
[list_length
];
971 BOOLEAN_template
& BOOLEAN_template::list_item(unsigned int list_index
)
973 if (template_selection
!= VALUE_LIST
&&
974 template_selection
!= COMPLEMENTED_LIST
)
975 TTCN_error("Accessing a list element of a non-list boolean template.");
976 if (list_index
>= value_list
.n_values
)
977 TTCN_error("Index overflow in a boolean value list template.");
978 return value_list
.list_value
[list_index
];
981 void BOOLEAN_template::log() const
983 switch (template_selection
) {
985 TTCN_Logger::log_event_str(single_value
? "true" : "false");
987 case COMPLEMENTED_LIST
:
988 TTCN_Logger::log_event_str("complement ");
991 TTCN_Logger::log_char('(');
992 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++) {
993 if (i
> 0) TTCN_Logger::log_event_str(", ");
994 value_list
.list_value
[i
].log();
996 TTCN_Logger::log_char(')');
1005 void BOOLEAN_template::log_match(const BOOLEAN
& match_value
,
1006 boolean
/* legacy */) const
1008 if (TTCN_Logger::VERBOSITY_COMPACT
== TTCN_Logger::get_matching_verbosity()
1009 && TTCN_Logger::get_logmatch_buffer_len() != 0) {
1010 TTCN_Logger::print_logmatch_buffer();
1011 TTCN_Logger::log_event_str(" := ");
1014 TTCN_Logger::log_event_str(" with ");
1016 if (match(match_value
)) TTCN_Logger::log_event_str(" matched");
1017 else TTCN_Logger::log_event_str(" unmatched");
1020 void BOOLEAN_template::set_param(Module_Param
& param
) {
1021 param
.basic_check(Module_Param::BC_TEMPLATE
, "boolean template");
1022 Module_Param_Ptr mp
= ¶m
;
1023 if (param
.get_type() == Module_Param::MP_Reference
) {
1024 mp
= param
.get_referenced_param();
1026 switch (mp
->get_type()) {
1027 case Module_Param::MP_Omit
:
1030 case Module_Param::MP_Any
:
1033 case Module_Param::MP_AnyOrNone
:
1034 *this = ANY_OR_OMIT
;
1036 case Module_Param::MP_List_Template
:
1037 case Module_Param::MP_ComplementList_Template
: {
1038 BOOLEAN_template temp
;
1039 temp
.set_type(mp
->get_type() == Module_Param::MP_List_Template
?
1040 VALUE_LIST
: COMPLEMENTED_LIST
, mp
->get_size());
1041 for (size_t i
=0; i
<mp
->get_size(); i
++) {
1042 temp
.list_item(i
).set_param(*mp
->get_elem(i
));
1046 case Module_Param::MP_Boolean
:
1047 *this = mp
->get_boolean();
1050 param
.type_error("boolean template");
1052 is_ifpresent
= param
.get_ifpresent() || mp
->get_ifpresent();
1055 Module_Param
* BOOLEAN_template::get_param(Module_Param_Name
& param_name
) const
1057 Module_Param
* mp
= NULL
;
1058 switch (template_selection
) {
1059 case UNINITIALIZED_TEMPLATE
:
1060 mp
= new Module_Param_Unbound();
1063 mp
= new Module_Param_Omit();
1066 mp
= new Module_Param_Any();
1069 mp
= new Module_Param_AnyOrNone();
1071 case SPECIFIC_VALUE
:
1072 mp
= new Module_Param_Boolean(single_value
);
1075 case COMPLEMENTED_LIST
: {
1076 if (template_selection
== VALUE_LIST
) {
1077 mp
= new Module_Param_List_Template();
1080 mp
= new Module_Param_ComplementList_Template();
1082 for (size_t i
= 0; i
< value_list
.n_values
; ++i
) {
1083 mp
->add_elem(value_list
.list_value
[i
].get_param(param_name
));
1090 mp
->set_ifpresent();
1095 void BOOLEAN_template::encode_text(Text_Buf
& text_buf
) const
1097 encode_text_base(text_buf
);
1098 switch (template_selection
) {
1103 case SPECIFIC_VALUE
:
1104 text_buf
.push_int(single_value
? 1 : 0);
1107 case COMPLEMENTED_LIST
:
1108 text_buf
.push_int(value_list
.n_values
);
1109 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++)
1110 value_list
.list_value
[i
].encode_text(text_buf
);
1113 TTCN_error("Text encoder: Encoding an uninitialized/unsupported boolean "
1118 void BOOLEAN_template::decode_text(Text_Buf
& text_buf
)
1121 decode_text_base(text_buf
);
1122 switch (template_selection
) {
1127 case SPECIFIC_VALUE
: {
1128 int int_value
= text_buf
.pull_int().get_val();
1129 switch (int_value
) {
1131 single_value
= FALSE
;
1134 single_value
= TRUE
;
1137 TTCN_error("Text decoder: An invalid boolean value (%d) was received for "
1138 "a template.", int_value
);
1142 case COMPLEMENTED_LIST
:
1143 value_list
.n_values
= text_buf
.pull_int().get_val();
1144 value_list
.list_value
= new BOOLEAN_template
[value_list
.n_values
];
1145 for (unsigned int i
= 0; i
< value_list
.n_values
; i
++)
1146 value_list
.list_value
[i
].decode_text(text_buf
);
1149 TTCN_error("Text decoder: An unknown/unsupported selection was "
1150 "received for a boolean template.");
1154 boolean
BOOLEAN_template::is_present(boolean legacy
/* = FALSE */) const
1156 if (template_selection
==UNINITIALIZED_TEMPLATE
) return FALSE
;
1157 return !match_omit(legacy
);
1160 boolean
BOOLEAN_template::match_omit(boolean legacy
/* = FALSE */) const
1162 if (is_ifpresent
) return TRUE
;
1163 switch (template_selection
) {
1168 case COMPLEMENTED_LIST
:
1170 // legacy behavior: 'omit' can appear in the value/complement list
1171 for (unsigned int i
=0; i
<value_list
.n_values
; i
++)
1172 if (value_list
.list_value
[i
].match_omit())
1173 return template_selection
==VALUE_LIST
;
1174 return template_selection
==COMPLEMENTED_LIST
;
1176 // else fall through
1183 #ifndef TITAN_RUNTIME_2
1184 void BOOLEAN_template::check_restriction(template_res t_res
, const char* t_name
,
1185 boolean legacy
/* = FALSE */) const
1187 if (template_selection
==UNINITIALIZED_TEMPLATE
) return;
1188 switch ((t_name
&&(t_res
==TR_VALUE
))?TR_OMIT
:t_res
) {
1190 if (!is_ifpresent
&& template_selection
==SPECIFIC_VALUE
) return;
1193 if (!is_ifpresent
&& (template_selection
==OMIT_VALUE
||
1194 template_selection
==SPECIFIC_VALUE
)) return;
1197 if (!match_omit(legacy
)) return;
1202 TTCN_error("Restriction `%s' on template of type %s violated.",
1203 get_res_name(t_res
), t_name
? t_name
: "boolean");