awtk/docs/fscript_ostream.md

336 lines
5.6 KiB
Markdown
Raw Normal View History

2021-01-12 14:29:47 +08:00
## ostream 扩展函数
### 1.ostream\_seek
2021-01-14 09:23:11 +08:00
> 定位到指定的偏移量。
----------------------------
#### 原型
```js
ostream_seek(ostream, offset) => bool
```
2022-08-23 10:12:36 +08:00
#### 示例
```js
2022-08-23 10:34:55 +08:00
var a = ostream_file_create("test.bin", "wb+")
2022-08-23 10:12:36 +08:00
ostream_seek(a, 10);
```
2021-01-12 14:29:47 +08:00
### 2.ostream\_tell
2021-01-14 09:23:11 +08:00
> 获取当前的偏移量。
----------------------------
#### 原型
```js
ostream_tell(ostream) => bool
```
> 仅当输入流支持 tell 才能调用本函数。
2022-08-23 10:12:36 +08:00
#### 示例
```js
2022-08-23 10:34:55 +08:00
var a = ostream_file_create("test.bin", "wb+")
2022-08-23 10:12:36 +08:00
ostream_seek(a, 10);
print(ostream_tell(a));
```
2021-01-12 14:29:47 +08:00
### 3.ostream\_flush
2021-01-14 09:23:11 +08:00
> 刷新缓冲区。
----------------------------
#### 原型
```js
ostream_flush(ostream) => bool
```
2022-08-23 10:12:36 +08:00
#### 示例
```js
2022-08-23 10:34:55 +08:00
var a = ostream_file_create("test.bin", "wb+")
2022-08-23 10:12:36 +08:00
ostream_flush(a);
```
2021-01-12 14:29:47 +08:00
### 4.ostream\_write\_uint8
2021-01-14 09:23:11 +08:00
> 写入 uint8_t 类型的数据。
----------------------------
#### 原型
```js
ostream_write_uint8(ostream, v, ...) => int32_t
```
> 返回写入数据的长度(字节数)。
2022-08-23 10:12:36 +08:00
#### 示例
```js
2022-08-23 10:34:55 +08:00
var a = ostream_file_create("test.bin", "wb+")
2022-08-23 10:12:36 +08:00
print(ostream_write_uint8(a, 1));
```
2021-01-12 14:29:47 +08:00
### 5.ostream\_write\_uint16
2021-01-14 09:23:11 +08:00
> 写入 uint16_t 类型的数据。
----------------------------
#### 原型
```js
ostream_write_uint16(ostream, v, ...) => int32_t
```
> 返回写入数据的长度(字节数)。
2022-08-23 10:12:36 +08:00
#### 示例
```js
2022-08-23 10:34:55 +08:00
var a = ostream_file_create("test.bin", "wb+")
2022-08-23 10:12:36 +08:00
print(ostream_write_uint16(a, 2));
```
2021-01-12 14:29:47 +08:00
### 6.ostream\_write\_uint32
2021-01-14 09:23:11 +08:00
> 写入 uint32_t 类型的数据。
----------------------------
#### 原型
```js
ostream_write_uint32(ostream, v, ...) => int32_t
```
2022-08-23 10:12:36 +08:00
2021-01-14 09:23:11 +08:00
> 返回写入数据的长度(字节数)。
2022-08-23 10:12:36 +08:00
#### 示例
```js
2022-08-23 10:34:55 +08:00
var a = ostream_file_create("test.bin", "wb+")
2022-08-23 10:12:36 +08:00
print(ostream_write_uint32(a, 3));
```
2021-01-12 14:29:47 +08:00
### 7.ostream\_write\_uint64
2021-01-14 09:23:11 +08:00
> 写入 uint64_t 类型的数据。
----------------------------
#### 原型
```js
ostream_write_uint64(ostream, v, ...) => int32_t
```
2022-08-23 10:12:36 +08:00
2021-01-14 09:23:11 +08:00
> 返回写入数据的长度(字节数)。
2022-08-23 10:12:36 +08:00
#### 示例
```js
2022-08-23 10:34:55 +08:00
var a = ostream_file_create("test.bin", "wb+")
2022-08-23 10:12:36 +08:00
print(ostream_write_uint32(a, 4));
```
2021-01-12 14:29:47 +08:00
### 8.ostream\_write\_int8
2021-01-14 09:23:11 +08:00
> 写入 int8_t 类型的数据。
----------------------------
#### 原型
```js
ostream_write_int8(ostream, v, ...) => int32_t
```
2022-08-23 10:12:36 +08:00
2021-01-14 09:23:11 +08:00
> 返回写入数据的长度(字节数)。
2022-08-23 10:12:36 +08:00
#### 示例
```js
2022-08-23 10:34:55 +08:00
var a = ostream_file_create("test.bin", "wb+")
2022-08-23 10:12:36 +08:00
print(ostream_write_int8(a, 1));
```
2021-01-12 14:29:47 +08:00
### 9.ostream\_write\_int16
2021-01-14 09:23:11 +08:00
> 写入 int16_t 类型的数据。
----------------------------
#### 原型
```js
ostream_write_int16(ostream, v, ...) => int32_t
```
2022-08-23 10:12:36 +08:00
2021-01-14 09:23:11 +08:00
> 返回写入数据的长度(字节数)。
2022-08-23 10:12:36 +08:00
#### 示例
```js
2022-08-23 10:34:55 +08:00
var a = ostream_file_create("test.bin", "wb+")
2022-08-23 10:12:36 +08:00
print(ostream_write_int16(a, 2));
```
2021-01-12 14:29:47 +08:00
### 10.ostream\_write\_int32
2021-01-14 09:23:11 +08:00
> 写入 int32_t 类型的数据。
----------------------------
#### 原型
```js
ostream_write_int32(ostream, v, ...) => int32_t
```
2022-08-23 10:12:36 +08:00
2021-01-14 09:23:11 +08:00
> 返回写入数据的长度(字节数)。
2022-08-23 10:12:36 +08:00
#### 示例
```js
2022-08-23 10:34:55 +08:00
var a = ostream_file_create("test.bin", "wb+")
2022-08-23 10:12:36 +08:00
print(ostream_write_int32(a, 3));
```
2021-01-12 14:29:47 +08:00
### 11.ostream\_write\_int64
2021-01-14 09:23:11 +08:00
> 写入 int64_t 类型的数据。
----------------------------
#### 原型
```js
ostream_write_int64(ostream, v, ...) => int32_t
```
2022-08-23 10:12:36 +08:00
2021-01-14 09:23:11 +08:00
> 返回写入数据的长度(字节数)。
2022-08-23 10:12:36 +08:00
#### 示例
```js
2022-08-23 10:34:55 +08:00
var a = ostream_file_create("test.bin", "wb+")
2022-08-23 10:12:36 +08:00
print(ostream_write_int64(a, 4));
```
2021-01-12 14:29:47 +08:00
### 12.ostream\_write\_float
2021-01-14 09:23:11 +08:00
> 写入 float 类型的数据。
----------------------------
#### 原型
```js
ostream_write_float(ostream, v, ...) => int32_t
```
2022-08-23 10:12:36 +08:00
2021-01-14 09:23:11 +08:00
> 返回写入数据的长度(字节数)。
2022-08-23 10:12:36 +08:00
#### 示例
```js
2022-08-23 10:34:55 +08:00
var a = ostream_file_create("test.bin", "wb+")
2022-08-23 10:12:36 +08:00
print(ostream_write_float(a, 5));
```
2021-01-12 14:29:47 +08:00
### 13.ostream\_write\_double
2021-01-14 09:23:11 +08:00
> 写入 double 类型的数据。
----------------------------
#### 原型
```js
ostream_write_double(ostream, v, ...) => int32_t
```
2022-08-23 10:12:36 +08:00
2021-01-14 09:23:11 +08:00
> 返回写入数据的长度(字节数)。
2022-08-23 10:12:36 +08:00
#### 示例
```js
2022-08-23 10:34:55 +08:00
var a = ostream_file_create("test.bin", "wb+")
2022-08-23 10:12:36 +08:00
print(ostream_write_double(a, 6));
```
2021-01-12 14:29:47 +08:00
### 14.ostream\_write\_string
2021-01-14 09:23:11 +08:00
> 写入 字符串 类型的数据。
----------------------------
#### 原型
```js
2021-07-30 15:57:57 +08:00
ostream_write_string(ostream, v, [size]) => int32_t
2021-01-14 09:23:11 +08:00
```
2021-07-30 15:57:57 +08:00
2022-08-23 10:12:36 +08:00
> * size为长度(可选),超过字符串长度时取字符串的长度。
> * 返回写入数据的长度(字节数)。
2021-07-30 15:57:57 +08:00
2022-08-23 10:12:36 +08:00
#### 示例
```js
2022-08-23 10:34:55 +08:00
var a = ostream_file_create("test.bin", "wb+")
2022-08-23 10:12:36 +08:00
print(ostream_write_string(a, 'str'));
print(ostream_write_string(a, 'hello', 5));
```
2021-01-14 09:23:11 +08:00
2021-01-12 14:29:47 +08:00
### 15.ostream\_write\_binary
2021-01-14 09:23:11 +08:00
> 写入 二进制 类型的数据。
----------------------------
#### 原型
```js
2021-07-30 15:57:57 +08:00
ostream_write_binary(ostream, v, [size]) => int32_t
2021-01-14 09:23:11 +08:00
```
2021-07-30 15:57:57 +08:00
2022-08-23 10:12:36 +08:00
> * size为长度在v是binary时可选为POINTER时则是必选。
> * 返回写入数据的长度(字节数)。
#### 示例
```js
2022-08-23 10:34:55 +08:00
var a = ostream_file_create("test.bin", "wb+")
2022-08-23 10:12:36 +08:00
print(ostream_write_binary(a, 'str'));
print(ostream_write_binary(a, 'hello', 5));
```
2021-01-14 09:23:11 +08:00
2021-01-12 14:29:47 +08:00
### 更多示例
```js
2022-08-23 10:34:55 +08:00
var a = ostream_file_create("test.bin", "wb+")
2021-01-12 14:29:47 +08:00
assert(ostream_write_uint8(a, 1) == 1)
assert(ostream_write_int8(a, -1) == 1)
assert(ostream_tell(a), 2)
assert(ostream_write_uint16(a, 2) == 2)
assert(ostream_write_int16(a, -2) == 2)
assert(ostream_tell(a), 6)
assert(ostream_write_uint32(a, 3) == 4)
assert(ostream_write_int32(a, -3) == 4)
assert(ostream_tell(a), 14)
assert(ostream_write_uint64(a, 4) == 8)
assert(ostream_write_int64(a, -4) == 8)
assert(ostream_tell(a), 30)
assert(ostream_write_float(a, 5) == 4)
assert(ostream_write_double(a, -5) == 8)
assert(ostream_tell(a), 42)
assert(ostream_write_string(a, "hello") == 5)
assert(ostream_write_binary(a, "world", 5) == 5)
assert(ostream_tell(a), 52)
assert(ostream_flush(a))
```