File: GprEdit.cpp

package info (click to toggle)
edb-debugger 1.3.0-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,124 kB
  • sloc: cpp: 46,241; xml: 4,998; ansic: 3,088; sh: 52; asm: 33; makefile: 5
file content (147 lines) | stat: -rw-r--r-- 5,019 bytes parent folder | download | duplicates (4)
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
136
137
138
139
140
141
142
143
144
145
146
147
/*
Copyright (C) 2015 Ruslan Kabatsayev <b7.10110111@gmail.com>

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, see <http://www.gnu.org/licenses/>.
*/

#include "GprEdit.h"
#include "QLongValidator.h"
#include "QULongValidator.h"
#include "util/Font.h"
#include <QApplication>
#include <QRegExpValidator>
#include <cmath>
#include <cstring>

namespace ODbgRegisterView {
namespace {

const QRegExpValidator byteHexValidator(QRegExp("[0-9a-fA-F]{0,2}"));
const QRegExpValidator wordHexValidator(QRegExp("[0-9a-fA-F]{0,4}"));
const QRegExpValidator dwordHexValidator(QRegExp("[0-9a-fA-F]{0,8}"));
const QRegExpValidator qwordHexValidator(QRegExp("[0-9a-fA-F]{0,16}"));
const QLongValidator byteSignedValidator(INT8_MIN, INT8_MAX);
const QLongValidator wordSignedValidator(INT16_MIN, INT16_MAX);
const QLongValidator dwordSignedValidator(INT32_MIN, INT32_MAX);
const QLongValidator qwordSignedValidator(INT64_MIN, INT64_MAX);
const QULongValidator byteUnsignedValidator(0, UINT8_MAX);
const QULongValidator wordUnsignedValidator(0, UINT16_MAX);
const QULongValidator dwordUnsignedValidator(0, UINT32_MAX);
const QULongValidator qwordUnsignedValidator(0, UINT64_MAX);

const std::map<int, const QRegExpValidator *> hexValidators = {
	{1, &byteHexValidator},
	{2, &wordHexValidator},
	{4, &dwordHexValidator},
	{8, &qwordHexValidator}};

const std::map<int, const QLongValidator *> signedValidators = {
	{1, &byteSignedValidator},
	{2, &wordSignedValidator},
	{4, &dwordSignedValidator},
	{8, &qwordSignedValidator}};

const std::map<int, const QULongValidator *> unsignedValidators = {
	{1, &byteUnsignedValidator},
	{2, &wordUnsignedValidator},
	{4, &dwordUnsignedValidator},
	{8, &qwordUnsignedValidator}};

}

void GprEdit::setupFormat(Format newFormat) {
	format_ = newFormat;
	switch (format_) {
	case Format::Hex:
		setValidator(hexValidators.at(integerSize_));
		naturalWidthInChars_ = 2 * integerSize_;
		break;
	case Format::Signed:
		setValidator(signedValidators.at(integerSize_));
		naturalWidthInChars_ = 1 + std::lround(integerSize_ * std::log10(256.));
		break;
	case Format::Unsigned:
		setValidator(unsignedValidators.at(integerSize_));
		naturalWidthInChars_ = std::lround(integerSize_ * std::log10(256.));
		break;
	case Format::Character:
		setMaxLength(1);
		break;
	default:
		Q_ASSERT("Unexpected format value" && 0);
	}
}

GprEdit::GprEdit(std::size_t offsetInInteger, std::size_t integerSize, Format format, QWidget *parent)
	: QLineEdit(parent), naturalWidthInChars_(2 * integerSize), integerSize_(integerSize), offsetInInteger_(offsetInInteger) {

	setupFormat(format);
}

void GprEdit::setGPRValue(std::uint64_t gprValue) {
	std::uint64_t value(0);
	signBit_ = format_ == Format::Signed ? 1ull << (8 * integerSize_ - 1) : 0;
	if ((gprValue >> 8 * offsetInInteger_) & signBit_)
		value = -1;
	std::memcpy(&value, reinterpret_cast<char *>(&gprValue) + offsetInInteger_, integerSize_);
	switch (format_) {
	case Format::Hex:
		setText(QString("%1").arg(value, naturalWidthInChars_, 16, QChar('0')));
		break;
	case Format::Signed:
		setText(QString("%1").arg(static_cast<std::int64_t>(value)));
		break;
	case Format::Unsigned:
		setText(QString("%1").arg(value));
		break;
	case Format::Character:
		setText(QChar(static_cast<char>(value)));
		break;
	}
}

void GprEdit::updateGPRValue(std::uint64_t &gpr) const {
	bool ok;
	std::uint64_t value;
	switch (format_) {
	case Format::Hex:
		value = text().toULongLong(&ok, 16);
		break;
	case Format::Signed:
		value = text().toLongLong(&ok);
		break;
	case Format::Unsigned:
		value = text().toULongLong(&ok);
		break;
	case Format::Character:
		value = text().toStdString()[0];
		break;
	default:
		Q_ASSERT("Unexpected format value" && 0);
	}
	std::memcpy(reinterpret_cast<char *>(&gpr) + offsetInInteger_, &value, integerSize_);
}

QSize GprEdit::sizeHint() const {

	const auto baseHint = QLineEdit::sizeHint();
	// taking long enough reference char to make enough room even in presence of inner shadows like in Oxygen style
	const auto charWidth       = Font::maxWidth(QFontMetrics(font()));
	const auto textMargins     = this->textMargins();
	const auto contentsMargins = this->contentsMargins();
	int customWidth            = charWidth * naturalWidthInChars_ + textMargins.left() + contentsMargins.left() + textMargins.right() + contentsMargins.right() + 1 * charWidth; // additional char to make edit field not too tight
	return QSize(customWidth, baseHint.height()).expandedTo(QApplication::globalStrut());
}

}