ObjFW  Check-in [f31197a153]

Overview
Comment:Add methods to read / write floats and doubles to OFStream.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f31197a1539c6a4508213aae90a4662d52d23615567ed2fe819571bbacd98d7b
User & Date: js on 2011-06-05 18:32:45
Other Links: manifest | tags
Context
2011-06-05
18:44
Check size of float and double. check-in: c3f475701c user: js tags: trunk
18:32
Add methods to read / write floats and doubles to OFStream. check-in: f31197a153 user: js tags: trunk
18:13
Always hash floats and doubles in big endian. check-in: 4560e88c45 user: js tags: trunk
Changes

Modified src/OFStream.h from [576cb8fb89] to [14891818f4].

113
114
115
116
117
118
119




















120
121
122
123
124
125
126
 * WARNING: Only call this when you know that enough data is available!
 *	    Otherwise you will get an exception!
 *
 * \return A uint64_t from the stream in the native endianess
 */
- (uint64_t)readBigEndianInt64;





















/**
 * Reads a uint16_t from the stream which is encoded in little endian.
 *
 * WARNING: Only call this when you know that enough data is available!
 *	    Otherwise you will get an exception!
 *
 * \return A uint16_t from the stream in native endianess







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







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
 * WARNING: Only call this when you know that enough data is available!
 *	    Otherwise you will get an exception!
 *
 * \return A uint64_t from the stream in the native endianess
 */
- (uint64_t)readBigEndianInt64;

/**
 * Reads a float from the stream which is encoded in big endian.
 *
 * WARNING: Only call this when you know that enough data is available!
 *	    Otherwise you will get an exception!
 *
 * \return A float from the stream in the native endianess
 */
- (float)readBigEndianFloat;

/**
 * Reads a double from the stream which is encoded in big endian.
 *
 * WARNING: Only call this when you know that enough data is available!
 *	    Otherwise you will get an exception!
 *
 * \return A double from the stream in the native endianess
 */
- (double)readBigEndianDouble;

/**
 * Reads a uint16_t from the stream which is encoded in little endian.
 *
 * WARNING: Only call this when you know that enough data is available!
 *	    Otherwise you will get an exception!
 *
 * \return A uint16_t from the stream in native endianess
143
144
145
146
147
148
149




















150
151
152
153
154
155
156
 * WARNING: Only call this when you know that enough data is available!
 *	    Otherwise you will get an exception!
 *
 * \return A uint64_t from the stream in the native endianess
 */
- (uint64_t)readLittleEndianInt64;





















/**
 * Reads nItems items with an item size of 1 from the stream and returns them
 * in an OFDataArray.
 *
 * WARNING: Only call this when you know that enough data is available!
 *	    Otherwise you will get an exception!
 *







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







163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
 * WARNING: Only call this when you know that enough data is available!
 *	    Otherwise you will get an exception!
 *
 * \return A uint64_t from the stream in the native endianess
 */
- (uint64_t)readLittleEndianInt64;

/**
 * Reads a float from the stream which is encoded in little endian.
 *
 * WARNING: Only call this when you know that enough data is available!
 *	    Otherwise you will get an exception!
 *
 * \return A float from the stream in the native endianess
 */
- (float)readLittleEndianFloat;

/**
 * Reads a double from the stream which is encoded in little endian.
 *
 * WARNING: Only call this when you know that enough data is available!
 *	    Otherwise you will get an exception!
 *
 * \return A double from the stream in the native endianess
 */
- (double)readLittleEndianDouble;

/**
 * Reads nItems items with an item size of 1 from the stream and returns them
 * in an OFDataArray.
 *
 * WARNING: Only call this when you know that enough data is available!
 *	    Otherwise you will get an exception!
 *
268
269
270
271
272
273
274














275
276
277
278
279
280
281
/**
 * Writes a uint64_t into the stream, encoded in big endian.
 *
 * \param int64 A uint64_t
 */
- (void)writeBigEndianInt64: (uint64_t)int64;















/**
 * Writes a uint16_t into the stream, encoded in little endian.
 *
 * \param int16 A uint16_t
 */
- (void)writeLittleEndianInt16: (uint16_t)int16;








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







308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
 * Writes a uint64_t into the stream, encoded in big endian.
 *
 * \param int64 A uint64_t
 */
- (void)writeBigEndianInt64: (uint64_t)int64;

/**
 * Writes a float into the stream, encoded in big endian.
 *
 * \param float_ A float
 */
- (void)writeBigEndianFloat: (float)float_;

/**
 * Writes a double into the stream, encoded in big endian.
 *
 * \param double_ A double
 */
- (void)writeBigEndianDouble: (double)double_;

/**
 * Writes a uint16_t into the stream, encoded in little endian.
 *
 * \param int16 A uint16_t
 */
- (void)writeLittleEndianInt16: (uint16_t)int16;

289
290
291
292
293
294
295














296
297
298
299
300
301
302
/**
 * Writes a uint64_t into the stream, encoded in little endian.
 *
 * \param int64 A uint64_t
 */
- (void)writeLittleEndianInt64: (uint64_t)int64;















/**
 * Writes from an OFDataArray into the stream.
 *
 * \param dataArray The OFDataArray to write into the stream
 * \return The number of bytes written
 */
- (size_t)writeDataArray: (OFDataArray*)dataArray;







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







343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**
 * Writes a uint64_t into the stream, encoded in little endian.
 *
 * \param int64 A uint64_t
 */
