CLEO SNIPPET Get all specific file names from directory

monday

Well-Known Member
Joined
Jun 23, 2014
Messages
1,036
Likes
71
Points
98
20
#1
Usage:
Code:
alloc [email protected] 100
format [email protected] "CLEO/audio/*.mp3"
call @load_file_names 2 max_filenames 1000 wildcard [email protected] _name_array [email protected] _index_of_highest_loaded_name [email protected]
for [email protected] = 0 to [email protected]
    call @get_nth_file_name 2 name_array [email protected] n [email protected] _returned_name [email protected]
    chatmsg "%d. %s" -1 [email protected] [email protected]
end

Functions to copy+paste somewhere at the end of your code:
Code:
:get_nth_file_name
{
    Input params:
    [email protected] = pointer to array of pointers, each leading to filename
    [email protected] = n (starting with 0)
   
    Usage:
    call @get_nth_file_name 2 name_array [email protected] n 0 _returned_name [email protected]
}
[email protected] *= 4 // each pointer has 4 bytes
005A: [email protected] += [email protected]  // (int)
0A8D: [email protected] = read_memory [email protected] size 4 virtual_protect 0
ret 1 [email protected]


:load_file_names
{
    Input params:
    [email protected] = filenames limit
    [email protected] = wildcard (e.g. "CLEO/audio/*.mp3")
   
    Usage:
    call @load_file_names 2 max_filenames 1000 wildcard [email protected] _name_array [email protected] _index_of_highest_loaded_name [email protected]
}
0085: [email protected] = [email protected]  // copy of max_filenames
[email protected] *= 4
[email protected] += 4
alloc [email protected] [email protected] // pointer to pointers (4000 will allow to store 1000 pointers, so if there is 1001 files found then the code will possibly crash)
//chatmsg "base addr = %X" -1 [email protected]

0085: [email protected] = [email protected] // [email protected] is a copy to manipulate ([email protected], the begining of array, will be returned)
[email protected] = 0  // loaded file count
alloc [email protected] 400       // max length of a single file name (if larger is encountered the code will possibly crash)
if 0AE6: [email protected] = find_first_file [email protected] get_filename_to [email protected]
then          
    repeat
    wait 0
        0C17: [email protected] = strlen [email protected]                      
        alloc [email protected] [email protected]                    // additional byte to end the string is not required because we will not store the ".mp3"
        0AA5: call 0x8220AD num_params 3 pop 3 [email protected] "%[^.]" [email protected]      // just so the title doesn't contain ".mp3" at the end  
        [email protected] += 1
       
        //chatmsg "string addr = %X at %X" -1 [email protected] [email protected]
        0A8C: write_memory [email protected] size 4 value [email protected] virtual_protect 0 // save the pointer in our array of pointers

        [email protected] += 4
       
        if 003B:   [email protected] == [email protected]
        then
            printf "Max number (%d) of files was reached. Rest is ignored." 5000 [email protected]
            break
        end
    until 8AE7: [email protected] = find_next_file [email protected]
   
    0AE8: find_close [email protected]
    0A8C: write_memory [email protected] size 4 value 0 virtual_protect 0
else
    printf "No files found at all using: %s" 10000 [email protected]
end

free [email protected]

[email protected] -= 1
ret 2 [email protected] [email protected]

:release_file_names
{
    If you use @load_file_names function consecutively without releasing the memory, then it will take too much of it.
   
    Input params:
    [email protected] = name_array (pointer to array of pointers to allocated memory)
   
    Usage:
    call @release_file_names 1 name_array [email protected]
}
0A8D: [email protected] = read_memory [email protected] size 4 virtual_protect 0
repeat
wait 0
    free [email protected]
    [email protected] += 4
    0A8D: [email protected] = read_memory [email protected] size 4 virtual_protect 0
until [email protected] == 0
ret 0


Full working example:
Code:
{$CLEO .cs}
0000: NOP

repeat
wait 50
until 0AFA: is_samp_structures_available

0B34: samp register_client_command "show_names" to_label @cmd_show_names
0B34: samp register_client_command "reload_names" to_label @cmd_reload_names

alloc [email protected] 100
format [email protected] "CLEO/audio/*.mp3"
call @load_file_names 2 max_filenames 1000 wildcard [email protected] _name_array [email protected] _index_of_highest_loaded_name [email protected]  //(_index_of_highest_loaded_name = how_many_loaded - 1)

