-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexViewModel.m
More file actions
111 lines (98 loc) · 2.93 KB
/
HexViewModel.m
File metadata and controls
111 lines (98 loc) · 2.93 KB
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
/*
*========================================================
* HexViewModel.m
* Hex View
*
* Created by Aaron Spiteri on 7/03/11.
* Copyright 2011 __MyCompanyName__. All rights reserved.
*========================================================
*/
#import "HexViewModel.h"
@implementation HexViewModel
@synthesize fileError, filePath, fbuffer;
/*
*========================================================
*
* Class constructor.
*========================================================
*/
-(HexViewModel *) initWithFilePath: (NSString *) filePathIn {
self = [super init];
if( self ){
self.filePath = filePathIn;
}
return self;
}
/*
*========================================================
*
* checkFile
*
* Check the file for any potential problems using the
* file manager, also prime the file for opening by
* setting encoding type and other attributes required.
*========================================================
*/
-(BOOL) checkFile {
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: self.filePath ] == NO){
self.fileError = [self.filePath stringByAppendingString:@": file does not exist"];
return NO;
}
else if([filemgr isReadableFileAtPath: self.filePath ] == NO){
self.fileError = [self.filePath stringByAppendingString:@": can not be read"];
return NO;
}
return YES;
}
/*
*========================================================
* getFileError
*========================================================
*/
-(NSString *) getFileError {
return self.fileError;
}
/*
*========================================================
* getBuffer
*
* Return the contents of the read file as ASCII text
*========================================================
*/
-(NSString *)getBufferAsHex: (id)prog {
NSString *sout = nil;
int i =0, *lary = calloc(1,sizeof(int)), byteCount = 0, pOffset = BYTE_SIZE;
char *cFileName = calloc(PATH_MAX,BYTESZ);
[filePath getCString: cFileName maxLength: PATH_MAX encoding: NSNonLossyASCIIStringEncoding];
if((i = getFileAsInt(cFileName, &lary,&byteCount)) != 0){
fileError = [NSString stringWithCString: strerror(i)];
return nil;
}
for(;i < byteCount;i ++){
if(sout == nil){
sout = [NSString stringWithFormat:@HEXFORMAT, lary[i]];
}
else {
sout = [sout stringByAppendingFormat:@HEXFORMAT, lary[i]];
}
if(i >= pOffset){
@try{
[prog setDoubleValue:100*i/(double)byteCount];
[prog displayIfNeeded];
}@finally {
// do nothing
}
pOffset += POFFSET;
}
}
return sout;
}
/*
*========================================================
* setProgressMeter
*
* Set the progress meter, or try too.
*========================================================
*/
@end