ObjFW  Check-in [6ed7c33611]

Overview
Comment:Add writeCString and writeWideCString to OFStream.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6ed7c3361181583aca89f80292c4b02109933634a4c25a9e8a0ecf6cd958b8d5
User & Date: js on 2008-12-07 02:53:55
Other Links: manifest | tags
Context
2008-12-07
02:59
glibc is broken once again. I guess no other libc is that broken... check-in: 6d069e2a83 user: js tags: trunk
02:53
Add writeCString and writeWideCString to OFStream. check-in: 6ed7c33611 user: js tags: trunk
02:35
Add inital OFSocket class. check-in: ba7219b1b6 user: js tags: trunk
Changes

Modified src/OFFile.h from [7e04efd7c9] to [38c6717cee].

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
















167
 * \param buf The buffer from which the data is written to the file
 * \param size The size of the data that should be written
 * \param nitem The number of items to write
 * \return The number of bytes written
 */
- (size_t)writeNItems: (size_t)nitems
	       ofSize: (size_t)size
	   fromBuffer: (uint8_t*)buf;

/**
 * Writes from a buffer into the file.
 *
 * \param buf The buffer from which the data is written to the file
 * \param size The size of the data that should be written
 * \return The number of bytes written
 */
- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (uint8_t*)buf;
















@end







|









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

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
 * \param buf The buffer from which the data is written to the file
 * \param size The size of the data that should be written
 * \param nitem The number of items to write
 * \return The number of bytes written
 */
- (size_t)writeNItems: (size_t)nitems
	       ofSize: (size_t)size
	   fromBuffer: (const uint8_t*)buf;

/**
 * Writes from a buffer into the file.
 *
 * \param buf The buffer from which the data is written to the file
 * \param size The size of the data that should be written
 * \return The number of bytes written
 */
- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const uint8_t*)buf;

/**
 * Writes a C string into the file, without the trailing zero.
 *
 * \param str The C string from which the data is written to the file
 * \return The number of bytes written
 */
- (size_t)writeCString: (const char*)str;

/**
 * Writes a C string into the file, without the trailing zero.
 *
 * \param str The wide C string from which the data is written to the file
 * \return The number of bytes written
 */
- (size_t)writeWideCString: (const wchar_t*)str;
@end

Modified src/OFFile.m from [ecb56994e1] to [5d576a9538].

9
10
11
12
13
14
15


16
17
18
19
20
21
22
 * the packaging of this file.
 */

#import "config.h"

#import <stdio.h>
#import <unistd.h>



#import <sys/types.h>
#import <sys/stat.h>

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








>
>







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 * the packaging of this file.
 */

#import "config.h"

#import <stdio.h>
#import <unistd.h>
#import <string.h>
#import <wchar.h>

#import <sys/types.h>
#import <sys/stat.h>

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

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
{
	return [self readNItems: size
			 ofSize: 1];
}

- (size_t)writeNItems: (size_t)nitems
	       ofSize: (size_t)size
	   fromBuffer: (uint8_t*)buf
{
	size_t ret;

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

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (uint8_t*)buf
{
	return [self writeNItems: size
			  ofSize: 1
		      fromBuffer: buf];
}














@end







|













|





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

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
{
	return [self readNItems: size
			 ofSize: 1];
}

- (size_t)writeNItems: (size_t)nitems
	       ofSize: (size_t)size
	   fromBuffer: (const uint8_t*)buf
{
	size_t ret;

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

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const uint8_t*)buf
{
	return [self writeNItems: size
			  ofSize: 1
		      fromBuffer: buf];
}

- (size_t)writeCString: (const char*)str
{
	return [self writeNItems: strlen(str)
			  ofSize: 1
		      fromBuffer: (const uint8_t*)str];
}

- (size_t)writeWideCString: (const wchar_t*)str
{
	return [self writeNItems: wcslen(str)
			  ofSize: sizeof(wchar_t)
		      fromBuffer: (const uint8_t*)str];
}
@end

Modified src/OFObject.m from [c722de15cb] to [16b59ee246].

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

	return ptr;
}

- (void*)getMemForNItems: (size_t)nitems
		  ofSize: (size_t)size
{
	size_t memsize;
	
	if (nitems == 0 || size == 0)
		return NULL;

	if (nitems > SIZE_MAX / size)
		[[OFOutOfRangeException newWithObject: self] raise];

	memsize = nitems * size;
	return [self getMemWithSize: memsize];
}

- (void*)resizeMem: (void*)ptr
	    toSize: (size_t)size
{
	void **iter;








<
<






<
|







79
80
81
82
83
84
85


86
87
88
89
90
91

92
93
94
95
96
97
98
99

	return ptr;
}

- (void*)getMemForNItems: (size_t)nitems
		  ofSize: (size_t)size
{


	if (nitems == 0 || size == 0)
		return NULL;

	if (nitems > SIZE_MAX / size)
		[[OFOutOfRangeException newWithObject: self] raise];


	return [self getMemWithSize: nitems * size];
}

- (void*)resizeMem: (void*)ptr
	    toSize: (size_t)size
{
	void **iter;

Modified src/OFSocket.h from [4a5db4859f] to [896e2569bc].

106
107
108
109
110
111
112
113
114
 * Sends data from a buffer.
 *
 * \param buf The buffer from which the data is written to the file
 * \param size The size of the data that should be written
 * \return The number of bytes written
 */
- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (uint8_t*)buf;
@end







|

106
107
108
109
110
111
112
113
114
 * Sends data from a buffer.
 *
 * \param buf The buffer from which the data is written to the file
 * \param size The size of the data that should be written
 * \return The number of bytes written
 */
- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const uint8_t*)buf;
@end

