Sync with 5.4.2
[deliverable/titan.core.git] / regression_test / customEncoding / Coders.cc
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 ******************************************************************************/
8
9 #include "Coders.hh"
10
11 using Custom1::c__separator;
12
13 namespace Custom2 {
14
15 BITSTRING f__enc__rec(const Custom3::Rec& x)
16 {
17 return int2bit(x.num(), 8);
18 }
19
20 INTEGER f__dec__rec(BITSTRING& b, Custom3::Rec& x)
21 {
22 x.num() = bit2int(b);
23 x.str() = "c++";
24 return 0;
25 }
26
27 BITSTRING f__enc__uni(const Custom1::Uni& x)
28 {
29 if (x.get_selection() == Custom1::Uni::ALT_i) {
30 return c__separator + int2bit(x.i(), 8) + c__separator;
31 }
32 else {
33 return c__separator + c__separator;
34 }
35 }
36
37 INTEGER f__dec__uni(BITSTRING& b, Custom1::Uni& x)
38 {
39 int b_len = b.lengthof();
40 int sep_len = c__separator.lengthof();
41 if (b_len >= 2 * sep_len &&
42 substr(b, 0, sep_len) == c__separator &&
43 substr(b, b_len - sep_len, sep_len) == c__separator) {
44 if (b_len > 2 * sep_len) {
45 x.i() = bit2int(substr(b, sep_len, b_len - 2 * sep_len));
46 }
47 return 0;
48 }
49 else {
50 return 1;
51 }
52 }
53
54 } // namespace Custom2
55
56 namespace Custom1 {
57
58 BITSTRING f__enc__recof(const RecOf& x)
59 {
60 BITSTRING res = x[0];
61 for (int i = 1; i < x.size_of(); ++i) {
62 res = res + c__separator + x[i];
63 }
64 return res;
65 }
66
67 int find_bitstring(const BITSTRING& src, int start, const BITSTRING& fnd)
68 {
69 int len = fnd.lengthof();
70 for (int i = start; i <= src.lengthof() - len; ++i) {
71 if (substr(src, i, len) == fnd) {
72 return i;
73 }
74 }
75 return -1;
76 }
77
78 INTEGER f__dec__recof(BITSTRING& b, RecOf& x)
79 {
80 int start = 0;
81 int end = find_bitstring(b, start, c__separator);
82 int index = 0;
83 while(end != -1) {
84 x[index] = substr(b, start, end - start);
85 ++index;
86 start = end + c__separator.lengthof();
87 end = find_bitstring(b, start, c__separator);
88 }
89 x[index] = substr(b, start, b.lengthof() - start);
90 return 0;
91 }
92
93 } // namespace Custom1
This page took 0.032164 seconds and 5 git commands to generate.