ObjFW  Check-in [71119a787b]

Overview
Comment:Added OFFile.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 71119a787bfd1248e054badb193173c4a8e579f3b068e2957f58dcc41c8b2a32
User & Date: js on 2008-10-07 17:18:54
Other Links: manifest | tags
Context
2008-10-07
21:25
OFFile improvements. check-in: 6072f61f83 user: js tags: trunk
17:18
Added OFFile. check-in: 71119a787b user: js tags: trunk
2008-10-05
19:20
Reworked String API. check-in: cf85bee74f user: js tags: trunk
Changes

Modified src/Makefile from [be14b6ad32] to [3ff534d15e].

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
LIB = ${LIB_PREFIX}objfw${LIB_SUFFIX}
LIB_MAJOR = 1
LIB_MINOR = 0

SRCS = OFConstCString.m		\
       OFCString.m		\
       OFConstWideCString.m	\
       OFExceptions.m		\

       OFList.m			\
       OFListObject.m		\
       OFObject.m		\
       OFString.m		\
       OFWideCString.m

INCLUDES = OFConstString.h	\
	   OFCString.h		\
	   OFConstWideString.h	\
	   OFExceptions.h	\

	   OFList.h		\
	   OFListObject.h	\
	   OFObject.h		\
	   OFString.h		\
	   OFWideString.h

include ../buildsys.mk

OBJCFLAGS += ${LIB_CFLAGS}
LD = ${OBJC}
LDFLAGS += ${LIB_LDFLAGS}
LIBS += -lobjc








>










>












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
LIB = ${LIB_PREFIX}objfw${LIB_SUFFIX}
LIB_MAJOR = 1
LIB_MINOR = 0

SRCS = OFConstCString.m		\
       OFCString.m		\
       OFConstWideCString.m	\
       OFExceptions.m		\
       OFFile.m			\
       OFList.m			\
       OFListObject.m		\
       OFObject.m		\
       OFString.m		\
       OFWideCString.m

INCLUDES = OFConstString.h	\
	   OFCString.h		\
	   OFConstWideString.h	\
	   OFExceptions.h	\
	   OFFile.h		\
	   OFList.h		\
	   OFListObject.h	\
	   OFObject.h		\
	   OFString.h		\
	   OFWideString.h

include ../buildsys.mk

OBJCFLAGS += ${LIB_CFLAGS}
LD = ${OBJC}
LDFLAGS += ${LIB_LDFLAGS}
LIBS += -lobjc

Modified src/OFExceptions.h from [da96e169ec] to [fce9b442b5].

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














 * the packaging of this file.
 */

#import <stddef.h>
#import "OFObject.h"

@interface OFException: OFObject
+ new: (id)obj;
- init: (id)obj;
@end

@interface OFNoMemException: OFException
+      new: (id)obj
  withSize: (size_t)size;
-     init: (id)obj
  withSize: (size_t)size;
@end

@interface OFNotImplementedException: OFException
+	 new: (id)obj
  withMethod: (const char*)method;
-       init: (id)obj
  withMethod: (const char*)method;
@end

@interface OFMemNotPartOfObjException: OFException
+     new: (id)obj
  withPtr: (void*)ptr;
-    init: (id)obj
  withPtr: (void*)ptr;
@end





















|
|



|
|
|
|



|
|
|
|



|
|
|
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
 * the packaging of this file.
 */

#import <stddef.h>
#import "OFObject.h"

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

@interface OFNoMemException: OFException
+ newWithObject: (id)obj
	andSize: (size_t)size;
- initWithObject: (id)obj
	 andSize: (size_t)size;
@end

@interface OFNotImplementedException: OFException
+ newWithObject: (id)obj
      andMethod: (const char*)method;
- initWithObject: (id)obj
       andMethod: (const char*)method;
@end

@interface OFMemNotPartOfObjException: OFException
+ newWithObject: (id)obj
     andPointer: (void*)ptr;
- initWithObject: (id)obj
      andPointer: (void*)ptr;
@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

Modified src/OFExceptions.m from [864964c046] to [77c759a696].

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





































 * the packaging of this file.
 */

#import <stdio.h>
#import "OFExceptions.h"

@implementation OFException
+ new: (id)obj
{
	return [[OFException alloc] init: obj];
}

- init: (id)obj
{

	return [super init];
}
@end

@implementation OFNoMemException
+      new: (id)obj
  withSize: (size_t)size
{
	return [[OFNoMemException alloc] init: obj
				     withSize: size];
}

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


	return [super init];
}
@end

@implementation OFNotImplementedException
+        new: (id)obj
  withMethod: (const char*)method
{
	return [[OFNotImplementedException alloc] init: obj
					    withMethod: method];
}

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


	return [super init];
}
@end

@implementation OFMemNotPartOfObjException
+     new: (id)obj
  withPtr: (void*)ptr
{
	return [[OFMemNotPartOfObjException alloc] init: obj
						withPtr: ptr];
}

-    init: (id)obj
  withPtr: (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]);


	return [super init];
}
@end












































|

|


|

>





|
|

|
|


|
|



>
>





|
|

|
|


|
|



>
>





|
|

|
|


|
|







>



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
 * the packaging of this file.
 */

#import <stdio.h>
#import "OFExceptions.h"

@implementation OFException
+ 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

Added src/OFFile.h version [3d1d09b7c8].

























































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import <stdio.h>

#import "OFObject.h"

@interface OFFile: OFObject
{
	FILE *fp;
}

+ 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

Added src/OFFile.m version [a6ec8db29d].





































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
 * Copyright (c) 2008
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of libobjfw. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import <stdio.h>

#import "OFFile.h"
#import "OFExceptions.h"

