Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parameters of the GetEnvVar function
#1
I need to make the second parameter of qmq's built-in function GetEnvVar optional.

Below is ChatGPT's answer, but it always produces garbled text in actual use.
Is there a better solution?

Function getEnv
Code:
Copy      Help
function$ $name [str&value] 

str R
if(GetEnvVar(name R))
,if(&value)
,,value = R
,,value.expandpath
,,ret value
,else
,,_s=R.expandpath
,,ret _s

Function GetEnvVar
Code:
Copy      Help
;/
function$ $name str&value

;Gets an environment variable.
;Returns value if successful, 0 if failed.

;value - variable for value.

;Added in: QM 2.3.0.


int n1(4) n2
;g1
BSTR b.alloc(n1)
n2=GetEnvironmentVariableW(@name b n1)
if(n2>n1) n1=n2; goto g1
if(n2) value.ansi(b); ret value
value.all
#2
Function getEnv
Code:
Copy      Help
function'str $name

if(GetEnvVar(name _s))
,_s.expandpath
,ret _s
#3
I want to keep the original definition, because my previous code uses that definition extensively.

I only recently discovered that my function produces garbled text — this happens randomly; sometimes it's correct, sometimes there is garbled text.

ChatGPT seems unable to understand the source code. Smile

Code:
Copy      Help
function$ $name [str&value]
#4
Solved using GLM5.3.

Function getEnv2
Code:
Copy      Help
function$ $name [str&value]

int n1(4) n2
str t
;g1
BSTR b.alloc(n1)
n2=GetEnvironmentVariableW(@name b n1)
if(n2>n1) n1=n2; goto g1
if(n2)
,if(&value) value.ansi(b); ret value
,t.ansi(b)
,ret t
if(&value) value.all
#5
function$ cannot return a local str variable. The return value is invalid. OK if function'str.
#6
Thanks for your help.

for function'str
I previously used the form of code A. Is it okay to change it to the form of code B now?

A CODE:
Code:
Copy      Help
if(empty(getEnv("__Roslyn")))

B CODE:

Code:
Copy      Help
if(!getEnv("__Roslyn"))
#7
Probably OK. I don't remember details, but it works.


Forum Jump:


Users browsing this thread: 1 Guest(s)