Skip to content

Commit

Permalink
Support for not showing leading zeros on 64-bit addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
eteran committed Oct 31, 2020
1 parent bd9f419 commit ab87333
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
48 changes: 41 additions & 7 deletions qhexview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,23 @@ QString QHexView::formatAddress(address_t address) {
const uint32_t hi = (address >> 32) & 0xffffffff;
const uint32_t lo = (address & 0xffffffff);

if (showAddressSeparator_) {
qsnprintf(buffer, sizeof(buffer), "%08x:%08x", hi, lo);
// if we encounter a large enough address, just give up on hiding leading zeros
if (address >= 0x8000'0000'0000) {
hideLeadingAddressZeros_ = false;
}

if (hideLeadingAddressZeros_ && address < 0x8000'0000'0000) {
if (showAddressSeparator_) {
qsnprintf(buffer, sizeof(buffer), "%04x:%08x", hi, lo);
} else {
qsnprintf(buffer, sizeof(buffer), "%04x%08x", hi, lo);
}
} else {
qsnprintf(buffer, sizeof(buffer), "%08x%08x", hi, lo);
if (showAddressSeparator_) {
qsnprintf(buffer, sizeof(buffer), "%08x:%08x", hi, lo);
} else {
qsnprintf(buffer, sizeof(buffer), "%08x%08x", hi, lo);
}
}
}
return QString::fromLocal8Bit(buffer);
Expand All @@ -155,6 +168,23 @@ int64_t QHexView::dataSize() const {
return data_ ? data_->size() : 0;
}

/**
*
* @brief QHexView::setHideLeadingAddressZeros
* @param value
*/
void QHexView::setHideLeadingAddressZeros(bool value) {
hideLeadingAddressZeros_ = value;
}

/**
*
* @brief QHexView::hideLeadingAddressZeros
*/
bool QHexView::hideLeadingAddressZeros() const {
return hideLeadingAddressZeros_;
}

/**
* overloaded version of setFont, calculates font metrics for later
*
Expand Down Expand Up @@ -486,7 +516,7 @@ void QHexView::keyPressEvent(QKeyEvent *event) {
}

/**
* @brief QHexView::vertline3
* @brief QHexView::line3
* @return the x coordinate of the 3rd line
*/
int QHexView::line3() const {
Expand All @@ -499,7 +529,7 @@ int QHexView::line3() const {
}

/**
* @brief QHexView::vertline2
* @brief QHexView::line2
* @return the x coordinate of the 2nd line
*/
int QHexView::line2() const {
Expand All @@ -512,7 +542,7 @@ int QHexView::line2() const {
}

/**
* @brief QHexView::vertline1
* @brief QHexView::line1
* @return the x coordinate of the 1st line
*/
int QHexView::line1() const {
Expand Down Expand Up @@ -561,6 +591,10 @@ int QHexView::charsPerWord() const {
* @return the lenth in characters the address will take up
*/
int QHexView::addressLength() const {
if (hideLeadingAddressZeros_ && addressSize_ == Address64) {
const int addressLength = ((addressSize_ * CHAR_BIT) / 4) - 4;
return addressLength + (showAddressSeparator_ ? 1 : 0);
}
const int addressLength = (addressSize_ * CHAR_BIT) / 4;
return addressLength + (showAddressSeparator_ ? 1 : 0);
}
Expand Down Expand Up @@ -1100,7 +1134,7 @@ QString QHexView::formatBytes(const QByteArray &row_data, int index) const {

byte_buffer[wordWidth_ * 2] = '\0';

return byte_buffer;
return QString::fromLatin1(byte_buffer);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion qhexview.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This file can be used under one of two licenses.
1. The GNU Public License, version 2.0, in COPYING-gpl2
2. A BSD-Style License, in COPYING-bsd2.
The license chosen is at the discretion of the user of this software.
The license chosen is at the discretion of the user of this software.
*/

#ifndef QHEXVIEW_H_
Expand Down Expand Up @@ -98,6 +98,7 @@ public Q_SLOTS:
void setUserConfigRowWidth(bool);
void setUserConfigWordWidth(bool);
void setWordWidth(int);
void setHideLeadingAddressZeros(bool);

public:
AddressSize addressSize() const;
Expand All @@ -116,6 +117,7 @@ public Q_SLOTS:
bool showHexDump() const;
bool userConfigRowWidth() const;
bool userConfigWordWidth() const;
bool hideLeadingAddressZeros() const;
int rowWidth() const;
int wordWidth() const;
uint64_t selectedBytesSize() const;
Expand Down Expand Up @@ -183,6 +185,7 @@ public Q_SLOTS:
bool showLine3_ = true;
bool userCanSetRowWidth_ = true;
bool userCanSetWordWidth_ = true;
bool hideLeadingAddressZeros_ = false;
char unprintableChar_ = '.';
int fontHeight_ = 0; // height of a character in this font
int fontWidth_ = 0; // width of a character in this font
Expand Down

0 comments on commit ab87333

Please sign in to comment.