[Gambas-user] Using EXEC
Stephen Bungay
sbungay at ...981...
Thu Sep 1 04:22:07 CEST 2011
On 08/31/2011 09:01 PM, Benoît Minisini wrote:
>> EXEC is a wonderful thing, lighter than SHELL, but it needs things
>> passed as elements of a string array and that is making for some ugly code.
>>
>> Say for example you want to play two videos as a playlist in VLC, in
>> exec the command might look like this;
>>
>> EXEC["vlc","--intf","rc", "Video1.avi","Video2.avi"]
>>
>> But what if you had different quantities of videos to play, lets say
>> next time you needed to play 4 videos? How would you use EXEC to do this?
>>
>>
>> I thought of building the string and embedding the quotes in it like this;
>>
>> ExecString = chr$(34)& "vlc"& chr$(34)& ","& chr$(34)& "--intf"&
>> chr$(34)& ","& chr$(34)& "rc"& chr$(34)& ","
>> For X= 0 TO sFile.Count - 1
>> ExecString = ExecString& chr$934)& sFile[X]& chr$(34)
>> IF X< sFile.Count -1 then
>> ExecString = ExecString& ","
>> End IF
>> NEXT
>>
>> Which would result in a string that looks like this;
>>
>> "vlc", "--intf", "rc", "video1.avi", "video2.avi"
>>
>> Nope. It sees the one string as one element when what I want is the
>> contents of the string to be inserted as multiple elements.
>>
>> At the moment I am fetching the video file names from a directory
>> listing, splitting that up into an array, then checking the number of
>> elements in the array and using a case statement to do the work...
>>
>> SELECT CASE sFile.Count
>> CASE 1
>> mpProcessHandle = EXEC ["vlc", "--intf", "rc", sFile[0]]
>> FOR INPUT OUTPUT AS "VLC"
>> CASE 2
>> mpProcessHandle = EXEC ["vlc", "--intf", "rc", sFile[0],
>> sFile[1]] FOR INPUT OUTPUT AS "VLC"
>> CASE 3
>> mpProcessHandle = EXEC ["vlc", "--intf", "rc", sFile[0],
>> sFile[1], sFile[2]] FOR INPUT OUTPUT AS "VLC"
>> CASE 4
>> mpProcessHandle = EXEC ["vlc", "--intf", "rc", sFile[0],
>> sFile[1], sFile[2]] FOR INPUT OUTPUT AS "VLC"
>> END SELECT
>>
>> It ain't pretty. "sFile" is a string array derived from another string
>> that contains the names of videos which in my case are delimited by
>> spaces. Definetly the brute-force/bulldozer approach, big, ugly, and
>> cumbersome, but it does work. Does anyone else know of a more elegant
>> solution?
>>
>> Regards
>> Steve.
>>
> [ ... ] is not a compiler thing, but an operator that build an array at
> runtime.
>
> So you can make the EXEC array with "New String[]" and populate it as you
> need:
>
> Dim aFile As String[] ' The files to open
> Dim aArg As String[]
>
> aArg = ["vlc", "--intf", "rc"]
> aArg.Insert(aFile)
> Exec aArg For Input Output As "VLC"
>
> Regards,
>
I see.... never thought of it that way.... thought about everything
else... including pointers, but not that. Will give it a whirl and see
what come of it.
Much more elegant.
More information about the User
mailing list