Skip to content

Commit 9763501

Browse files
binread/binwrite/sockbinread/sockbinwrite/memalloc/memrelease/filesize
1 parent c5f6de2 commit 9763501

10 files changed

Lines changed: 256 additions & 1 deletion

File tree

docpages/basic-language-reference/functions/integer/02_INDEX.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The following functions operate on or return **integer values** in Retro Rocket
2121
* \subpage DAY
2222
* \subpage EOF
2323
* \subpage EXISTSVARI
24+
* \subpage FILESIZE
2425
* \subpage GETNAMECOUNT
2526
* \subpage GETPROCCOUNT
2627
* \subpage GETPROCCPUID
@@ -40,6 +41,7 @@ The following functions operate on or return **integer values** in Retro Rocket
4041
* \subpage LCPUID
4142
* \subpage LEN
4243
* \subpage LGETLASTCPUID
44+
* \subpage MEMALLOC
4345
* \subpage MEMFREE
4446
* \subpage MEMORY
4547
* \subpage MEMPEAK
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
\page FILESIZE FILESIZE Function
2+
3+
```basic
4+
FILESIZE(string-expression)
5+
```
6+
7+
Returns the **size of a file** in **bytes**.
8+
The parameter is a path to a file (absolute or relative to \ref CSD "CSD\$").
9+
10+
---
11+
12+
### Examples
13+
14+
```basic
15+
PRINT FILESIZE("/system/webserver/index.html")
16+
```
17+
18+
```basic
19+
REM Guard against missing files
20+
path$ = "data.bin"
21+
IF FILETYPE$(path$) = "file" THEN
22+
PRINT "Size = "; FILESIZE(path$); " bytes"
23+
ELSE
24+
PRINT "Not a regular file"
25+
ENDIF
26+
```
27+
28+
---
29+
30+
### Notes
31+
32+
* Raises an error if the path does not exist or refers to a directory.
33+
* Result is a 64-bit integer (bytes).
34+
35+
---
36+
37+
**See also:**
38+
\ref FILETYPE "FILETYPE$" · \ref OPENIN "OPENIN" · \ref READ "READ$" · \ref BINREAD "BINREAD"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
\page MEMALLOC MEMALLOC Function
2+
3+
```basic
4+
MEMALLOC(integer-expression)
5+
```
6+
7+
Allocates a block of **heap memory** of the given size (in bytes) and returns a **pointer/integer handle** to it.
8+
9+
---
10+
11+
### Examples
12+
13+
```basic
14+
size = FILESIZE("/system/webserver/logo.png")
15+
buf = MEMALLOC(size)
16+
REM ... use buf with BINREAD / SOCKBINWRITE ...
17+
MEMRELEASE buf
18+
```
19+
20+
---
21+
22+
### Notes
23+
24+
* Contents are unspecified on allocation.
25+
* Always pair with \ref MEMRELEASE "MEMRELEASE" to avoid leaks.
26+
* Returned handle is suitable for binary I/O (\ref BINREAD "BINREAD", \ref BINWRITE "BINWRITE", \ref SOCKBINWRITE "SOCKBINWRITE", \ref SOCKBINREAD "SOCKBINREAD").
27+
28+
**See also:**
29+
\ref MEMRELEASE "MEMRELEASE" · \ref BINREAD "BINREAD" · \ref BINWRITE "BINWRITE"
30+

docpages/basic-language-reference/keywords/00_INDEX.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ The following parameter types are used throughout this documentation:
4141

