Skip to content

Commit e49c7bc

Browse files
author
Matthias Kastner
committed
update getter for global var
getter function should not create the datafolder if it doesn't exist. Instead return a negative value to state that the variable did not exist.
1 parent acb750d commit e49c7bc

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

procedures/CodeBrowser_utils.ipf

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,23 @@ Function setGlobalVar(globalVar, numValue)
181181
End
182182

183183
// returns the Value of a (positive) numeric global Variable. Returns -1 if Variable does not exist.
184+
//
185+
// note: do not use `DebugPrint()` here, as it will cause recursion
186+
//
184187
Function getGlobalVar(globalVar)
185188
String globalVar
186-
DFREF dfr = createDFWithAllParents(pkgFolder)
187189

188-
NVAR/Z/SDFR=dfr myVar = dfr:$globalVar
190+
DFREF dfr = $pkgFolder
191+
if(!DataFolderExistsDFR(dfr))
192+
return -1
193+
endif
189194

195+
NVAR/Z/SDFR=dfr myVar = dfr:$globalVar
190196
if(!NVAR_Exists(myVar))
191197
return -1
192-
else
193-
return myVar
194198
endif
199+
200+
return myVar
195201
End
196202

197203
// set a global string variable
@@ -298,3 +304,28 @@ Function isCompiled([funcList])
298304
endif
299305
return 1
300306
End
307+
308+
/// Checks if the datafolder referenced by dfr exists.
309+
/// Unlike DataFolderExists() a dfref pointing to an empty ("") dataFolder is considered non-existing here.
310+
///
311+
/// https://www.wavemetrics.com/code-snippet/datafolderexists-data-folder-references
312+
///
313+
/// @returns one if dfr is valid and references an existing datafolder, zero otherwise
314+
Function DataFolderExistsDFR(dfr)
315+
dfref dfr
316+
317+
string dataFolder
318+
319+
switch(DataFolderRefStatus(dfr))
320+
case 0: // invalid ref, does not exist
321+
return 0
322+
case 1: // might be valid
323+
dataFolder = GetDataFolder(1,dfr)
324+
return cmpstr(dataFolder,"") != 0 && DataFolderExists(dataFolder)
325+
case 3: // free data folders always exist
326+
return 1
327+
default:
328+
Abort "unknown status"
329+
return 0
330+
endswitch
331+
End

0 commit comments

Comments
 (0)