Author Topic: Load data from memory  (Read 301 times)

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10815
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Load data from memory
« on: 2021-Oct-14 »
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) [Select]



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

Offline Qedo

  • Dr. Type
  • ****
  • Posts: 352
  • to program what I have todo how should I program?
    • View Profile
Re: Load data from memory
« Reply #1 on: 2021-Oct-15 »
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.

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2747
    • View Profile
Re: Load data from memory
« Reply #2 on: 2021-Oct-15 »
Interesting, thanks Gernot  :good:
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline dreamerman

  • Global Moderator
  • Dr. Type
  • *******
  • Posts: 401
    • View Profile
    • my personal website
Re: Load data from memory
« Reply #3 on: 2021-Oct-20 »
Interesting secret, if you have more of such, don't wait to share them :D
Check my source code editor for GLBasic - link Update: 20.04.2020

Offline hardyx

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 500
    • View Profile
Re: Load data from memory
« Reply #4 on: 2021-Oct-20 »
Wow, this is very cool for creating data at runtime using the "mem://" protocol.
 :booze:

Offline Qedo

  • Dr. Type
  • ****
  • Posts: 352
  • to program what I have todo how should I program?
    • View Profile
Re: Load data from memory
« Reply #5 on: 2021-Dec-02 »
help me good people
To improve the custom shoebox for audio files because there is no MEM2SOUND command or something similar I'm using Gernot's excellent solution of using memory as a file.
It works fine for bmp, jpg, png, wav files but not for avi, mp3 (what I need :(). I think it is a win10 problem. Do you have any ideas about it?
Ciao a tutti

Offline dreamerman

  • Global Moderator
  • Dr. Type
  • *******
  • Posts: 401
    • View Profile
    • my personal website
Re: Load data from memory
« Reply #6 on: 2021-Dec-02 »
I didn't play with this trick, so can't say much, but I didn't see any file format restrictions in GLB source. Did You check ogg files for music? This shouldn't be OS specific so I doubt it rather something else, can You share any source code to check this?
Check my source code editor for GLBasic - link Update: 20.04.2020

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10815
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Load data from memory
« Reply #7 on: Yesterday at 06:10 AM »
Unfortunately, PLAYMUSIC is a bit different. You might have to extract that file to the tmp directory. :(

Offline Qedo

  • Dr. Type
  • ****
  • Posts: 352
  • to program what I have todo how should I program?
    • View Profile
Re: Load data from memory
« Reply #8 on: Yesterday at 08:30 AM »
if I always have to save to disk as I did before, nothing changes with the update  :|
Thanks for the information