record_of.c 201 KB
Newer Older
Elemer Lelik's avatar
Elemer Lelik committed
1
/******************************************************************************
Adam Knapp's avatar
Adam Knapp committed
2
 * Copyright (c) 2000-2021 Ericsson Telecom AB
Elemer Lelik's avatar
Elemer Lelik committed
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v2.0
Elemer Lelik's avatar
Elemer Lelik committed
5
 * which accompanies this distribution, and is available at
6
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
Elemer Lelik's avatar
Elemer Lelik committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Contributors:
 *   Baji, Laszlo
 *   Balasko, Jeno
 *   Baranyi, Botond
 *   Beres, Szabolcs
 *   Delic, Adam
 *   Forstner, Matyas
 *   Kovacs, Ferenc
 *   Raduly, Csaba
 *   Szabados, Kristof
 *   Szabo, Bence Janos
 *   Szabo, Janos Zoltan – initial implementation
 *   Szalai, Gabor
 *   Zalanyi, Balazs Andor
 *
 ******************************************************************************/
Elemer Lelik's avatar
Elemer Lelik committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include "../common/memory.h"
#include "datatypes.h"
#include "record_of.h"
#include "encdec.h"

#include "main.hh"
#include "ttcn3/compiler.h"

/** code generation for original runtime */
static void defRecordOfClass1(const struct_of_def *sdef, output_struct *output);
static void defRecordOfTemplate1(const struct_of_def *sdef, output_struct *output);
/** code generation for alternative runtime (TITAN_RUNTIME_2) */
static void defRecordOfClass2(const struct_of_def *sdef, output_struct *output);
static void defRecordOfTemplate2(const struct_of_def *sdef, output_struct *output);

void defRecordOfClass(const struct_of_def *sdef, output_struct *output)
{
  if (use_runtime_2) defRecordOfClass2(sdef, output);
  else defRecordOfClass1(sdef, output);
}