Modified src/OFSocket.m from [b2671e0dfd] to [ebbb94e27f].

10
11
12
13
14
15
16

17
18

19
20
21
22
23
24
25
 */

#import "config.h"

#import <stdlib.h>
#import <string.h>
#import <unistd.h>


#import "OFSocket.h"


@implementation OFSocketAddress
+ newWithHost: (const char*)host
      andPort: (uint16_t)port
    andFamily: (int)family
      andType: (int)type
  andProtocol: (int)protocol







>


>







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 */

#import "config.h"

#import <stdlib.h>
#import <string.h>
#import <unistd.h>
#import <wchar.h>

#import "OFSocket.h"
#import "OFExceptions.h"

@implementation OFSocketAddress
+ newWithHost: (const char*)host
      andPort: (uint16_t)port
    andFamily: (int)family
      andType: (int)type
  andProtocol: (int)protocol
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

















165
		@throw exception;
	}

	return ret;
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (uint8_t*)buf
{
	ssize_t ret;

	if ((ret = send(sock, buf, size, 0)) < 0) {
		/* FIXME: Throw exception */
		return 0;
	}

	/* This is safe, as we already checked < 0 */
	return ret;
}

















@end







|











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

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
		@throw exception;
	}

	return ret;
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const uint8_t*)buf
{
	ssize_t ret;

	if ((ret = send(sock, buf, size, 0)) < 0) {
		/* FIXME: Throw exception */
		return 0;
	}

	/* This is safe, as we already checked < 0 */
	return ret;
}

- (size_t)writeCString: (const char*)str
{
	return [self writeNBytes: strlen(str)
		      fromBuffer: (const uint8_t*)str];
}

- (size_t)writeWideCString: (const wchar_t*)str
{
	size_t len = wcslen(str);

	if (len > SIZE_MAX / sizeof(wchar_t))
		[[OFOutOfRangeException newWithObject: self] raise];

	return [self writeNBytes: len * sizeof(wchar_t)
		      fromBuffer: (const uint8_t*)str];
}
@end

Modified src/OFStream.h from [6c96d5c694] to [6335fea124].

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
















45
 * \param size The size of the data that should be read
 * \return A new buffer with the data read.
 *	   It is part of the memory pool of the OFFile.
 */
- (uint8_t*)readNBytes: (size_t)size;

/**
 * Writes from a buffer into the file.
 *
 * \param buf The buffer from which the data is written to the file
 * \param size The size of the data that should be written
 * \return The number of bytes written
 */
- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (uint8_t*)buf;
















@end







|

|




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

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
 * \param size The size of the data that should be read
 * \return A new buffer with the data read.
 *	   It is part of the memory pool of the OFFile.
 */
- (uint8_t*)readNBytes: (size_t)size;

/**
 * Writes from a buffer into the stream.
 *
 * \param buf The buffer from which the data is written to the stream
 * \param size The size of the data that should be written
 * \return The number of bytes written
 */
- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const uint8_t*)buf;

/**
 * Writes a C string into the stream, without the trailing zero.
 *
 * \param str The C string from which the data is written to the stream
 * \return The number of bytes written
 */
- (size_t)writeCString: (const char*)str;

/**
 * Writes a C string into the stream, without the trailing zero.
 *
 * \param str The wide C string from which the data is written to the stream
 * \return The number of bytes written
 */
- (size_t)writeWideCString: (const wchar_t*)str;
@end

Modified tests/OFSocket/OFSocket.m from [53eb38c1df] to [cdce310541].

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
#import "config.h"

#import <string.h>

#import "OFSocket.h"
#import "OFExceptions.h"

const char *sendstr = "GET / HTTP/1.1\r\nHost: webkeks.org\r\n\r\n";

int
main()
{
	OFSocketAddress *addr;
	OFSocket *sock;

	@try {
		addr = [OFSocketAddress newWithHost: "webkeks.org"
					    andPort: 80
					  andFamily: AF_UNSPEC
					    andType: SOCK_STREAM
					andProtocol: 0];
		sock = [OFSocket new];
		[sock connect: addr];
		[addr free];

		[sock writeNBytes: strlen(sendstr)
		       fromBuffer: (uint8_t*)sendstr];

		puts((char*)[sock readNBytes: 1024]);

		[sock free];
	} @catch(OFException *e) {
		printf("EXCEPTION: %s\n", [e cString]);
	}

	return 0;
}







<
<
















|
<
|









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
#import "config.h"

#import <string.h>

#import "OFSocket.h"
#import "OFExceptions.h"



int
main()
{
	OFSocketAddress *addr;
	OFSocket *sock;

	@try {
		addr = [OFSocketAddress newWithHost: "webkeks.org"
					    andPort: 80
					  andFamily: AF_UNSPEC
					    andType: SOCK_STREAM
					andProtocol: 0];
		sock = [OFSocket new];
		[sock connect: addr];
		[addr free];

		[sock writeCString: "GET / HTTP/1.1\r\n"

				    "Host: webkeks.org\r\n\r\n"];
		puts((char*)[sock readNBytes: 1024]);

		[sock free];
	} @catch(OFException *e) {
		printf("EXCEPTION: %s\n", [e cString]);
	}

	return 0;
}