Welcome to mirror list, hosted at ThFree Co, Russian Federation.

HexFile.cpp « Utils « slic3r « src « xs - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 282c647bdc6dfc041501ea90dd4a652be3253a63 (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
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
#include "HexFile.hpp"

#include <sstream>
#include <boost/filesystem/fstream.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>

namespace fs = boost::filesystem;
namespace pt = boost::property_tree;


namespace Slic3r {
namespace Utils {


static HexFile::DeviceKind parse_device_kind(const std::string &str)
{
	     if (str == "mk2") { return HexFile::DEV_MK2; }
	else if (str == "mk3") { return HexFile::DEV_MK3; }
	else if (str == "mm-control") { return HexFile::DEV_MM_CONTROL; }
	else { return HexFile::DEV_GENERIC; }
}

static size_t hex_num_sections(fs::ifstream &file)
{
	file.seekg(0);
	if (! file.good()) {
		return 0;
	}

	static const char *hex_terminator = ":00000001FF\r";
	size_t res = 0;
	std::string line;
	while (getline(file, line, '\n').good()) {
		// Account for LF vs CRLF
		if (!line.empty() && line.back() != '\r') {
			line.push_back('\r');
		}

		if (line == hex_terminator) {
			res++;
		}
	}

	return res;
}

HexFile::HexFile(fs::path path) :
	path(std::move(path))
{
	fs::ifstream file(this->path);
	if (! file.good()) {
		return;
	}

	std::string line;
	std::stringstream header_ini;
	while (std::getline(file, line, '\n').good()) {
		if (line.empty()) {
			continue;
		}

		// Account for LF vs CRLF
		if (!line.empty() && line.back() == '\r') {
			line.pop_back();
		}

		if (line.front() == ';') {
			line.front() = ' ';
			header_ini << line << std::endl;
		} else if (line.front() == ':') {
			break;
		}
	}

	pt::ptree ptree;
	try {
		pt::read_ini(header_ini, ptree);
	} catch (std::exception &e) {
		return;
	}

	bool has_device_meta = false;
	const auto device = ptree.find("device");
	if (device != ptree.not_found()) {
		this->device = parse_device_kind(device->second.data());
		has_device_meta = true;
	}

	const auto model_id = ptree.find("model_id");
	if (model_id != ptree.not_found()) {
		this->model_id = model_id->second.data();
	}

	if (! has_device_meta) {
		// No device metadata, look at the number of 'sections'
		if (hex_num_sections(file) == 2) {
			// Looks like a pre-metadata l10n firmware for the MK3, assume that's the case
			this->device = DEV_MK3;
		}
	}
}


}
}