ObjFW  Check-in [6072f61f83]

Overview
Comment:OFFile improvements.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6072f61f831606cae092358b27e1b2f6a6e1b89dc40d8f43295e064447d88094
User & Date: js on 2008-10-07 21:25:10
Other Links: manifest | tags
Context
2008-10-07
22:04
#import <stdint.h> was missing. check-in: 1c99e71818 user: js tags: trunk
21:25
OFFile improvements. check-in: 6072f61f83 user: js tags: trunk
17:18
Added OFFile. check-in: 71119a787b user: js tags: trunk
Changes

Modified src/OFExceptions.h from [fce9b442b5] to [8109e7dcaa].

39
40
41
42
43
44
45









46
47
48
49









50
51
52
53
@end

@interface OFOverflowException: OFException
+ newWithObject: (id)obj;
- initWithObject: (id)obj;
@end










@interface OFReadFailedException: OFException
+ newWithObject: (id)obj
	andSize: (size_t)size
      andNItems: (size_t)nitems;









- initWithObject: (id)obj
	 andSize: (size_t)size
       andNItems: (size_t)nitems;
@end







>
>
>
>
>
>
>
>
>
|



>
>
>
>
>
>
>
>
>




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
@end

@interface OFOverflowException: OFException
+ newWithObject: (id)obj;
- initWithObject: (id)obj;
@end

@interface OFOpenFileFailedException: OFException
+ newWithObject: (id)obj
	andPath: (const char*)path
	andMode: (const char*)mode;
- initWithObject: (id)obj
	 andPath: (const char*)path
	 andMode: (const char*)mode;
@end

@interface OFReadOrWriteFailedException: OFException
+ newWithObject: (id)obj
	andSize: (size_t)size
      andNItems: (size_t)nitems;
@end

@interface OFReadFailedException: OFReadOrWriteFailedException
- initWithObject: (id)obj
	 andSize: (size_t)size
       andNItems: (size_t)nitems;
@end

@interface OFWriteFailedException: OFReadOrWriteFailedException
- initWithObject: (id)obj
	 andSize: (size_t)size
       andNItems: (size_t)nitems;
@end

Modified src/OFExceptions.m from [77c759a696] to [37b58b7cb0].

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
+ newWithObject: (id)obj
{
	return [[OFException alloc] initWithObject: obj];
}

- initWithObject: (id)obj
{

	@throw self;
	return [super init];
}
@end

@implementation OFNoMemException
+ newWithObject: (id)obj
	andSize: (size_t)size
{
	return [[OFNoMemException alloc] initWithObject: obj
						andSize: size];
}

- initWithObject: (id)obj
	 andSize: (size_t)size
{
	fprintf(stderr, "ERROR: Could not allocate %zu bytes for object %s!\n",
	    size, [obj name]);


	@throw self;
	return [super init];
}
@end

@implementation OFNotImplementedException
+ newWithObject: (id)obj
      andMethod: (const char*)method
{
	return [[OFNotImplementedException alloc] initWithObject: obj
						       andMethod: method];
}

- initWithObject: (id)obj
       andMethod: (const char*)method
{
	fprintf(stderr, "ERROR: Requested method %s not implemented in %s!\n",
	    method, [obj name]);


	@throw self;
	return [super init];
}
@end

@implementation OFMemNotPartOfObjException
+ newWithObject: (id)obj
     andPointer: (void*)ptr
{
	return [[OFMemNotPartOfObjException alloc] initWithObject: obj
						       andPointer: ptr];
}

- initWithObject: (id)obj
      andPointer: (void*)ptr
{
	fprintf(stderr, "ERROR: Memory at %p was not allocated as part of "
	    "object %s!\n"
	    "ERROR: -> Not changing memory allocation!\n"
	    "ERROR: (Hint: It is possible that you tried to free the same "
	    "memory twice!)\n", ptr, [obj name]);


	@throw self;
	return [super init];
}
@end

@implementation OFOverflowException
+ newWithObject: (id)obj
{
	return [[OFOverflowException alloc] initWithObject: obj];
}

