home *** CD-ROM | disk | FTP | other *** search
- // Tb312.prg
- //
- // Browsing database file names and their structures
- // Optimized version of tb311. Does not rebuild "structures" TBrowse
- // object each time. If the database changes, this version simply updates
- // the array reference, stored in cargo.
-
- #include "Inkey.ch"
- #include "Dbstruct.ch"
- #include "Directry.ch"
- #include "Tbutils.ch"
-
- FUNCTION Tb312
-
- LOCAL aFiles := Directory("*.dbf")
- LOCAL aStru
- LOCAL nKey
-
- // The TBrowse object to display the DBF file names
- LOCAL oTbrFiles
-
- // The TBrowse object to display the current DBF's structure
- LOCAL oTbrStrus
- LOCAL lExitRequested := .F.
- LOCAL cLastDbf := ""
-
- SET CURSOR OFF
- CLEAR SCREEN
-
- oTbrFiles := TBaH2dBrw(aFiles, ;
- {"Name", " Size", ;
- "Date", "Time", "Attr"})
-
- oTbrFiles:nTop := 1
- oTbrFiles:nLeft := 1
- oTbrFiles:nBottom := MaxRow() - 1
- oTbrFiles:nRight := 39
- TBFrame(oTbrFiles)
-
- USE (aFiles[oTbrFiles:cargo[1], F_NAME])
- aStru := DbStruct()
- CLOSE (aFiles[oTbrFiles:cargo[1], F_NAME])
-
- oTbrStrus := TBaH2dBrw(aStru, ;
- { "Name", "Type", ;
- " Len", " Dec" })
-
- oTbrStrus:nTop := 1
- oTbrStrus:nLeft := 42
- oTbrStrus:nBottom := MaxRow() - 1
- oTbrStrus:nRight := 78
- TBFrame(oTbrStrus)
-
- DO WHILE !lExitRequested
- FullStabilize(oTbrFiles)
- IF !(cLastDbf == aFiles[oTbrFiles:cargo[1], F_NAME])
- USE (aFiles[oTbrFiles:cargo[1], F_NAME])
- aStru := DbStruct()
- // Just update array reference directly
- oTbrStrus:cargo[2] := aStru
- CLOSE (aFiles[oTbrFiles:cargo[1], F_NAME])
- oTbrStrus:refreshAll()
- FullStabilize(oTbrStrus)
- oTbrStrus:deHilite()
- ENDIF
- nKey := InKey(0)
- IF !Stdmeth(nKey, oTbrFiles)
- DO CASE
- CASE nKey == K_ESC
- lExitRequested := .T.
- ENDCASE
- ENDIF
- ENDDO
-
- RETURN NIL
-
-
- FUNCTION TBFrame(oTbr)
-
- @ oTbr:nTop - 1, oTbr:nLeft - 1 TO oTbr:nBottom + 1, oTbr:nRight + 1
-
- RETURN NIL
-
-
- // As TBa2dBrw but allows headings. Column width is set to the larger
- // of the heading and the data width
-
- FUNCTION TBaH2dBrw(aVar, aHeadings)
-
- LOCAL oTbr := TBrowseNew()
- LOCAL nCols := Len(aVar[1])
- LOCAL i
- LOCAL oTbc
-
- // We maintain cargo as a two element array, where the first element
- // contains the row number, the second a reference to the array to
- // browse
- oTbr:cargo := {1, aVar}
- oTbr:goTopBlock := {|| oTbr:cargo[1] := 1 }
- oTbr:goBottomBlock := {|| oTbr:cargo[1] := Len(aVar) }
- oTbr:skipBlock := ARRAY_SKIPPER(oTbr:cargo[1], oTbr:cargo[2])
-
- FOR i := 1 TO nCols
- oTbc := TBColumnNew(aHeadings[i], A2DBlock(oTbr, i))
- // Find width for column here ...
- oTbc:width := Max(A2dWidth(aVar, i), Len(aHeadings[i]))
- oTbr:addColumn(oTbc)
- NEXT
-
- RETURN oTbr
-
-
- FUNCTION A2dBlock(oTbr, nCol)
-
- RETURN {|| oTbr:cargo[2, oTbr:cargo[1], nCol] }
-
- FUNCTION A2dwidth(aVar, nCol)
-
- LOCAL i
- LOCAL nLen := Len(aVar)
- LOCAL nLongest := 0
-
- FOR i := 1 TO nLen
- nLongest := Max(nLongest, DataLen(aVar[i, nCol]))
- NEXT
-
- RETURN nLongest
-
-
- FUNCTION DataLen(xElem)
-
- LOCAL nLen
- LOCAL cType := ValType(xElem)
-
- DO CASE
- CASE cType $ "ACM"
- nLen := Len(xElem)
-
- CASE cType == "N"
- nLen := Len(Str(xElem))
-
- CASE cType == "L"
- nLen := 1
-
- CASE cType == "D"
- nLen := 8
-
- CASE cType == "U" // Value NIL
- nLen := 3
- ENDCASE
-
- RETURN nLen