The Conley-Morse Graphs Software
datatext.h
Go to the documentation of this file.
1/////////////////////////////////////////////////////////////////////////////
2///
3/// @file datatext.h
4///
5/// Converting a binary data buffer into a printable text and vice versa.
6///
7/// @author Pawel Pilarczyk
8///
9/////////////////////////////////////////////////////////////////////////////
10
11// Copyright (C) 1997-2014 by Pawel Pilarczyk.
12//
13// This file is part of my research software package. This is free software:
14// you can redistribute it and/or modify it under the terms of the GNU
15// General Public License as published by the Free Software Foundation,
16// either version 3 of the License, or (at your option) any later version.
17//
18// This software is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with this software; see the file "license.txt". If not,
25// please, see <https://www.gnu.org/licenses/>.
26
27// Started on November 12, 2013. Last revision: November 12, 2013.
28
29
30#ifndef _CMGRAPHS_DATATEXT_H_
31#define _CMGRAPHS_DATATEXT_H_
32
33
34// include some standard C++ header files
35#include <string>
36
37// include all the necessary header files from the CHomP library
38#include "chomp/system/config.h"
39#include "chomp/multiwork/mwdata.h"
40
41
42// --------------------------------------------------
43// ---------- binary data - printable text ----------
44// --------------------------------------------------
45
46/// Encodes the entire binary data into a text string (single line,
47/// printable characters only).
48inline std::string data2text (const chomp::multiwork::mwData &data)
49{
50 const char *buffer (data. Buffer ());
51 int length (data. Length ());
52 std::string str;
53 str. resize (8 + 2 * length);
54
55 // encode the length of the buffer
56 for (int i = 0; i < 4; ++ i)
57 {
58 int byte ((length & (0xFF << (i << 3))) >> (i << 3));
59 str [i << 1] = 'A' + (byte & 0x0F);
60 str [(i << 1) + 1] = 'G' + ((byte >> 4) & 0x0F);
61 }
62
63 // encode the buffer
64 for (int i = 0; i < length; ++ i)
65 {
66 str [8 + (i << 1)] = 'a' + (buffer [i] & 0x0F);
67 str [9 + (i << 1)] = 'g' + (((buffer [i]) >> 4) & 0x0F);
68 }
69
70 return str;
71} /* data2text */
72
73/// Decodes a prevously prepared text back into binary data.
74/// Resets the data and replaces its contents with the decoded buffer.
75/// Throws an exception in case of errors.
76inline void text2data (const std::string &str,
77 chomp::multiwork::mwData &data)
78{
79 if (str. size () < 8)
80 throw "Trying to decode a string that is too short.";
81
82 // decode the length of the data buffer
83 int length (0);
84 for (int i = 0; i < 4; ++ i)
85 {
86 int b1 = str [i << 1] - 'A';
87 int b2 = str [(i << 1) + 1] - 'G';
88 if ((b1 < 0) || (b1 > 0x0F) || (b2 < 0) || (b2 > 0x0F))
89 throw "Wrong length while decoding a string.";
90 length |= (b1 | (b2 << 4)) << (i << 3);
91 }
92 if (length < 0)
93 throw "Negative length while decoding a string.";
94
95 // reset the data to prepare it for a new binary buffer
96 data. Reset ();
97 if (!length)
98 return;
99
100 // allocate the data buffer if necessary
101 unsigned char *buffer = new unsigned char [length];
102 data. Take (buffer, length);
103
104 // decode the data buffer
105 for (int i = 0; i < length; ++ i)
106 {
107 int b1 = str [8 + (i << 1)] - 'a';
108 int b2 = str [9 + (i << 1)] - 'g';
109 if ((b1 < 0) || (b1 > 0x0F) || (b2 < 0) || (b2 > 0x0F))
110 throw "Wrong contents while decoding a string.";
111 buffer [i] = b1 | (b2 << 4);
112 }
113
114 return;
115} /* text2data */
116
117
118#endif // _CMGRAPHS_DATATEXT_H_
119
void text2data(const std::string &str, chomp::multiwork::mwData &data)
Decodes a prevously prepared text back into binary data.
Definition: datatext.h:76
std::string data2text(const chomp::multiwork::mwData &data)
Encodes the entire binary data into a text string (single line, printable characters only).
Definition: datatext.h:48