Author Topic: Redirect Console Output and extract info with Posix Regex  (Read 720 times)

airr

  • Sr. Member
  • ****
  • Posts: 265
    • View Profile
Redirect Console Output and extract info with Posix Regex
« on: May 17, 2024, 04:56:10 PM »
NOTE: This will probably require changes for non Pelles builds.

Currently, the BC.exe file doesn't include version information via a Manifest, so I worked out a different way to get the current version of the file.

This redirects the output of the 'bc.exe -v' invocation directly to a variable, and then extracts the version number using Posix Regex.

You'll need to point the bcx variable below to where you have BCX installed.

Code: [Select]
#include <regex.h>

Dim As File commandOutput
Dim As String buffer, version, bcx
Dim As regex_t regex
Dim As regmatch_t pmatch[bcxstrsize]
Dim As Integer ret, length

bcx = "c:\Users\riveraa\Apps\BCX\BC.exe"

commandOutput = popen( bcx & " -v", "r" )

ret = regcomp( &regex, "([0-9]+\.[0-9]+\.[0-9]+)", REG_ICASE Or REG_EXTENDED )

Get$ commandOutput, buffer, bcxstrsize

ret = regexec( &regex, buffer, bcxstrsize, pmatch, 0 )

length = ( pmatch[1].rm_eo - pmatch[1].rm_so ) + 1
version = Mid$( buffer, pmatch[1].rm_so, length )

Print crlf$,"Captured Version: ", Trim$( version )

regfree(&regex)
pclose( commandOutput )
Pause

« Last Edit: May 17, 2024, 11:46:40 PM by airr »

airr

  • Sr. Member
  • ****
  • Posts: 265
    • View Profile
Re: Redirect Console Output and extract info with Posix Regex
« Reply #1 on: August 28, 2024, 06:32:28 PM »
Another way of doing this, without the regex dependency:

Code: [Select]
$HEADER
    #ifdef _MSC_VER
        #define popen _popen
        #define pclose _pclose
    #endif
$HEADER

Const bcx = "C:\Users\riveraa\Apps\BED\Bcx\BC.exe"

Print CRLF$,"BCX Version: ", GetBcxVersion(bcx)

Pause

Function Grab$(src$, start$, end$)
    dim result$, last$
    last$ = remain$(src$,start$)
    result$ = extract$(last$, end$)
    return strim$(result$)
End Function

Function GetBcxVersion$(path$)
    Dim As File commandOutput
    Dim buffer$*MAX_PATH, version$

    commandOutput = popen( path$ + " -v", "r" )
    Get$ commandOutput, buffer, MAX_PATH
    pclose(commandOutput)

    version = Grab$(buffer,"Version "," (")

    return version
End Function


Compiles with Pelles and MSVC on my machine.

AIR.