- (void)writeLittleEndianInt64: (uint64_t)int64;

/**
 * Writes a float into the stream, encoded in little endian.
 *
 * \param float_ A float
 */
- (void)writeLittleEndianFloat: (float)float_;

/**
 * Writes a double into the stream, encoded in little endian.
 *
 * \param double_ A double
 */
- (void)writeLittleEndianDouble: (double)double_;

/**
 * Writes from an OFDataArray into the stream.
 *
 * \param dataArray The OFDataArray to write into the stream
 * \return The number of bytes written
 */
- (size_t)writeDataArray: (OFDataArray*)dataArray;

Modified src/OFStream.m from [17a9e9e523] to [168d0501c3].

170
171
172
173
174
175
176




















177
178
179
180
181
182
183
	uint64_t ret;

	[self readExactlyNBytes: 8
		     intoBuffer: (char*)&ret];

	return of_bswap64_if_le(ret);
}





















- (uint16_t)readLittleEndianInt16
{
	uint16_t ret;

	[self readExactlyNBytes: 2
		     intoBuffer: (char*)&ret];







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







170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
	uint64_t ret;

	[self readExactlyNBytes: 8
		     intoBuffer: (char*)&ret];

	return of_bswap64_if_le(ret);
}

- (float)readBigEndianFloat
{
	float ret;

	[self readExactlyNBytes: 4
		     intoBuffer: (char*)&ret];

	return of_bswap_float_if_le(ret);
}

- (double)readBigEndianDouble
{
	double ret;

	[self readExactlyNBytes: 8
		     intoBuffer: (char*)&ret];

	return of_bswap_double_if_le(ret);
}

- (uint16_t)readLittleEndianInt16
{
	uint16_t ret;

	[self readExactlyNBytes: 2
		     intoBuffer: (char*)&ret];
200
201
202
203
204
205
206




















207
208
209
210
211
212
213
	uint64_t ret;

	[self readExactlyNBytes: 8
		     intoBuffer: (char*)&ret];

	return of_bswap64_if_be(ret);
}





















- (OFDataArray*)readDataArrayWithNItems: (size_t)nItems
{
	return [self readDataArrayWithItemSize: 1
				     andNItems: nItems];
}








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







220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
	uint64_t ret;

	[self readExactlyNBytes: 8
		     intoBuffer: (char*)&ret];

	return of_bswap64_if_be(ret);
}

- (float)readLittleEndianFloat
{
	float ret;

	[self readExactlyNBytes: 4
		     intoBuffer: (char*)&ret];

	return of_bswap_float_if_be(ret);
}

- (double)readLittleEndianDouble
{
	double ret;

	[self readExactlyNBytes: 8
		     intoBuffer: (char*)&ret];

	return of_bswap_double_if_be(ret);
}

- (OFDataArray*)readDataArrayWithNItems: (size_t)nItems
{
	return [self readDataArrayWithItemSize: 1
				     andNItems: nItems];
}

626
627
628
629
630
631
632
















633
634
635
636
637
638
639
- (void)writeBigEndianInt64: (uint64_t)int64
{
	int64 = of_bswap64_if_le(int64);

	[self writeNBytes: 8
	       fromBuffer: (char*)&int64];
}

















- (void)writeLittleEndianInt16: (uint16_t)int16
{
	int16 = of_bswap16_if_be(int16);

	[self writeNBytes: 2
	       fromBuffer: (char*)&int16];







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







666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
- (void)writeBigEndianInt64: (uint64_t)int64
{
	int64 = of_bswap64_if_le(int64);

	[self writeNBytes: 8
	       fromBuffer: (char*)&int64];
}

- (void)writeBigEndianFloat: (float)float_
{
	float_ = of_bswap_float_if_le(float_);

	[self writeNBytes: 4
	       fromBuffer: (char*)&float_];
}

- (void)writeBigEndianDouble: (double)double_
{
	double_ = of_bswap_double_if_le(double_);

	[self writeNBytes: 8
	       fromBuffer: (char*)&double_];
}

- (void)writeLittleEndianInt16: (uint16_t)int16
{
	int16 = of_bswap16_if_be(int16);

	[self writeNBytes: 2
	       fromBuffer: (char*)&int16];
650
651
652
653
654
655
656
















657
658
659
660
661
662
663
- (void)writeLittleEndianInt64: (uint64_t)int64
{
	int64 = of_bswap64_if_be(int64);

	[self writeNBytes: 8
	       fromBuffer: (char*)&int64];
}

















- (size_t)writeDataArray: (OFDataArray*)dataArray
{
	return [self writeNBytes: [dataArray count] * [dataArray itemSize]
		      fromBuffer: [dataArray cArray]];
}








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







706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
- (void)writeLittleEndianInt64: (uint64_t)int64
{
	int64 = of_bswap64_if_be(int64);

	[self writeNBytes: 8
	       fromBuffer: (char*)&int64];
}

- (void)writeLittleEndianFloat: (float)float_
{
	float_ = of_bswap_float_if_be(float_);

	[self writeNBytes: 4
	       fromBuffer: (char*)&float_];
}

- (void)writeLittleEndianDouble: (double)double_
{
	double_ = of_bswap_double_if_be(double_);

	[self writeNBytes: 8
	       fromBuffer: (char*)&double_];
}

- (size_t)writeDataArray: (OFDataArray*)dataArray
{
	return [self writeNBytes: [dataArray count] * [dataArray itemSize]
		      fromBuffer: [dataArray cArray]];
}