@implementation OFFile
+ newWithPath: (const char*)path
      andMode: (const char*)mode
{
	return [[OFFile alloc] initWithPath: path
				    andMode: mode];
}

- initWithPath: (const char*)path
       andMode: (const char*)mode
{
	if ((self = [super init])) {
		if ((fp = fopen(path, mode)) == NULL) {
			[self free];
			return nil;
		}
	}
	return self;
}

- 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

Modified src/OFObject.m from [0937ee4297] to [deca1fe096].

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
}

- (void*)getMem: (size_t)size
{
	struct __ofobject_allocated_mem *iter;

	if ((iter = malloc(sizeof(struct __ofobject_allocated_mem))) == NULL) {
		@throw [OFNoMemException new: self
				    withSize: sizeof(
					      struct __ofobject_allocated_mem)];
		return NULL;
	}

	if ((iter->ptr = malloc(size)) == NULL) {
		free(iter);
		@throw [OFNoMemException new: self
				    withSize: size];
		return NULL;
	}

	iter->next = NULL;
	iter->prev = __mem_pool;

	if (__mem_pool != NULL)







|
|
|





|
|







23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
}

- (void*)getMem: (size_t)size
{
	struct __ofobject_allocated_mem *iter;

	if ((iter = malloc(sizeof(struct __ofobject_allocated_mem))) == NULL) {
		[OFNoMemException newWithObject: self
					andSize: sizeof(struct
						     __ofobject_allocated_mem)];
		return NULL;
	}

	if ((iter->ptr = malloc(size)) == NULL) {
		free(iter);
		[OFNoMemException newWithObject: self
					andSize: size];
		return NULL;
	}

	iter->next = NULL;
	iter->prev = __mem_pool;

	if (__mem_pool != NULL)
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
	    toSize: (size_t)size
{
	struct __ofobject_allocated_mem *iter;

	for (iter = __mem_pool; iter != NULL; iter = iter->prev) {
		if (iter->ptr == ptr) {
			if ((ptr = realloc(iter->ptr, size)) == NULL) {
				@throw [OFNoMemException new: self
						    withSize: size];
				return NULL;
			}
			
			iter->ptr = ptr;
			return ptr;
		}
	}

	@throw [OFMemNotPartOfObjException new: self
				       withPtr: ptr];
	return NULL;
}

- (void)freeMem: (void*)ptr;
{
	struct __ofobject_allocated_mem *iter;








|
|








|
|







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
	    toSize: (size_t)size
{
	struct __ofobject_allocated_mem *iter;

	for (iter = __mem_pool; iter != NULL; iter = iter->prev) {
		if (iter->ptr == ptr) {
			if ((ptr = realloc(iter->ptr, size)) == NULL) {
				[OFNoMemException newWithObject: self
							andSize: size];
				return NULL;
			}
			
			iter->ptr = ptr;
			return ptr;
		}
	}

	[OFMemNotPartOfObjException newWithObject: self
				       andPointer: ptr];
	return NULL;
}

- (void)freeMem: (void*)ptr;
{
	struct __ofobject_allocated_mem *iter;

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
			free(iter);
			free(ptr);

			return;
		}
	}

	@throw [OFMemNotPartOfObjException new: self
				       withPtr: ptr];
}

- free
{
	struct __ofobject_allocated_mem *iter, *iter2;

	for (iter = __mem_pool; iter != NULL; iter = iter2) {







|
|







90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
			free(iter);
			free(ptr);

			return;
		}
	}

	[OFMemNotPartOfObjException newWithObject: self
				       andPointer: ptr];
}

- free
{
	struct __ofobject_allocated_mem *iter, *iter2;

	for (iter = __mem_pool; iter != NULL; iter = iter2) {

Modified src/OFString.m from [3e627dd8f1] to [7599c02c21].

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
+ newWithWideCString: (wchar_t*)str
{
	return [[OFWideCString alloc] initWithWideCString: str];
}

- (char*)cString
{
	@throw [OFNotImplementedException new: self withMethod: "cString"];

	return NULL;
}

- (wchar_t*)wcString
{
	@throw [OFNotImplementedException new: self withMethod: "wcString"];

	return NULL;
}

- (size_t)length
{
	return length;
}

- (OFString*)setTo: (OFString*)str
{
	[self free];
	self = [str clone];
	return self;
}

- (OFString*)clone
{
	@throw [OFNotImplementedException new: self withMethod: "clone"];

	return nil;
}

- (int)compare: (OFString*)str
{
	@throw [OFNotImplementedException new: self withMethod: "compare:"];

	return 0;
}

- (OFString*)append: (OFString*)str
{
	@throw [OFNotImplementedException new: self withMethod: "append:"];

	return nil;
}
@end







|
>





|
>

















|
>





|
>





|
>



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
+ newWithWideCString: (wchar_t*)str
{
	return [[OFWideCString alloc] initWithWideCString: str];
}

- (char*)cString
{
	[OFNotImplementedException newWithObject: self
				       andMethod: "cString"];
	return NULL;
}

- (wchar_t*)wcString
{
	[OFNotImplementedException newWithObject: self
				       andMethod: "wcString"];
	return NULL;
}

- (size_t)length
{
	return length;
}

- (OFString*)setTo: (OFString*)str
{
	[self free];
	self = [str clone];
	return self;
}

- (OFString*)clone
{
	[OFNotImplementedException newWithObject: self
				       andMethod: "clone"];
	return nil;
}

- (int)compare: (OFString*)str
{
	[OFNotImplementedException newWithObject: self
				       andMethod: "compare:"];
	return 0;
}

- (OFString*)append: (OFString*)str
{
	[OFNotImplementedException newWithObject: self
				       andMethod: "append:"];
	return nil;
}
@end