Last active
June 19, 2024 23:10
-
-
Save jbratu/1d731be9c5e9bfcbdc7be78d4b6c0cd2 to your computer and use it in GitHub Desktop.
Function accepts and OpenInsight table handle and resolve the handle back to a table name.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Function CS_RESOLVE_TABLE_HANDLE_TO_NAME(OpenTableHandle) | |
| Declare Function List_volume_Sub | |
| /* | |
| Given the example: | |
| Open 'SYSLISTS' To OpenTableHandle Else Debug | |
| TableName = CS_RESOLVE_TABLE_HANDLE_TO_NAME(OpenTableHandle) | |
| The Function will return the name of the table From the handle. | |
| */ | |
| Common /CS_RESOLVE_TABLE_CACHE/ csResolveTableNames@, csResolveTableFiles@, csResolveTablePaths@, csResolveTableUnunsed2@, csResolveTableUnunsed3@ | |
| If Unassigned(csResolveTableNames@) Then csResolveTableNames@ = '' | |
| If Unassigned(csResolveTableFiles@) Then csResolveTableFiles@ = '' | |
| If Unassigned(csResolveTablePaths@) Then csResolveTablePaths@ = '' | |
| // For testing | |
| // OpenTableHandle = "00000000076SOME_DATA\REV55235.LK" | |
| If Unassigned(OpenTableHandle) Then Return '' | |
| // Most handles have the file name path in this position | |
| FileVarName = OpenTableHandle<1,2> | |
| AtTables5Count = DCOUNT(@Tables(5), @FM) | |
| // Loop over all the table handles looking for the handle we were passed | |
| For i = 1 To AtTables5Count | |
| TableHandleEntry = @Tables(5)<i> | |
| // Table handles can have multiple values so look through each value | |
| CheckHandleCount = DCOUNT(TableHandleEntry, @VM) | |
| For CheckHandleLoop = 1 To CheckHandleCount | |
| CheckHandle = TableHandleEntry<1, CheckHandleLoop> | |
| // Does this value match the handle we're looking for? | |
| If CheckHandle EQ FileVarName Then | |
| *Found our handle in @TABLES(5) | |
| TableName = @Tables(2)<i> | |
| Return TableName | |
| End | |
| Next CheckHandleLoop | |
| Next | |
| // Couldn't find handle in @TABLES(5) | |
| // If we get here we couldn't find it in memory so use list_volume_sub | |
| OpenTableHandleCleaned = '' | |
| OpenTableHandleCharCount = Len(OpenTableHandle) | |
| // Look for the first character that is not numeric | |
| For i = 1 To OpenTableHandleCharCount | |
| Character = OpenTableHandle[i,1] | |
| If Alpha(Character) Then | |
| StartOfDirectoryPos = i | |
| OpenTableHandleCleaned = OpenTableHandle[StartOfDirectoryPos, OpenTableHandleCharCount] | |
| // Break the loop | |
| i = OpenTableHandleCharCount | |
| End | |
| Next i | |
| If OpenTableHandleCleaned EQ '' Then | |
| // We couldn't resolve the entry to a directory and filename | |
| Return '' | |
| End | |
| // This will store the result of our search and return to the calling program | |
| ResolvedTableName = '' | |
| // The handle should contain a path and a filename. Find what the filename is i.e. REV123.lk | |
| TableFileName = OpenTableHandleCleaned[-1, 'B\'] | |
| TableFileNameWithoutPrefix = TableFileName[1,'F.'] | |
| // Extract the volume path by removing the filename from the path | |
| Volumename = OpenTableHandleCleaned | |
| Swap '\':TableFilename With '' In Volumename | |
| // Have we already cached this from a previous call to this routine? | |
| Locate OpenTableHandleCleaned In csResolveTablePaths@ Using @FM Setting Pos Then | |
| // We already resolved this name | |
| ResolvedtableName = csResolveTableNames@<Pos> | |
| End Else | |
| // We did not have this entry cached so search the volume | |
| VolumeTables = list_volume_sub(Volumename,"","TABLE_NAME":@fm:"TABLE_FOREIGN_NAME","") | |
| VolumeTablesCount = DCOUNT(VolumeTables, @FM) | |
| // Loop over the results and see if the REV filename is in the volume so we can get the tablename. | |
| For i = 1 To VolumeTablesCount | |
| Entry = VolumeTables<i> | |
| // Does the filename i.e. REV123 match? | |
| If Entry<1,2> EQ TableFilenameWithoutprefix Then | |
| // We now know the table name | |
| ResolvedTableName = Entry<1,1> | |
| End | |
| Next i | |
| If ResolvedTableName NE '' Then | |
| // Cache this entry for future use | |
| If csResolveTableNames@ EQ '' Then | |
| // Make a new array | |
| csResolveTableNames@<1> = ResolvedTableName | |
| csResolveTablePaths@<1> = OpenTableHandleCleaned | |
| csResolveTableFiles@<1> = TableFilenameWithoutprefix | |
| End Else | |
| // Append to existing array | |
| csResolveTableNames@<-1> = ResolvedTableName | |
| csResolveTablePaths@<-1> = OpenTableHandleCleaned | |
| csResolveTableFiles@<-1> = TableFilenameWithoutprefix | |
| End | |
| End | |
| End | |
| // Return the result or an emptry string if not found | |
| Return ResolvedTableName |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment