Minsky
schemaHelper.cc
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2020
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "cairoItems.h"
21 #include "schemaHelper.h"
22 #include "selection.h"
23 #include "lasso.h"
24 #include "xml_common.h"
25 #include "minsky_epilogue.h"
26 #include "a85.h"
27 #include "zStream.h"
28 #include <zlib.h>
29 using namespace std;
30 using namespace classdesc;
31 
32 namespace minsky
33 {
34  classdesc::pack_t decode(const classdesc::CDATA& data)
35  {
36  string trimmed; //trim whitespace
37  for (auto c: data)
38  if (!isspace(c)) trimmed+=c;
39 
40  vector<unsigned char> zbuf(a85::size_for_bin(trimmed.size()));
41  // reverse transformation required to avoid the escape sequence ']]>'
42  replace(trimmed.begin(),trimmed.end(),'~',']');
43  a85::from_a85(trimmed.data(), trimmed.size(),zbuf.data());
44 
45  InflateZStream zs(zbuf);
46  zs.inflate();
47  return std::move(zs.output);
48  }
49 
50 
51  CDATA encode(const pack_t& buf)
52  {
53  vector<unsigned char> zbuf(buf.size());
54  DeflateZStream zs(buf, zbuf);
55  zs.deflate();
56 
57  vector<char> cbuf(a85::size_for_a85(zs.total_out,false));
58  a85::to_a85(zbuf.data(),zs.total_out, cbuf.data(), false);
59  // this ensures that the escape sequence ']]>' never appears in the data
60  replace(cbuf.begin(),cbuf.end(),']','~');
61  return CDATA(cbuf.begin(),cbuf.end());
62  }
63 }
int size_for_bin(int textlen)
Definition: a85.cc:52
classdesc::pack_t decode(const classdesc::CDATA &data)
decode ascii-encoded representation to binary data
Definition: schemaHelper.cc:34
void to_a85(const u8 *data, int binlen, char *text, bool append_null)
Definition: a85.cc:12
void from_a85(const char *text, int textlen, u8 *data)
Definition: a85.cc:56
STL namespace.
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
CDATA encode(const pack_t &buf)
Definition: schemaHelper.cc:51
int size_for_a85(int binlen, bool append_null)
Definition: a85.cc:8