void defRecordOfTemplate(const struct_of_def *sdef, output_struct *output)
{
  if (use_runtime_2) defRecordOfTemplate2(sdef, output);
  else defRecordOfTemplate1(sdef, output);
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

void defRecordOfClass1(const struct_of_def *sdef, output_struct *output)
{
  char *def = NULL, *src = NULL;
  const char *name = sdef->name, *dispname = sdef->dispname;
  const char *type = sdef->type;
Elemer Lelik's avatar
Elemer Lelik committed
58 59 60 61 62
  boolean ber_needed = force_gen_seof || (sdef->isASN1 && enable_ber());
  boolean raw_needed = force_gen_seof || (sdef->hasRaw && enable_raw());
  boolean text_needed = force_gen_seof || (sdef->hasText && enable_text());
  boolean xer_needed = force_gen_seof || (sdef->hasXer && enable_xer());
  boolean json_needed = force_gen_seof || (sdef->hasJson && enable_json());
63
  boolean oer_needed = force_gen_seof || (sdef->hasOer && enable_oer());
Elemer Lelik's avatar
Elemer Lelik committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

  /* Class definition and private data members */
  def = mputprintf(def,
#ifndef NDEBUG
    "// written by %s in " __FILE__ " at %d\n"
#endif
    "class %s : public Base_Type {\n"
    "struct recordof_setof_struct {\n"
    "int ref_count;\n"
    "int n_elements;\n"
    "%s **value_elements;\n"
    "} *val_ptr;\n"
#ifndef NDEBUG
      , __FUNCTION__, __LINE__
#endif
    , name, type);

  /* constant unbound element */
  def = mputprintf(def, "\nstatic const %s UNBOUND_ELEM;\n", type);
  src = mputprintf(src, "\nconst %s %s::UNBOUND_ELEM;\n", type, name);

  /* private member functions */
  def = mputprintf(def,
    "private:\n"
    "friend boolean operator==(null_type null_value, "
	  "const %s& other_value);\n", name);

  if (sdef->kind == SET_OF) {
    /* callback function for comparison */
    def = mputstr(def, "static boolean compare_function("
	"const Base_Type *left_ptr, int left_index, "
        "const Base_Type *right_ptr, int right_index);\n");
    src = mputprintf(src, "boolean %s::compare_function("
	"const Base_Type *left_ptr, int left_index, "
        "const Base_Type *right_ptr, int right_index)\n"
        "{\n"
        "if (((const %s*)left_ptr)->val_ptr == NULL) "
        "TTCN_error(\"The left operand of comparison is an unbound value of "
        "type %s.\");\n"
        "if (((const %s*)right_ptr)->val_ptr == NULL) "
        "TTCN_error(\"The right operand of comparison is an unbound value of "
        "type %s.\");\n"
        "if (((const %s*)left_ptr)->val_ptr->value_elements[left_index] != NULL){\n"
        "if (((const %s*)right_ptr)->val_ptr->value_elements[right_index] != NULL){\n"
        "return *((const %s*)left_ptr)->val_ptr->value_elements[left_index] == "
        "*((const %s*)right_ptr)->val_ptr->value_elements[right_index];\n"
        "} else return FALSE;\n"
        "} else {\n"
        "return ((const %s*)right_ptr)->val_ptr->value_elements[right_index] == NULL;\n"
        "}\n"
        "}\n\n", name, name, dispname, name, dispname, name, name, name, name, name);
  }

  /* public member functions */
  def = mputstr(def, "\npublic:\n");
  def = mputprintf(def, "  typedef %s of_type;\n", sdef->type);

  /* constructors */
  def = mputprintf(def, "%s();\n", name);
  src = mputprintf(src,
    "%s::%s()\n"
    "{\n"
    "val_ptr = NULL;\n"
    "}\n\n", name, name);

  def = mputprintf(def, "%s(null_type other_value);\n", name);
  src = mputprintf(src,
    "%s::%s(null_type)\n"
    "{\n"
    "val_ptr = new recordof_setof_struct;\n"
    "val_ptr->ref_count = 1;\n"
    "val_ptr->n_elements = 0;\n"
    "val_ptr->value_elements = NULL;\n"
    "}\n\n", name, name);

  /* copy constructor */
  def = mputprintf(def, "%s(const %s& other_value);\n", name, name);
  src = mputprintf(src,
     "%s::%s(const %s& other_value)\n"
     "{\n"
     "if (!other_value.is_bound()) "
     "TTCN_error(\"Copying an unbound value of type %s.\");\n"
     "val_ptr = other_value.val_ptr;\n"
     "val_ptr->ref_count++;\n"
Elemer Lelik's avatar
Elemer Lelik committed
148
     "}\n\n", name, name, name, dispname);
Elemer Lelik's avatar
Elemer Lelik committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

  /* destructor */
  def = mputprintf(def, "~%s();\n\n", name);
  src = mputprintf(src,
    "%s::~%s()\n"
    "{\n"
    "clean_up();\n"
    "if (val_ptr != NULL) val_ptr = NULL;\n"
    "}\n\n", name, name);

  /* clean_up function */
  def = mputstr(def, "void clean_up();\n");
  src = mputprintf
    (src,
     "void %s::clean_up()\n"
     "{\n"
     "if (val_ptr != NULL) {\n"
     "if (val_ptr->ref_count > 1) {\n"
     "val_ptr->ref_count--;\n"
     "val_ptr = NULL;\n"
     "}\n"
     "else if (val_ptr->ref_count == 1) {\n"
     "for (int elem_count = 0; elem_count < val_ptr->n_elements;\n"
     "elem_count++)\n"
     "if (val_ptr->value_elements[elem_count] != NULL)\n"
     "delete val_ptr->value_elements[elem_count];\n"
     "free_pointers((void**)val_ptr->value_elements);\n"
     "delete val_ptr;\n"
     "val_ptr = NULL;\n"
     "}\n"
     "else\n"
     "TTCN_error(\"Internal error: Invalid reference counter in a record "
     "of/set of value.\");\n"
     "}\n"
     "}\n\n", name);

  /* assignment operators */
  def = mputprintf(def, "%s& operator=(null_type other_value);\n", name);
  src = mputprintf(src,
    "%s& %s::operator=(null_type)\n"
    "{\n"
Elemer Lelik's avatar
Elemer Lelik committed
190 191 192 193 194
    "clean_up();\n"
    "val_ptr = new recordof_setof_struct;\n"
    "val_ptr->ref_count = 1;\n"
    "val_ptr->n_elements = 0;\n"
    "val_ptr->value_elements = NULL;\n"
Elemer Lelik's avatar
Elemer Lelik committed
195 196 197 198 199 200 201 202
    "return *this;\n"
    "}\n\n", name, name);

  def = mputprintf(def, "%s& operator=(const %s& other_value);\n\n",
                   name, name);
  src = mputprintf(src,
    "%s& %s::operator=(const %s& other_value)\n"
    "{\n"
Elemer Lelik's avatar
Elemer Lelik committed
203
    "if (other_value.val_ptr == NULL) "
Elemer Lelik's avatar
Elemer Lelik committed
204 205 206 207 208 209 210
    "TTCN_error(\"Assigning an unbound value of type %s.\");\n"
    "if (this != &other_value) {\n"
    "clean_up();\n"
    "val_ptr = other_value.val_ptr;\n"
    "val_ptr->ref_count++;\n"
    "}\n"
    "return *this;\n"
Elemer Lelik's avatar
Elemer Lelik committed
211
    "}\n\n", name, name, name, dispname);
Elemer Lelik's avatar
Elemer Lelik committed
212 213 214 215 216 217 218 219 220

  /* comparison operators */
  def = mputstr(def, "boolean operator==(null_type other_value) const;\n");
  src = mputprintf(src,
    "boolean %s::operator==(null_type) const\n"
    "{\n"
    "if (val_ptr == NULL)\n"
    "TTCN_error(\"The left operand of comparison is an unbound value of "
    "type %s.\");\n"
Elemer Lelik's avatar
Elemer Lelik committed
221
    "return val_ptr->n_elements == 0 ;\n"
Elemer Lelik's avatar
Elemer Lelik committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    "}\n\n", name, dispname);

  def = mputprintf(def, "boolean operator==(const %s& other_value) const;\n",
                   name);
  src = mputprintf(src,
    "boolean %s::operator==(const %s& other_value) const\n"
    "{\n"
    "if (val_ptr == NULL) "
    "TTCN_error(\"The left operand of comparison is an unbound value of type "
    "%s.\");\n"
    "if (other_value.val_ptr == NULL) "
    "TTCN_error(\"The right operand of comparison is an unbound value of type "
    "%s.\");\n"
    "if (val_ptr == other_value.val_ptr) return TRUE;\n", name, name,
    dispname, dispname);
  if (sdef->kind == SET_OF) {
    src = mputstr(src,
Elemer Lelik's avatar
Elemer Lelik committed
239 240
       "return compare_set_of(this, val_ptr->n_elements, &other_value, "
       "(other_value.val_ptr)->n_elements, compare_function);\n");
Elemer Lelik's avatar
Elemer Lelik committed
241 242 243
  } else {
    src = mputstr
      (src,
Elemer Lelik's avatar
Elemer Lelik committed
244
       "if (val_ptr->n_elements != (other_value.val_ptr)->n_elements)\n"
Elemer Lelik's avatar
Elemer Lelik committed
245
       "return FALSE;\n"
Elemer Lelik's avatar
Elemer Lelik committed
246 247 248
       "for (int elem_count = 0; elem_count < val_ptr->n_elements; elem_count++){\n"
       "if (val_ptr->value_elements[elem_count] != NULL){\n"
       "if ((other_value.val_ptr)->value_elements[elem_count] != NULL){\n"
Elemer Lelik's avatar
Elemer Lelik committed
249 250 251 252 253
       "  if (*val_ptr->value_elements[elem_count] != "
          "*(other_value.val_ptr)->value_elements[elem_count]) "
          "return FALSE;\n"
       "} else return FALSE;\n"
       "} else {\n"
Elemer Lelik's avatar
Elemer Lelik committed
254 255
       "if ((other_value.val_ptr)->value_elements[elem_count] != NULL) "
         "return FALSE;\n"
Elemer Lelik's avatar
Elemer Lelik committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
       "}\n"
       "}\n"
       "return TRUE;\n");
  }
  src = mputstr(src, "}\n\n");

  def = mputstr(def, "inline boolean operator!=(null_type other_value) const "
                "{ return !(*this == other_value); }\n");
  def = mputprintf(def, "inline boolean operator!=(const %s& other_value) "
                   "const { return !(*this == other_value); }\n\n", name);

  /* indexing operators */
  /* Non-const operator[] is allowed to extend the record-of */
  def = mputprintf(def, "%s& operator[](int index_value);\n", type);
  src = mputprintf(src,
    "%s& %s::operator[](int index_value)\n"
    "{\n"
    "if (index_value < 0) TTCN_error(\"Accessing an element of type %s "
    "using a negative index: %%d.\", index_value);\n"
    "if (val_ptr == NULL) {\n"
    "val_ptr = new recordof_setof_struct;\n"
    "val_ptr->ref_count = 1;\n"
    "val_ptr->n_elements = 0;\n"
    "val_ptr->value_elements = NULL;\n"
    "} else if (val_ptr->ref_count > 1) {\n" /* copy-on-write */
    "struct recordof_setof_struct *new_val_ptr = new recordof_setof_struct;\n"
    "new_val_ptr->ref_count = 1;\n"
    "new_val_ptr->n_elements = (index_value >= val_ptr->n_elements) ? "
    "index_value + 1 : val_ptr->n_elements;\n"
    "new_val_ptr->value_elements = "
    "(%s**)allocate_pointers(new_val_ptr->n_elements);\n"
    "for (int elem_count = 0; elem_count < val_ptr->n_elements; "
    "elem_count++){\n"
    "if (val_ptr->value_elements[elem_count] != NULL){\n"
    "new_val_ptr->value_elements[elem_count] = "
    "new %s(*(val_ptr->value_elements[elem_count]));\n"
    "}\n"
    "}\n"
    "clean_up();\n"
    "val_ptr = new_val_ptr;\n"
    "}\n"
    "if (index_value >= val_ptr->n_elements) set_size(index_value + 1);\n"
    "if (val_ptr->value_elements[index_value] == NULL) {\n"
    "val_ptr->value_elements[index_value] = new %s;\n"
    "}\n"
    "return *val_ptr->value_elements[index_value];\n"
    "}\n\n", type, name, dispname, type, type, type);

  def = mputprintf(def, "%s& operator[](const INTEGER& index_value);\n",
                   type);
  src = mputprintf(src,
    "%s& %s::operator[](const INTEGER& index_value)\n"
    "{\n"
    "index_value.must_bound(\"Using an unbound integer value for indexing "
    "a value of type %s.\");\n"
    "return (*this)[(int)index_value];\n"
    "}\n\n", type, name, dispname);

  /* Const operator[] throws an error if over-indexing */
  def = mputprintf(def, "const %s& operator[](int index_value) const;\n",
                   type);
  src = mputprintf(src,
    "const %s& %s::operator[](int index_value) const\n"
    "{\n"
    "if (val_ptr == NULL)\n"
    "TTCN_error(\"Accessing an element in an unbound value of type %s.\");\n"
    "if (index_value < 0) TTCN_error(\"Accessing an element of type %s "
    "using a negative index: %%d.\", index_value);\n"
Elemer Lelik's avatar
Elemer Lelik committed
324
    "if (index_value >= val_ptr->n_elements) TTCN_error(\"Index overflow in "
Elemer Lelik's avatar
Elemer Lelik committed
325
    "a value of type %s: The index is %%d, but the value has only %%d "
Elemer Lelik's avatar
Elemer Lelik committed
326
    "elements.\", index_value, val_ptr->n_elements);\n"
Kristof Szabados's avatar
Kristof Szabados committed
327 328
    "return (val_ptr->value_elements[index_value] == NULL) ?\n"
    "UNBOUND_ELEM : *val_ptr->value_elements[index_value];\n"
Elemer Lelik's avatar
Elemer Lelik committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
    "}\n\n", type, name, dispname, dispname, dispname);

  def = mputprintf(def, "const %s& operator[](const INTEGER& index_value) "
                   "const;\n\n", type);
  src = mputprintf(src,
    "const %s& %s::operator[](const INTEGER& index_value) const\n"
    "{\n"
    "index_value.must_bound(\"Using an unbound integer value for indexing "
    "a value of type %s.\");\n"
    "return (*this)[(int)index_value];\n"
    "}\n\n", type, name, dispname);

  /* rotation operators */
  def = mputprintf(def,
    "%s operator<<=(int rotate_count) const;\n"
    "%s operator<<=(const INTEGER& rotate_count) const;\n"
    "%s operator>>=(int rotate_count) const;\n"
    "%s operator>>=(const INTEGER& rotate_count) const;\n\n",
    name, name, name, name);
  src = mputprintf(src,
    "%s %s::operator<<=(int rotate_count) const\n"
    "{\n"
    "return *this >>= (-rotate_count);\n"
    "}\n\n"
    "%s %s::operator<<=(const INTEGER& rotate_count) const\n"
    "{\n"
    "rotate_count.must_bound(\""
    "Unbound integer operand of rotate left operator.\");\n"
    "return *this >>= (int)(-rotate_count);\n"
    "}\n\n"
    "%s %s::operator>>=(const INTEGER& rotate_count) const\n"
    "{\n"
    "rotate_count.must_bound(\""
    "Unbound integer operand of rotate right operator.\");\n"
    "return *this >>= (int)rotate_count;\n"
    "}\n\n"
    "%s %s::operator>>=(int rotate_count) const\n"
    "{\n"
    "if (val_ptr == NULL) "
    "TTCN_error(\"Performing rotation operation on an unbound value of type "
    "%s.\");\n"
Elemer Lelik's avatar
Elemer Lelik committed
370
    "if (val_ptr->n_elements == 0) return *this;\n"
Elemer Lelik's avatar
Elemer Lelik committed
371
    "int rc;\n"
Elemer Lelik's avatar
Elemer Lelik committed
372 373
    "if (rotate_count>=0) rc = rotate_count %% val_ptr->n_elements;\n"
    "else rc = val_ptr->n_elements - ((-rotate_count) %% val_ptr->n_elements);\n"
Elemer Lelik's avatar
Elemer Lelik committed
374 375
    "if (rc == 0) return *this;\n"
    "%s ret_val;\n"
Elemer Lelik's avatar
Elemer Lelik committed
376 377 378 379
    "ret_val.set_size(val_ptr->n_elements);\n"
    "for (int i=0; i<val_ptr->n_elements; i++) {\n"
    "if (val_ptr->value_elements[i] != NULL) {\n"
    "ret_val.val_ptr->value_elements[(i+rc)%%val_ptr->n_elements] ="
Elemer Lelik's avatar
Elemer Lelik committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
      "new %s(*val_ptr->value_elements[i]);\n"
    "}\n"
    "}\n"
    "return ret_val;\n"
    "}\n\n",
    name, name, name, name, name, name, name, name, dispname, name, type);

  /* concatenation */
  def = mputprintf(def,
    "%s operator+(const %s& other_value) const;\n\n", name, name);
  src = mputprintf(src,
    "%s %s::operator+(const %s& other_value) const\n"
    "{\n"
    "if (val_ptr == NULL || other_value.val_ptr == NULL) "
      "TTCN_error(\"Unbound operand of %s concatenation.\");\n"
Elemer Lelik's avatar
Elemer Lelik committed
395 396
    "if (val_ptr->n_elements == 0) return other_value;\n"
    "if (other_value.val_ptr->n_elements == 0) return *this;\n"
Elemer Lelik's avatar
Elemer Lelik committed
397
    "%s ret_val;\n"
Elemer Lelik's avatar
Elemer Lelik committed
398 399 400
    "ret_val.set_size(val_ptr->n_elements+other_value.val_ptr->n_elements);\n"
    "for (int i=0; i<val_ptr->n_elements; i++) {\n"
    "if (val_ptr->value_elements[i] != NULL) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
401 402 403
    "ret_val.val_ptr->value_elements[i] = new %s(*val_ptr->value_elements[i]);\n"
    "}\n"
    "}\n"
Elemer Lelik's avatar
Elemer Lelik committed
404 405 406
    "for (int i=0; i<other_value.val_ptr->n_elements; i++) {\n"
    "if (other_value.val_ptr->value_elements[i] != NULL) {\n"
    "ret_val.val_ptr->value_elements[i+val_ptr->n_elements] = "
Elemer Lelik's avatar
Elemer Lelik committed
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
    "new %s(*other_value.val_ptr->value_elements[i]);\n"
    "}\n"
    "}\n"
    "return ret_val;\n"
    "}\n\n", name, name, name, dispname, name, type, type);

  /* substr() */
  def = mputprintf(def,
    "%s substr(int index, int returncount) const;\n\n", name);
  src = mputprintf(src,
    "%s %s::substr(int index, int returncount) const\n"
    "{\n"
    "if (val_ptr == NULL) "
      "TTCN_error(\"The first argument of substr() is an unbound value of "
        "type %s.\");\n"
Elemer Lelik's avatar
Elemer Lelik committed
422
    "check_substr_arguments(val_ptr->n_elements, index, returncount, "
Elemer Lelik's avatar
Elemer Lelik committed
423 424 425 426
      "\"%s\",\"element\");\n"
    "%s ret_val;\n"
    "ret_val.set_size(returncount);\n"
    "for (int i=0; i<returncount; i++) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
427
    "if (val_ptr->value_elements[i+index] != NULL) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
    "ret_val.val_ptr->value_elements[i] = "
    "new %s(*val_ptr->value_elements[i+index]);\n"
    "}\n"
    "}\n"
    "return ret_val;\n"
    "}\n\n", name, name, dispname, dispname, name, type);

  /* replace() */
  def = mputprintf(def,
    "%s replace(int index, int len, const %s& repl) const;\n\n", name, name);
  src = mputprintf(src,
    "%s %s::replace(int index, int len, const %s& repl) const\n"
    "{\n"
    "if (val_ptr == NULL) "
      "TTCN_error(\"The first argument of replace() is an unbound value of "
        "type %s.\");\n"
    "if (repl.val_ptr == NULL) "
      "TTCN_error(\"The fourth argument of replace() is an unbound value of "
        "type %s.\");\n"
Elemer Lelik's avatar
Elemer Lelik committed
447
    "check_replace_arguments(val_ptr->n_elements, index, len, "
Elemer Lelik's avatar
Elemer Lelik committed
448 449
      "\"%s\",\"element\");\n"
    "%s ret_val;\n"
Elemer Lelik's avatar
Elemer Lelik committed
450
    "ret_val.set_size(val_ptr->n_elements + repl.val_ptr->n_elements - len);\n"
Elemer Lelik's avatar
Elemer Lelik committed
451
    "for (int i = 0; i < index; i++) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
452
    "if (val_ptr->value_elements[i] != NULL) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
453 454 455
    "ret_val.val_ptr->value_elements[i] = new %s(*val_ptr->value_elements[i]);\n"
    "}\n"
    "}\n"
Elemer Lelik's avatar
Elemer Lelik committed
456 457
    "for (int i = 0; i < repl.val_ptr->n_elements; i++) {\n"
    "if (repl.val_ptr->value_elements[i] != NULL) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
458 459 460 461
    "ret_val.val_ptr->value_elements[i+index] = "
    "new %s(*repl.val_ptr->value_elements[i]);\n"
    "}\n"
    "}\n"
Elemer Lelik's avatar
Elemer Lelik committed
462 463 464
    "for (int i = 0; i < val_ptr->n_elements - index - len; i++) {\n"
    "if (val_ptr->value_elements[index+i+len] != NULL) {\n"
    "ret_val.val_ptr->value_elements[index+i+repl.val_ptr->n_elements] = "
Elemer Lelik's avatar
Elemer Lelik committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
    "new %s(*val_ptr->value_elements[index+i+len]);\n"
    "}\n"
    "}\n"
    "return ret_val;\n"
  "}\n\n", name, name, name, dispname, dispname, dispname, name, type, type, type);
  def = mputprintf(def,
    "%s replace(int index, int len, const %s_template& repl) const;\n\n",
    name, name);
  src = mputprintf(src,
    "%s %s::replace(int index, int len, const %s_template& repl) const\n"
    "{\n"
    "if (!repl.is_value()) TTCN_error(\"The fourth argument of function "
      "replace() is a template with non-specific value.\");\n"
    "return replace(index, len, repl.valueof());\n"
    "}\n\n", name, name, name);

  /* set_size function */
  def = mputstr(def, "void set_size(int new_size);\n");
  src = mputprintf(src, "void %s::set_size(int new_size)\n"
    "{\n"
    "if (new_size < 0) TTCN_error(\"Internal error: Setting a negative size "
      "for a value of type %s.\");\n"
    "if (val_ptr == NULL) {\n"
    "val_ptr = new recordof_setof_struct;\n"
    "val_ptr->ref_count = 1;\n"
    "val_ptr->n_elements = 0;\n"
    "val_ptr->value_elements = NULL;\n"
    "} else if (val_ptr->ref_count > 1) {\n" /* copy-on-write */
    "struct recordof_setof_struct *new_val_ptr = new recordof_setof_struct;\n"
    "new_val_ptr->ref_count = 1;\n"
    "new_val_ptr->n_elements = (new_size < val_ptr->n_elements) ? "
    "new_size : val_ptr->n_elements;\n"
    "new_val_ptr->value_elements = "
    "(%s**)allocate_pointers(new_val_ptr->n_elements);\n"
    "for (int elem_count = 0; elem_count < new_val_ptr->n_elements; "
    "elem_count++) {\n"
    "if (val_ptr->value_elements[elem_count] != NULL){\n"
    "new_val_ptr->value_elements[elem_count] = "
    "new %s(*(val_ptr->value_elements[elem_count]));\n"
    "}\n"
    "}\n"
    "clean_up();\n"
    "val_ptr = new_val_ptr;\n"
    "}\n"
    "if (new_size > val_ptr->n_elements) {\n"
    "val_ptr->value_elements = (%s**)"
    "reallocate_pointers((void**)val_ptr->value_elements, "
    "val_ptr->n_elements, new_size);\n"
    "#ifdef TITAN_MEMORY_DEBUG_SET_RECORD_OF\n"
    "if((val_ptr->n_elements/1000)!=(new_size/1000)) "
    "TTCN_warning(\"New size of type %s: %%d\",new_size);\n"
    "#endif\n"
    "val_ptr->n_elements = new_size;\n"
    "} else if (new_size < val_ptr->n_elements) {\n"
    "for (int elem_count = new_size; elem_count < val_ptr->n_elements; "
Elemer Lelik's avatar
Elemer Lelik committed
520 521
    "elem_count++)\n"
    "if (val_ptr->value_elements[elem_count] != NULL)"
Elemer Lelik's avatar
Elemer Lelik committed
522 523 524 525 526 527 528 529 530 531
    "delete val_ptr->value_elements[elem_count];\n"
    "val_ptr->value_elements = (%s**)"
    "reallocate_pointers((void**)val_ptr->value_elements, "
    "val_ptr->n_elements, new_size);\n"
    "val_ptr->n_elements = new_size;\n"
    "}\n"
    "}\n\n", name, dispname, type, type, type, dispname, type);

  /* is_bound function */
  def = mputstr(def,
Elemer Lelik's avatar
Elemer Lelik committed
532
    "inline boolean is_bound() const {return val_ptr != NULL; }\n");
Elemer Lelik's avatar
Elemer Lelik committed
533 534 535 536 537 538 539 540 541 542 543

  /* is_present function */
  def = mputstr(def,
    "inline boolean is_present() const { return is_bound(); }\n");

  /* is_value function */
  def = mputstr(def,
    "boolean is_value() const;\n");
  src = mputprintf(src,
    "boolean %s::is_value() const\n"
    "{\n"
544
    "if (val_ptr == NULL) return FALSE;\n"
Elemer Lelik's avatar
Elemer Lelik committed
545 546
    "for(int i = 0; i < val_ptr->n_elements; ++i) {\n"
    "if (val_ptr->value_elements[i] == NULL || "
Elemer Lelik's avatar
Elemer Lelik committed
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
    "!val_ptr->value_elements[i]->is_value()) return FALSE;\n"
    "}\n"
    "return TRUE;\n"
    "}\n\n", name);

  /* sizeof operation */
  def = mputstr(def,
    "int size_of() const;\n"
    "int n_elem() const { return size_of(); }\n");
  src = mputprintf(src,
    "int %s::size_of() const\n"
    "{\n"
    "if (val_ptr == NULL) "
    "TTCN_error(\"Performing sizeof operation on an unbound value of type "
    "%s.\");\n"
Elemer Lelik's avatar
Elemer Lelik committed
562
    "return val_ptr->n_elements;\n"
Elemer Lelik's avatar
Elemer Lelik committed
563 564 565 566 567 568 569 570 571 572
    "}\n\n", name, dispname);

  /* lengthof operation */
  def = mputstr(def, "int lengthof() const;\n");
  src = mputprintf(src,
    "int %s::lengthof() const\n"
    "{\n"
    "if (val_ptr == NULL) "
      "TTCN_error(\"Performing lengthof operation on an unbound value of type "
    "%s.\");\n"
Elemer Lelik's avatar
Elemer Lelik committed
573 574
    "for (int my_length=val_ptr->n_elements; my_length>0; my_length--) "
      "if (val_ptr->value_elements[my_length-1] != NULL) return my_length;\n"
Elemer Lelik's avatar
Elemer Lelik committed
575 576 577 578 579 580 581 582 583 584 585 586 587
    "return 0;\n"
    "}\n\n", name, dispname);

  /* log function */
  def = mputstr(def, "void log() const;\n");
  src = mputprintf
    (src,
     "void %s::log() const\n"
     "{\n"
     "if (val_ptr == NULL) {;\n"
     "TTCN_Logger::log_event_unbound();\n"
     "return;\n"
     "}\n"
Elemer Lelik's avatar
Elemer Lelik committed
588
     "switch (val_ptr->n_elements) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
589 590 591 592 593
     "case 0:\n"
     "TTCN_Logger::log_event_str(\"{ }\");\n"
     "break;\n"
     "default:\n"
     "TTCN_Logger::log_event_str(\"{ \");\n"
Elemer Lelik's avatar
Elemer Lelik committed
594
     "for (int elem_count = 0; elem_count < val_ptr->n_elements; "
Elemer Lelik's avatar
Elemer Lelik committed
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
     "elem_count++) {\n"
     "if (elem_count > 0) TTCN_Logger::log_event_str(\", \");\n"
     "(*this)[elem_count].log();\n"
     "}\n"
     "TTCN_Logger::log_event_str(\" }\");\n"
     "}\n"
     "}\n\n", name);

  /* set_param function */
  def = mputstr(def, "void set_param(Module_Param& param);\n");
  src = mputprintf(src,
    "void %s::set_param(Module_Param& param)\n"
    "{\n"
    "  param.basic_check(Module_Param::BC_VALUE|Module_Param::BC_LIST, \"%s value\");\n"
    "  switch (param.get_operation_type()) {\n"
    "  case Module_Param::OT_ASSIGN:\n"
611
    "    if (param.get_type()==Module_Param::MP_Value_List && param.get_size()==0) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
612 613 614
    "      *this = NULL_VALUE;\n"
    "      return;\n"
    "    }\n"
615
    "    switch (param.get_type()) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
616
    "    case Module_Param::MP_Value_List:\n"
617 618 619
    "      set_size(param.get_size());\n"
    "      for (size_t i=0; i<param.get_size(); ++i) {\n"
    "        Module_Param* const curr = param.get_elem(i);\n"
Elemer Lelik's avatar
Elemer Lelik committed
620 621
    "        if (curr->get_type()!=Module_Param::MP_NotUsed) {\n"
    "          (*this)[i].set_param(*curr);\n"
622 623 624 625
    "          if (!(*this)[i].is_bound()) {\n"
    "            delete val_ptr->value_elements[i];\n"
    "            val_ptr->value_elements[i] = NULL;\n"
    "          }\n"
Elemer Lelik's avatar
Elemer Lelik committed
626 627 628 629
    "        }\n"
    "      }\n"
    "      break;\n"
    "    case Module_Param::MP_Indexed_List:\n"
630 631
    "      for (size_t i=0; i<param.get_size(); ++i) {\n"
    "        Module_Param* const curr = param.get_elem(i);\n"
Elemer Lelik's avatar
Elemer Lelik committed
632
    "        (*this)[curr->get_id()->get_index()].set_param(*curr);\n"
633 634 635 636
    "        if (!(*this)[curr->get_id()->get_index()].is_bound()) {\n"
    "          delete val_ptr->value_elements[curr->get_id()->get_index()];\n"
    "          val_ptr->value_elements[curr->get_id()->get_index()] = NULL;\n"
    "        }\n"
Elemer Lelik's avatar
Elemer Lelik committed
637 638 639 640 641 642 643
    "      }\n"
    "      break;\n"
    "    default:\n"
    "      param.type_error(\"%s value\", \"%s\");\n"
    "    }\n"
    "    break;\n"
    "  case Module_Param::OT_CONCAT:\n"
644
    "    switch (param.get_type()) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
645 646 647
    "    case Module_Param::MP_Value_List: {\n"
    "      if (!is_bound()) *this = NULL_VALUE;\n"
    "      int start_idx = lengthof();\n"
648 649
    "      for (size_t i=0; i<param.get_size(); ++i) {\n"
    "        Module_Param* const curr = param.get_elem(i);\n"
Elemer Lelik's avatar
Elemer Lelik committed
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
    "        if ((curr->get_type()!=Module_Param::MP_NotUsed)) {\n"
    "          (*this)[start_idx+(int)i].set_param(*curr);\n"
    "        }\n"
    "      }\n"
    "    } break;\n"
    "    case Module_Param::MP_Indexed_List:\n"
    "      param.error(\"Cannot concatenate an indexed value list\");\n"
    "      break;\n"
    "    default:\n"
    "      param.type_error(\"%s value\", \"%s\");\n"
    "    }\n"
    "    break;\n"
    "  default:\n"
    "    TTCN_error(\"Internal error: Unknown operation type.\");\n"
    "  }\n"
Elemer Lelik's avatar
Elemer Lelik committed
665
    "}\n\n", name, sdef->kind == RECORD_OF ? "record of" : "set of",
Elemer Lelik's avatar
Elemer Lelik committed
666 667 668 669 670 671 672 673
    sdef->kind == RECORD_OF ? "record of" : "set of", dispname,
    sdef->kind == RECORD_OF ? "record of" : "set of", dispname);

  /* set implicit omit function, recursive */
  def = mputstr(def, "  void set_implicit_omit();\n");
  src = mputprintf(src,
    "void %s::set_implicit_omit()\n{\n"
    "if (val_ptr == NULL) return;\n"
Elemer Lelik's avatar
Elemer Lelik committed
674 675
    "for (int i = 0; i < val_ptr->n_elements; i++) {\n"
    "if (val_ptr->value_elements[i] != NULL) val_ptr->value_elements[i]->set_implicit_omit();\n"
Elemer Lelik's avatar
Elemer Lelik committed
676 677 678 679 680 681 682 683 684
    "}\n}\n\n", name);

  /* encoding / decoding functions */
  def = mputstr(def, "void encode_text(Text_Buf& text_buf) const;\n");
  src = mputprintf(src,
    "void %s::encode_text(Text_Buf& text_buf) const\n"
    "{\n"
    "if (val_ptr == NULL) "
    "TTCN_error(\"Text encoder: Encoding an unbound value of type %s.\");\n"
Elemer Lelik's avatar
Elemer Lelik committed
685 686
    "text_buf.push_int(val_ptr->n_elements);\n"
    "for (int elem_count = 0; elem_count < val_ptr->n_elements; "
Elemer Lelik's avatar
Elemer Lelik committed
687 688 689 690 691 692 693 694
    "elem_count++)\n"
    "(*this)[elem_count].encode_text(text_buf);\n"
    "}\n\n", name, dispname);

  def = mputstr(def, "void decode_text(Text_Buf& text_buf);\n");
  src = mputprintf(src,
    "void %s::decode_text(Text_Buf& text_buf)\n"
    "{\n"
Elemer Lelik's avatar
Elemer Lelik committed
695 696 697 698 699
    "clean_up();\n"
    "val_ptr = new recordof_setof_struct;\n"
    "val_ptr->ref_count = 1;\n"
    "val_ptr->n_elements = text_buf.pull_int().get_val();\n"
    "if (val_ptr->n_elements < 0) TTCN_error(\"Text decoder: Negative size "
Elemer Lelik's avatar
Elemer Lelik committed
700
    "was received for a value of type %s.\");\n"
Elemer Lelik's avatar
Elemer Lelik committed
701 702
    "val_ptr->value_elements = (%s**)allocate_pointers(val_ptr->n_elements);\n"
    "for (int elem_count = 0; elem_count < val_ptr->n_elements; "
Elemer Lelik's avatar
Elemer Lelik committed
703 704 705 706
    "elem_count++) {\n"
    "val_ptr->value_elements[elem_count] = new %s;\n"
    "val_ptr->value_elements[elem_count]->decode_text(text_buf);\n"
    "}\n"
Elemer Lelik's avatar
Elemer Lelik committed
707
    "}\n\n", name, dispname, type, type);
Elemer Lelik's avatar
Elemer Lelik committed
708

709 710
  if(ber_needed || raw_needed || text_needed || xer_needed || json_needed 
    || oer_needed) {
Elemer Lelik's avatar
Elemer Lelik committed
711
    def_encdec(name, &def, &src, ber_needed, raw_needed, text_needed,
712 713
               xer_needed, json_needed, oer_needed, FALSE);
  }
Elemer Lelik's avatar
Elemer Lelik committed
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732

  if(text_needed){
   src=mputprintf(src,
      "int %s::TEXT_encode(const TTCN_Typedescriptor_t& p_td,"
      " TTCN_Buffer& p_buf) const{\n"
      "  int encoded_length=0;\n"
      "  if(p_td.text->begin_encode){\n"
      "    p_buf.put_cs(*p_td.text->begin_encode);\n"
      "    encoded_length+=p_td.text->begin_encode->lengthof();\n"
      "  }\n"
      "  if(val_ptr==NULL) {\n"
      "    TTCN_EncDec_ErrorContext::error\n"
      "      (TTCN_EncDec::ET_UNBOUND, \"Encoding an unbound value.\");\n"
      "    if(p_td.text->end_encode){\n"
      "      p_buf.put_cs(*p_td.text->end_encode);\n"
      "      encoded_length+=p_td.text->end_encode->lengthof();\n"
      "    }\n"
      "    return encoded_length;\n"
      "  }\n"
Elemer Lelik's avatar
Elemer Lelik committed
733
      "  for(int a=0;a<val_ptr->n_elements;a++){\n"
Elemer Lelik's avatar
Elemer Lelik committed
734 735 736 737
      "   if(a!=0 && p_td.text->separator_encode){\n"
      "    p_buf.put_cs(*p_td.text->separator_encode);\n"
      "    encoded_length+=p_td.text->separator_encode->lengthof();\n"
      "   }\n"
Elemer Lelik's avatar
Elemer Lelik committed
738
      "   encoded_length+=(*this)[a].TEXT_encode(*p_td.oftype_descr,p_buf);\n"
Elemer Lelik's avatar
Elemer Lelik committed
739 740 741 742 743 744 745
      "  }\n"
      "  if(p_td.text->end_encode){\n"
      "    p_buf.put_cs(*p_td.text->end_encode);\n"
      "    encoded_length+=p_td.text->end_encode->lengthof();\n"
      "  }\n"
      "  return encoded_length;\n"
      "}\n"
Elemer Lelik's avatar
Elemer Lelik committed
746
      ,name
Elemer Lelik's avatar
Elemer Lelik committed
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
      );
    src = mputprintf(src,
      "int %s::TEXT_decode(const TTCN_Typedescriptor_t& p_td,"
      " TTCN_Buffer& p_buf, Limit_Token_List& limit, boolean no_err"
      ", boolean first_call){\n"
      "  int decoded_length=0;\n"
      "  size_t pos=p_buf.get_pos();\n"
      "  boolean sep_found=FALSE;\n"
      "  int sep_length=0;\n"
      "  int ml=0;\n"
      "  if(p_td.text->begin_decode){\n"
      "    int tl;\n"
      "    if((tl=p_td.text->begin_decode->match_begin(p_buf))<0){\n"
      "          if(no_err)return -1;\n"
      "          TTCN_EncDec_ErrorContext::error\n"
      "              (TTCN_EncDec::ET_TOKEN_ERR, \"The specified token '%%s'"
      " not found for '%%s': \",(const char*)*(p_td.text->begin_decode)"
      ", p_td.name);\n"
      "          return 0;\n"
      "        }\n"
      "    decoded_length+=tl;\n"
      "    p_buf.increase_pos(tl);\n"
      "  }\n"
      "  if(p_td.text->end_decode){\n"
      "    limit.add_token(p_td.text->end_decode);\n"
      "    ml++;\n"
      "  }\n"
      "  if(p_td.text->separator_decode){\n"
      "    limit.add_token(p_td.text->separator_decode);\n"
      "    ml++;\n"
      "  }\n"
      "  if(first_call) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
779 780 781 782 783
      "    clean_up();\n"
      "    val_ptr=new recordof_setof_struct;\n"
      "    val_ptr->ref_count=1;\n"
      "    val_ptr->n_elements=0;\n"
      "    val_ptr->value_elements=NULL;\n"
Elemer Lelik's avatar
Elemer Lelik committed
784
      "  }\n"
Elemer Lelik's avatar
Elemer Lelik committed
785
      "  int more=val_ptr->n_elements;\n"
Elemer Lelik's avatar
Elemer Lelik committed
786 787 788
      "  while(TRUE){\n"
      "    %s *val=new %s;\n"
      "    pos=p_buf.get_pos();\n"
Elemer Lelik's avatar
Elemer Lelik committed
789
      "    int len=val->TEXT_decode(*p_td.oftype_descr,p_buf,limit,TRUE);\n"
Elemer Lelik's avatar
Elemer Lelik committed
790 791 792 793 794 795 796 797 798 799
      "    if(len==-1 || (len==0 && !limit.has_token())){\n"
      "      p_buf.set_pos(pos);\n"
      "      delete val;\n"
      "      if(sep_found){\n"
      "        p_buf.set_pos(p_buf.get_pos()-sep_length);\n"
      "        decoded_length-=sep_length;\n"
      "      }\n"
      "      break;\n"
      "    }\n"
      "    sep_found=FALSE;\n"
Elemer Lelik's avatar
Elemer Lelik committed
800
      "    val_ptr->value_elements = (%s**)reallocate_pointers"
Elemer Lelik's avatar
Elemer Lelik committed
801 802
      "((void**)val_ptr->value_elements, val_ptr->n_elements, "
      "val_ptr->n_elements + 1);\n"
Elemer Lelik's avatar
Elemer Lelik committed
803 804
      "    val_ptr->value_elements[val_ptr->n_elements]=val;\n"
      "    val_ptr->n_elements++;\n"
Elemer Lelik's avatar
Elemer Lelik committed
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
      "    decoded_length+=len;\n"
      "    if(p_td.text->separator_decode){\n"
      "      int tl;\n"
      "      if((tl=p_td.text->separator_decode->match_begin(p_buf))<0){\n"
      "        break;\n"
      "      }\n"
      "      decoded_length+=tl;\n"
      "      p_buf.increase_pos(tl);\n"
      "      sep_length=tl;\n"
      "      sep_found=TRUE;\n"
      "    } else if(p_td.text->end_decode){\n"
      "      int tl;\n"
      "      if((tl=p_td.text->end_decode->match_begin(p_buf))!=-1){\n"
      "        decoded_length+=tl;\n"
      "        p_buf.increase_pos(tl);\n"
      "        limit.remove_tokens(ml);\n"
      "        return decoded_length;\n"
      "      }\n"
      "    } else if(limit.has_token(ml)){\n"
824
      "      if(limit.match(p_buf,ml)==0){\n"
Elemer Lelik's avatar
Elemer Lelik committed
825 826 827 828 829
      "        sep_found=FALSE;\n"
      "        break;\n"
      "      }\n"
      "    }\n"
      "  }\n"
Elemer Lelik's avatar
Elemer Lelik committed
830
      ,name,type,type,type
Elemer Lelik's avatar
Elemer Lelik committed
831 832 833 834 835 836
     );
    src = mputstr(src,
      "   limit.remove_tokens(ml);\n"
      "  if(p_td.text->end_decode){\n"
      "    int tl;\n"
      "    if((tl=p_td.text->end_decode->match_begin(p_buf))<0){\n"
837
      "          if(no_err){\n"
Elemer Lelik's avatar
Elemer Lelik committed
838
      "            if(!first_call){\n"
Elemer Lelik's avatar
Elemer Lelik committed
839 840 841
      "              for(int a=more; a<val_ptr->n_elements; a++) "
      "delete val_ptr->value_elements[a];\n"
      "              val_ptr->n_elements=more;\n"
Elemer Lelik's avatar
Elemer Lelik committed
842 843 844 845 846 847 848 849 850 851 852 853
      "            }\n"
      "            return -1;\n"
      "          }\n"
      "          TTCN_EncDec_ErrorContext::error"
      "(TTCN_EncDec::ET_TOKEN_ERR, \"The specified token '%s'"
      " not found for '%s': \",(const char*)*(p_td.text->end_decode)"
      ",p_td.name);\n"
      "          return decoded_length;\n"
      "        }\n"
      "    decoded_length+=tl;\n"
      "    p_buf.increase_pos(tl);\n"
      "  }\n"
Elemer Lelik's avatar
Elemer Lelik committed
854
      "  if(val_ptr->n_elements==0){\n"
Elemer Lelik's avatar
Elemer Lelik committed
855 856 857 858 859 860 861
      "    if(!(p_td.text->end_decode || p_td.text->begin_decode)) {\n"
      "      if(no_err)return -1;\n"
      "      TTCN_EncDec_ErrorContext::error"
      "(TTCN_EncDec::ET_TOKEN_ERR, \"No record/set of member found.\");\n"
      "      return decoded_length;\n"
      "    }\n"
      "  }\n"
Elemer Lelik's avatar
Elemer Lelik committed
862
      "  if(!first_call && more==val_ptr->n_elements && "
Elemer Lelik's avatar
Elemer Lelik committed
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
      "!(p_td.text->end_decode || p_td.text->begin_decode)) return -1;\n"
      "  return decoded_length;\n"
      "}\n"
     );
  }

  /* BER functions */
  if(ber_needed) {
    /* BER_encode_TLV() */
    src=mputprintf
      (src,
       "ASN_BER_TLV_t* %s::BER_encode_TLV(const TTCN_Typedescriptor_t&"
       " p_td, unsigned p_coding) const\n"
       "{\n"
       "  BER_chk_descr(p_td);\n"
       "  ASN_BER_TLV_t *new_tlv=BER_encode_chk_bound(is_bound());\n"
       "  if(!new_tlv) {\n"
       "    new_tlv=ASN_BER_TLV_t::construct(NULL);\n"
       "    TTCN_EncDec_ErrorContext ec;\n"
Elemer Lelik's avatar
Elemer Lelik committed
882
       "    for(int elem_i=0; elem_i<val_ptr->n_elements; elem_i++) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
883 884
       "      ec.set_msg(\"Component #%%d: \", elem_i);\n"
       "      new_tlv->add_TLV((*this)[elem_i].BER_encode_TLV"
Elemer Lelik's avatar
Elemer Lelik committed
885
       "(*p_td.oftype_descr, p_coding));\n"
Elemer Lelik's avatar
Elemer Lelik committed
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
       "    }\n"
       "%s"
       "  }\n"
       "  new_tlv=ASN_BER_V2TLV(new_tlv, p_td, p_coding);\n"
       "  return new_tlv;\n"
       "}\n"
       "\n"
       /* BER_decode_TLV() */
       "boolean %s::BER_decode_TLV(const TTCN_Typedescriptor_t& p_td,"
       " const ASN_BER_TLV_t& p_tlv, unsigned L_form)\n"
       "{\n"
       "  BER_chk_descr(p_td);\n"
       "  ASN_BER_TLV_t stripped_tlv;\n"
       "  BER_decode_strip_tags(*p_td.ber, p_tlv, L_form, stripped_tlv);\n"
       "  TTCN_EncDec_ErrorContext ec_0(\"While decoding '%%s' type: \","
       " p_td.name);\n"
       "  stripped_tlv.chk_constructed_flag(TRUE);\n"
Elemer Lelik's avatar
Elemer Lelik committed
903 904 905 906 907
       "  clean_up();\n"
       "  val_ptr = new recordof_setof_struct;\n"
       "  val_ptr->ref_count = 1;\n"
       "  val_ptr->n_elements = 0;\n"
       "  val_ptr->value_elements = NULL;\n"
Elemer Lelik's avatar
Elemer Lelik committed
908 909 910 911 912 913
       "  size_t V_pos=0;\n"
       "  ASN_BER_TLV_t tmp_tlv;\n"
       "  TTCN_EncDec_ErrorContext ec_1(\"Component #\");\n"
       "  TTCN_EncDec_ErrorContext ec_2(\"0: \");\n"
       "  while(BER_decode_constdTLV_next(stripped_tlv, V_pos, L_form, "
       "tmp_tlv)) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
914 915 916 917 918
       "    val_ptr->value_elements = (%s**)reallocate_pointers("
       "(void**)val_ptr->value_elements, val_ptr->n_elements, "
       "val_ptr->n_elements + 1);\n"
       "    val_ptr->n_elements++;\n"
       "    val_ptr->value_elements[val_ptr->n_elements - 1] = new %s;\n"
Elemer Lelik's avatar
Elemer Lelik committed
919
       "    val_ptr->value_elements[val_ptr->n_elements - 1]->BER_decode_TLV(*p_td.oftype_descr, tmp_tlv, "
Elemer Lelik's avatar
Elemer Lelik committed
920
       "L_form);\n"
Elemer Lelik's avatar
Elemer Lelik committed
921
       "    ec_2.set_msg(\"%%d: \", val_ptr->n_elements);\n"
Elemer Lelik's avatar
Elemer Lelik committed
922 923 924 925
       "  }\n"
       "  return TRUE;\n"
       "}\n"
       "\n"
Elemer Lelik's avatar
Elemer Lelik committed
926
       , name
Elemer Lelik's avatar
Elemer Lelik committed
927
       , sdef->kind==SET_OF?"    new_tlv->sort_tlvs();\n":""
Elemer Lelik's avatar
Elemer Lelik committed
928
       , name, type, type
Elemer Lelik's avatar
Elemer Lelik committed
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
       );

    if(sdef->has_opentypes) {
      /* BER_decode_opentypes() */
      def=mputstr
        (def,
         "void BER_decode_opentypes(TTCN_Type_list& p_typelist,"
         " unsigned L_form);\n");
      src=mputprintf
        (src,
         "void %s::BER_decode_opentypes(TTCN_Type_list& p_typelist,"
         " unsigned L_form)\n"
         "{\n"
         "  p_typelist.push(this);\n"
         "  TTCN_EncDec_ErrorContext ec_0(\"Component #\");\n"
         "  TTCN_EncDec_ErrorContext ec_1;\n"
Elemer Lelik's avatar
Elemer Lelik committed
945
         "  for(int elem_i=0; elem_i<val_ptr->n_elements; elem_i++) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
946
         "    ec_1.set_msg(\"%%d: \", elem_i);\n"
Elemer Lelik's avatar
Elemer Lelik committed
947
         "    val_ptr->value_elements[elem_i]->BER_decode_opentypes(p_typelist,"
Elemer Lelik's avatar
Elemer Lelik committed
948 949 950 951 952 953 954 955 956 957 958 959 960 961
         " L_form);\n"
         "  }\n"
         "  p_typelist.pop();\n"
         "}\n"
         "\n"
         , name
         );
    }
  }

  if(raw_needed){
      src=mputprintf(src,
    "int %s::RAW_decode(const TTCN_Typedescriptor_t& p_td, TTCN_Buffer& p_buf, "
    "int limit, raw_order_t top_bit_ord, boolean /*no_err*/, int sel_field"
962
    ", boolean first_call, const RAW_Force_Omit*){\n"
Elemer Lelik's avatar
Elemer Lelik committed
963 964 965 966 967 968
    "  int prepaddlength=p_buf.increase_pos_padd(p_td.raw->prepadding);\n"
    "  limit-=prepaddlength;\n"
    "  int decoded_length=0;\n"
    "  int decoded_field_length=0;\n"
    "  size_t start_of_field=0;\n"
    "  if(first_call) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
969 970 971 972 973
    "    clean_up();\n"
    "    val_ptr=new recordof_setof_struct;\n"
    "    val_ptr->ref_count=1;\n"
    "    val_ptr->n_elements=0;\n"
    "    val_ptr->value_elements=NULL;\n"
Elemer Lelik's avatar
Elemer Lelik committed
974
    "  }\n"
Elemer Lelik's avatar
Elemer Lelik committed
975
    "  int start_field=val_ptr->n_elements;\n"
Elemer Lelik's avatar
Elemer Lelik committed
976 977 978
    "  if(p_td.raw->fieldlength || sel_field!=-1){\n"
    "    int a=0;\n"
    "    if(sel_field==-1) sel_field=p_td.raw->fieldlength;\n"
979
    "    start_of_field=p_buf.get_pos_bit();\n"
Elemer Lelik's avatar
Elemer Lelik committed
980
    "    for(a=0;a<sel_field;a++){\n"
Elemer Lelik's avatar
Elemer Lelik committed
981
    "      decoded_field_length=(*this)[a+start_field].RAW_decode(*p_td.oftype_descr,"
Elemer Lelik's avatar
Elemer Lelik committed
982
    "p_buf,limit,top_bit_ord,TRUE);\n"
983 984 985 986 987 988 989 990 991
    "      if(decoded_field_length < 0){\n"
    "        while(a>=0){\n"
    "          delete &(*this)[a+start_field];\n"
    "          a--;\n"
    "          val_ptr->n_elements--;\n"
    "        }\n"
    "        p_buf.set_pos_bit(start_of_field);\n"
    "        return decoded_field_length;\n"
    "      }\n"
Elemer Lelik's avatar
Elemer Lelik committed
992 993 994
    "      decoded_length+=decoded_field_length;\n"
    "      limit-=decoded_field_length;\n"
    "    }\n"
Elemer Lelik's avatar
Elemer Lelik committed
995
    "    if(a==0) val_ptr->n_elements=0;\n"
Elemer Lelik's avatar
Elemer Lelik committed
996 997 998
    "  } else {\n"
    "    if(limit==0){\n"
    "      if(!first_call) return -1;\n"
Elemer Lelik's avatar
Elemer Lelik committed
999
    "      val_ptr->n_elements=0;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1000 1001 1002
    "      return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)"
    "+prepaddlength;\n"
    "    }\n"
1003
    "    int a=start_field;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1004 1005
    "    while(limit>0){\n"
    "      start_of_field=p_buf.get_pos_bit();\n"
Elemer Lelik's avatar
Elemer Lelik committed
1006
    "      decoded_field_length=(*this)[a].RAW_decode(*p_td.oftype_descr,p_buf,limit,"
Elemer Lelik's avatar
Elemer Lelik committed
1007 1008
    "top_bit_ord,TRUE);\n"
    "      if(decoded_field_length < 0){\n"
Elemer Lelik's avatar
Elemer Lelik committed
1009 1010
    "        delete &(*this)[a];\n"
    "        val_ptr->n_elements--;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1011 1012 1013 1014 1015 1016 1017 1018 1019
    "        p_buf.set_pos_bit(start_of_field);\n"
    "        if(a>start_field){\n"
    "        return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)"
    "+prepaddlength;\n"
    "        } else return -1;\n"
    "      }\n"
    "      decoded_length+=decoded_field_length;\n"
    "      limit-=decoded_field_length;\n"
    "      a++;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1020
    ,name
Elemer Lelik's avatar
Elemer Lelik committed
1021
    );
Elemer Lelik's avatar
Elemer Lelik committed
1022
    if (force_gen_seof || (sdef->raw.extension_bit!=XDEFNO && sdef->raw.extension_bit!=XDEFDEFAULT)){
Elemer Lelik's avatar
Elemer Lelik committed
1023
      src=mputprintf(src,
Elemer Lelik's avatar
Elemer Lelik committed
1024
    "      if (%s%sp_buf.get_last_bit()%s)\n"
Elemer Lelik's avatar
Elemer Lelik committed
1025
    "        return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)"
Elemer Lelik's avatar
Elemer Lelik committed
1026 1027 1028 1029
    "+prepaddlength;\n"
    , force_gen_seof ? "EXT_BIT_NO != p_td.raw->extension_bit && ((EXT_BIT_YES != p_td.raw->extension_bit) ^ " : ""
    , (force_gen_seof || sdef->raw.extension_bit == XDEFYES) ? "" : "!"
    , force_gen_seof ? ")" : "");
Elemer Lelik's avatar
Elemer Lelik committed
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
    }
      src=mputprintf(src,
    "    }\n"
    "  }\n"
    " return decoded_length+p_buf.increase_pos_padd(p_td.raw->padding)"
    "+prepaddlength;\n"
    "}\n\n"
    "int %s::RAW_encode(const TTCN_Typedescriptor_t& p_td,"
    "RAW_enc_tree& myleaf) const{\n"
    "  int encoded_length=0;\n"
    "  int encoded_num_of_records=p_td.raw->fieldlength?"
Elemer Lelik's avatar
Elemer Lelik committed
1041 1042
    "smaller(val_ptr->n_elements, p_td.raw->fieldlength)"
    ":val_ptr->n_elements;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1043 1044 1045 1046 1047 1048
    "  myleaf.isleaf=FALSE;\n"
    "  myleaf.rec_of=TRUE;\n"
    "  myleaf.body.node.num_of_nodes=encoded_num_of_records;\n"
    "  myleaf.body.node.nodes=init_nodes_of_enc_tree(encoded_num_of_records);\n"
    "  for(int a=0;a<encoded_num_of_records;a++){\n"
    "    myleaf.body.node.nodes[a]=new RAW_enc_tree(TRUE,&myleaf,"
Elemer Lelik's avatar
Elemer Lelik committed
1049 1050
    "&(myleaf.curr_pos),a,p_td.oftype_descr->raw);\n"
    "    encoded_length+=(*this)[a].RAW_encode(*p_td.oftype_descr,"
Elemer Lelik's avatar
Elemer Lelik committed
1051 1052 1053
    "*myleaf.body.node.nodes[a]);\n"
    "  }\n"
    " return myleaf.length=encoded_length;\n}\n\n"
Elemer Lelik's avatar
Elemer Lelik committed
1054
    , name
Elemer Lelik's avatar
Elemer Lelik committed
1055 1056 1057 1058 1059
    );
  }

  if (xer_needed) { /* XERSTUFF encoder codegen for record-of, RT1 */
    def = mputstr(def,
1060
      "char **collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns, unsigned int flavor = 0) const;\n");
Elemer Lelik's avatar
Elemer Lelik committed
1061 1062 1063 1064 1065

    /* Write the body of the XER encoder/decoder functions. The declaration
     * is written by def_encdec() in encdec.c */
    src = mputprintf(src,
      "boolean %s::can_start(const char *name, const char *uri, "
1066
      "XERdescriptor_t const& xd, unsigned int flavor, unsigned int flavor2) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
1067
      "  boolean e_xer = is_exer(flavor);\n"
Elemer Lelik's avatar
Elemer Lelik committed
1068 1069 1070
      "  if ((!e_xer || !(xd.xer_bits & UNTAGGED)) && !(flavor & XER_RECOF)) return "
      "check_name(name, xd, e_xer) && (!e_xer || check_namespace(uri, xd));\n"
      "  if (e_xer && (xd.oftype_descr->xer_bits & ANY_ELEMENT)) "
Elemer Lelik's avatar
Elemer Lelik committed
1071 1072
      , name
      );
Elemer Lelik's avatar
Elemer Lelik committed
1073
    if (!force_gen_seof && sdef->nFollowers) {
Elemer Lelik's avatar
Elemer Lelik committed
1074 1075 1076 1077 1078 1079 1080
      /* If there are optional fields following the record-of, then seeing
       * {any XML tag that belongs to those fields} where the record-of may be
       * means that the record-of is empty. */
      size_t f;
      src = mputstr(src, "{\n");
      for (f = 0; f < sdef->nFollowers; ++f) {
        src = mputprintf(src,
1081
          "    if (%s::can_start(name, uri, %s_xer_, flavor, flavor2)) return FALSE;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
          , sdef->followers[f].type
          , sdef->followers[f].typegen
          );
      }
      src = mputstr(src,
        "    return TRUE;\n"
        "  }\n");
    }
    else src = mputstr(src, "return TRUE;\n");

    src = mputprintf(src,
1093
      "  return %s::can_start(name, uri, *xd.oftype_descr, flavor | XER_RECOF, flavor2);\n"
Elemer Lelik's avatar
Elemer Lelik committed
1094 1095 1096 1097 1098
      "}\n\n"
      , sdef->type
      );

    src = mputprintf(src,
BenceJanosSzabo's avatar
BenceJanosSzabo committed
1099
      "char ** %s::collect_ns(const XERdescriptor_t& p_td, size_t& num, bool& def_ns, unsigned int flavor) const {\n"
Elemer Lelik's avatar
Elemer Lelik committed
1100
      "  size_t num_collected;\n"
BenceJanosSzabo's avatar
BenceJanosSzabo committed
1101
      "  char **collected_ns = Base_Type::collect_ns(p_td, num_collected, def_ns, flavor);\n"
Elemer Lelik's avatar
Elemer Lelik committed
1102 1103 1104 1105
      /* The above may throw but then nothing was allocated. */
      "  if (val_ptr) try {\n"
      "    char **new_ns;\n"
      "    size_t num_new;\n"
Elemer Lelik's avatar
Elemer Lelik committed
1106
      "    for (int i = 0; i < val_ptr->n_elements; ++i) {\n"
1107
      "      boolean def_ns_1 = FALSE;\n"
BenceJanosSzabo's avatar
BenceJanosSzabo committed
1108
      "      new_ns = (*this)[i].collect_ns(*p_td.oftype_descr, num_new, def_ns_1, flavor);\n"
Elemer Lelik's avatar
Elemer Lelik committed
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
      "      merge_ns(collected_ns, num_collected, new_ns, num_new);\n"
      "      def_ns = def_ns || def_ns_1;\n" /* alas, no ||= */
      "    }\n"
      "  }\n"
      "  catch (...) {\n"
      /* Probably a TC_Error thrown from elements[i]->collect_ns() if e.g.
       * encoding an unbound value. */
      "    while (num_collected > 0) Free(collected_ns[--num_collected]);\n"
      "    Free(collected_ns);\n"
      "    throw;\n"
      "  }\n"
      "  num = num_collected;\n"
      "  return collected_ns;\n"
      "}\n\n"
Elemer Lelik's avatar
Elemer Lelik committed
1123
      , name);
Elemer Lelik's avatar
Elemer Lelik committed
1124 1125

    src=mputprintf(src,
Elemer Lelik's avatar
Elemer Lelik committed
1126
      "int %s::XER_encode(const XERdescriptor_t& p_td, TTCN_Buffer& p_buf, "
1127
      "unsigned int p_flavor, unsigned int p_flavor2, int p_indent, embed_values_enc_struct_t* emb_val) const\n{\n"
Elemer Lelik's avatar
Elemer Lelik committed
1128 1129 1130 1131 1132 1133 1134
      "  if (val_ptr == 0) TTCN_error(\"Attempt to XER-encode an unbound record of\");\n" /* TODO type name */
      "  int encoded_length=(int)p_buf.get_len();\n"
      "  boolean e_xer = is_exer(p_flavor);\n"
      "  boolean own_tag = !(e_xer && p_indent && ((p_td.xer_bits & (ANY_ELEMENT|ANY_ATTRIBUTES|UNTAGGED))\n"
      "    || (p_flavor & USE_TYPE_ATTR)));\n"
      "  boolean indenting = !is_canonical(p_flavor) && own_tag;\n"
      "%s" /* Factor out p_indent if not attribute */
Elemer Lelik's avatar
Elemer Lelik committed
1135
      "  if (val_ptr->n_elements==0) {\n" /* Empty record of */
Elemer Lelik's avatar
Elemer Lelik committed
1136
      , name
Elemer Lelik's avatar
Elemer Lelik committed
1137 1138
      , force_gen_seof ? "  if (indenting && !(p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n"
      : (sdef->xerAttribute ? "" : "  if (indenting) do_indent(p_buf, p_indent);\n")
Elemer Lelik's avatar
Elemer Lelik committed
1139
      );
Elemer Lelik's avatar
Elemer Lelik committed
1140 1141 1142
    if (force_gen_seof || sdef->xerAttribute) {
      src=mputprintf(src,
        "    if (e_xer%s) {\n" /* Empty attribute. */
Elemer Lelik's avatar
Elemer Lelik committed
1143 1144
        "      begin_attribute(p_td, p_buf);\n"
        "      p_buf.put_c('\\'');\n"
Elemer Lelik's avatar
Elemer Lelik committed
1145 1146
        "    } else\n"
        , force_gen_seof ? " && (p_td.xer_bits & XER_ATTRIBUTE)" : "");
Elemer Lelik's avatar
Elemer Lelik committed
1147
    }
Elemer Lelik's avatar
Elemer Lelik committed
1148
    if (force_gen_seof || !sdef->xerAttribute) {
Elemer Lelik's avatar
Elemer Lelik committed
1149 1150 1151 1152
      src = mputstr(src,
        "    if (own_tag)");
    }
    src=mputprintf(src,
1153
      " {\n" /* Do empty element tag */
Elemer Lelik's avatar
Elemer Lelik committed
1154 1155 1156 1157 1158 1159 1160 1161 1162
      "%s"
      "      p_buf.put_c('<');\n"
      "      if (e_xer) write_ns_prefix(p_td, p_buf);\n"
      "      p_buf.put_s((size_t)p_td.namelens[e_xer]-2, (const unsigned char*)p_td.names[e_xer]);\n"
      /* namespace declarations for the toplevel element */
      "      if (e_xer && p_indent==0)\n"
      "      {\n"
      "        size_t num_collected = 0;\n"
      "        char **collected_ns = NULL;\n"
1163
      "        boolean def_ns = FALSE;\n"
BenceJanosSzabo's avatar
BenceJanosSzabo committed
1164
      "        collected_ns = collect_ns(p_td, num_collected, def_ns, p_flavor2);\n"
Elemer Lelik's avatar
Elemer Lelik committed
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
      "        for (size_t cur_coll = 0; cur_coll < num_collected; ++cur_coll) {\n"
      "          p_buf.put_s(strlen(collected_ns[cur_coll]), (cbyte*)collected_ns[cur_coll]);\n"
      "          Free(collected_ns[cur_coll]);\n"
      "        }\n"
      "        Free(collected_ns);\n"
      "      }\n"

      "      p_buf.put_s(2 + indenting, (const unsigned char*)\"/>\\n\");\n"
      "    }\n"
      "  }\n"
      "  else {\n" /* Not empty record of. Start tag or attribute */
Elemer Lelik's avatar
Elemer Lelik committed
1176 1177
      , force_gen_seof ? "      if (indenting && (p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n"
      : (sdef->xerAttribute ? "      if (indenting) do_indent(p_buf, p_indent);\n" : "")
Elemer Lelik's avatar
Elemer Lelik committed
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
      );
    if (sdef->xerAnyAttrElem) {
      src = mputstr(src,
        "    if (e_xer && (p_td.xer_bits & ANY_ATTRIBUTES)) {\n"
        "      static const universal_char sp = { 0,0,0,' ' };\n"
        "      static const universal_char tb = { 0,0,0,9 };\n"
        "      size_t buf_len = p_buf.get_len(), shorter = 0;\n"
        "      const unsigned char * const buf_data = p_buf.get_data();\n"
        "      if (buf_data[buf_len - 1 - shorter] == '\\n') ++shorter;\n"
        "      if (buf_data[buf_len - 1 - shorter] == '>' ) ++shorter;\n"
        "      unsigned char saved[4];\n"
        "      memcpy(saved, buf_data + (buf_len - shorter), shorter);\n"
        "      p_buf.increase_length(-shorter);\n"
Elemer Lelik's avatar
Elemer Lelik committed
1191
        "      for (int i = 0; i < val_ptr->n_elements; ++i) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
1192
        "        TTCN_EncDec_ErrorContext ec_0(\"Attribute %d: \", i);\n"
Elemer Lelik's avatar
Elemer Lelik committed
1193
        "        if (val_ptr->value_elements[i] == NULL) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
        "          TTCN_EncDec_ErrorContext::error(TTCN_EncDec::ET_UNBOUND,\n"
        "            \"Encoding an unbound universal charstring value.\");\n"
        "          continue;\n"
        "        }\n"
        "        size_t len = val_ptr->value_elements[i]->lengthof();\n"
        "        for (;;) {\n"
        "          const UNIVERSAL_CHARSTRING_ELEMENT& ue = (*val_ptr->value_elements[i])[len - 1];\n"
        "          if (sp == ue || tb == ue) --len;\n"
        "          else break;\n"
        "        }\n"
        "        size_t j, sp_at = 0;\n"
        /* Find the "separators" in each string */
        "        for (j = 0; j < len; j++) {\n"
        "          UNIVERSAL_CHARSTRING_ELEMENT ue = (*val_ptr->value_elements[i])[j];\n"
        "          if (sp_at) {\n"
        "            if (sp == ue || tb == ue) {}\n"
        "            else break;\n"
        "          } else {\n"
        "            if (sp == ue || tb == ue) sp_at = j;\n"
        "          }\n"
        "        } // next j\n"
        "        size_t buf_start = p_buf.get_len();\n"
        /* Now write them */
        "        if (sp_at > 0) {\n"
        "          char * ns = mprintf(\" xmlns:b%d='\", i);\n"
        "          size_t ns_len = mstrlen(ns);\n"
        "          p_buf.put_s(ns_len, (const unsigned char*)ns);\n"

        "          UNIVERSAL_CHARSTRING before(sp_at, (const universal_char*)(*val_ptr->value_elements[i]));\n"
1223
        "          before.XER_encode(UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | ANY_ATTRIBUTES, p_flavor2, p_indent, 0);\n"
1224 1225 1226 1227 1228 1229 1230 1231 1232

        "          p_buf.put_c('\\'');\n"
        "          p_buf.put_c(' ');\n"

        /* Keep just the "b%d" part from ns */
        "          p_buf.put_s(ns_len - 9, (const unsigned char*)ns + 7);\n"
        "          p_buf.put_c(':');\n"
        "          Free(ns);\n"

Elemer Lelik's avatar
Elemer Lelik committed
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
        // Ensure the namespace abides to its restrictions
        "          if (p_td.xer_bits & (ANY_FROM | ANY_EXCEPT)) {\n"
        "            TTCN_Buffer ns_buf;\n"
        "            before.encode_utf8(ns_buf);\n"
        "            CHARSTRING cs;\n"
        "            ns_buf.get_string(cs);\n"
        "            check_namespace_restrictions(p_td, (const char*)cs);\n"
        "          }\n"

        "        }\n"
        "        else {\n"
        "          p_buf.put_c(' ');\n"
        "          j = 0;\n"
        // Make sure the unqualified namespace is allowed
        "          if (p_td.xer_bits & (ANY_FROM | ANY_EXCEPT)) {\n"
        "            check_namespace_restrictions(p_td, NULL);\n"
        "          }\n"
        "        }\n"

        "        UNIVERSAL_CHARSTRING after(len - j, (const universal_char*)(*val_ptr->value_elements[i]) + j);\n"
1253
        "        after.XER_encode(UNIVERSAL_CHARSTRING_xer_, p_buf, p_flavor | ANY_ATTRIBUTES, p_flavor2, p_indent, 0);\n"
Elemer Lelik's avatar
Elemer Lelik committed
1254 1255
        // Put this attribute in a dummy element and walk through it to check its validity
        "        TTCN_Buffer check_buf;\n"
1256
        "        check_buf.put_s(2, (const unsigned char*)\"<a\");\n"
Elemer Lelik's avatar
Elemer Lelik committed
1257
        "        check_buf.put_s(p_buf.get_len() - buf_start, p_buf.get_data() + buf_start);\n"
1258
        "        check_buf.put_s(2, (const unsigned char*)\"/>\");"
Elemer Lelik's avatar
Elemer Lelik committed
1259 1260 1261 1262 1263 1264 1265
        "        XmlReaderWrap checker(check_buf);\n"
        "        while (1 == checker.Read()) ;\n"
        "      }\n"

        "      p_buf.put_s(shorter, saved);\n" /* restore the '>' and anything after */
        "    } else {\n");
    }
Elemer Lelik's avatar
Elemer Lelik committed
1266 1267 1268
    if (force_gen_seof || sdef->xerAttribute) {
      src=mputprintf(src,
        "    if (e_xer%s) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
1269
        "      begin_attribute(p_td, p_buf);\n"
Elemer Lelik's avatar
Elemer Lelik committed
1270 1271
        "    } else\n"
        , force_gen_seof ? " && (p_td.xer_bits & XER_ATTRIBUTE)" : "");
Elemer Lelik's avatar
Elemer Lelik committed
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
    }
    src=mputprintf(src,
      "    if (own_tag) {\n"
      "%s"
      "      p_buf.put_c('<');\n"
      "      boolean write_ns = (e_xer && p_indent==0);\n"
      "      boolean keep_newline = (indenting && !(e_xer && (p_td.xer_bits & XER_LIST)));\n"
      "      if (e_xer) write_ns_prefix(p_td, p_buf);\n"
      "      p_buf.put_s((size_t)p_td.namelens[e_xer]-write_ns-(write_ns || !keep_newline), "
      "(const unsigned char*)p_td.names[e_xer]);\n"

      /* namespace declarations for the toplevel element */
      "      if (e_xer && p_indent==0)\n"
      "      {\n"
      "        size_t num_collected = 0;\n"
      "        char **collected_ns = NULL;\n"
1288
      "        boolean def_ns = FALSE;\n"
BenceJanosSzabo's avatar
BenceJanosSzabo committed
1289
      "        collected_ns = collect_ns(p_td, num_collected, def_ns, p_flavor2);\n"
Elemer Lelik's avatar
Elemer Lelik committed
1290 1291 1292 1293 1294 1295 1296
      "        for (size_t cur_coll = 0; cur_coll < num_collected; ++cur_coll) {\n"
      "          p_buf.put_s(strlen(collected_ns[cur_coll]), (cbyte*)collected_ns[cur_coll]);\n"
      "          Free(collected_ns[cur_coll]);\n"
      "        }\n"
      "        Free(collected_ns);\n"
      "        p_buf.put_s(1 + keep_newline, (cbyte*)\">\\n\");\n"
      "      }\n"
Elemer Lelik's avatar
Elemer Lelik committed
1297 1298
      , force_gen_seof ? "      if (indenting && (p_td.xer_bits & XER_ATTRIBUTE)) do_indent(p_buf, p_indent);\n"
      : (sdef->xerAttribute ? "      if (indenting) do_indent(p_buf, p_indent);\n" : "")
Elemer Lelik's avatar
Elemer Lelik committed
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
      );
    if (sdef->xmlValueList) {
      src=mputstr(src, "      if (indenting && !e_xer) do_indent(p_buf, p_indent+1);\n"); /* !e_xer or GDMO */
    }
    src=mputstr(src,
      "    }\n"
      "    p_flavor |= XER_RECOF | (p_td.xer_bits & XER_LIST);\n"
      "    TTCN_EncDec_ErrorContext ec_0(\"Index \");\n"
      "    TTCN_EncDec_ErrorContext ec_1;\n"
      );
Elemer Lelik's avatar
Elemer Lelik committed
1309
    src=mputstr(src,
Elemer Lelik's avatar
Elemer Lelik committed
1310
      "    for (int i = 0; i < val_ptr->n_elements; ++i) {\n"
Elemer Lelik's avatar
Elemer Lelik committed
1311 1312 1313 1314