blib/lib/SDL2/rwops.pm untested

filter: Statementstmt 0.0% 0 / 78 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod n/a totaltotal 0.0% 0 / 84 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package SDL2::rwops_h {
2 use SDL2::Utils;
3 use experimental 'signatures';
4 use FFI::C::File;
5 ffi->type( 'object(FFI::C::File)' => 'FILE' );
6 #
7 use SDL2::stdinc;
8 use SDL2::error;
9 #
10 package SDL2::RWops { # TODO: All of this
11 use SDL2::Utils;
12 has type => 'uint32';
13 };
14 #
15 define rwops => [
16 [ SDL_RWOPS_UNKNOWN => 0 ],
17 [ SDL_RWOPS_WINFILE => 1 ],
18 [ SDL_RWOPS_STDFILE => 2 ],
19 [ SDL_RWOPS_JNIFILE => 3 ],
20 [ SDL_RWOPS_MEMORY => 4 ],
21 [ SDL_RWOPS_MEMORY_RO => 5 ],
22 [ SDL_RWOPS_VITAFILE => 6 ],
23 [ RW_SEEK_SET => 0 ],
24 [ RW_SEEK_CUR => 1 ],
25 [ RW_SEEK_END => 2 ],
26 ];
27 attach rwops => {
28 SDL_RWFromFile => [ [ 'string', 'string' ], 'SDL_RWops' ],
29 SDL_RWFromFP => [ [ 'FILE', 'SDL_bool' ], 'SDL_RWops' ],
30 SDL_RWFromMem => [ [ 'string', 'int' ], 'SDL_RWops' ],
31 SDL_RWFromConstMem => [ [ 'string', 'int' ], 'SDL_RWops' ],
32 SDL_AllocRW => [ [], 'SDL_RWops' ],
33 SDL_FreeRW => [ ['SDL_RWops'] ],
34 SDL_RWsize => [ ['SDL_RWops'], 'sint64' ],
35 SDL_RWseek => [ [ 'SDL_RWops', 'sint64', 'int' ], 'sint64' ],
36 SDL_RWtell => [ ['SDL_RWops'], 'sint64' ],
37 SDL_RWread => [ [ 'SDL_RWops', 'string', 'size_t', 'size_t' ], 'size_t' ],
38 SDL_RWwrite => [ [ 'SDL_RWops', 'string', 'size_t', 'size_t' ], 'size_t' ],
39 SDL_RWclose => [ ['SDL_RWops'], 'int' ],
40 SDL_LoadFile_RW => [ [ 'SDL_RWops', 'size_t', 'int' ], 'string' ],
41 SDL_LoadFile => [ [ 'string', 'size_t' ], 'string' ],
42 #
43 SDL_ReadU8 => [ ['SDL_RWops'], 'uint8' ],
44 SDL_ReadLE16 => [ ['SDL_RWops'], 'uint16' ],
45 SDL_ReadBE16 => [ ['SDL_RWops'], 'uint16' ],
46 SDL_ReadLE32 => [ ['SDL_RWops'], 'uint32' ],
47 SDL_ReadBE32 => [ ['SDL_RWops'], 'uint32' ],
48 SDL_ReadLE64 => [ ['SDL_RWops'], 'uint64' ],
49 SDL_ReadBE64 => [ ['SDL_RWops'], 'uint64' ],
50 #
51 SDL_WriteU8 => [ [ 'SDL_RWops', 'uint8' ], 'size_t' ],
52 SDL_WriteLE16 => [ [ 'SDL_RWops', 'uint16' ], 'size_t' ],
53 SDL_WriteBE16 => [ [ 'SDL_RWops', 'uint16' ], 'size_t' ],
54 SDL_WriteLE32 => [ [ 'SDL_RWops', 'uint32' ], 'size_t' ],
55 SDL_WriteBE32 => [ [ 'SDL_RWops', 'uint32' ], 'size_t' ],
56 SDL_WriteLE64 => [ [ 'SDL_RWops', 'uint64' ], 'size_t' ],
57 SDL_WriteBE64 => [ [ 'SDL_RWops', 'uint64' ], 'size_t' ]
58 };
59  
60 =encoding utf-8
61  
62 =head1 NAME
63  
64 SDL2::rwops - General Interface to Read and Write Data Streams
65  
66 =head1 SYNOPSIS
67  
68 use SDL2 qw[:rwops];
69  
70 =head1 DESCRIPTION
71  
72 This package provides a general interface for SDL to read and write data
73 streams. It can easily be extended to files, memory, etc.
74  
75 =head1 Functions
76  
77 These functions may imported by name or with the C<:rwops> tag.
78  
79 =head2 C<SDL_RWFromFile( ... )>
80  
81 Creates a L<SDL2::RWops> structure from a file.
82  
83 my $file = SDL_RWFromFile('myimage.bmp', 'rb');
84  
85 Expected parameters include:
86  
87 =over
88  
89 =item C<file> - a UTF-8 string representing the filename to open
90  
91 =item C<mode> - an ASCII string representing the mode to be used for opening the file
92  
93 =back
94  
95 The C<mode> string is treated roughly the same as in a call to the C library's
96 C<fopen()>, even if SDL doesn't happen to use C<fopen()> behind the scenes.
97  
98 Available mode strings:
99  
100 =over
101  
102 =item C<r>
103  
104 Open a file for reading. The file must exist.
105  
106 =item C<w>
107  
108 Create an empty file for writing. If a file with the same name already exists
109 its content is erased and the file is treated as a new empty file.
110  
111 =item C<a>
112  
113 Append to a file. Writing operations append data at the end of the file. The
114 file is created if it does not exist.
115  
116 =item C<r+>
117  
118 Open a file for update both reading and writing. The file must exist.
119  
120 =item C<w+>
121  
122 Create an empty file for both reading and writing. If a file with the same name
123 already exists its content is erased and the file is treated as a new empty
124 file.
125  
126 =item C<a+>
127  
128 Open a file for reading and appending. All writing operations are performed at
129 the end of the file, protecting the previous content to be overwritten. You can
130 reposition (fseek, rewind) the internal pointer to anywhere in the file for
131 reading, but writing operations will move it back to the end of file. The file
132 is created if it does not exist.
133  
134 =back
135  
136 In order to open a file as a binary file, a "b" character has to be included in
137 the C<mode> string. This additional "b" character can either be appended at the
138 end of the string (thus making the following compound modes: "rb", "wb", "ab",
139 "r+b", "w+b", "a+b") or be inserted between the letter and the "+" sign for the
140 mixed modes ("rb+", "wb+", "ab+"). Additional characters may follow the
141 sequence, although they should have no effect. For example, "t" is sometimes
142 appended to make explicit the file is a text file.
143  
144 This function supports Unicode filenames, but they must be encoded in UTF-8
145 format, regardless of the underlying operating system.
146  
147 Returns a new L<SDL2::RWops> structure on success or undef on failure; call
148 C<SDL_GetErro( )> for more information.
149  
150 =head2 C<SDL_RWFromFP( ... )>
151  
152 Create a C<SDL2::RWops> structure from a standard I/O file pointer.
153  
154 use FFI::C::File;
155 my $file = FFI::C::File->fopen( "foo.txt", "w" );
156 my $ops = SDL_RWFromFP( $file, SDL_FALSE );
157  
158 Expected parameters include:
159  
160 =over
161  
162 =item C<fp> - L<FFI::C::File> object which wraps the C C<FILE> pointer
163  
164 =item C<autoclose> - C<SDL_TRUE> to close the file handle when closing the L<SDL2::RWops>, C<SDL_FALSE> to leave the file handle open when the RWops is closed
165  
166 =back
167  
168 Returns a new L<SDL2::RWops> structure on success or undef on failure; call
169 C<SDL_GetErro( )> for more information.
170  
171 =head2 C<SDL_RWFromMem( ... )>
172  
173 Use this function to prepare a read-write memory buffer for use with
174 L<SDL2::RWops>.
175  
176 my $bitmap = ' ' x 100; # Must preallocate it in perl
177 my $ops = SDL_RWFromMem( $bitmap, length $bitmap );
178 # ...write to $ops...
179 print $bitmap; # contains everything you wrote
180  
181 Expected parameters include:
182  
183 =over
184  
185 =item C<mem> - pointer to a buffer to feed an L<SDL2::RWops> stream
186  
187 =item C<size> - the buffer size, in bytes
188  
189 =back
190  
191 Returns a new L<SDL2::RWops> structure on success or undef on failure; call
192 C<SDL_GetErro( )> for more information.
193  
194 This memory buffer is not copied by the RWops; the pointer you provide must
195 remain valid until you close the stream. Closing the stream will not free the
196 original buffer.
197  
198 =head2 C<SDL_RWFromConstMem( ... )>
199  
200 Use this function to prepare a read-only memory buffer for use with RWops.
201  
202 my $bitmap = ...; # raw data
203 my $rw = SDL_RWFromConstMem( $bitmap, length $bitmap );
204 my $img = SDL_LoadBMP_RW( $rw, 1 );
205 # Do something with img
206  
207 Expected parameters include:
208  
209 =over
210  
211 =item C<mem> - pointer to a read-only buffer to fee to an L<SDL2::RWops> stream
212  
213 =item C<size> - the buffer size, in bytes
214  
215 =back
216  
217 Returns a new L<SDL2::RWops> structure on success or undef on failure; call
218 C<SDL_GetErro( )> for more information.
219  
220 This function sets up an L<SDL2::RWops> struct based on a memory area of a
221 certain size. It assumes the memory area is not writable.
222  
223 Attempting to write to this RWops stream will report an error without writing
224 to the memory buffer.
225  
226 This memory buffer is not copied by the RWops; the pointer you provide must
227 remain valid until you close the stream. Closing the stream will not free the
228 original buffer.
229  
230 =head2 C<SDL_AllocRW( ... )>
231  
232 Creates an empty, unpopulated L<SDL2::RWops> structure.
233  
234 my $rw = SDL_AllocRW( );
235  
236 Returns a new L<SDL2::RWops> structure on success or undef on failure; call
237 C<SDL_GetErro( )> for more information.
238  
239 Applications do not need to use this function unless they are providing their
240 own C<SDL_RWops> implementation. If you just need a C<SDL_RWops> to read/write
241 a common data source, you should use the built-in implementations in SDL, like
242 C<SDL_RWFromFile( )> or C<SDL_RWFromMem( ... )>, etc.
243  
244 You must free the returned pointer with C<SDL_FreeRW( ... )>. Depending on your
245 operating system and compiler, there may be a difference between the C<malloc(
246 )> and C<free( )> your program uses and the versions SDL calls internally.
247 Trying to mix the two can cause crashing such as segmentation faults. Since all
248 C<SDL_RWops> must free themselves when their close method is called, all
249 C<SDL_RWops> must be allocated through this function, so they can all be freed
250 correctly with C<SDL_FreeRW( )>.
251  
252 =head2 C<SDL_FreeRW( ... )>
253  
254 Use this function to free an L<SDL2::RWops> structure allocated by L<<
255 C<SDL_AllocRW( ... )>|/C<SDL_AllocRW( ... )> >>.
256  
257 SDL_FreeRW( $rw );
258  
259 Expected parameters include:
260  
261 =over
262  
263 =item C<area> - the L<SDL2::RWops> structure to be freed
264  
265 =back
266  
267 Applications do not need to use this function unless they are providing their
268 own L<SDL2::RWops> implementation. If you just need a L<SDL2::RWops> to
269 read/write a common data source, you should use the built-in implementations in
270 SDL, like C<SDL_RWFromFile( ... )> or C<SDL_RWFromMem( ... )>, etc, and call
271 the close method on those L<SDL2::RWops> pointers when you are done with them.
272  
273 Only use C<SDL_FreeRW( ... )> on pointers returned by C<SDL_AllocRW( )>. The
274 pointer is invalid as soon as this function returns. Any extra memory allocated
275 during creation of the L<SDL2::RWops> is not freed by C<SDL_FreeRW( ... )>; the
276 programmer must be responsible for managing that memory in their close method.
277  
278 =head2 C<SDL_RWsize( ... )>
279  
280 Get the size of the data stream in an L<SDL2::RWops>.
281  
282 Expected parameters include:
283  
284 =over
285  
286 =item C<context> - the L<SDL2::RWops> structure to query
287  
288 =back
289  
290 Returns the size of the data stream on success, C<-1> if unknown or a negative
291 error code on failure; call C<SDL_GetError( )> for more information.
292  
293 =head2 C<SDL_RWseek( ... )>
294  
295 Seek within an L<SDL2::RWops> data stream.
296  
297 This function seeks to byte C<offset>, relative to C<whence>.
298  
299 Expected parameters include:
300  
301 =over
302  
303 =item C<context> - the L<SDL2::RWops> structure to seek through
304  
305 =item C<offset> - an offset in bytes, relative to C<whence> location; can be negative
306  
307 =item C<whence> - any of the L<< RWops week (C<whence>) values|/RWops Seek (C<whence>) Values >>
308  
309 =back
310  
311 Returns the final offset in the data stream after the seek or C<-1> on error.
312  
313 =head2 C<SDL_RWtell( ... )>
314  
315 Determine the current read/write offset in an L<SDL2::RWops> data stream.
316  
317 Expected parameters include:
318  
319 =over
320  
321 =item C<context> - the L<SDL2::RWops> structure to query
322  
323 =back
324  
325 Returns the current offset in the stream, or C<-1> if the information can not
326 be determined.
327  
328 =head2 C<SDL_RWread( ... )>
329  
330 Read from a data source.
331  
332 This function reads up to C<maxnum> objects each of size C<size> from the data
333 source to the area pointed at by C<ptr>. This function may read less objects
334 than requested. It will return zero when there has been an error or the data
335 stream is completely read.
336  
337 Expected parameters include:
338  
339 =over
340  
341 =item C<context> - the L<SDL2::RWops> structure to read
342  
343 =item C<ptr> - a pointer to a buffer to read data into
344  
345 =item C<size> - the size of each object to read, in bytes
346  
347 =item C<maxnum> - the maximum number of objects to be read
348  
349 =back
350  
351 Returns the number of objects read, or C<0> at error or end of file; call
352 C<SDL_GetError( )> for more information.
353  
354 =head2 C<SDL_RWwrite( ... )>
355  
356 Write to an L<SDL2::RWops> data stream.
357  
358 This function writes exactly C<num> objects each of size C<size> from the area
359 pointed at by C<ptr> to the stream. If this fails for any reason, it'll return
360 less than C<num> to demonstrate how far the write progressed. On success, it
361 returns C<num>.
362  
363 Expected parameters include:
364  
365 =over
366  
367 =item C<context> - a pointer to an L<SDL2::RWops> structure
368  
369 =item C<ptr> - a pointer to a buffer containing data to write
370  
371 =item C<size> - the size of an object to write, in bytes
372  
373 =item C<num> - the number of objects to write
374  
375 =back
376  
377 Returns the number of objects written, which will be less than C<num> on error;
378 call C<SDL_GetError( )> for more information.
379  
380 =head2 C<SDL_RWclose( ... )>
381  
382 Close and free an allocated L<SDL2::RWops> structure.
383  
384 This function releases any resources used by the stream and frees the structure
385 itself with C<SDL_FreeRW( ... )>.
386  
387 Expected parameters include:
388  
389 =over
390  
391 =item C<context> - a pointer to an L<SDL2::RWops> structure to close
392  
393 =back
394  
395 Returns C<0> on success, or C<-1> if the stream failed to flush to its output
396 (e.g. to disk); call C<SDL_GetError( )> for more information.
397  
398 =head2 C<SDL_LoadFile_RW( ... )>
399  
400 Load all the data from an SDL data stream.
401  
402 The data is allocated with a zero byte at the end (null terminated) for
403 convenience. This extra byte is not included in the value reported via
404 C<datasize>.
405  
406 The data should be freed with C<SDL_free( ... )>.
407  
408 Expected parameters include:
409  
410 =over
411  
412 =item C<src> - the L<SDL2::RWops> to read all available data from
413  
414 =item C<datasize> - if not C<undef>, will store the number of bytes read
415  
416 =item C<freesrc> - if non-zero, calls C<SDL_RWclose( ... )> on C<src> before returning
417  
418 =back
419  
420 Returns the data, or C<undef> if there was an error.
421  
422 =head2 C<SDL_LoadFile( ... )>
423  
424 Load all the data from a file path.
425  
426 The data is allocated with a zero byte at the end (null terminated) for
427 convenience. This extra byte is not included in the value reported via
428 C<datasize>.
429  
430 The data should be freed with C<SDL_free( ... )>.
431  
432 Expected parameters include:
433  
434 =over
435  
436 =item C<file> - the path to read all available data from
437  
438 =item C<datasize> - if not C<undef>, will store the number of bytes read
439  
440 =back
441  
442 Returns the data, or C<undef> if there was an error.
443  
444 =head2 C<SDL_ReadU8( ... )>
445  
446 Read an item of little endianness and return in native format.
447  
448 Expected parameters include:
449  
450 =over
451  
452 =item C<src> - a L<SDL2::RWops> structure to read from
453  
454 =back
455  
456 Returns a 8-bit integer.
457  
458 =head2 C<SDL_ReadLE16( ... )>
459  
460 Read an item of little endianness and return in native format.
461  
462 Expected parameters include:
463  
464 =over
465  
466 =item C<src> - a L<SDL2::RWops> structure to read from
467  
468 =back
469  
470 Returns a 16-bit integer.
471  
472 =head2 C<SDL_ReadBE16( ... )>
473  
474 Read an item of big endianness and return in native format.
475  
476 Expected parameters include:
477  
478 =over
479  
480 =item C<src> - a L<SDL2::RWops> structure to read from
481  
482 =back
483  
484 Returns a 16-bit integer.
485  
486 =head2 C<SDL_ReadLE32( ... )>
487  
488 Read an item of little endianness and return in native format.
489  
490 Expected parameters include:
491  
492 =over
493  
494 =item C<src> - a L<SDL2::RWops> structure to read from
495  
496 =back
497  
498 Returns a 32-bit integer.
499  
500 =head2 C<SDL_ReadBE32( ... )>
501  
502 Read an item of big endianness and return in native format.
503  
504 Expected parameters include:
505  
506 =over
507  
508 =item C<src> - a L<SDL2::RWops> structure to read from
509  
510 =back
511  
512 Returns a 32-bit integer.
513  
514 =head2 C<SDL_ReadLE64( ... )>
515  
516 Read an item of little endianness and return in native format.
517  
518 Expected parameters include:
519  
520 =over
521  
522 =item C<src> - a L<SDL2::RWops> structure to read from
523  
524 =back
525  
526 Returns a 64-bit integer.
527  
528 =head2 C<SDL_ReadBE64( ... )>
529  
530 Read an item of big endianness and return in native format.
531  
532 Expected parameters include:
533  
534 =over
535  
536 =item C<src> - a L<SDL2::RWops> structure to read from
537  
538 =back
539  
540 Returns a 64-bit integer.
541  
542 =head2 C<SDL_WriteU8( ... )>
543  
544 Write an item of native format to little endianness.
545  
546 Expected parameters include:
547  
548 =over
549  
550 =item C<dst> - a L<SDL2::RWops> structure to write to
551  
552 =item C<value> - 8-bit unsigned integer
553  
554 =back
555  
556 Returns the amount of data written, in bytes.
557  
558 =head2 C<SDL_WriteLE16( ... )>
559  
560 Write an item of native format to little endianness.
561  
562 Expected parameters include:
563  
564 =over
565  
566 =item C<dst> - a L<SDL2::RWops> structure to write to
567  
568 =item C<value> - 16-bit unsigned integer
569  
570 =back
571  
572 Returns the amount of data written, in bytes.
573  
574 =head2 C<SDL_WriteBE16( ... )>
575  
576 Write an item of native format to big endianness.
577  
578 Expected parameters include:
579  
580 =over
581  
582 =item C<dst> - a L<SDL2::RWops> structure to write to
583  
584 =item C<value> - 16-bit unsigned integer
585  
586 =back
587  
588 Returns the amount of data written, in bytes.
589  
590 =head2 C<SDL_WriteLE32( ... )>
591  
592 Write an item of native format to little endianness.
593  
594 Expected parameters include:
595  
596 =over
597  
598 =item C<dst> - a L<SDL2::RWops> structure to write to
599  
600 =item C<value> - 32-bit unsigned integer
601  
602 =back
603  
604 Returns the amount of data written, in bytes.
605  
606 =head2 C<SDL_WriteBE32( ... )>
607  
608 Write an item of native format to big endianness.
609  
610 Expected parameters include:
611  
612 =over
613  
614 =item C<dst> - a L<SDL2::RWops> structure to write to
615  
616 =item C<value> - 32-bit unsigned integer
617  
618 =back
619  
620 Returns the amount of data written, in bytes.
621  
622 =head2 C<SDL_WriteLE64( ... )>
623  
624 Write an item of native format to little endianness.
625  
626 Expected parameters include:
627  
628 =over
629  
630 =item C<dst> - a L<SDL2::RWops> structure to write to
631  
632 =item C<value> - 64-bit unsigned integer
633  
634 =back
635  
636 Returns the amount of data written, in bytes.
637  
638 =head2 C<SDL_WriteBE64( ... )>
639  
640 Write an item of native format to big endianness.
641  
642 Expected parameters include:
643  
644 =over
645  
646 =item C<dst> - a L<SDL2::RWops> structure to write to
647  
648 =item C<value> - 64-bit unsigned integer
649  
650 =back
651  
652 Returns the amount of data written, in bytes.
653  
654 =head1 Defined Values and Enumerations
655  
656 These may be imported by name of with the C<:rwops> tag.
657  
658 =head2 RWops Types
659  
660 =over
661  
662 =item C<SDL_RWOPS_UNKNOWN> - Unknown stream type
663  
664 =item C<SDL_RWOPS_WINFILE> - Win32 file
665  
666 =item C<SDL_RWOPS_STDFILE> - Stdio file
667  
668 =item C<SDL_RWOPS_JNIFILE> - Android asset
669  
670 =item C<SDL_RWOPS_MEMORY> - Memory stream
671  
672 =item C<SDL_RWOPS_MEMORY_RO> - Read-Only memory stream
673  
674 =item C<SDL_RWOPS_VITAFILE> - Vita file (if applicable)
675  
676 =back
677  
678 =head2 RWops Seek (C<whence>) Values
679  
680 =over
681  
682 =item C<RW_SEEK_SET> - Seek from the beginning of data
683  
684 =item C<RW_SEEK_CUR> - Seek relative to current read point
685  
686 =item C<RW_SEEK_END> - Seek relative to the end of data
687  
688 =back
689  
690 =head1 LICENSE
691  
692 Copyright (C) Sanko Robinson.
693  
694 This library is free software; you can redistribute it and/or modify it under
695 the terms found in the Artistic License 2. Other copyrights, terms, and
696 conditions may apply to data transmitted through this module.
697  
698 =head1 AUTHOR
699  
700 Sanko Robinson E<lt>sanko@cpan.orgE<gt>
701  
702 =begin stopwords
703  
704 rb wb rb+ wb+ RWops fseek struct unpopulated endianness
705  
706 =end stopwords
707  
708 =cut
709  
710 };
711 1;