-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMessage.cpp
More file actions
135 lines (127 loc) · 2.62 KB
/
Message.cpp
File metadata and controls
135 lines (127 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
58
59
60
61
62
63
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
/*
* Copyright (c) 2025 by Thomas A. Early N7TAE
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include "Message.h"
void CMessage::Init()
{
ctl = 0;
memset(msg, 0, 53);
}
void CMessage::MakeBlocks(const std::string &msg, std::vector<SMeta> &blks)
{
blks.clear();
unsigned size = msg.size();
unsigned count = size / 13u;
if (count > 4u)
count = 4;
else if ((count < 4u) and (size % 13u))
count++;
if (0u == count)
return;
uint8_t ctrl;
switch (count)
{
case 1u:
ctrl = 0x1u;
break;
case 2u:
ctrl = 0x3u;
break;
case 3u:
ctrl = 0x7u;
break;
default:
ctrl = 0xfu;
break;
}
SMeta meta;
for (unsigned i=0; i<count; i++)
{
meta.data[0] = (0x1u << (4+i)) | ctrl;
for (unsigned j=0; j<13; j++)
{
auto o = 13u * i +j;
if (o < size)
meta.data[j+1] = uint8_t(msg.at(0));
else
break;
}
blks.push_back(meta);
}
}
bool CMessage::GetBlock(const uint8_t *data)
{
if (data[0] == 0u)
return false;
switch (data[0] >> 4)
{
case 0x1u:
case 0x3u:
case 0x7u:
case 0xfu:
switch(data[0] & 0xfu)
{
case 0x1u:
Clean(msg, data+1);
break;
case 0x2u:
Clean(msg+13, data+1);
break;
case 0x4u:
Clean(msg+26, data+1);
break;
case 0x8u:
Clean(msg+39, data+1);
break;
default:
Init();
return false;
}
ctl |= data[0];
break;
default:
Init();
return false;
}
if ((ctl & 0xfu) == (ctl >> 4))
{
Trim();
return true;
}
return false;
}
void CMessage::Clean(char *to, const uint8_t *from)
{
for (unsigned i=0; i<13u; i++)
{
auto c = char(from[i]);
if (not isgraph(c))
c = ' ';
to[i] = c;
}
}
void CMessage::Trim()
{
str.assign(msg);
str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](int ch) { return not std::isspace(ch); }));
str.erase(std::find_if(str.rbegin(), str.rend(), [](int ch) { return not std::isspace(ch); }).base(), str.end());
}
const std::string &CMessage::GetMessage()
{
return str;
}