blob: 1d587b9a78ed4faf9147e78b486c85f37b30dd7f (
plain)
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
|
/* SPDX-License-Identifier: Unlicense
*/
#include "chardev.hpp"
#include <cstring>
#include <cstdlib>
static bool startsWith(const char* str, const char* pattern)
{
const size_t pattern_len = strlen(pattern);
const size_t str_len = strlen(str);
if (str_len < pattern_len) {
return false;
}
return 0 == memcmp(str, pattern, pattern_len);
}
static CharDev parseTcp(const char* path)
{
// We need to make sure there is at least one more ':' delimiter in the
// string
const char* rest = strchr(path + (sizeof "tcp:") - 1, ':');
if (rest == nullptr) {
return CharDev::Invalid(path);
}
const char* port_str;
// Find last ':' before string ends, i.e. when strchr returns NULL
do {
port_str = rest + 1;
rest = strchr(port_str, ':');
} while (rest);
const unsigned long port = strtoul(port_str, nullptr, 0);
if (port <= UINT16_MAX) {
path += (sizeof "tcp:") - 1;
return CharDev::Tcp(path, port_str - 1 - path, port);
}
return CharDev::Invalid(path);
}
CharDev CharDev::Parse(const char* path)
{
if (0 == strcmp(path, "pty")) {
return CharDev::Pty();
}
if (startsWith(path, "tcp:")) {
return parseTcp(path);
}
return CharDev::Invalid(path);
}
|