1 /*
2 DSFML - The Simple and Fast Multimedia Library for D
3 
4 Copyright (c) 2013 - 2015 Jeremy DeHaan (dehaan.jeremiah@gmail.com)
5 
6 This software is provided 'as-is', without any express or implied warranty.
7 In no event will the authors be held liable for any damages arising from the use of this software.
8 
9 Permission is granted to anyone to use this software for any purpose, including commercial applications,
10 and to alter it and redistribute it freely, subject to the following restrictions:
11 
12 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
13 If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
14 
15 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
16 
17 3. This notice may not be removed or altered from any source distribution
18 */
19 
20 ///A module containing the InputStream interface.
21 module dsfml.system.inputstream;
22 
23 /**
24 *Abstract class for custom file input streams.
25 *
26 *This class allows users to define their own file input sources from which SFML can load resources.
27 *
28 *SFML resource classes like sf::Texture and sf::SoundBuffer provide loadFromFile and loadFromMemory functions, which read data from conventional sources.
29 *However, if you have data coming from a different source (over a network, embedded, encrypted, compressed, etc) you can derive your own class from sf::InputStream and load SFML resources with their loadFromStream function.
30 */
31 interface InputStream
32 {
33 	///Read data from the stream.
34 	///
35 	///Params:
36  	///	data =	Buffer where to copy the read data
37  	///			and sized to the amount of bytes to be read.
38  	///
39  	///Returns: The number of bytes actually read, or -1 on error.
40 	long read(void[] data);
41 
42 	///Change the current reading position.
43 	///Params:
44     ///			position = The position to seek to, from the beginning.
45     ///
46 	///Returns: The position actually sought to, or -1 on error.
47 	long seek(long position);
48 
49 	///Get the current reading position in the stream.
50 	///
51 	///Returns: The current position, or -1 on error. 
52 	long tell();
53 
54 	///Return the size of the stream.
55 	///
56 	///Returns: The total number of bytes available in the stream, or -1 on error.
57 	long getSize();
58 }
59 
60 unittest
61 {
62 	version(DSFML_Unittest_System)
63 	{
64 		import dsfml.graphics.texture;
65 		import std.stdio;
66 
67 
68 
69 		//File Stream ported from Laurent's example here: http://www.sfml-dev.org/tutorials/2.0/system-stream.php
70 
71 		class FileStream:InputStream
72 		{
73 			File m_file;
74 		
75 			this()
76 			{
77 				// Constructor code
78 			}
79 			bool open(string fileName)
80 			{
81 				try
82 				{
83 					m_file.open(fileName);
84 				}
85 				catch(Exception e)
86 				{
87 					writeln(e.msg);
88 				}
89 			
90 				return m_file.isOpen;
91 			}
92 		
93 			long read(void[] data)
94 			{
95 			
96 				if(m_file.isOpen)
97 				{
98 					return m_file.rawRead(data).length;
99 				}
100 				else
101 				{
102 					return -1;
103 				}
104 			}
105 		
106 			long seek(long position)
107 			{
108 				if(m_file.isOpen)
109 				{
110 					m_file.seek(position);
111 					return tell();
112 				}
113 				else
114 				{
115 					return -1;
116 				}
117 			}
118 		
119 			long tell()
120 			{
121 			
122 				if(m_file.isOpen)
123 				{
124 					return m_file.tell;
125 				}
126 				else
127 				{
128 					return -1;
129 				}
130 			}
131 		
132 			long getSize()
133 			{
134 				if(m_file.isOpen)
135 				{
136 					long position = m_file.tell;
137 				
138 					m_file.seek(0,SEEK_END);
139 				
140 					long size = tell();
141 				
142 					seek(position);
143 					
144 					return size;
145 				
146 				
147 				}
148 				else
149 				{
150 					return -1;
151 				}
152 			}
153 		}
154 	
155 
156 
157 
158 		writeln("Unit test for InputStream");
159 
160 		auto streamTexture = new Texture();
161 
162 		writeln();
163 		writeln("Using a basic file stream to load a non existant texture to confirm correct errors are found.");
164 
165 
166 		auto failStream = new FileStream();
167 		failStream.open("nonexistantTexture.png");//doesn't open the stream, but you should be checking if open returns true
168 		streamTexture.loadFromStream(failStream);//prints errors to err
169 
170 		writeln();
171 		writeln("Using a basic file stream to load a texture that exists.");
172 		auto successStream = new FileStream();
173 		successStream.open("res/TestImage.png");//using a png of Crono for now. Will replace with something that won't get me in trouble
174 		assert(streamTexture.loadFromStream(successStream));
175 		
176 		writeln("Texture loaded!");
177 		
178 
179 		writeln();
180 
181 	}
182 }
183 
184 
185 
186 
187