while true
wait 1000
end

:cmd_show_names
if [email protected] > -1
then
    chatmsg "{00FF00}File names:" -1
   
    for [email protected] = 0 to [email protected]
        call @get_nth_file_name 2 name_array [email protected] n [email protected] _returned_name [email protected]
       
        chatmsg "%d. %s" -1 [email protected] [email protected]
        //chatmsg "%d. %s {AAAAAA} - addr = %X" -1 3[email protected] [email protected] [email protected]
    end
else
    chatmsg "{FF0000}ERROR: {FFFFFF}No names were loaded..." -1
end
samp.CmdRet()

:cmd_reload_names
call @release_file_names 1 _name_array [email protected]
call @load_file_names 2 max_filenames 1000 wildcard [email protected] _name_array [email protected] _index_of_highest_loaded_name [email protected]
samp.CmdRet()





:get_nth_file_name
{
    Input params:
    [email protected] = pointer to array of pointers, each leading to filename
    [email protected] = n (starting with 0)
   
    Usage:
    call @get_nth_file_name 2 name_array [email protected] n 0 _returned_name [email protected]
}
[email protected] *= 4 // each pointer has 4 bytes
005A: [email protected] += [email protected]  // (int)
0A8D: [email protected] = read_memory [email protected] size 4 virtual_protect 0
ret 1 [email protected]


:load_file_names
{
    Input params:
    [email protected] = filenames limit
    [email protected] = wildcard (e.g. "CLEO/audio/*.mp3")
   
    Usage:
    call @load_file_names 2 max_filenames 1000 wildcard [email protected] _name_array [email protected] _index_of_highest_loaded_name [email protected]
}
0085: [email protected] = [email protected]  // copy of max_filenames
[email protected] *= 4
[email protected] += 4
alloc [email protected] [email protected] // pointer to pointers (4000 will allow to store 1000 pointers, so if there is 1001 files found then the code will possibly crash)
//chatmsg "base addr = %X" -1 [email protected]

0085: [email protected] = [email protected] // [email protected] is a copy to manipulate ([email protected], the begining of array, will be returned)
[email protected] = 0  // loaded file count
alloc [email protected] 400       // max length of a single file name (if larger is encountered the code will possibly crash)
if 0AE6: [email protected] = find_first_file [email protected] get_filename_to [email protected]
then          
    repeat
    wait 0
        0C17: [email protected] = strlen [email protected]                      
        alloc [email protected] [email protected]                    // additional byte to end the string is not required because we will not store the ".mp3"
        0AA5: call 0x8220AD num_params 3 pop 3 [email protected] "%[^.]" [email protected]      // just so the title doesn't contain ".mp3" at the end  
        [email protected] += 1
       
        //chatmsg "string addr = %X at %X" -1 [email protected] [email protected]
        0A8C: write_memory [email protected] size 4 value [email protected] virtual_protect 0 // save the pointer in our array of pointers

        [email protected] += 4
       
        if 003B:   [email protected] == [email protected]
        then
            printf "Max number (%d) of files was reached. Rest is ignored." 5000 [email protected]
            break
        end
    until 8AE7: [email protected] = find_next_file [email protected]
   
    0AE8: find_close [email protected]
    0A8C: write_memory [email protected] size 4 value 0 virtual_protect 0
else
    printf "No files found at all using: %s" 10000 [email protected]
end

free [email protected]

[email protected] -= 1
ret 2 [email protected] [email protected]

:release_file_names
{
    If you use @load_file_names function consecutively without releasing the memory, then it will take too much of it.
   
    Input params:
    [email protected] = name_array (pointer to array of pointers to allocated memory)
   
    Usage:
    call @release_file_names 1 name_array [email protected]
}
0A8D: [email protected] = read_memory [email protected] size 4 virtual_protect 0
repeat
wait 0
    free [email protected]
    [email protected] += 4
    0A8D: [email protected] = read_memory [email protected] size 4 virtual_protect 0
until [email protected] == 0
ret 0
Tested with:
- Cleo 4.1
- Samp 0.3.7 R1
- Sampfuncs 5.4.1-final
 

Attachments

Top