CWindow release candidate 15.
Incorporated all the changes discussed in the thread for the release candidate 14.
The main changes are three new classes:
CFileFind
Performs local file searches. CFindFile includes member functions that begin a search, locate a file, and return the title, name, or path of the file.
Example: Simple file search
#define unicode
#INCLUDE ONCE "windows.bi"
#INCLUDE ONCE "Afx/CFindFile.inc"
USING Afx
DIM pFinder AS CFindFile
cwsPath = "C:\Users\Pepe\FreeBasic64\Tests\test.bas"
IF pFinder.FindFile(cwsPath) = S_OK THEN
PRINT STR(pFinder.FileSize)
END IF
pFinder.Close
Example: Directory listing
#define unicode
#INCLUDE ONCE "windows.bi"
#INCLUDE ONCE "Afx/CFindFile.inc"
USING Afx
DIM pFinder AS CFindFile
DIM cwsPath AS CWSTR = "C:\Users\Pepe\FreeBasic64\Tests\*.bas"
IF pFinder.FindFile(cwsPath) = S_OK THEN
DO
IF pFinder.IsDots = FALSE THEN ' // skip . and .. files
IF UCASE(pFinder.FileExt) = "BAS" THEN
PRINT pFinder.FileNameX
END IF
END IF
IF pFinder.FindNext = 0 THEN EXIT DO
LOOP
END IF
pFinder.Close
CBStrSA class
CBSTRSA is a class that provides wrapper methods for the SAFEARRAY structure, making it easy to create and manage one-dimensional BSTR (dynamic unicode string) arrays.
The lower bound of a CBSTRSA array can start at any user-defined value; however, arrays that are accessed through C++ should use a lower bound of 0. Other languages may use other bounding values (for example, -10 to 10).
Usage example:
' // Dimension a one based safe array with 3 elements
DIM psa AS CBSTRSA = CBSTRSA(1, 3)
' // Assign data to them
psa.Put(1, "One")
psa.Put(2, "Two")
psa.Put(3, "Three")
' // Append another element
psa.Append("Four")
' // Sort the list
psa.Sort
' // Delete the second element
psa.DeleteItem(2)
' // The array now contains three elements
print psa.Count
' // Print the contents of the array
FOR i AS LONG = psa.LBound TO psa.UBOUND
PRINT psa.Get(i)
NEXT
CBStrDic class
CBSTRDIC is an associative BSTR (dynamic unicode string) array. Each item is associated with a unique key. The key is used to retrieve an individual item.
Example
#define unicode
#INCLUDE ONCE "Afx/CBstrDic.inc"
USING Afx
' // Initialize the COM library
CoInitialize NULL
' // Creates an instance of the CBstrDic class
' // Must use NEW to be able to delete the class before the call to CoUninitialize
' // We can use the dotted syntax if pDic goes out of scope before the call to CoUninitialize
DIM pDic AS CBSTRDIC PTR = NEW CBSTRDIC
' // Adds some key, value pairs
pDic->Add "a", "Athens"
pDic->Add "b", "Belgrade"
pDic->Add "c", "Cairo"
' // Get all the items and display them
DIM cbsa AS CBSTRSA = pDic->Items
PRINT cbsa.LBound, cbsa.UBound
FOR i AS LONG = cbsa.LBound TO cbsa.UBound
print cbsa.Item(i)
NEXT
' // Get all the keys and display them
DIM cbsa2 AS CBSTRSA = pDic->Keys
PRINT cbsa2.LBound, cbsa2.UBound
FOR i AS LONG = cbsa2.LBound TO cbsa2.UBound
print cbsa2.Item(i)
NEXT
' // Get the key's count
DIM nCount AS LONG = pDic->Count
PRINT "Count: ", nCount
' // Change key "b" to "m" and "Belgrade" to "México"
pDic->Key("b") = "m"
pDic->Item("m") = "México"
PRINT pDic->Item("m")
' // Check if key "m" exists
IF pDic->Exists("m") THEN PRINT "Key m exists" ELSE PRINT "Key m doesn't exists"
' // Get the item for key "m" and display it
DIM cbsItem AS CBSTR = pDic->Item("m")
PRINT "Value of key m: " & cbsItem
' // Remove key "m"
pDic->Remove "m"
IF pDic->Exists("m") THEN PRINT "Key m exists" ELSE PRINT "Key m doesn't exists"
' // Remove all keys
pDic->RemoveAll
PRINT "All the keys must have been deleted"
nCount = pDic->Count
PRINT "Count: ", nCount
' // Destroy the class
Delete pDic
' // Uninitialize the COM library
CoUninitialize
PRINT
PRINT "press esc"
SLEEP
Thanks Jose! I just downloaded the files and about to start testing the new safearray code. I did notice that you rar file for the Help file contains your .HMX file rather than the compiled .CHM.
I included cbstrsa.inc in my code but when I try to compile your simple test program, I get a lot of errors related to msxml.bi (see attachment).
Quote from: TechSupport on July 28, 2016, 10:28:43 AM
Thanks Jose! I just downloaded the files and about to start testing the new safearray code. I did notice that you rar file for the Help file contains your .HMX file rather than the compiled .CHM.
Sorry, I have reuploaded the correct file.
> I included cbstrsa.inc in my code but when I try to compile your simple test program, I get a lot of errors related to msxml.bi (see attachment).
It compiles without problem in my system. Are you using the test as posted or are you including other files? FB is picky about the order of the include files.
I am using the test as posted just adding a reference to the #Include file.
I am compiling using 32-bit. I haven't tried 64 bit.
Sorry, I tested it using a GUI program, that already includes CWSTR.inc.
The attached file will work.
It is worth nothing that, for speed, in the Keys and Items methods, that return a safe array of BSTR variants, instead of making a copy of the data I'm accessing directly to it and transfering ownership of the BSTR pointers to the newly created CBStrSA array. After transfering ownership, we just don't clear the variant. We can do this because as FB does not support variants natively, it does not call VariantClear automatically when the variant goes out scope. If it was PB, we could set the transfered pointer to null in the variant and mark it as empty by changing it's type, as shown in the remed code:
' pvData[i].bstrVal = NULL
' pvData[i].vt = VT_EMPTY
Just having fun with pointers :)
Quote from: Jose Roca on July 28, 2016, 02:41:38 PM
Sorry, I tested it using a GUI program, that already includes CWSTR.inc.
The attached file will work.
Thanks Jose, it is working perfectly now. :)