@@ -181,17 +181,23 @@ Function setGlobalVar(globalVar, numValue)
181
181
End
182
182
183
183
// 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
+ //
184
187
Function getGlobalVar ( globalVar)
185
188
String globalVar
186
- DFREF dfr = createDFWithAllParents ( pkgFolder)
187
189
188
- NVAR /Z/ SDFR= dfr myVar = dfr:$ globalVar
190
+ DFREF dfr = $ pkgFolder
191
+ if ( ! DataFolderExistsDFR ( dfr))
192
+ return -1
193
+ endif
189
194
195
+ NVAR /Z/ SDFR= dfr myVar = dfr:$ globalVar
190
196
if ( ! NVAR_Exists ( myVar))
191
197
return -1
192
- else
193
- return myVar
194
198
endif
199
+
200
+ return myVar
195
201
End
196
202
197
203
// set a global string variable
@@ -298,3 +304,28 @@ Function isCompiled([funcList])
298
304
endif
299
305
return 1
300
306
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