Codesnippets > Code Snippets

Load data from memory

(1/2) > >>

Kitty Hello:
I tell you a secret. GLBasic can load data from a memory buffer, as if it was a file. With the attached code, you can use the contents of a string to "read" it as a file. This should work for about everything. Sprites, sounds, binary files, ...


--- Code: (glbasic) ---


LOCAL mem$ = "Hello World"
LOCAL memlen = LEN(mem$)

LOCAL file$ = MemMakeFile$(mem$)

LOCAL text$
OPENFILE(1, file$, TRUE)
READSTR 1, text$, 64
CLOSEFILE 1
STDOUT text$ + "\n"
KEYWAIT

FUNCTION foo:
ENDFUNCTION

INLINE
extern "C" {int sprintf(char *str, const char *format, ...); }
ENDINLINE


FUNCTION MemMakeFile$: BYREF data$, nofBytes = -1
IF nofBytes < 0 THEN nofBytes = LEN(data$)
LOCAL url$
LOCAL hx$ = RIGHT$("0000000000000000" + HEX$(nofBytes), 8)
INLINE
void* address = &data_Str[0];
char buffer[64];
sprintf(buffer, "%x", address);

url_Str = CGStr("mem://") + hx_Str + CGStr(".") + CGStr(buffer);
ENDINLINE


RETURN url$
ENDFUNCTION

--- End code ---

Qedo:
Thanks Gernot for this very very interesting secret.  :good:
I will try to apply it to my custom shoebox where I save and reload some files.

bigsofty:
Interesting, thanks Gernot  :good:

dreamerman:
Interesting secret, if you have more of such, don't wait to share them :D

hardyx:
Wow, this is very cool for creating data at runtime using the "mem://" protocol.
 :booze:

Navigation

[0] Message Index

[#] Next page

Go to full version