File: hexdata.cpp

package info (click to toggle)
kdeutils 2%3A980312-6
  • links: PTS
  • area: contrib
  • in suites: hamm
  • size: 5,380 kB
  • ctags: 3,423
  • sloc: cpp: 17,658; ansic: 9,867; sh: 6,347; makefile: 2,060; awk: 378; sed: 111; perl: 108
file content (54 lines) | stat: -rw-r--r-- 1,218 bytes parent folder | download
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
#include "hexdata.h"
#include <qfile.h>
#include <unistd.h>
#include <sys/mman.h>
#include <kapp.h>
#include <qmsgbox.h>

HexData::HexData() {
    hexdata = 0;
    data_size = 0;
}

int HexData::save(const char *filename) {
    QFile file(filename);
    file.open(IO_Truncate | IO_WriteOnly | IO_Raw);
    file.writeBlock(reinterpret_cast<char*>(hexdata), data_size);
    file.close();
    return 0;
}

uchar HexData::byteAt(unsigned long i) const {
    if (i < data_size)
	return hexdata[i]; 
    warning("HexData: index out of range");
    return 0;
}

int HexData::load(const char *Filename) {
    QString fileString(Filename);
    QFile file(fileString);
    if (!file.open(IO_ReadOnly | IO_Raw)) {
	QString txt;
	txt.sprintf(i18n("Error opening %s"),fileString.data());
	QMessageBox::message(i18n("Error"),txt,
			     i18n("Close"));
	return -1;
    }
    hexdata = new uchar[file.size()];
    data_size = file.size();
    uint alr_read = 0;
    while (alr_read < data_size) {
	int ret =  file.readBlock(reinterpret_cast<char*>(hexdata),
				  file.size());
	if (ret < 0) {
	    warning("loading failed");
	    data_size = 0;
	    delete [] hexdata;
	    hexdata = 0;
	}
	alr_read += ret;
    }
    return 0;
}