stdiolib.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. .. _stdlib_stdiolib:
  2. ========================
  3. The Input/Output library
  4. ========================
  5. the i/o library implements basic input/output routines.
  6. --------------
  7. Squirrel API
  8. --------------
  9. ++++++++++++++
  10. Global Symbols
  11. ++++++++++++++
  12. .. js:function:: dofile(path, [raiseerror])
  13. compiles a squirrel script or loads a precompiled one and executes it.
  14. returns the value returned by the script or null if no value is returned.
  15. if the optional parameter 'raiseerror' is true, the compiler error handler is invoked
  16. in case of a syntax error. If raiseerror is omitted or set to false, the compiler
  17. error handler is not invoked.
  18. When squirrel is compiled in Unicode mode the function can handle different character encodings,
  19. UTF8 with and without prefix and UCS-2 prefixed(both big endian an little endian).
  20. If the source stream is not prefixed UTF8 encoding is used as default.
  21. .. js:function:: loadfile(path, [raiseerror])
  22. compiles a squirrel script or loads a precompiled one an returns it as as function.
  23. if the optional parameter 'raiseerror' is true, the compiler error handler is invoked
  24. in case of a syntax error. If raiseerror is omitted or set to false, the compiler
  25. error handler is not invoked.
  26. When squirrel is compiled in Unicode mode the function can handle different character encodings,
  27. UTF8 with and without prefix and UCS-2 prefixed(both big endian an little endian).
  28. If the source stream is not prefixed UTF8 encoding is used as default.
  29. .. js:function:: writeclosuretofile(destpath, closure)
  30. serializes a closure to a bytecode file (destpath). The serialized file can be loaded
  31. using loadfile() and dofile().
  32. .. js:data:: stderr
  33. File object bound on the os *standard error* stream
  34. .. js:data:: stdin
  35. File object bound on the os *standard input* stream
  36. .. js:data:: stdout
  37. File object bound on the os *standard output* stream
  38. ++++++++++++++
  39. The file class
  40. ++++++++++++++
  41. The file object implements a stream on a operating system file.
  42. .. js:class:: file(path, patten)
  43. It's constructor imitates the behaviour of the C runtime function fopen for eg. ::
  44. local myfile = file("test.xxx","wb+");
  45. creates a file with read/write access in the current directory.
  46. .. js:function:: file.close()
  47. closes the file.
  48. .. js:function:: file.eos()
  49. returns a non null value if the read/write pointer is at the end of the stream.
  50. .. js:function:: file.flush()
  51. flushes the stream.return a value != null if succeeded, otherwise returns null
  52. .. js:function:: file.len()
  53. returns the length of the stream
  54. .. js:function:: file.readblob(size)
  55. :param int size: number of bytes to read
  56. read n bytes from the stream and returns them as blob
  57. .. js:function:: file.readn(type)
  58. :param int type: type of the number to read
  59. reads a number from the stream according to the type parameter.
  60. `type` can have the following values:
  61. +--------------+--------------------------------------------------------------------------------+----------------------+
  62. | parameter | return description | return type |
  63. +==============+================================================================================+======================+
  64. | 'l' | processor dependent, 32bits on 32bits processors, 64bits on 64bits processors | integer |
  65. +--------------+--------------------------------------------------------------------------------+----------------------+
  66. | 'i' | 32bits number | integer |
  67. +--------------+--------------------------------------------------------------------------------+----------------------+
  68. | 's' | 16bits signed integer | integer |
  69. +--------------+--------------------------------------------------------------------------------+----------------------+
  70. | 'w' | 16bits unsigned integer | integer |
  71. +--------------+--------------------------------------------------------------------------------+----------------------+
  72. | 'c' | 8bits signed integer | integer |
  73. +--------------+--------------------------------------------------------------------------------+----------------------+
  74. | 'b' | 8bits unsigned integer | integer |
  75. +--------------+--------------------------------------------------------------------------------+----------------------+
  76. | 'f' | 32bits float | float |
  77. +--------------+--------------------------------------------------------------------------------+----------------------+
  78. | 'd' | 64bits float | float |
  79. +--------------+--------------------------------------------------------------------------------+----------------------+
  80. .. js:function:: file.resize(size)
  81. :param int size: the new size of the blob in bytes
  82. resizes the blob to the specified `size`
  83. .. js:function:: file.seek(offset [,origin])
  84. :param int offset: indicates the number of bytes from `origin`.
  85. :param int origin: origin of the seek
  86. +--------------+-------------------------------------------+
  87. | 'b' | beginning of the stream |
  88. +--------------+-------------------------------------------+
  89. | 'c' | current location |
  90. +--------------+-------------------------------------------+
  91. | 'e' | end of the stream |
  92. +--------------+-------------------------------------------+
  93. Moves the read/write pointer to a specified location.
  94. .. note:: If origin is omitted the parameter is defaulted as 'b'(beginning of the stream).
  95. .. js:function:: file.tell()
  96. returns the read/write pointer absolute position
  97. .. js:function:: file.writeblob(src)
  98. :param blob src: the source blob containing the data to be written
  99. writes a blob in the stream
  100. .. js:function:: file.writen(n, type)
  101. :param number n: the value to be written
  102. :param int type: type of the number to write
  103. writes a number in the stream formatted according to the `type` pamraeter
  104. `type` can have the following values:
  105. +--------------+--------------------------------------------------------------------------------+
  106. | parameter | return description |
  107. +==============+================================================================================+
  108. | 'i' | 32bits number |
  109. +--------------+--------------------------------------------------------------------------------+
  110. | 's' | 16bits signed integer |
  111. +--------------+--------------------------------------------------------------------------------+
  112. | 'w' | 16bits unsigned integer |
  113. +--------------+--------------------------------------------------------------------------------+
  114. | 'c' | 8bits signed integer |
  115. +--------------+--------------------------------------------------------------------------------+
  116. | 'b' | 8bits unsigned integer |
  117. +--------------+--------------------------------------------------------------------------------+
  118. | 'f' | 32bits float |
  119. +--------------+--------------------------------------------------------------------------------+
  120. | 'd' | 64bits float |
  121. +--------------+--------------------------------------------------------------------------------+
  122. --------------
  123. C API
  124. --------------
  125. .. _sqstd_register_iolib:
  126. .. c:function:: SQRESULT sqstd_register_iolib(HSQUIRRELVM v)
  127. :param HSQUIRRELVM v: the target VM
  128. :returns: an SQRESULT
  129. :remarks: The function aspects a table on top of the stack where to register the global library functions.
  130. initialize and register the io library in the given VM.
  131. ++++++++++++++
  132. File Object
  133. ++++++++++++++
  134. .. c:function:: SQRESULT sqstd_createfile(HSQUIRRELVM v, SQFILE file, SQBool owns)
  135. :param HSQUIRRELVM v: the target VM
  136. :param SQFILE file: the stream that will be rapresented by the file object
  137. :param SQBool owns: if different true the stream will be automatically closed when the newly create file object is destroyed.
  138. :returns: an SQRESULT
  139. creates a file object bound to the SQFILE passed as parameter
  140. and pushes it in the stack
  141. .. c:function:: SQRESULT sqstd_getfile(HSQUIRRELVM v, SQInteger idx, SQFILE* file)
  142. :param HSQUIRRELVM v: the target VM
  143. :param SQInteger idx: and index in the stack
  144. :param SQFILE * file: A pointer to a SQFILE handle that will store the result
  145. :returns: an SQRESULT
  146. retrieve the pointer of a stream handle from an arbitrary
  147. position in the stack.
  148. ++++++++++++++++++++++++++++++++
  149. Script loading and serialization
  150. ++++++++++++++++++++++++++++++++
  151. .. c:function:: SQRESULT sqstd_loadfile(HSQUIRRELVM v, const SQChar * filename, SQBool printerror)
  152. :param HSQUIRRELVM v: the target VM
  153. :param SQChar * filename: path of the script that has to be loaded
  154. :param SQBool printerror: if true the compiler error handler will be called if a error occurs
  155. :returns: an SQRESULT
  156. Compiles a squirrel script or loads a precompiled one an pushes it as closure in the stack.
  157. When squirrel is compiled in Unicode mode the function can handle different character encodings,
  158. UTF8 with and without prefix and UCS-2 prefixed(both big endian an little endian).
  159. If the source stream is not prefixed UTF8 encoding is used as default.
  160. .. c:function:: SQRESULT sqstd_dofile(HSQUIRRELVM v, const SQChar * filename, SQBool retval, SQBool printerror)
  161. :param HSQUIRRELVM v: the target VM
  162. :param SQChar * filename: path of the script that has to be loaded
  163. :param SQBool retval: if true the function will push the return value of the executed script in the stack.
  164. :param SQBool printerror: if true the compiler error handler will be called if a error occurs
  165. :returns: an SQRESULT
  166. :remarks: the function expects a table on top of the stack that will be used as 'this' for the execution of the script. The 'this' parameter is left untouched in the stack.
  167. Compiles a squirrel script or loads a precompiled one and executes it.
  168. Optionally pushes the return value of the executed script in the stack.
  169. When squirrel is compiled in unicode mode the function can handle different character encodings,
  170. UTF8 with and without prefix and UCS-2 prefixed(both big endian an little endian).
  171. If the source stream is not prefixed, UTF8 encoding is used as default. ::
  172. sq_pushroottable(v); //push the root table(were the globals of the script will are stored)
  173. sqstd_dofile(v, _SC("test.nut"), SQFalse, SQTrue);// also prints syntax errors if any
  174. .. c:function:: SQRESULT sqstd_writeclosuretofile(HSQUIRRELVM v, const SQChar * filename)
  175. :param HSQUIRRELVM v: the target VM
  176. :param SQChar * filename: destination path of serialized closure
  177. :returns: an SQRESULT
  178. serializes the closure at the top position in the stack as bytecode in
  179. the file specified by the parameter filename. If a file with the
  180. same name already exists, it will be overwritten.