stdbloblib.rst 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. .. _stdlib_stdbloblib:
  2. ==================
  3. The Blob library
  4. ==================
  5. The blob library implements binary data manipulations routines. The library is
  6. based on `blob objects` that represent a buffer of arbitrary
  7. binary data.
  8. ---------------
  9. Squirrel API
  10. ---------------
  11. +++++++++++++++
  12. Global symbols
  13. +++++++++++++++
  14. .. js:function:: castf2i(f)
  15. casts a float to a int
  16. .. js:function:: casti2f(n)
  17. casts a int to a float
  18. .. js:function:: swap2(n)
  19. swap the byte order of a number (like it would be a 16bits integer)
  20. .. js:function:: swap4(n)
  21. swap the byte order of an integer
  22. .. js:function:: swapfloat(n)
  23. swaps the byteorder of a float
  24. ++++++++++++++++++
  25. The blob class
  26. ++++++++++++++++++
  27. The blob object is a buffer of arbitrary binary data. The object behaves like
  28. a file stream, it has a read/write pointer and it automatically grows if data
  29. is written out of his boundary.
  30. A blob can also be accessed byte by byte through the `[]` operator.
  31. .. js:class:: blob(size)
  32. :param int size: initial size of the blob
  33. returns a new instance of a blob class of the specified size in bytes
  34. .. js:function:: blob.eos()
  35. returns a non null value if the read/write pointer is at the end of the stream.
  36. .. js:function:: blob.flush()
  37. flushes the stream.return a value != null if succeded, otherwise returns null
  38. .. js:function:: blob.len()
  39. returns the length of the stream
  40. .. js:function:: blob.readblob(size)
  41. :param int size: number of bytes to read
  42. read n bytes from the stream and returns them as blob
  43. .. js:function:: blob.readn(type)
  44. :param int type: type of the number to read
  45. reads a number from the stream according to the type parameter.
  46. `type` can have the following values:
  47. +--------------+--------------------------------------------------------------------------------+----------------------+
  48. | parameter | return description | return type |
  49. +==============+================================================================================+======================+
  50. | 'l' | processor dependent, 32bits on 32bits processors, 64bits on 64bits processors | integer |
  51. +--------------+--------------------------------------------------------------------------------+----------------------+
  52. | 'i' | 32bits number | integer |
  53. +--------------+--------------------------------------------------------------------------------+----------------------+
  54. | 's' | 16bits signed integer | integer |
  55. +--------------+--------------------------------------------------------------------------------+----------------------+
  56. | 'w' | 16bits unsigned integer | integer |
  57. +--------------+--------------------------------------------------------------------------------+----------------------+
  58. | 'c' | 8bits signed integer | integer |
  59. +--------------+--------------------------------------------------------------------------------+----------------------+
  60. | 'b' | 8bits unsigned integer | integer |
  61. +--------------+--------------------------------------------------------------------------------+----------------------+
  62. | 'f' | 32bits float | float |
  63. +--------------+--------------------------------------------------------------------------------+----------------------+
  64. | 'd' | 64bits float | float |
  65. +--------------+--------------------------------------------------------------------------------+----------------------+
  66. .. js:function:: blob.resize(size)
  67. :param int size: the new size of the blob in bytes
  68. resizes the blob to the specified `size`
  69. .. js:function:: blob.seek(offset [,origin])
  70. :param int offset: indicates the number of bytes from `origin`.
  71. :param int origin: origin of the seek
  72. +--------------+-------------------------------------------+
  73. | 'b' | beginning of the stream |
  74. +--------------+-------------------------------------------+
  75. | 'c' | current location |
  76. +--------------+-------------------------------------------+
  77. | 'e' | end of the stream |
  78. +--------------+-------------------------------------------+
  79. Moves the read/write pointer to a specified location.
  80. .. note:: If origin is omitted the parameter is defaulted as 'b'(beginning of the stream).
  81. .. js:function:: blob.swap2()
  82. swaps the byte order of the blob content as it would be an array of `16bits integers`
  83. .. js:function:: blob.swap4()
  84. swaps the byte order of the blob content as it would be an array of `32bits integers`
  85. .. js:function:: blob.tell()
  86. returns the read/write pointer absolute position
  87. .. js:function:: blob.writeblob(src)
  88. :param blob src: the source blob containing the data to be written
  89. writes a blob in the stream
  90. .. js:function:: blob.writen(n, type)
  91. :param number n: the value to be written
  92. :param int type: type of the number to write
  93. writes a number in the stream formatted according to the `type` parameter
  94. `type` can have the following values:
  95. +--------------+--------------------------------------------------------------------------------+
  96. | parameter | return description |
  97. +==============+================================================================================+
  98. | 'i' | 32bits number |
  99. +--------------+--------------------------------------------------------------------------------+
  100. | 's' | 16bits signed integer |
  101. +--------------+--------------------------------------------------------------------------------+
  102. | 'w' | 16bits unsigned integer |
  103. +--------------+--------------------------------------------------------------------------------+
  104. | 'c' | 8bits signed integer |
  105. +--------------+--------------------------------------------------------------------------------+
  106. | 'b' | 8bits unsigned integer |
  107. +--------------+--------------------------------------------------------------------------------+
  108. | 'f' | 32bits float |
  109. +--------------+--------------------------------------------------------------------------------+
  110. | 'd' | 64bits float |
  111. +--------------+--------------------------------------------------------------------------------+
  112. ------
  113. C API
  114. ------
  115. .. _sqstd_register_bloblib:
  116. .. c:function:: SQRESULT sqstd_register_bloblib(HSQUIRRELVM v)
  117. :param HSQUIRRELVM v: the target VM
  118. :returns: an SQRESULT
  119. :remarks: The function aspects a table on top of the stack where to register the global library functions.
  120. initializes and registers the blob library in the given VM.
  121. .. _sqstd_getblob:
  122. .. c:function:: SQRESULT sqstd_getblob(HSQUIRRELVM v, SQInteger idx, SQUserPointer * ptr)
  123. :param HSQUIRRELVM v: the target VM
  124. :param SQInteger idx: and index in the stack
  125. :param SQUserPointer * ptr: A pointer to the userpointer that will point to the blob's payload
  126. :returns: an SQRESULT
  127. retrieve the pointer of a blob's payload from an arbitrary
  128. position in the stack.
  129. .. _sqstd_getblobsize:
  130. .. c:function:: SQInteger sqstd_getblobsize(HSQUIRRELVM v, SQInteger idx)
  131. :param HSQUIRRELVM v: the target VM
  132. :param SQInteger idx: and index in the stack
  133. :returns: the size of the blob at `idx` position
  134. retrieves the size of a blob's payload from an arbitrary
  135. position in the stack.
  136. .. _sqstd_createblob:
  137. .. c:function:: SQUserPointer sqstd_createblob(HSQUIRRELVM v, SQInteger size)
  138. :param HSQUIRRELVM v: the target VM
  139. :param SQInteger size: the size of the blob payload that has to be created
  140. :returns: a pointer to the newly created blob payload
  141. creates a blob with the given payload size and pushes it in the stack.