forked from ImpulseAdventure/JPEGsnoop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OffsetDlg.cpp
187 lines (156 loc) · 4.25 KB
/
OffsetDlg.cpp
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// JPEGsnoop - JPEG Image Decoder & Analysis Utility
// Copyright (C) 2017 - Calvin Hass
// http://www.impulseadventure.com/photo/jpeg-snoop.html
//
// 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/>.
//
// dlg_offset.cpp : implementation file
//
#include "stdafx.h"
#include "JPEGsnoop.h"
#include "OffsetDlg.h"
// COffsetDlg dialog
IMPLEMENT_DYNAMIC(COffsetDlg, CDialog)
COffsetDlg::COffsetDlg(CWnd* pParent /*=NULL*/)
: CDialog(COffsetDlg::IDD, pParent)
, m_sOffsetVal(_T(""))
{
m_nBaseMode = 0; // 0=hex, 1=dec
m_nRadioBaseMode = 0;
}
COffsetDlg::~COffsetDlg()
{
}
// Set the initial file offset
void COffsetDlg::SetOffset(unsigned nPos)
{
m_nOffsetVal = nPos;
OffsetNum2Str();
}
// Fetch the current offset value from the dialog
unsigned COffsetDlg::GetOffset()
{
return m_nOffsetVal;
}
void COffsetDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_OFFSETVAL, m_sOffsetVal);
DDX_Radio(pDX, IDC_BASEH, m_nRadioBaseMode);
}
BEGIN_MESSAGE_MAP(COffsetDlg, CDialog)
ON_BN_CLICKED(IDC_BASEH, OnBnClickedBaseh)
ON_BN_CLICKED(IDC_BASED, OnBnClickedBased)
ON_BN_CLICKED(IDOK, OnBnClickedOk)
END_MESSAGE_MAP()
// COffsetDlg message handlers
// Convert internal offset number to dialog string
void COffsetDlg::OffsetNum2Str()
{
CString strVal;
if (m_nBaseMode == 0) {
// Hex
strVal.Format(_T("0x%08X"),m_nOffsetVal);
m_sOffsetVal = strVal;
} else {
// Dec
strVal.Format(_T("%u"),m_nOffsetVal);
m_sOffsetVal = strVal;
}
}
// Convert the current dialog offset string into
// the internal offset value. If data validation
// fails, then display a dialog box.
//
// RETURN:
// - True if validation OK
//
bool COffsetDlg::OffsetStr2Num()
{
CString strVal;
if (m_nBaseMode == 0) {
// Hex
if (!Str2Uint32(m_sOffsetVal,16,m_nOffsetVal)) {
AfxMessageBox(_T("Invalid hex string"));
return false;
}
} else {
// Decimal
if (!Str2Uint32(m_sOffsetVal,10,m_nOffsetVal)) {
AfxMessageBox(_T("Invalid decimal string"));
return false;
}
}
return true;
}
// Change dialog mode to hex
// - If in decimal mode, convert from decimal to hex
// - Report data validation error
void COffsetDlg::OnBnClickedBaseh()
{
CString strVal;
bool bOk;
UpdateData(true);
if (m_nBaseMode == 0) {
// If it was hex before, don't do anything
} else {
// If it was decimal before, convert it!
bOk = OffsetStr2Num();
m_nBaseMode = 0;
if (!bOk) {
// There was a validation error
// - Force the value to zero
// TODO: Would be nice if we could instead
// prevent the radio button from transitioning state.
m_nOffsetVal = 0;
}
}
OffsetNum2Str();
UpdateData(false);
}
// Change dialog mode to decimal
// - If in hex mode, convert from hex to decimal
// - Report data validation error
void COffsetDlg::OnBnClickedBased()
{
CString strVal;
bool bOk;
UpdateData(true);
if (m_nBaseMode == 1) {
// If it was decimal before, don't do anything
} else {
// If it was hex before, convert it!
bOk = OffsetStr2Num();
m_nBaseMode = 1;
if (!bOk) {
// There was a validation error
// - Force the value to zero
// TODO: Would be nice if we could instead
// prevent the radio button from transitioning state.
m_nOffsetVal = 0;
}
}
OffsetNum2Str();
UpdateData(false);
}
void COffsetDlg::OnBnClickedOk()
{
// Store the new local value before we return to dialog caller
UpdateData();
bool bOk = OffsetStr2Num();
// Only leave the dialog if the data validation was successful
if (bOk) {
OnOK();
}
}