Zen API
 All Classes Files Functions Variables Typedefs Friends Macros Modules Pages
kStream.h
Go to the documentation of this file.
1 
10 #ifndef K_API_STREAM_H
11 #define K_API_STREAM_H
12 
13 #include <kApi/kApiDef.h>
14 #include <kApi/Io/kStream.x.h>
15 
22 //typedef kObject kStream; --forward-declared in kApiDef.x.h
23 
24 /*
25 * Public
26 */
27 
37 kInlineFx(kStatus) kStream_Read(kStream stream, void* buffer, kSize size)
38 {
39  kObj(kStream, stream);
40 
41  if ((obj->readEnd - obj->readBegin) < size)
42  {
43  return xkStream_VTable(stream)->VReadSomeImpl(stream, buffer, size, size, kNULL);
44  }
45 
46  kMemCopy(buffer, &obj->readBuffer[obj->readBegin], size);
47  obj->readBegin += size;
48 
49  return kOK;
50 }
51 
63 kInlineFx(kStatus) kStream_ReadSome(kStream stream, void* buffer, kSize minCount, kSize maxCount, kSize* bytesRead)
64 {
65  kObj(kStream, stream);
66  kSize bufferAvailable = obj->readEnd - obj->readBegin;
67 
68  if (bufferAvailable < minCount)
69  {
70  return xkStream_VTable(stream)->VReadSomeImpl(stream, buffer, minCount, maxCount, bytesRead);
71  }
72  else
73  {
74  kSize size = kMin_(bufferAvailable, maxCount);
75 
76  kMemCopy(buffer, &obj->readBuffer[obj->readBegin], size);
77  obj->readBegin += size;
78 
79  if (!kIsNull(bytesRead))
80  {
81  *bytesRead = size;
82  }
83  }
84 
85  return kOK;
86 }
87 
97 kInlineFx(kStatus) kStream_Write(kStream stream, const void* buffer, kSize size)
98 {
99  kObj(kStream, stream);
100 
101  if ((obj->writeEnd - obj->writeBegin) < size)
102  {
103  return xkStream_VTable(stream)->VWriteImpl(stream, buffer, size);
104  }
105 
106  kMemCopy(&obj->writeBuffer[obj->writeBegin], buffer, size);
107  obj->writeBegin += size;
108 
109  return kOK;
110 }
111 
122 {
123  return kStream_CopyEx(stream, source, size, kNULL, kNULL);
124 }
125 
141 kFx(kStatus) kStream_CopyEx(kStream stream, kStream source, kSize size, kCallbackFx progress, kPointer context);
142 
151 kFx(kStatus) kStream_CopyAll(kStream stream, kStream source);
152 
163 {
164  return xkStream_VTable(stream)->VSeek(stream, offset, origin);
165 }
166 
175 {
176  return xkStream_VTable(stream)->VFlush(stream);
177 }
178 
187 {
188  kObj(kStream, stream);
189 
190  kCheckState(!kIsNull(obj->readBuffer));
191 
192  if ((obj->readEnd - obj->readBegin) > 0)
193  {
194  return kOK;
195  }
196 
197  return xkStream_VTable(stream)->VFill(stream);
198 }
199 
208 {
209  kObj(kStream, stream);
210 
211  return obj->bytesRead - (k64u)(obj->readEnd - obj->readBegin);
212 }
213 
222 {
223  kObj(kStream, stream);
224 
225  return obj->bytesWritten + (k64u)obj->writeBegin;
226 }
227 
236 {
237  kObj(kStream, stream);
238 
239  obj->bytesRead = 0;
240  obj->bytesWritten = 0;
241 
242  return kOK;
243 }
244 
245 /*
246 * Protected
247 */
248 
261 {
262  kCheck(kObject_Init(stream, type, allocator));
263 
264  kInitFields(kStream, stream);
265 
266  return kOK;
267 }
268 
278 {
279  return kObject_VRelease(stream);
280 }
281 
304 kInlineFx(kStatus) kStream_VReadSomeImpl(kStream stream, void* buffer, kSize minCount, kSize maxCount, kSize* bytesRead)
305 {
306  //legacy support: for classes that haven't been updated to implement VReadSomeImpl,
307  //try VReadImpl instead (deprecated, but still in the vtable)
308  kCheck(xkStream_VTable(stream)->VReadImpl(stream, buffer, minCount));
309 
310  if (!kIsNull(bytesRead))
311  {
312  *bytesRead = minCount;
313  }
314 
315  return kOK;
316 }
317 
329 kInlineFx(kStatus) kStream_VWriteImpl(kStream stream, const void* buffer, kSize size)
330 {
331  return kERROR_UNIMPLEMENTED;
332 }
333 
346 {
347  return kERROR_UNIMPLEMENTED;
348 }
349 
360 {
361  return kOK;
362 }
363 
374 {
375  return kERROR_UNIMPLEMENTED;
376 }
377 
378 #endif
kStatus kObject_VRelease(kObject object)
Protected virtual method that deallocates any resources owned by the object.
Definition: kObject.h:436
kStatus kStream_VFlush(kStream stream)
Protected virtual method that flushes buffered writes to the underlying medium.
Definition: kStream.h:359
k64u kStream_BytesRead(kStream stream)
Reports the number of bytes read from this stream.
Definition: kStream.h:207
#define kMin_(A, B)
Returns the minimum of two numbers.
Definition: kApiDef.h:1811
Represents a 64-bit unsigned integer.
Represents a void pointer.
#define kIsNull(POINTER)
Tests for equality with null pointer.
Definition: kApiDef.h:339
kStatus kStream_Copy(kStream stream, kStream source, kSize size)
Copies the specified number of bytes from one stream to another.
Definition: kStream.h:121
kStatus kStream_Flush(kStream stream)
Flushes buffered writes to the underlying medium.
Definition: kStream.h:174
kStatus kStream_VRelease(kStream stream)
Protected virtual method that deallocates any resources owned by the object.
Definition: kStream.h:277
kStatus kObject_Init(kObject object, kType type, kAlloc alloc)
Protected method called by derived classes to initialize the kObject base class.
Definition: kObject.h:347
Represents an unsigned integer that can store a pointer address.
Abstract base class for memory allocator types.
#define kCheck(EXPRESSION)
Executes a return statement if the given expression is not kOK.
Definition: kApiDef.h:559
kStatus kStream_ReadSome(kStream stream, void *buffer, kSize minCount, kSize maxCount, kSize *bytesRead)
Reads up to the specified number of bytes from the stream.
Definition: kStream.h:63
kStatus kStream_VWriteImpl(kStream stream, const void *buffer, kSize size)
Protected virtual method that writes the specified number of bytes to the stream. ...
Definition: kStream.h:329
#define kInlineFx(TYPE)
Inline method declaration helper.
Definition: kApiDef.h:26
kStatus kStream_Seek(kStream stream, k64s offset, kSeekOrigin origin)
Moves the read/write pointer to the specified location, if supported by the underlying stream...
Definition: kStream.h:162
kStatus kMemCopy(void *dest, const void *src, kSize size)
Copies memory from a source buffer to a non-overlapping destination.
Represents a stream seek origin.
kStatus kStream_VReadSomeImpl(kStream stream, void *buffer, kSize minCount, kSize maxCount, kSize *bytesRead)
Protected virtual method that deallocates any resources owned by the object.
Definition: kStream.h:304
kStatus kStream_VFill(kStream stream)
Protected virtual method that partially fills the read buffer with bytes from the underlying medium...
Definition: kStream.h:373
#define kObj(TypeName_T, T_object)
Declares a local "obj" (this-pointer) variable and initializes it from a type-checked object handle...
Definition: kApiDef.h:2892
kStatus kStream_CopyAll(kStream stream, kStream source)
Copies all bytes from one stream to another.
kStatus kStream_VSeek(kStream stream, k64s offset, kSeekOrigin origin)
Protected virtual method that moves the read/write pointer to the specified location.
Definition: kStream.h:345
#define kCheckState(EXPRESSION)
Executes a return statement if the given expression is not kTRUE.
Definition: kApiDef.h:593
Core Zen type declarations.
Represents an I/O stream.
kStatus kStream_Fill(kStream stream)
Partially fills the read buffer with bytes from the underlying medium.
Definition: kStream.h:186
kStatus(kCall * kCallbackFx)(kPointer receiver, kPointer sender, void *args)
Callback signature for a generic event handler.
Definition: kApiDef.h:1706
Represents a 64-bit signed integer.
kStatus kStream_CopyEx(kStream stream, kStream source, kSize size, kCallbackFx progress, kPointer context)
Copies the specified number of bytes from one stream to another, with progress feedback.
#define kERROR_UNIMPLEMENTED
Feature not implemented.
Definition: kApiDef.h:492
k64u kStream_BytesWritten(kStream stream)
Reports the number of bytes written to this stream.
Definition: kStream.h:221
Represents metadata about a type (class, interface, or value).
#define kInitFields(TYPE, OBJECT)
Zero-initializes the instance fields of the specified object.
Definition: kApiDef.h:2953
#define kOK
Operation successful.
Definition: kApiDef.h:513
Represents an error code.
kStatus kStream_Write(kStream stream, const void *buffer, kSize size)
Writes the specified number of bytes to the stream.
Definition: kStream.h:97
#define kNULL
Null pointer.
Definition: kApiDef.h:267
kStatus kStream_Init(kStream stream, kType type, kAlloc allocator)
Protected method called by derived classes to initialize the kStream base class.
Definition: kStream.h:260
kStatus kStream_Read(kStream stream, void *buffer, kSize size)
Reads the specified number of bytes from the stream.
Definition: kStream.h:37
kStatus kStream_ClearStats(kStream stream)
Clears stream statistics (e.g.
Definition: kStream.h:235