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 
153 {
154  return xkStream_VTable(stream)->VSeek(stream, offset, origin);
155 }
156 
165 {
166  return xkStream_VTable(stream)->VFlush(stream);
167 }
176 {
177  kObj(kStream, stream);
178 
179  return obj->bytesRead - (k64u)(obj->readEnd - obj->readBegin);
180 }
181 
190 {
191  kObj(kStream, stream);
192 
193  return obj->bytesWritten + (k64u)obj->writeBegin;
194 }
195 
204 {
205  kObj(kStream, stream);
206 
207  obj->bytesRead = 0;
208  obj->bytesWritten = 0;
209 
210  return kOK;
211 }
212 
213 
214 /*
215 * Protected
216 */
217 
230 {
231  kCheck(kObject_Init(stream, type, allocator));
232 
233  kInitFields(kStream, stream);
234 
235  return kOK;
236 }
237 
247 {
248  return kObject_VRelease(stream);
249 }
250 
273 kInlineFx(kStatus) kStream_VReadSomeImpl(kStream stream, void* buffer, kSize minCount, kSize maxCount, kSize* bytesRead)
274 {
275  //legacy support: for classes that haven't been updated to implement VReadSomeImpl,
276  //try VReadImpl instead (deprecated, but still in the vtable)
277  kCheck(xkStream_VTable(stream)->VReadImpl(stream, buffer, minCount));
278 
279  if (!kIsNull(bytesRead))
280  {
281  *bytesRead = minCount;
282  }
283 
284  return kOK;
285 }
286 
298 kInlineFx(kStatus) kStream_VWriteImpl(kStream stream, const void* buffer, kSize size)
299 {
300  return kERROR_UNIMPLEMENTED;
301 }
302 
315 {
316  return kERROR_UNIMPLEMENTED;
317 }
318 
329 {
330  return kOK;
331 }
332 
333 #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:328
k64u kStream_BytesRead(kStream stream)
Reports the number of bytes read from this stream.
Definition: kStream.h:175
#define kMin_(A, B)
Returns the minimum of two numbers.
Definition: kApiDef.h:1779
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:164
kStatus kStream_VRelease(kStream stream)
Protected virtual method that deallocates any resources owned by the object.
Definition: kStream.h:246
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:298
#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:152
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:273
#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:2858
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:314
Core Zen type declarations.
Represents an I/O stream.
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:189
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:2919
#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:229
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:203