sbgStreamBufferCommon.h
Go to the documentation of this file.
1 
21 #ifndef __SBG_STREAM_BUFFER_COMMON_H__
22 #define __SBG_STREAM_BUFFER_COMMON_H__
23 
24 #include <sbgCommon.h>
25 
26 //----------------------------------------------------------------------//
27 //- General definitions -//
28 //----------------------------------------------------------------------//
29 
33 #if SBG_CONFIG_BIG_ENDIAN == 1
34 
37  #define sbgStreamBufferReadUint16 sbgStreamBufferReadUint16BE
38  #define sbgStreamBufferReadInt16 sbgStreamBufferReadInt16BE
39  #define sbgStreamBufferReadUint24 sbgStreamBufferReadUint24BE
40  #define sbgStreamBufferReadInt24 sbgStreamBufferReadInt24BE
41  #define sbgStreamBufferReadUint32 sbgStreamBufferReadUint32BE
42  #define sbgStreamBufferReadInt32 sbgStreamBufferReadInt32BE
43  #define sbgStreamBufferReadUint64 sbgStreamBufferReadUint64BE
44  #define sbgStreamBufferReadInt64 sbgStreamBufferReadInt64BE
45  #define sbgStreamBufferReadSizeT32 sbgStreamBufferReadSizeT32BE
46  #define sbgStreamBufferReadSizeT64 sbgStreamBufferReadSizeT64BE
47  #define sbgStreamBufferReadFloat sbgStreamBufferReadFloatBE
48  #define sbgStreamBufferReadDouble sbgStreamBufferReadDoubleBE
49 
50  #define sbgStreamBufferWriteUint16 sbgStreamBufferWriteUint16BE
51  #define sbgStreamBufferWriteInt16 sbgStreamBufferWriteInt16BE
52  #define sbgStreamBufferWriteUint24 sbgStreamBufferWriteUint24BE
53  #define sbgStreamBufferWriteInt24 sbgStreamBufferWriteInt24BE
54  #define sbgStreamBufferWriteUint32 sbgStreamBufferWriteUint32BE
55  #define sbgStreamBufferWriteInt32 sbgStreamBufferWriteInt32BE
56  #define sbgStreamBufferWriteUint64 sbgStreamBufferWriteUint64BE
57  #define sbgStreamBufferWriteInt64 sbgStreamBufferWriteInt64BE
58  #define sbgStreamBufferWriteSizeT32 sbgStreamBufferWriteSizeT32BE
59  #define sbgStreamBufferWriteSizeT64 sbgStreamBufferWriteSizeT64BE
60  #define sbgStreamBufferWriteFloat sbgStreamBufferWriteFloatBE
61  #define sbgStreamBufferWriteDouble sbgStreamBufferWriteDoubleBE
62 #else
63 
66  #define sbgStreamBufferReadUint16 sbgStreamBufferReadUint16LE
67  #define sbgStreamBufferReadInt16 sbgStreamBufferReadInt16LE
68  #define sbgStreamBufferReadUint24 sbgStreamBufferReadUint24LE
69  #define sbgStreamBufferReadInt24 sbgStreamBufferReadInt24LE
70  #define sbgStreamBufferReadUint32 sbgStreamBufferReadUint32LE
71  #define sbgStreamBufferReadInt32 sbgStreamBufferReadInt32LE
72  #define sbgStreamBufferReadUint64 sbgStreamBufferReadUint64LE
73  #define sbgStreamBufferReadInt64 sbgStreamBufferReadInt64LE
74  #define sbgStreamBufferReadSizeT32 sbgStreamBufferReadSizeT32LE
75  #define sbgStreamBufferReadSizeT64 sbgStreamBufferReadSizeT64LE
76  #define sbgStreamBufferReadFloat sbgStreamBufferReadFloatLE
77  #define sbgStreamBufferReadDouble sbgStreamBufferReadDoubleLE
78 
79  #define sbgStreamBufferWriteUint16 sbgStreamBufferWriteUint16LE
80  #define sbgStreamBufferWriteInt16 sbgStreamBufferWriteInt16LE
81  #define sbgStreamBufferWriteUint24 sbgStreamBufferWriteUint24LE
82  #define sbgStreamBufferWriteInt24 sbgStreamBufferWriteInt24LE
83  #define sbgStreamBufferWriteUint32 sbgStreamBufferWriteUint32LE
84  #define sbgStreamBufferWriteInt32 sbgStreamBufferWriteInt32LE
85  #define sbgStreamBufferWriteUint64 sbgStreamBufferWriteUint64LE
86  #define sbgStreamBufferWriteInt64 sbgStreamBufferWriteInt64LE
87  #define sbgStreamBufferWriteSizeT32 sbgStreamBufferWriteSizeT32LE
88  #define sbgStreamBufferWriteSizeT64 sbgStreamBufferWriteSizeT64LE
89  #define sbgStreamBufferWriteFloat sbgStreamBufferWriteFloatLE
90  #define sbgStreamBufferWriteDouble sbgStreamBufferWriteDoubleLE
91 #endif
92 
97 #define sbgStreamBufferReadUint8LE sbgStreamBufferReadUint8
98 #define sbgStreamBufferReadInt8LE sbgStreamBufferReadInt8
99 #define sbgStreamBufferReadBufferLE sbgStreamBufferReadBuffer
100 
101 #define sbgStreamBufferWriteUint8LE sbgStreamBufferWriteUint8
102 #define sbgStreamBufferWriteInt8LE sbgStreamBufferWriteInt8
103 #define sbgStreamBufferWriteBufferLE sbgStreamBufferWriteBuffer
104 
105 #define sbgStreamBufferReadUint8BE sbgStreamBufferReadUint8
106 #define sbgStreamBufferReadInt8BE sbgStreamBufferReadInt8
107 #define sbgStreamBufferReadBufferBE sbgStreamBufferReadBuffer
108 
109 #define sbgStreamBufferWriteUint8BE sbgStreamBufferWriteUint8
110 #define sbgStreamBufferWriteInt8BE sbgStreamBufferWriteInt8
111 #define sbgStreamBufferWriteBufferBE sbgStreamBufferWriteBuffer
112 
113 //----------------------------------------------------------------------//
114 //- Structure definitions -//
115 //----------------------------------------------------------------------//
116 
120 typedef enum _SbgSBMode
121 {
124 } SbgSBMode;
125 
129 typedef enum _SbgSBSeekOrigin
130 {
136 
140 typedef struct _SbgStreamBuffer
141 {
143  size_t bufferSize;
148 
149 //----------------------------------------------------------------------//
150 //- Common operations methods -//
151 //----------------------------------------------------------------------//
152 
161 {
162  //
163  // Check input parameters
164  //
165  SBG_ASSERT(pHandle);
166  SBG_ASSERT(pLinkedBuffer);
167  SBG_ASSERT(bufferSize > 0);
168 
169  //
170  // Initialize stream parameters
171  //
172  pHandle->modes = SB_MODE_WRITE;
173  pHandle->bufferSize = bufferSize;
174  pHandle->errorCode = SBG_NO_ERROR;
175 
176  //
177  // Initialize the buffer
178  //
179  pHandle->pBufferPtr = (uint8*)pLinkedBuffer;
180  pHandle->pCurrentPtr = (uint8*)pLinkedBuffer;
181 
182  //
183  // For now, we don't handle any error, maybe we could add checks in debug mode only
184  //
185  return SBG_NO_ERROR;
186 }
187 
196 {
197  //
198  // Check input parameters
199  //
200  SBG_ASSERT(pHandle);
201  SBG_ASSERT(pLinkedBuffer);
202  SBG_ASSERT(bufferSize > 0);
203 
204  //
205  // Initialize stream parameters
206  //
207  pHandle->modes = SB_MODE_READ;
208  pHandle->bufferSize = bufferSize;
209  pHandle->errorCode = SBG_NO_ERROR;
210 
211  //
212  // Initialize the buffer
213  //
214  pHandle->pBufferPtr = (uint8*)pLinkedBuffer;
215  pHandle->pCurrentPtr = (uint8*)pLinkedBuffer;
216 
217  //
218  // For now, we don't handle any error, maybe we could add checks in debug mode only
219  //
220  return SBG_NO_ERROR;
221 }
222 
229 {
230  //
231  // Check input parameters
232  //
233  SBG_ASSERT(pHandle);
234 
235  //
236  // Return error code
237  //
238  return pHandle->errorCode;
239 }
240 
246 {
247  //
248  // Check input parameters
249  //
250  SBG_ASSERT(pHandle);
251 
252  //
253  // Return error code
254  //
255  pHandle->errorCode = SBG_NO_ERROR;
256 }
257 
267 {
268  //
269  // Check input parameters
270  //
271  SBG_ASSERT(pHandle);
272 
273  //
274  // Return the linked buffer size
275  //
276  return pHandle->bufferSize;
277 }
278 
288 {
289  //
290  // Check input parameters
291  //
292  SBG_ASSERT(pHandle);
293 
294  //
295  // Return the number of bytes between the begin of the stream and the current pointer
296  //
297  return ((size_t)pHandle->pCurrentPtr - (size_t)pHandle->pBufferPtr);
298 }
299 
308 {
309  //
310  // Check input parameters
311  //
312  SBG_ASSERT(pHandle);
313 
314  //
315  // Return the space left in bytes
316  //
317  return sbgStreamBufferGetSize(pHandle) - sbgStreamBufferGetLength(pHandle);
318 }
319 
328 {
329  //
330  // Check input parameters
331  //
332  SBG_ASSERT(pHandle);
333 
334  //
335  // Test if we haven't already an error
336  //
337  if (pHandle->errorCode == SBG_NO_ERROR)
338  {
339  //
340  // According to the origin reference point
341  //
342  switch (origin)
343  {
344  case SB_SEEK_SET:
345  pHandle->pCurrentPtr = pHandle->pBufferPtr + offset;
346  break;
347  case SB_SEEK_CUR_INC:
348  pHandle->pCurrentPtr += offset;
349  break;
350  case SB_SEEK_CUR_DEC:
351  pHandle->pCurrentPtr -= offset;
352  break;
353  case SB_SEEK_END:
354  pHandle->pCurrentPtr = pHandle->pBufferPtr + (pHandle->bufferSize - offset);
355  break;
356  default:
357  pHandle->errorCode = SBG_INVALID_PARAMETER;
358  SBG_LOG_ERROR(pHandle->errorCode, "Invalid origin parameter");
359  }
360 
361  //
362  // Make sure that no error has occurred
363  //
364  if (pHandle->errorCode == SBG_NO_ERROR)
365  {
366  //
367  // Test if the current ptr is still within the buffer bounds
368  //
369  if (pHandle->pCurrentPtr < pHandle->pBufferPtr)
370  {
371  //
372  // We are before the buffer so clamp to the begining of the buffer and raise an error
373  //
374  pHandle->pCurrentPtr = pHandle->pBufferPtr;
375  pHandle->errorCode = SBG_BUFFER_OVERFLOW;
376 
377  //
378  // Stream buffer underflow
379  //
380  SBG_LOG_ERROR(pHandle->errorCode, "Trying to seek before the buffer");
381  }
382  else if (pHandle->pCurrentPtr > pHandle->pBufferPtr + pHandle->bufferSize)
383  {
384  //
385  // We are after the buffer so clamp to the end of the buffer and raise an error
386  //
387  pHandle->pCurrentPtr = pHandle->pBufferPtr + pHandle->bufferSize;
388  pHandle->errorCode = SBG_BUFFER_OVERFLOW;
389 
390  //
391  // Stream buffer overflow
392  //
393  SBG_LOG_ERROR(pHandle->errorCode, "Trying to seek after the buffer");
394  }
395  }
396  }
397 
398  return pHandle->errorCode;
399 }
400 
407 {
408  //
409  // Check input parameters
410  //
411  SBG_ASSERT(pHandle);
412 
413  return (size_t)pHandle->pCurrentPtr - (size_t)pHandle->pBufferPtr;
414 }
415 
422 {
423  //
424  // Check input parameters
425  //
426  SBG_ASSERT(pHandle);
427 
428  return pHandle->pBufferPtr;
429 }
430 
437 {
438  //
439  // Check input parameters
440  //
441  SBG_ASSERT(pHandle);
442 
443  return pHandle->pCurrentPtr;
444 }
445 
446 //----------------------------------------------------------------------//
447 //- Read operations methods -//
448 //----------------------------------------------------------------------//
449 
456 {
457  //
458  // Check input parameters
459  //
460  SBG_ASSERT(pHandle);
461 
462  //
463  // Test if we haven't already an error
464  //
465  if (pHandle->errorCode == SBG_NO_ERROR)
466  {
467  //
468  // Test if we can access this item
469  //
470  if (sbgStreamBufferGetSpace(pHandle) >= sizeof(int8))
471  {
472  //
473  // Read the byte
474  //
475  return *((int8*)(pHandle->pCurrentPtr++));
476  }
477  else
478  {
479  //
480  // We have a buffer overflow
481  //
482  pHandle->errorCode = SBG_BUFFER_OVERFLOW;
483  }
484  }
485 
486  //
487  // If we are here, it means we have an error so return 0
488  //
489  return 0;
490 }
491 
498 {
499  //
500  // Check input parameters
501  //
502  SBG_ASSERT(pHandle);
503 
504  //
505  // Test if we haven't already an error
506  //
507  if (pHandle->errorCode == SBG_NO_ERROR)
508  {
509  //
510  // Test if we can access this item
511  //
512  if (sbgStreamBufferGetSpace(pHandle) >= sizeof(uint8))
513  {
514  //
515  // Read the byte
516  //
517  return *((uint8*)(pHandle->pCurrentPtr++));
518  }
519  else
520  {
521  //
522  // We have a buffer overflow
523  //
524  pHandle->errorCode = SBG_BUFFER_OVERFLOW;
525  }
526  }
527 
528  //
529  // If we are here, it means we have an error so return 0
530  //
531  return 0;
532 }
533 
541 SBG_INLINE SbgErrorCode sbgStreamBufferReadBuffer(SbgStreamBuffer *pHandle, void *pBuffer, size_t numBytesToRead)
542 {
543  //
544  // Check input parameters
545  //
546  SBG_ASSERT(pHandle);
547  SBG_ASSERT((pBuffer) || (numBytesToRead == 0));
548 
549  //
550  // Test if we haven't already an error
551  //
552  if (pHandle->errorCode == SBG_NO_ERROR)
553  {
554  //
555  // Test if enough bytes in stream
556  //
557  if (sbgStreamBufferGetSpace(pHandle) >= numBytesToRead)
558  {
559  //
560  // Copy from the stream buffer to the output buffer
561  //
562  memcpy(pBuffer, pHandle->pCurrentPtr, numBytesToRead);
563 
564  //
565  // Update the current pointer
566  //
567  pHandle->pCurrentPtr += numBytesToRead;
568  }
569  else
570  {
571  //
572  // Not enough data in stream
573  //
574  pHandle->errorCode = SBG_BUFFER_OVERFLOW;
575  }
576  }
577 
578  return pHandle->errorCode;
579 }
580 
581 //----------------------------------------------------------------------//
582 //- Write operations methods -//
583 //----------------------------------------------------------------------//
584 
592 {
593  //
594  // Check input parameters
595  //
596  SBG_ASSERT(pHandle);
597 
598  //
599  // Test if we haven't already an error
600  //
601  if (pHandle->errorCode == SBG_NO_ERROR)
602  {
603  //
604  // Test if we can access this item
605  //
606  if (sbgStreamBufferGetSpace(pHandle) >= sizeof(int8))
607  {
608  //
609  // Write each byte
610  //
611  *(pHandle->pCurrentPtr++) = (int8)(value);
612  }
613  else
614  {
615  //
616  // We are accessing a data that is outside the stream buffer
617  //
618  pHandle->errorCode = SBG_BUFFER_OVERFLOW;
619  }
620  }
621 
622  return pHandle->errorCode;
623 }
624 
632 {
633  //
634  // Check input parameters
635  //
636  SBG_ASSERT(pHandle);
637 
638  //
639  // Test if we haven't already an error
640  //
641  if (pHandle->errorCode == SBG_NO_ERROR)
642  {
643  //
644  // Test if we can access this item
645  //
646  if (sbgStreamBufferGetSpace(pHandle) >= sizeof(uint8))
647  {
648  //
649  // Write each byte
650  //
651  *(pHandle->pCurrentPtr++) = (uint8)(value);
652  }
653  else
654  {
655  //
656  // We are accessing a data that is outside the stream buffer
657  //
658  pHandle->errorCode = SBG_BUFFER_OVERFLOW;
659  }
660  }
661 
662  return pHandle->errorCode;
663 }
664 
672 SBG_INLINE SbgErrorCode sbgStreamBufferWriteBuffer(SbgStreamBuffer *pHandle, const void *pBuffer, size_t numBytesToWrite)
673 {
674  //
675  // Check input parameters
676  //
677  SBG_ASSERT(pHandle);
678  SBG_ASSERT((pBuffer) || (numBytesToWrite == 0));
679 
680  //
681  // Test if we haven't already an error
682  //
683  if (pHandle->errorCode == SBG_NO_ERROR)
684  {
685  //
686  // Test if we can access this item
687  //
688  if (sbgStreamBufferGetSpace(pHandle) >= numBytesToWrite)
689  {
690  //
691  // Copy from the stream buffer to the output buffer
692  //
693  memcpy(pHandle->pCurrentPtr, pBuffer, numBytesToWrite);
694 
695  //
696  // Update the current pointer
697  //
698  pHandle->pCurrentPtr += numBytesToWrite;
699  }
700  else
701  {
702  //
703  // We are accessing a data that is outside the stream buffer
704  //
705  pHandle->errorCode = SBG_BUFFER_OVERFLOW;
706  }
707  }
708 
709  return pHandle->errorCode;
710 }
711 
712 #endif /* __SBG_STREAM_BUFFER_COMMON_H__ */
SBG_INLINE SbgErrorCode sbgStreamBufferInitForRead(SbgStreamBuffer *pHandle, const void *pLinkedBuffer, size_t bufferSize)
SBG_INLINE SbgErrorCode sbgStreamBufferWriteUint8(SbgStreamBuffer *pHandle, uint8 value)
SBG_INLINE SbgErrorCode sbgStreamBufferSeek(SbgStreamBuffer *pHandle, size_t offset, SbgSBSeekOrigin origin)
struct _SbgStreamBuffer SbgStreamBuffer
enum _SbgSBMode SbgSBMode
SBG_INLINE size_t sbgStreamBufferGetSize(SbgStreamBuffer *pHandle)
SBG_INLINE SbgErrorCode sbgStreamBufferInitForWrite(SbgStreamBuffer *pHandle, void *pLinkedBuffer, size_t bufferSize)
signed char int8
Definition: sbgTypes.h:62
SBG_INLINE SbgErrorCode sbgStreamBufferReadBuffer(SbgStreamBuffer *pHandle, void *pBuffer, size_t numBytesToRead)
enum _SbgSBSeekOrigin SbgSBSeekOrigin
SBG_INLINE size_t sbgStreamBufferGetSpace(SbgStreamBuffer *pHandle)
SBG_INLINE void * sbgStreamBufferGetLinkedBuffer(SbgStreamBuffer *pHandle)
SBG_INLINE SbgErrorCode sbgStreamBufferWriteBuffer(SbgStreamBuffer *pHandle, const void *pBuffer, size_t numBytesToWrite)
SBG_INLINE size_t sbgStreamBufferTell(SbgStreamBuffer *pHandle)
#define SBG_INLINE
Definition: sbgDefines.h:94
Main header file for SBG Systems common C library.
SBG_INLINE SbgErrorCode sbgStreamBufferWriteInt8(SbgStreamBuffer *pHandle, int8 value)
SBG_INLINE int8 sbgStreamBufferReadInt8(SbgStreamBuffer *pHandle)
unsigned char uint8
Definition: sbgTypes.h:56
#define SBG_ASSERT(expression)
Definition: sbgDebug.h:52
SBG_INLINE size_t sbgStreamBufferGetLength(SbgStreamBuffer *pHandle)
SBG_INLINE uint8 sbgStreamBufferReadUint8(SbgStreamBuffer *pHandle)
SBG_INLINE void sbgStreamBufferClearLastError(SbgStreamBuffer *pHandle)
#define SBG_LOG_ERROR(errorCode, format,...)
Definition: sbgDebug.h:63
SBG_INLINE void * sbgStreamBufferGetCursor(SbgStreamBuffer *pHandle)
enum _SbgErrorCode SbgErrorCode
SBG_INLINE SbgErrorCode sbgStreamBufferGetLastError(SbgStreamBuffer *pHandle)


sbg_driver
Author(s):
autogenerated on Sun Jan 27 2019 03:42:20