Re: Integrating string types into EUPHORIA handling

new topic     » goto parent     » topic index » view thread      » older message » newer message
jimcbrown said...

This is doable without adding a new builtin type. We'd just add a new C type, say C_STRING in addition to C_POINTER and C_CHAR. The user passes that type into define_c_func/proc and then later when the user calls c_func/c_proc, the callc code would take a sequence, validate it (so no subsequences in that sequence, for sample), and convert it to a NUL-terminated char array.

Or, for C_WSTRING, do conversion into UTF-16. For rare cases, C_WWSTRING would convert to UTF-32, while C_USTRING would do UTF-8.

euadvlib does this. smile

override function c_func( integer rid, sequence args = {} ) 
     
    -- get the argument types and return type 
    sequence arg_types = map:get( m_args, rid, {} ) 
    atom return_type = map:get( m_retv, rid, 0 ) 
     
    -- make sure we have the correct number of arguments 
    if length( args ) = length( arg_types ) then 
         
        for i = 1 to length( args ) do 
             
            -- allocate strings as necessary 
             
            if sequence( args[i] ) and arg_types[i] = C_STRING then 
                args[i] = allocate_string( args[i], 1 ) 
            elsif sequence( args[i] ) and arg_types[i] = C_WSTRING then 
                args[i] = allocate_wstring( args[i], 1 ) 
            end if 
             
        end for 
         
    end if 
     
    -- call the function 
    object result = eu:c_func( rid, args ) 
     
    -- fetch strings as necessary 
    if return_type = C_STRING then 
        result = peek_string( result ) 
    elsif return_type = C_WSTRING then 
        result = peek_wstring( result ) 
    end if 
     
    return result 
end function 

-Greg

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu