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 
40  #define sbgStreamBufferReadUint24 sbgStreamBufferReadUint24BE
41  #define sbgStreamBufferReadInt24 sbgStreamBufferReadInt24BE
42 
43  #define sbgStreamBufferReadUint32 sbgStreamBufferReadUint32BE
44  #define sbgStreamBufferReadInt32 sbgStreamBufferReadInt32BE
45 
46  #define sbgStreamBufferReadUint40 sbgStreamBufferReadUint40BE
47  #define sbgStreamBufferReadInt40 sbgStreamBufferReadInt40BE
48 
49  #define sbgStreamBufferReadUint48 sbgStreamBufferReadUint48BE
50  #define sbgStreamBufferReadInt48 sbgStreamBufferReadInt48BE
51 
52  #define sbgStreamBufferReadUint56 sbgStreamBufferReadUint56BE
53  #define sbgStreamBufferReadInt56 sbgStreamBufferReadInt56BE
54 
55  #define sbgStreamBufferReadUint64 sbgStreamBufferReadUint64BE
56  #define sbgStreamBufferReadInt64 sbgStreamBufferReadInt64BE
57 
58  #define sbgStreamBufferReadSizeT32 sbgStreamBufferReadSizeT32BE
59  #define sbgStreamBufferReadSizeT64 sbgStreamBufferReadSizeT64BE
60 
61  #define sbgStreamBufferReadFloat sbgStreamBufferReadFloatBE
62  #define sbgStreamBufferReadDouble sbgStreamBufferReadDoubleBE
63 
64  #define sbgStreamBufferWriteUint16 sbgStreamBufferWriteUint16BE
65  #define sbgStreamBufferWriteInt16 sbgStreamBufferWriteInt16BE
66 
67  #define sbgStreamBufferWriteUint24 sbgStreamBufferWriteUint24BE
68  #define sbgStreamBufferWriteInt24 sbgStreamBufferWriteInt24BE
69 
70  #define sbgStreamBufferWriteUint32 sbgStreamBufferWriteUint32BE
71  #define sbgStreamBufferWriteInt32 sbgStreamBufferWriteInt32BE
72 
73  #define sbgStreamBufferWriteUint64 sbgStreamBufferWriteUint64BE
74  #define sbgStreamBufferWriteInt64 sbgStreamBufferWriteInt64BE
75 
76  #define sbgStreamBufferWriteSizeT32 sbgStreamBufferWriteSizeT32BE
77  #define sbgStreamBufferWriteSizeT64 sbgStreamBufferWriteSizeT64BE
78 
79  #define sbgStreamBufferWriteFloat sbgStreamBufferWriteFloatBE
80  #define sbgStreamBufferWriteDouble sbgStreamBufferWriteDoubleBE
81 
82  #define sbgStreamBufferReadString sbgStreamBufferReadStringBE
83  #define sbgStreamBufferWriteString sbgStreamBufferWriteStringBE
84 #else
85 
88  #define sbgStreamBufferReadUint16 sbgStreamBufferReadUint16LE
89  #define sbgStreamBufferReadInt16 sbgStreamBufferReadInt16LE
90 
91  #define sbgStreamBufferReadUint24 sbgStreamBufferReadUint24LE
92  #define sbgStreamBufferReadInt24 sbgStreamBufferReadInt24LE
93 
94  #define sbgStreamBufferReadUint32 sbgStreamBufferReadUint32LE
95  #define sbgStreamBufferReadInt32 sbgStreamBufferReadInt32LE
96 
97  #define sbgStreamBufferReadUint40 sbgStreamBufferReadUint40LE
98  #define sbgStreamBufferReadInt40 sbgStreamBufferReadInt40LE
99 
100  #define sbgStreamBufferReadUint48 sbgStreamBufferReadUint48LE
101  #define sbgStreamBufferReadInt48 sbgStreamBufferReadInt48LE
102 
103  #define sbgStreamBufferReadUint56 sbgStreamBufferReadUint56LE
104  #define sbgStreamBufferReadInt56 sbgStreamBufferReadInt56LE
105 
106  #define sbgStreamBufferReadUint64 sbgStreamBufferReadUint64LE
107  #define sbgStreamBufferReadInt64 sbgStreamBufferReadInt64LE
108 
109  #define sbgStreamBufferReadSizeT32 sbgStreamBufferReadSizeT32LE
110  #define sbgStreamBufferReadSizeT64 sbgStreamBufferReadSizeT64LE
111 
112  #define sbgStreamBufferReadFloat sbgStreamBufferReadFloatLE
113  #define sbgStreamBufferReadDouble sbgStreamBufferReadDoubleLE
114 
115  #define sbgStreamBufferWriteUint16 sbgStreamBufferWriteUint16LE
116  #define sbgStreamBufferWriteInt16 sbgStreamBufferWriteInt16LE
117 
118  #define sbgStreamBufferWriteUint24 sbgStreamBufferWriteUint24LE
119  #define sbgStreamBufferWriteInt24 sbgStreamBufferWriteInt24LE
120 
121  #define sbgStreamBufferWriteUint32 sbgStreamBufferWriteUint32LE
122  #define sbgStreamBufferWriteInt32 sbgStreamBufferWriteInt32LE
123 
124  #define sbgStreamBufferWriteUint64 sbgStreamBufferWriteUint64LE
125  #define sbgStreamBufferWriteInt64 sbgStreamBufferWriteInt64LE
126 
127  #define sbgStreamBufferWriteSizeT32 sbgStreamBufferWriteSizeT32LE
128  #define sbgStreamBufferWriteSizeT64 sbgStreamBufferWriteSizeT64LE
129 
130  #define sbgStreamBufferWriteFloat sbgStreamBufferWriteFloatLE
131  #define sbgStreamBufferWriteDouble sbgStreamBufferWriteDoubleLE
132 
133  #define sbgStreamBufferReadString sbgStreamBufferReadStringLE
134  #define sbgStreamBufferWriteString sbgStreamBufferWriteStringLE
135 #endif
136 
141 #define sbgStreamBufferReadUint8LE sbgStreamBufferReadUint8
142 #define sbgStreamBufferReadInt8LE sbgStreamBufferReadInt8
143 #define sbgStreamBufferReadBooleanLE sbgStreamBufferReadBoolean
144 #define sbgStreamBufferReadBufferLE sbgStreamBufferReadBuffer
145 
146 #define sbgStreamBufferWriteUint8LE sbgStreamBufferWriteUint8
147 #define sbgStreamBufferWriteInt8LE sbgStreamBufferWriteInt8
148 #define sbgStreamBufferWriteBooleanLE sbgStreamBufferWriteBoolean
149 #define sbgStreamBufferWriteBufferLE sbgStreamBufferWriteBuffer
150 
151 #define sbgStreamBufferReadUint8BE sbgStreamBufferReadUint8
152 #define sbgStreamBufferReadInt8BE sbgStreamBufferReadInt8
153 #define sbgStreamBufferReadBooleanBE sbgStreamBufferReadBoolean
154 #define sbgStreamBufferReadBufferBE sbgStreamBufferReadBuffer
155 
156 #define sbgStreamBufferWriteUint8BE sbgStreamBufferWriteUint8
157 #define sbgStreamBufferWriteInt8BE sbgStreamBufferWriteInt8
158 #define sbgStreamBufferWriteBooleanBE sbgStreamBufferWriteBoolean
159 #define sbgStreamBufferWriteBufferBE sbgStreamBufferWriteBuffer
160 
161 //----------------------------------------------------------------------//
162 //- Structure definitions -//
163 //----------------------------------------------------------------------//
164 
168 typedef enum _SbgSBMode
169 {
172 } SbgSBMode;
173 
177 typedef enum _SbgSBSeekOrigin
178 {
184 
188 typedef struct _SbgStreamBuffer
189 {
191  size_t bufferSize;
192  uint8_t *pBufferPtr;
193  uint8_t *pCurrentPtr;
196 
197 //----------------------------------------------------------------------//
198 //- Common operations methods -//
199 //----------------------------------------------------------------------//
200 
209 {
210  //
211  // Check input parameters
212  //
213  assert(pHandle);
214  assert(pLinkedBuffer);
215 
216  //
217  // Initialize stream parameters
218  //
219  pHandle->modes = SB_MODE_WRITE;
220  pHandle->bufferSize = bufferSize;
221  pHandle->errorCode = SBG_NO_ERROR;
222 
223  //
224  // Initialize the buffer
225  //
226  pHandle->pBufferPtr = (uint8_t*)pLinkedBuffer;
227  pHandle->pCurrentPtr = (uint8_t*)pLinkedBuffer;
228 
229  //
230  // For now, we don't handle any error, maybe we could add checks in debug mode only
231  //
232  return SBG_NO_ERROR;
233 }
234 
243 {
244  //
245  // Check input parameters
246  //
247  assert(pHandle);
248  assert(pLinkedBuffer);
249 
250  //
251  // Initialize stream parameters
252  //
253  pHandle->modes = SB_MODE_READ;
254  pHandle->bufferSize = bufferSize;
255  pHandle->errorCode = SBG_NO_ERROR;
256 
257  //
258  // Initialize the buffer
259  //
260  pHandle->pBufferPtr = (uint8_t*)pLinkedBuffer;
261  pHandle->pCurrentPtr = (uint8_t*)pLinkedBuffer;
262 
263  //
264  // For now, we don't handle any error, maybe we could add checks in debug mode only
265  //
266  return SBG_NO_ERROR;
267 }
268 
275 {
276  //
277  // Check input parameters
278  //
279  assert(pHandle);
280 
281  //
282  // Return error code
283  //
284  return pHandle->errorCode;
285 }
286 
292 {
293  //
294  // Check input parameters
295  //
296  assert(pHandle);
297 
298  //
299  // Return error code
300  //
301  pHandle->errorCode = SBG_NO_ERROR;
302 }
303 
313 {
314  //
315  // Check input parameters
316  //
317  assert(pHandle);
318 
319  //
320  // Return the linked buffer size
321  //
322  return pHandle->bufferSize;
323 }
324 
334 {
335  //
336  // Check input parameters
337  //
338  assert(pHandle);
339 
340  //
341  // Return the number of bytes between the begin of the stream and the current pointer
342  //
343  return ((size_t)pHandle->pCurrentPtr - (size_t)pHandle->pBufferPtr);
344 }
345 
354 {
355  //
356  // Check input parameters
357  //
358  assert(pHandle);
359 
360  //
361  // Return the space left in bytes
362  //
363  return sbgStreamBufferGetSize(pHandle) - sbgStreamBufferGetLength(pHandle);
364 }
365 
374 {
375  //
376  // Check input parameters
377  //
378  assert(pHandle);
379 
380  //
381  // Test if we haven't already an error
382  //
383  if (pHandle->errorCode == SBG_NO_ERROR)
384  {
385  //
386  // According to the origin reference point
387  //
388  switch (origin)
389  {
390  case SB_SEEK_SET:
391  pHandle->pCurrentPtr = pHandle->pBufferPtr + offset;
392  break;
393  case SB_SEEK_CUR_INC:
394  pHandle->pCurrentPtr += offset;
395  break;
396  case SB_SEEK_CUR_DEC:
397  pHandle->pCurrentPtr -= offset;
398  break;
399  case SB_SEEK_END:
400  pHandle->pCurrentPtr = pHandle->pBufferPtr + (pHandle->bufferSize - offset);
401  break;
402  default:
403  pHandle->errorCode = SBG_INVALID_PARAMETER;
404  SBG_LOG_ERROR(pHandle->errorCode, "Invalid origin parameter");
405  }
406 
407  //
408  // Make sure that no error has occurred
409  //
410  if (pHandle->errorCode == SBG_NO_ERROR)
411  {
412  //
413  // Test if the current ptr is still within the buffer bounds
414  //
415  if (pHandle->pCurrentPtr < pHandle->pBufferPtr)
416  {
417  //
418  // We are before the buffer so clamp to the begining of the buffer and raise an error
419  //
420  pHandle->pCurrentPtr = pHandle->pBufferPtr;
421  pHandle->errorCode = SBG_BUFFER_OVERFLOW;
422 
423  //
424  // Stream buffer underflow
425  //
426  SBG_LOG_ERROR(pHandle->errorCode, "Trying to seek before the buffer");
427  }
428  else if (pHandle->pCurrentPtr > pHandle->pBufferPtr + pHandle->bufferSize)
429  {
430  //
431  // We are after the buffer so clamp to the end of the buffer and raise an error
432  //
433  pHandle->pCurrentPtr = pHandle->pBufferPtr + pHandle->bufferSize;
434  pHandle->errorCode = SBG_BUFFER_OVERFLOW;
435 
436  //
437  // Stream buffer overflow
438  //
439  SBG_LOG_ERROR(pHandle->errorCode, "Trying to seek after the buffer");
440  }
441  }
442  }
443 
444  return pHandle->errorCode;
445 }
446 
453 {
454  //
455  // Check input parameters
456  //
457  assert(pHandle);
458 
459  return (size_t)pHandle->pCurrentPtr - (size_t)pHandle->pBufferPtr;
460 }
461 
468 {
469  //
470  // Check input parameters
471  //
472  assert(pHandle);
473 
474  return pHandle->pBufferPtr;
475 }
476 
483 {
484  //
485  // Check input parameters
486  //
487  assert(pHandle);
488 
489  return pHandle->pCurrentPtr;
490 }
491 
492 //----------------------------------------------------------------------//
493 //- Read operations methods -//
494 //----------------------------------------------------------------------//
495 
502 {
503  //
504  // Check input parameters
505  //
506  assert(pHandle);
507 
508  //
509  // Test if we haven't already an error
510  //
511  if (pHandle->errorCode == SBG_NO_ERROR)
512  {
513  //
514  // Test if we can access this item
515  //
516  if (sbgStreamBufferGetSpace(pHandle) >= sizeof(int8_t))
517  {
518  //
519  // Read the byte
520  //
521  return *((int8_t*)(pHandle->pCurrentPtr++));
522  }
523  else
524  {
525  //
526  // We have a buffer overflow
527  //
528  pHandle->errorCode = SBG_BUFFER_OVERFLOW;
529  }
530  }
531 
532  //
533  // If we are here, it means we have an error so return 0
534  //
535  return 0;
536 }
537 
544 {
545  //
546  // Check input parameters
547  //
548  assert(pHandle);
549 
550  //
551  // Test if we haven't already an error
552  //
553  if (pHandle->errorCode == SBG_NO_ERROR)
554  {
555  //
556  // Test if we can access this item
557  //
558  if (sbgStreamBufferGetSpace(pHandle) >= sizeof(uint8_t))
559  {
560  //
561  // Read the byte
562  //
563  return *((uint8_t*)(pHandle->pCurrentPtr++));
564  }
565  else
566  {
567  //
568  // We have a buffer overflow
569  //
570  pHandle->errorCode = SBG_BUFFER_OVERFLOW;
571  }
572  }
573 
574  //
575  // If we are here, it means we have an error so return 0
576  //
577  return 0;
578 }
579 
586 {
587  //
588  // Check input parameters
589  //
590  assert(pHandle);
591 
592  //
593  // Test if we haven't already an error
594  //
595  if (pHandle->errorCode == SBG_NO_ERROR)
596  {
597  //
598  // Test if we can access this item
599  //
600  if (sbgStreamBufferGetSpace(pHandle) >= sizeof(uint8_t))
601  {
602  //
603  // Read the byte and check if the value is different than zero or not
604  //
605  if (*((uint8_t*)(pHandle->pCurrentPtr++)))
606  {
607  return true;
608  }
609  else
610  {
611  return false;
612  }
613  }
614  else
615  {
616  //
617  // We have a buffer overflow
618  //
619  pHandle->errorCode = SBG_BUFFER_OVERFLOW;
620  }
621  }
622 
623  //
624  // If we are here, it means we have an error so return false
625  //
626  return false;
627 }
628 
636 SBG_INLINE SbgErrorCode sbgStreamBufferReadBuffer(SbgStreamBuffer *pHandle, void *pBuffer, size_t numBytesToRead)
637 {
638  //
639  // Check input parameters
640  //
641  assert(pHandle);
642  assert((pBuffer) || (numBytesToRead == 0));
643 
644  //
645  // Test if we haven't already an error
646  //
647  if (pHandle->errorCode == SBG_NO_ERROR)
648  {
649  //
650  // Test if enough bytes in stream
651  //
652  if (sbgStreamBufferGetSpace(pHandle) >= numBytesToRead)
653  {
654  //
655  // Copy from the stream buffer to the output buffer
656  //
657  memcpy(pBuffer, pHandle->pCurrentPtr, numBytesToRead);
658 
659  //
660  // Update the current pointer
661  //
662  pHandle->pCurrentPtr += numBytesToRead;
663  }
664  else
665  {
666  //
667  // Not enough data in stream
668  //
669  pHandle->errorCode = SBG_BUFFER_OVERFLOW;
670  }
671  }
672 
673  return pHandle->errorCode;
674 }
675 
676 //----------------------------------------------------------------------//
677 //- Write operations methods -//
678 //----------------------------------------------------------------------//
679 
687 {
688  //
689  // Check input parameters
690  //
691  assert(pHandle);
692 
693  //
694  // Test if we haven't already an error
695  //
696  if (pHandle->errorCode == SBG_NO_ERROR)
697  {
698  //
699  // Test if we can access this item
700  //
701  if (sbgStreamBufferGetSpace(pHandle) >= sizeof(int8_t))
702  {
703  //
704  // Write each byte
705  //
706  *(pHandle->pCurrentPtr++) = (int8_t)(value);
707  }
708  else
709  {
710  //
711  // We are accessing a data that is outside the stream buffer
712  //
713  pHandle->errorCode = SBG_BUFFER_OVERFLOW;
714  }
715  }
716 
717  return pHandle->errorCode;
718 }
719 
727 {
728  //
729  // Check input parameters
730  //
731  assert(pHandle);
732 
733  //
734  // Test if we haven't already an error
735  //
736  if (pHandle->errorCode == SBG_NO_ERROR)
737  {
738  //
739  // Test if we can access this item
740  //
741  if (sbgStreamBufferGetSpace(pHandle) >= sizeof(uint8_t))
742  {
743  //
744  // Write each byte
745  //
746  *(pHandle->pCurrentPtr++) = (uint8_t)(value);
747  }
748  else
749  {
750  //
751  // We are accessing a data that is outside the stream buffer
752  //
753  pHandle->errorCode = SBG_BUFFER_OVERFLOW;
754  }
755  }
756 
757  return pHandle->errorCode;
758 }
759 
767 {
768  //
769  // Check input parameters
770  //
771  assert(pHandle);
772 
773  //
774  // Test if we haven't already an error
775  //
776  if (pHandle->errorCode == SBG_NO_ERROR)
777  {
778  //
779  // Test if we can access this item
780  //
781  if (sbgStreamBufferGetSpace(pHandle) >= sizeof(uint8_t))
782  {
783  //
784  // Write the boolean as an uint8_t value (1 byte)
785  //
786  if (value)
787  {
788  *(pHandle->pCurrentPtr++) = 1;
789  }
790  else
791  {
792  *(pHandle->pCurrentPtr++) = 0;
793  }
794  }
795  else
796  {
797  //
798  // We are accessing a data that is outside the stream buffer
799  //
800  pHandle->errorCode = SBG_BUFFER_OVERFLOW;
801  }
802  }
803 
804  return pHandle->errorCode;
805 }
806 
814 SBG_INLINE SbgErrorCode sbgStreamBufferWriteBuffer(SbgStreamBuffer *pHandle, const void *pBuffer, size_t numBytesToWrite)
815 {
816  //
817  // Check input parameters
818  //
819  assert(pHandle);
820  assert((pBuffer) || (numBytesToWrite == 0));
821 
822  //
823  // Test if we haven't already an error
824  //
825  if (pHandle->errorCode == SBG_NO_ERROR)
826  {
827  //
828  // Test if we can access this item
829  //
830  if (sbgStreamBufferGetSpace(pHandle) >= numBytesToWrite)
831  {
832  //
833  // Copy from the stream buffer to the output buffer
834  //
835  memcpy(pHandle->pCurrentPtr, pBuffer, numBytesToWrite);
836 
837  //
838  // Update the current pointer
839  //
840  pHandle->pCurrentPtr += numBytesToWrite;
841  }
842  else
843  {
844  //
845  // We are accessing a data that is outside the stream buffer
846  //
847  pHandle->errorCode = SBG_BUFFER_OVERFLOW;
848  }
849  }
850 
851  return pHandle->errorCode;
852 }
853 
854 #endif /* SBG_STREAM_BUFFER_COMMON_H */
SBG_INLINE int8_t sbgStreamBufferReadInt8(SbgStreamBuffer *pHandle)
SBG_INLINE bool sbgStreamBufferReadBoolean(SbgStreamBuffer *pHandle)
SBG_INLINE SbgErrorCode sbgStreamBufferInitForRead(SbgStreamBuffer *pHandle, const void *pLinkedBuffer, size_t bufferSize)
SBG_INLINE SbgErrorCode sbgStreamBufferSeek(SbgStreamBuffer *pHandle, size_t offset, SbgSBSeekOrigin origin)
SBG_INLINE SbgErrorCode sbgStreamBufferWriteUint8(SbgStreamBuffer *pHandle, uint8_t value)
struct _SbgStreamBuffer SbgStreamBuffer
SBG_INLINE uint8_t sbgStreamBufferReadUint8(SbgStreamBuffer *pHandle)
enum _SbgSBMode SbgSBMode
SBG_INLINE size_t sbgStreamBufferGetSize(SbgStreamBuffer *pHandle)
SBG_INLINE SbgErrorCode sbgStreamBufferWriteInt8(SbgStreamBuffer *pHandle, int8_t value)
SBG_INLINE SbgErrorCode sbgStreamBufferInitForWrite(SbgStreamBuffer *pHandle, void *pLinkedBuffer, size_t bufferSize)
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:186
Main header file for SBG Systems common C library.
#define SBG_LOG_ERROR(format,...)
Definition: sbgDebug.h:62
SBG_INLINE size_t sbgStreamBufferGetLength(SbgStreamBuffer *pHandle)
SBG_INLINE void sbgStreamBufferClearLastError(SbgStreamBuffer *pHandle)
SBG_INLINE SbgErrorCode sbgStreamBufferWriteBoolean(SbgStreamBuffer *pHandle, bool value)
SBG_INLINE void * sbgStreamBufferGetCursor(SbgStreamBuffer *pHandle)
enum _SbgErrorCode SbgErrorCode
SBG_INLINE SbgErrorCode sbgStreamBufferGetLastError(SbgStreamBuffer *pHandle)


sbg_driver
Author(s): SBG Systems
autogenerated on Thu Oct 22 2020 03:47:22