- initWithObject: (id)obj
{
	fprintf(stderr, "ERROR: Overflow in object %s!\n", [obj name]);


	@throw self;





















	return [super init];


}
@end

@implementation OFReadFailedException
+ newWithObject: (id)obj
	andSize: (size_t)size
      andNItems: (size_t)nitems
{
	return [[OFReadFailedException alloc] initWithObject: obj
						     andSize: size
						   andNItems: nitems];
}



- initWithObject: (id)obj
	 andSize: (size_t)size
       andNItems: (size_t)nitems
{
	fprintf(stderr, "ERROR: Failed to read %zu items of size %zu in "
	    "object %s!\n", nitems, size, [obj name]);


	@throw self;












	return [super init];


}
@end







>

|

















>

|

















>

|




















>

|













>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>



|




|
|
|

>

>







>

>
>
>
>
>
>
>
>
>
>
>
>
|
>
>


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
+ newWithObject: (id)obj
{
	return [[OFException alloc] initWithObject: obj];
}

- initWithObject: (id)obj
{
	self = [super init];
	@throw self;
	return self;
}
@end

@implementation OFNoMemException
+ newWithObject: (id)obj
	andSize: (size_t)size
{
	return [[OFNoMemException alloc] initWithObject: obj
						andSize: size];
}

- initWithObject: (id)obj
	 andSize: (size_t)size
{
	fprintf(stderr, "ERROR: Could not allocate %zu bytes for object %s!\n",
	    size, [obj name]);

	self = [super init];
	@throw self;
	return self;
}
@end

@implementation OFNotImplementedException
+ newWithObject: (id)obj
      andMethod: (const char*)method
{
	return [[OFNotImplementedException alloc] initWithObject: obj
						       andMethod: method];
}

- initWithObject: (id)obj
       andMethod: (const char*)method
{
	fprintf(stderr, "ERROR: Requested method %s not implemented in %s!\n",
	    method, [obj name]);

	self = [super init];
	@throw self;
	return self;
}
@end

@implementation OFMemNotPartOfObjException
+ newWithObject: (id)obj
     andPointer: (void*)ptr
{
	return [[OFMemNotPartOfObjException alloc] initWithObject: obj
						       andPointer: ptr];
}

- initWithObject: (id)obj
      andPointer: (void*)ptr
{
	fprintf(stderr, "ERROR: Memory at %p was not allocated as part of "
	    "object %s!\n"
	    "ERROR: -> Not changing memory allocation!\n"
	    "ERROR: (Hint: It is possible that you tried to free the same "
	    "memory twice!)\n", ptr, [obj name]);

	self = [super init];
	@throw self;
	return self;
}
@end

@implementation OFOverflowException
+ newWithObject: (id)obj
{
	return [[OFOverflowException alloc] initWithObject: obj];
}

- initWithObject: (id)obj
{
	fprintf(stderr, "ERROR: Overflow in object %s!\n", [obj name]);

	self = [super init];
	@throw self;
	return self;
}
@end

@implementation OFOpenFileFailedException
+ newWithObject: (id)obj
	andPath: (const char*)path
	andMode: (const char*)mode
{
	return [[OFOpenFileFailedException alloc] initWithObject: obj
							 andPath: path
							 andMode: mode];
}

- initWithObject: (id)obj
	 andPath: (const char*)path
	 andMode: (const char*)mode
{
	fprintf(stderr, "ERROR: Failed to open file %s with mode %s in "
	    "object %s!\n", path, mode, [self name]);

	self = [super init];
	@throw self;
	return self;
}
@end

@implementation OFReadOrWriteFailedException
+ newWithObject: (id)obj
	andSize: (size_t)size
      andNItems: (size_t)nitems
{
	return [[OFReadOrWriteFailedException alloc] initWithObject: obj
							    andSize: size
							  andNItems: nitems];
}
@end

