1 module nudsfml.system.inputstream;
2 
3 /**
4 * Interface for custom file input streams.
5 */
6 interface InputStream
7 {
8 	/**
9 	 * Read data from the stream.
10 	 *
11 	 * Params:
12  	 * 	data =	Buffer where to copy the read data
13  	 * 			and sized to the amount of bytes to be read
14  	 *
15  	 * Returns: The number of bytes actually read, or -1 on error.
16 	 */
17 	long read(void[] data);
18 
19 	/**
20 	 * Change the current reading position.
21 	 * Params:
22      * 		position = The position to seek to, from the beginning
23      *
24 	 * Returns: The position actually sought to, or -1 on error.
25 	 */
26 	long seek(long position);
27 
28 	/**
29 	 * Get the current reading position in the stream.
30 	 *
31 	 * Returns: The current position, or -1 on error.
32 	 */
33 	long tell();
34 
35 	/**
36 	 * Return the size of the stream.
37 	 *
38 	 * Returns: Total number of bytes available in the stream, or -1 on error.
39 	 */
40 	long getSize();
41 }
42 
43 unittest
44 {
45 	//version(DSFML_Unittest_System)
46 	version(none) //temporarily not doing this test
47 	{
48 		import nudsfml.graphics.texture;
49 		import std.stdio;
50 
51 		//File Stream ported from Laurent's example here:
52 		//http://www.sfml-dev.org/tutorials/2.0/system-stream.php
53 
54 		class FileStream:InputStream
55 		{
56 			File m_file;
57 
58 			this()
59 			{
60 				// Constructor code
61 			}
62 			bool open(string fileName)
63 			{
64 				try
65 				{
66 					m_file.open(fileName);
67 				}
68 				catch(Exception e)
69 				{
70 					writeln(e.msg);
71 				}
72 
73 				return m_file.isOpen;
74 			}
75 
76 			long read(void[] data)
77 			{
78 
79 				if(m_file.isOpen)
80 				{
81 					return m_file.rawRead(data).length;
82 				}
83 				else
84 				{
85 					return -1;
86 				}
87 			}
88 
89 			long seek(long position)
90 			{
91 				if(m_file.isOpen)
92 				{
93 					m_file.seek(position);
94 					return tell();
95 				}
96 				else
97 				{
98 					return -1;
99 				}
100 			}
101 
102 			long tell()
103 			{
104 
105 				if(m_file.isOpen)
106 				{
107 					return m_file.tell;
108 				}
109 				else
110 				{
111 					return -1;
112 				}
113 			}
114 
115 			long getSize()
116 			{
117 				if(m_file.isOpen)
118 				{
119 					long position = m_file.tell;
120 
121 					m_file.seek(0,SEEK_END);
122 
123 					long size = tell();
124 
125 					seek(position);
126 
127 					return size;
128 				}
129 				else
130 				{
131 					return -1;
132 				}
133 			}
134 		}
135 
136 		writeln("Unit test for InputStream");
137 
138 		auto streamTexture = new Texture();
139 
140 		writeln();
141 		writeln("Using a basic file stream to load a non existant texture to confirm correct errors are found.");
142 
143 		auto failStream = new FileStream();
144 		failStream.open("nonexistantTexture.png");//doesn't open the stream, but you should be checking if open returns true
145 		streamTexture.loadFromStream(failStream);//prints errors to err
146 
147 		writeln();
148 		writeln("Using a basic file stream to load a texture that exists.");
149 		auto successStream = new FileStream();
150 		successStream.open("res/TestImage.png");//using a png of Crono for now. Will replace with something that won't get me in trouble
151 		assert(streamTexture.loadFromStream(successStream));
152 
153 		writeln("Texture loaded!");
154 
155 		writeln();
156 	}
157 }