NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Stream.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "io/Stream.hpp"
29 
30 #include <stdio.h>
31 
32 using namespace FW;
33 
34 //------------------------------------------------------------------------
35 
37 {
38  S32 numRead = read(ptr, size);
39  if (numRead != size)
40  {
41  FW_ASSERT(numRead >= 0 && numRead <= size);
42  memset((U8*)ptr + numRead, 0, size - numRead);
43  setError("Unexpected end of stream!");
44  }
45 }
46 
47 //------------------------------------------------------------------------
48 
50 : m_stream (stream),
51  m_numRead (0),
52  m_numConsumed (0),
53  m_buffer (NULL, bufferSize)
54 {
55  FW_ASSERT(bufferSize > 0);
56 }
57 
58 //------------------------------------------------------------------------
59 
61 {
62 }
63 
64 //------------------------------------------------------------------------
65 
67 {
68  if (!size)
69  return 0;
70 
71  FW_ASSERT(ptr && size > 0);
72  int ofs = 0;
73  while (ofs < size)
74  {
75  fillBuffer(1);
76  int num = min(size - ofs, getBufferSize());
77  if (!num)
78  break;
79 
80  memcpy((U8*)ptr + ofs, getBufferPtr(), num);
81  consumeBuffer(num);
82  ofs += num;
83  }
84  return ofs;
85 }
86 
87 //------------------------------------------------------------------------
88 
89 char* BufferedInputStream::readLine(bool combineWithBackslash, bool normalizeWhitespace)
90 {
91  if (!getBufferSize() && !fillBuffer(1))
92  return NULL;
93 
94  U8* ptr = getBufferPtr();
95  int size = getBufferSize();
96  int inPos = 0;
97  int outPos = 0;
98  bool pendingBackslash = false;
99 
100  for (;;)
101  {
102  U8 chr = ptr[inPos++];
103  if (chr >= 32 && chr != '\\' && !pendingBackslash)
104  ptr[outPos++] = chr;
105  else if (chr == '\n')
106  {
107  if (!pendingBackslash)
108  break;
109  ptr[outPos++] = ' ';
110  pendingBackslash = false;
111  }
112  else if (chr != '\r')
113  {
114  if (pendingBackslash)
115  {
116  ptr[outPos++] = '\\';
117  pendingBackslash = false;
118  }
119  if (chr == '\t' && normalizeWhitespace)
120  ptr[outPos++] = ' ';
121  else if (chr == '\\' && combineWithBackslash)
122  pendingBackslash = true;
123  else
124  ptr[outPos++] = chr;
125  }
126 
127  if (inPos == size)
128  {
129  fillBuffer(inPos + 1);
130  ptr = getBufferPtr();
131  size = getBufferSize();
132  if (inPos == size)
133  {
134  if (pendingBackslash)
135  ptr[outPos++] = '\\';
136  break;
137  }
138  }
139  }
140 
141  ptr[outPos] = '\0';
142  char* line = (char*)ptr;
143  consumeBuffer(inPos);
144  return line;
145 }
146 
147 //------------------------------------------------------------------------
148 
150 {
151  FW_ASSERT(size >= 0);
152 
153  // Already have the data => done.
154 
155  if (m_numRead - size >= m_numConsumed)
156  return true;
157 
158  // Buffer is full => grow or shift.
159 
160  if (m_numRead == m_buffer.getSize())
161  {
162  if (!m_numConsumed)
163  m_buffer.resize(m_buffer.getSize() * 2);
164  else
165  {
166  memcpy(m_buffer.getPtr(), m_buffer.getPtr(m_numConsumed), m_numRead - m_numConsumed);
167  m_numRead -= m_numConsumed;
168  m_numConsumed = 0;
169  }
170  }
171 
172  // Read more data.
173 
174  m_numRead += m_stream.read(m_buffer.getPtr(m_numRead), m_buffer.getSize() - m_numRead);
175  return (m_numRead - size >= m_numConsumed);
176 }
177 
178 //------------------------------------------------------------------------
179 
181 {
182  FW_ASSERT(num >= 0);
183  int numLeft = num;
184  while (numLeft)
185  {
186  fillBuffer(1);
187  int tmp = min(numLeft, m_numRead - m_numConsumed);
188  numLeft -= tmp;
189  m_numConsumed += tmp;
190  }
191 }
192 
193 //------------------------------------------------------------------------
194 
195 BufferedOutputStream::BufferedOutputStream(OutputStream& stream, int bufferSize, bool writeOnLF, bool emulateCR)
196 : m_stream (stream),
197  m_writeOnLF (writeOnLF),
198  m_emulateCR (emulateCR),
199 
200  m_buffer (NULL, bufferSize),
201  m_numValid (0),
202  m_lineStart (0),
203  m_currOfs (0),
204  m_numFlushed (0)
205 {
206  FW_ASSERT(bufferSize > 0);
207 }
208 
209 //------------------------------------------------------------------------
210 
212 {
213 }
214 
215 //------------------------------------------------------------------------
216 
217 void BufferedOutputStream::write(const void* ptr, int size)
218 {
219  if (size <= 0)
220  return;
221 
222  int ofs = 0;
223  for (;;)
224  {
225  int num = min(size - ofs, m_buffer.getSize() - m_numValid);
226  memcpy(m_buffer.getPtr(m_numValid), (const U8*)ptr + ofs, num);
227  addValid(num);
228 
229  ofs += num;
230  if (ofs >= size)
231  break;
232 
233  flushInternal();
234  }
235 }
236 
237 //------------------------------------------------------------------------
238 
239 void BufferedOutputStream::writef(const char* fmt, ...)
240 {
241  va_list args;
242  va_start(args, fmt);
243  writefv(fmt, args);
244  va_end(args);
245 }
246 
247 //------------------------------------------------------------------------
248 
249 void BufferedOutputStream::writefv(const char* fmt, va_list args)
250 {
251  int space = m_buffer.getSize() - m_numValid;
252  int size = vsnprintf_s((char*)m_buffer.getPtr(m_numValid), space, space - 1, fmt, args);
253  if (size >= 0)
254  {
255  addValid(size);
256  return;
257  }
258 
259  flushInternal();
260  size = _vscprintf(fmt, args);
261  if (size < m_buffer.getSize())
262  addValid(vsprintf_s((char*)m_buffer.getPtr(), m_buffer.getSize(), fmt, args));
263  else
264  {
265  char* tmp = new char[size + 1];
266  vsprintf_s(tmp, size + 1, fmt, args);
267  m_stream.write(tmp, size);
268  m_numFlushed += size;
269  delete[] tmp;
270  }
271 }
272 
273 //------------------------------------------------------------------------
274 
276 {
277  m_lineStart = 0;
278  flushInternal();
279  m_stream.flush();
280 }
281 
282 //------------------------------------------------------------------------
283 
284 void BufferedOutputStream::addValid(int size)
285 {
286  FW_ASSERT(size >= 0);
287  if (!size)
288  return;
289 
290  // Increase valid size.
291 
292  int old = m_numValid;
293  m_numValid += size;
294  if (!m_writeOnLF && !m_emulateCR)
295  return;
296 
297  // Write on LF => find the last LF.
298 
299  if (!m_emulateCR)
300  {
301  for (int i = m_numValid - 1; i >= old; i--)
302  {
303  if (m_buffer[i] == '\n')
304  {
305  m_lineStart = i + 1;
306  flushInternal();
307  break;
308  }
309  }
310  return;
311  }
312 
313  // Emulate CR => scan through the new bytes.
314 
315  int lineEnd = old;
316  for (int i = old; i < m_numValid; i++)
317  {
318  U8 v = m_buffer[i];
319  if (v == '\r')
320  m_currOfs = m_lineStart;
321  else if (v == '\n')
322  {
323  m_currOfs = lineEnd;
324  m_buffer[m_currOfs++] = v;
325  m_lineStart = m_currOfs;
326  lineEnd = m_currOfs;
327  }
328  else
329  {
330  m_buffer[m_currOfs++] = v;
331  lineEnd = max(lineEnd, m_currOfs);
332  }
333  }
334 
335  m_numValid = lineEnd;
336  if (m_writeOnLF && m_lineStart)
337  flushInternal();
338 }
339 
340 //------------------------------------------------------------------------
341 
342 void BufferedOutputStream::flushInternal(void)
343 {
344  int size = (m_lineStart) ? m_lineStart : m_numValid;
345  if (!size)
346  return;
347 
348  m_stream.write(m_buffer.getPtr(), size);
349  m_numFlushed += size;
350 
351  m_numValid -= size;
352  memmove(m_buffer.getPtr(), m_buffer.getPtr(size), m_numValid);
353  m_lineStart = max(m_lineStart - size, 0);
354  m_currOfs = max(m_currOfs - size, 0);
355 }
356 
357 //------------------------------------------------------------------------
358 
360 {
361 }
362 
363 //------------------------------------------------------------------------
364 
365 int MemoryInputStream::read(void* ptr, int size)
366 {
367  int numRead = min(size, m_size - m_ofs);
368  memcpy(ptr, m_ptr + m_ofs, numRead);
369  m_ofs += numRead;
370  return numRead;
371 }
372 
373 //------------------------------------------------------------------------
374 
376 {
377 }
378 
379 //------------------------------------------------------------------------
380 
381 void MemoryOutputStream::write(const void* ptr, int size)
382 {
383  m_data.add((const U8*)ptr, size);
384 }
385 
386 //------------------------------------------------------------------------
387 
389 {
390 }
391 
392 //------------------------------------------------------------------------
virtual void flush(void)
Definition: Stream.cpp:275
#define NULL
Definition: Defs.hpp:39
BufferedOutputStream(OutputStream &stream, int bufferSize=64<< 10, bool writeOnLF=false, bool emulateCR=false)
Definition: Stream.cpp:195
void setError(const char *fmt,...)
Definition: Defs.cpp:253
void ** ptr
Definition: DLLImports.cpp:74
virtual void write(const void *ptr, int size)
Definition: Stream.cpp:217
virtual void write(const void *ptr, int size)=0
U8 * getBufferPtr(void)
Definition: Stream.hpp:91
char * readLine(bool combineWithBackslash=false, bool normalizeWhitespace=false)
Definition: Stream.cpp:89
virtual void flush(void)
Definition: Stream.cpp:388
virtual int read(void *ptr, int size)
Definition: Stream.cpp:365
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v
Definition: DLLImports.inl:329
void readFully(void *ptr, int size)
Definition: Stream.cpp:36
FW_CUDA_FUNC T min(const VectorBase< T, L, S > &v)
Definition: Math.hpp:461
FW_CUDA_FUNC T max(const VectorBase< T, L, S > &v)
Definition: Math.hpp:462
BufferedInputStream(InputStream &stream, int bufferSize=64<< 10)
Definition: Stream.cpp:49
virtual void flush(void)=0
#define FW_ASSERT(X)
Definition: Defs.hpp:67
signed int S32
Definition: Defs.hpp:88
void consumeBuffer(int num)
Definition: Stream.cpp:180
T & add(void)
Definition: Array.hpp:384
virtual ~BufferedInputStream(void)
Definition: Stream.cpp:60
virtual ~MemoryOutputStream(void)
Definition: Stream.cpp:375
int getBufferSize(void)
Definition: Stream.hpp:90
unsigned char U8
Definition: Defs.hpp:83
virtual void write(const void *ptr, int size)
Definition: Stream.cpp:381
virtual ~BufferedOutputStream(void)
Definition: Stream.cpp:211
void writefv(const char *fmt, va_list args)
Definition: Stream.cpp:249
void writef(const char *fmt,...)
Definition: Stream.cpp:239
const T * getPtr(S idx=0) const
Definition: Array.hpp:202
bool fillBuffer(int size)
Definition: Stream.cpp:149
virtual ~MemoryInputStream(void)
Definition: Stream.cpp:359
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr size
Definition: DLLImports.inl:319
void resize(S size)
Definition: Array.hpp:366
S getSize(void) const
Definition: Array.hpp:188
virtual int read(void *ptr, int size)
Definition: Stream.cpp:66
virtual int read(void *ptr, int size)=0