4242
* \subpage AUTOFLIP
4343
* \subpage BACKGROUND
44+
* \subpage BINREAD
45+
* \subpage BINWRITE
4446
* \subpage BITAND
4547
* \subpage BITEOR
4648
* \subpage BITNAND
@@ -88,6 +90,7 @@ The following parameter types are used throughout this documentation:
8890
* \subpage LIBRARY
8991
* \subpage LINE
9092
* \subpage LOCAL
93+
* \subpage MEMRELEASE
9194
* \subpage MKDIR
9295
* \subpage MOUNT
9396
* \subpage NEXT
@@ -119,6 +122,8 @@ The following parameter types are used throughout this documentation:
119122
* \subpage SETVARR
120123
* \subpage SETVARS
121124
* \subpage SLEEP
125+
* \subpage SOCKBINREAD
126+
* \subpage SOCKBINWRITE
122127
* \subpage SOCKCLOSE
123128
* \subpage SOCKFLUSH
124129
* \subpage SOCKREAD
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
\page BINREAD BINREAD Statement
2+
3+
```basic
4+
BINREAD(integer-handle, integer-buffer, integer-length)
5+
```
6+
7+
Reads **binary data** from an **open file** into memory.
8+
9+
* `integer-handle` — file handle from \ref OPENIN "OPENIN" or \ref OPENUP "OPENUP".
10+
* `integer-buffer` — destination memory handle/pointer (from \ref MEMALLOC "MEMALLOC").
11+
* `integer-length` — number of bytes to read.
12+
13+
---
14+
15+
### Examples
16+
17+
```basic
18+
fh = OPENIN("asset.bin")
19+
size = FILESIZE("asset.bin")
20+
buf = MEMALLOC(size)
21+
22+
BINREAD fh, buf, size
23+
24+
REM ... use buffer ...
25+
MEMRELEASE buf
26+
CLOSE fh
27+
```
28+
29+
---
30+
31+
### Notes
32+
33+
* Use \ref FILESIZE "FILESIZE" to size your buffer appropriately.
34+
* Binary-safe (no line translation).
35+
* Reading past end of file yields fewer bytes or an error, depending on context; check with \ref EOF "EOF" as needed.
36+
37+
---
38+
39+
**See also:**
40+
\ref OPENIN "OPENIN" · \ref OPENUP "OPENUP" · \ref FILESIZE "FILESIZE" · \ref EOF "EOF" · \ref MEMALLOC "MEMALLOC"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
\page BINWRITE BINWRITE Statement
2+
3+
```basic
4+
BINWRITE(integer-handle, integer-buffer, integer-length)
5+
```
6+
7+
Writes **binary data** from memory to an **open file**.
8+
9+
* `integer-handle` — file handle from \ref OPENOUT "OPENOUT" or \ref OPENUP "OPENUP".
10+
* `integer-buffer` — source memory handle/pointer (from \ref MEMALLOC "MEMALLOC" or other).
11+
* `integer-length` — number of bytes to write.
12+
13+
---
14+
15+
### Examples
16+
17+
```basic
18+
fh = OPENOUT("copy.bin")
19+
buf = MEMALLOC(1024)
20+
REM ... fill buf with data ...
21+
BINWRITE fh, buf, 1024
22+
CLOSE fh
23+
MEMRELEASE buf
24+
```
25+
26+
---
27+
28+
### Notes
29+
30+
* Binary-safe; writes bytes exactly as stored in memory.
31+
* Ensure the file was opened with appropriate mode (\ref OPENOUT "OPENOUT" to create/overwrite, \ref OPENUP "OPENUP" to update).
32+
33+
---
34+
35+
**See also:**
36+
\ref OPENOUT "OPENOUT" · \ref OPENUP "OPENUP" · \ref MEMALLOC "MEMALLOC" · \ref MEMRELEASE "MEMRELEASE"
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
\page MEMRELEASE MEMRELEASE Function
2+
3+
```basic
4+
MEMRELEASE(integer-expression)
5+
```
6+
7+
Releases (frees) a block of memory previously obtained via \ref MEMALLOC "MEMALLOC".
8+
9+
---
10+
11+
### Examples
12+
13+
```basic
14+
buf = MEMALLOC(65536)
15+
REM ... use buf ...
16+
MEMRELEASE buf
17+
```
18+
19+
---
20+
21+
### Notes
22+
23+
* Passing an invalid or already-freed handle raises an error.
24+
* After releasing, set your variable to `0` if you want to avoid accidental reuse.
25+
26+
---
27+
28+
**See also:**
29+
\ref MEMALLOC "MEMALLOC"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
\page SOCKBINREAD SOCKBINREAD Statement
2+
3+
```basic
4+
SOCKBINREAD(integer-socket, integer-buffer, integer-length)
5+
```
6+
7+
Reads **binary data** from a **connected socket** into memory.
8+
9+
* `integer-socket` — socket file descriptor.
10+
* `integer-buffer` — destination memory handle/pointer (from \ref MEMALLOC "MEMALLOC").
11+
* `integer-length` — maximum number of bytes to read.
12+
13+
---
14+
15+
### Examples
16+
17+
```basic
18+
buf = MEMALLOC(4096)
19+
SOCKBINREAD client, buf, 4096
20+
REM ... process buffer ...
21+
MEMRELEASE buf
22+
```
23+
24+
---
25+
26+
### Notes
27+
28+
* Reads up to the specified number of bytes into the buffer.
29+
* Use \ref SOCKSTATUS "SOCKSTATUS" to detect disconnects.
30+
* Pair with \ref MEMALLOC "MEMALLOC" / \ref MEMRELEASE "MEMRELEASE" for buffer management.
31+
32+
---
33+
34+
**See also:**
35+
\ref SOCKREAD "SOCKREAD" · \ref SOCKWRITE "SOCKWRITE" · \ref MEMALLOC "MEMALLOC" · \ref MEMRELEASE "MEMRELEASE"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
\page SOCKBINWRITE SOCKBINWRITE Statement
2+
3+
```basic
4+
SOCKBINWRITE(integer-socket, integer-buffer, integer-length)
5+
```
6+
7+
Writes **binary data** from memory to a **connected socket**.
8+
9+
* `integer-socket` — socket file descriptor (e.g. from \ref CONNECT "CONNECT" or \ref SOCKACCEPT "SOCKACCEPT").
10+
* `integer-buffer` — memory handle/pointer returned by \ref MEMALLOC "MEMALLOC".
11+
* `integer-length` — number of bytes to write.
12+
13+
---
14+
15+
### Examples
16+
17+
```basic
18+
size = FILESIZE(file$)
19+
buf = MEMALLOC(size)
20+
BINREAD fh, buf, size
21+
22+
SOCKWRITE client, "Content-Length: "; size; CHR$(13); CHR$(10); CHR$(13); CHR$(10);
23+
SOCKBINWRITE client, buf, size
24+
SOCKFLUSH client
25+
26+
MEMRELEASE buf
27+
```
28+
29+
---
30+
31+
### Notes
32+
33+
* Intended for sending file contents or other raw buffers.
34+
* Socket must be connected; use \ref SOCKSTATUS "SOCKSTATUS" to check.
35+
* May write less than requested if the peer closes; handle errors accordingly.
36+
37+
---
38+
39+
**See also:**
40+
\ref SOCKREAD "SOCKREAD" · \ref SOCKWRITE "SOCKWRITE" · \ref SOCKSTATUS "SOCKSTATUS" · \ref BINREAD "BINREAD" · \ref MEMALLOC "MEMALLOC"

src/basic/sockets.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ void sockbinwrite_statement(struct basic_ctx* ctx) {
331331
accept_or_return(COMMA, ctx);
332332
uint64_t buffer_pointer = expr(ctx);
333333
accept_or_return(COMMA, ctx);
334-
int length = expr(ctx);
334+
int64_t length = expr(ctx);
335335
if (length && !address_valid_read(buffer_pointer, length)) {
336336
tokenizer_error_printf(ctx, "Invalid address: %016lx", buffer_pointer);
337337
return;

0 commit comments

Comments
 (0)