CLEO Help Having problem dealing with parameters of SCM functions

CLEO related
Status
Not open for further replies.

tieuthienthan

Active member
Joined
Nov 6, 2019
Messages
43
Reaction score
10
Location
Vietnam
I'm new to writing cleo and I'm trying to make a cleo which reads file name from .ini file.

Code:
{$CLEO .cs}
0000:
10@ = 0
while true
    wait 0
    if and
        0AB0:   key_pressed 18 // alt
        0AB0:   key_pressed 37 // left arrow
        10@ == 0
    then
        10@ = 1
        wait 500
    end
        
    if
        10@ == 1
    then
        10@ = 2
        wait 500
        0AC8: 11@ = allocate_memory_size 1024
        0AF4: 11@ = read_string_from_ini_file "cleo\dev.ini" section "CONFIG" key "RECORD" // a string value as file name
        0AB1: call @xianzai 1 filename "%s" 11@ store_to 10@ // open file in section "config" key "record"
    end     
end

:xianzai
    0AC8: 30@ = allocate_memory_size 1024
    05AA: 30@ = 0@ // get the first parameter
    if
        0AAB: file_exists "%s" 30@ 
    then
        0B00: delete_file "%s" 30@
        0AD1: "~g~file found! %s" time 1337 30@
    else
        0AD1: "~g~file not found! %s" time 1337 30@ 
    end
    0A9A: 28@ = openfile "%s" mode "wb+" 30@
    while true
        wait 0
        {to-do things with file}
        break
    end
    0A9B: closefile 28@
    0AB2: return 1 0
My dev.ini file:

Code:
[CONFIG]
RECORD=cleo\2.dev
If I use 0A9A: 28@ = openfile "cleo\2.dev" mode "wb+" and without any parameters it works perfectly but when I try to read a string from file and then use that to open file, it doens't work and just gives some opcode's error warnings.
Please help me, I really need this to make my cleo work properly.
Thanks.
 

monday

Expert
Joined
Jun 23, 2014
Messages
1,127
Solutions
1
Reaction score
158
hi, you could check this tutorial:
http://ugbase.eu/index.php?threads/tutorial-0ab1-call_scm_function.10880/

There are few issues with the code you posted.

0AB1: call @xianzai 1 filename "%s" 11@ store_to 10@
Only some opcodes allow supplying format parameter, this 0AB1 isn't one of them
so "%s" is treated as 1st parameter
and 11@ is treated as 2nd parameter

0AC8: 30@ = allocate_memory_size 1024
05AA: 30@ = 0@
0AC8 will allocate memory, the begining address of that memory is stored in 30@.
So when you set 30@ to be 0@ (first parameter), you lose reference to the memory you just allocated.
It's called "memory leak" because program has no way to deallocate memory.

There's similar situation with "0AC8: 11@" in the main loop, memory is not deallocated after it's used.

Opcodes below also don't accept format specifiers:
0AAB: file_exists 0@
0B00: delete_file 0@
0A9A: 28@ = openfile 0@ mode "wb+"


Btw here's an example I just tested, it seems to be working:
Code:
{$CLEO .cs}
0000:
10@ = 0
while true
    wait 0
    if and
        0AB0:   key_pressed 18 // alt
        0AB0:   key_pressed 37 // left arrow
    then
        wait 500
        0AC8: 11@ = allocate_memory_size 1024
        0AF4: 11@ = read_string_from_ini_file "cleo\dev.ini" section "CONFIG" key "RECORD" // a string value as file name       
        
        0AB1: call @xianzai 1 filename 11@ store_to 10@ // open file in section "config" key "record"
        0AC9: free_allocated_memory 11@
    end     
end

:xianzai
    if 0AAB: file_exists 0@
    then
        0B00: delete_file 0@
        0AD1: "~g~file found! %s" time 1337 0@
    else
        0AD1: "~g~file not found! %s" time 1337 0@
    end
    0A9A: 28@ = openfile 0@ mode "wb+"
    0AD9: write_formatted_text "test %d %d %d :)" in_file 28@ 1 2 3
/*
    while true
        wait 0
        {to-do things with file}
        break
    end
*/
    0A9B: closefile 28@
0AB2: return 1 0

If some opcode doesn't allow supplying format as a parameter, you can use opcode below to format it beforehand:
0AD3: 0@ = format "%d + %d = %d" 2 2 4
 

tieuthienthan

Active member
Joined
Nov 6, 2019
Messages
43
Reaction score
10
Location
Vietnam
hi, you could check this tutorial:
http://ugbase.eu/index.php?threads/tutorial-0ab1-call_scm_function.10880/

There are few issues with the code you posted.


Only some opcodes allow supplying format parameter, this 0AB1 isn't one of them
so "%s" is treated as 1st parameter
and 11@ is treated as 2nd parameter


0AC8 will allocate memory, the begining address of that memory is stored in 30@.
So when you set 30@ to be 0@ (first parameter), you lose reference to the memory you just allocated.
It's called "memory leak" because program has no way to deallocate memory.

There's similar situation with "0AC8: 11@" in the main loop, memory is not deallocated after it's used.

Opcodes below also don't accept format specifiers:
0AAB: file_exists 0@
0B00: delete_file 0@
0A9A: 28@ = openfile 0@ mode "wb+"


Btw here's an example I just tested, it seems to be working:

If some opcode doesn't allow supplying format as a parameter, you can use opcode below to format it beforehand:
0AD3: 0@ = format "%d + %d = %d" 2 2 4
Thanks @monday for helping me and giving me some useful information! My problem is solved
 
Status
Not open for further replies.
Top