@implementation OFReadFailedException
- initWithObject: (id)obj
	 andSize: (size_t)size
       andNItems: (size_t)nitems
{
	fprintf(stderr, "ERROR: Failed to read %zu items of size %zu in "
	    "object %s!\n", nitems, size, [obj name]);

	self = [super init];
	@throw self;
	return self;
}
@end

@implementation OFWriteFailedException
- initWithObject: (id)obj
	 andSize: (size_t)size
       andNItems: (size_t)nitems
{
	fprintf(stderr, "ERROR: Failed to write %zu items of size %zu in "
	    "object %s!\n", nitems, size, [obj name]);

	self = [super init];
	@throw self;
	return self;
}
@end

Modified src/OFFile.h from [3d1d09b7c8] to [f1d0a00de8].

19
20
21
22
23
24
25




26
27



28
}

+ newWithPath: (const char*)path
      andMode: (const char*)mode;
- initWithPath: (const char*)path
       andMode: (const char*)mode;
- free;




- (char*)readWithSize: (size_t)size
	    andNItems: (size_t)nitems;



@end







>
>
>
>


>
>
>

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
}

+ newWithPath: (const char*)path
      andMode: (const char*)mode;
- initWithPath: (const char*)path
       andMode: (const char*)mode;
- free;
- (BOOL)isEndOfFile;
- (size_t)readIntoBuffer: (char*)buf
		withSize: (size_t)size
	       andNItems: (size_t)nItems;
- (char*)readWithSize: (size_t)size
	    andNItems: (size_t)nitems;
- (size_t)writeBuffer: (char*)buf
	     withSize: (size_t)size
	    andNItems: (size_t)nitems;
@end

Modified src/OFFile.m from [a6ec8db29d] to [fca767783e].

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
}

- free
{
	fclose(fp);
	return [super free];
}




















- (char*)readWithSize: (size_t)size
	    andNItems: (size_t)nitems
{
	uint64_t memsize;
	char *ret;
       

	if ((memsize = (uint64_t)nitems * size) > 0xFFFFFFFF) {
		[OFOverflowException newWithObject: self];
		return NULL;
	}
	
	ret = [self getMem: (size_t)memsize];

	if (fread(ret, size, nitems, fp) <= 0 && !feof(fp)) {
		[self freeMem: ret];
		[OFReadFailedException newWithObject: self
					     andSize: size
					   andNItems: nitems];



		return NULL;
	}
















	return ret;
}
@end







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







>
|






|
|
<
|
|
>
>
>



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



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
}

- free
{
	fclose(fp);
	return [super free];
}

- (BOOL)isEndOfFile
{
	return feof(fp);
}

- (size_t)readIntoBuffer: (char*)buf
		withSize: (size_t)size
	       andNItems: (size_t)nitems
{
	size_t ret;

	if ((ret = fread(buf, size, nitems, fp)) == 0 && ![self isEndOfFile])
		[OFReadFailedException newWithObject: self
					     andSize: size
					   andNItems: nitems];

	return ret;
}

- (char*)readWithSize: (size_t)size
	    andNItems: (size_t)nitems
{
	uint64_t memsize;
	char *ret;
       
	if (size >= 0xFFFFFFFF || nitems >= 0xFFFFFFFF ||
	    (memsize = (uint64_t)nitems * size) > 0xFFFFFFFF) {
		[OFOverflowException newWithObject: self];
		return NULL;
	}
	
	ret = [self getMem: (size_t)memsize];

	@try {
		[self readIntoBuffer: ret

			    withSize: size
			   andNItems: nitems];
	} @catch (OFReadFailedException *e) {
		[self freeMem: ret];
		@throw e;
		return NULL;
	}

	return ret;
}

- (size_t)writeBuffer: (char*)buf
	     withSize: (size_t)size
	    andNItems: (size_t)nitems
{
	size_t ret;

	if ((ret = fwrite(buf, size, nitems, fp)) == 0 &&
	    size != 0 && nitems != 0)
		[OFWriteFailedException newWithObject: self
					      andSize: size
					    andNItems: nitems];
	
	return ret;
}
@end