a1


This article covers the basic use of the Small Basic Stack, with an example recursively finding all jpg image files in and below an initial directory.
What is a Stack
A stack is an ordered list where entries are added (pushed) and retrieved (popped) in a last-in first-out order.  The name stack come from the fact that it is like a stack of plates.  When we add a plate it goes on the top and when we remove a plate it is the last one added that is removed.
A stack is useful to store things that we want process gradually as we unwind the stack, retrieving things to process.  Often the things to add to the stack are not known at the start, but are the result of processing previous stack values.  If we knew them all at the start we could just put them in an array.
Stack methods
Only three methods are present, they are:
  • count = GetCount(stackName) – the number of items present in the stack.
  • PushValue(stackName, value) – add a value to the stack.
  • value = PopValue(stackName) – retrieve a value and remove it from the stack.
The stackName is a string, that is something enclosed in double quotes, for example “my_stack”.  The value may be any Small Basic variable or value.
Example
We want to create an array with all of the jpg image files in a directory and all sub-directories, searching all folders recursively.
First we create an empty array to store the image file paths and the number of files in it (zero to start with).  We also set the starting folder to search and add it to a stack we call “folders”.  In this example the starting folder location is the folder where the Small Basic source file is saved (Program.Directory).
images = “”
imageCount = 0
startFolder = Program.Directory
Stack.PushValue(“folders”,startFolder)
Next we want to keep working while we have folders still to check, or the stack still has some folders in it.  We use a while loop for this.  Inside the while loop we want to process the last folder added, so it will be popped first.
While Stack.GetCount(“folders”) > 0
currentFolder = Stack.PopValue(“folders”)
‘ More work to do here
EndWhile
We now want to process this folder; first we get all the sub-folders in the current working folder and push these onto the stack for later checking as the while loop repeats.
folders = File.GetDirectories(currentFolder)
For i = 1 To Array.GetItemCount(folders)
Stack.PushValue(“folders”,folders[i])
EndFor
Having added the sub-folders to check later as the stack unwinds, we find all the files in the current working folder and add ones ending in “.jpg” to the array list.  We check the file by first converting to lower case, in order to include all case variants of jpg JPG etc.
files = File.GetFiles(currentFolder)
For i = 1 To Array.GetItemCount(files)
fileName = files[i]
fileNameLower = Text.ConvertToLowerCase(fileName)
If (Text.EndsWith(fileNameLower,”.jpg”)) Then
imageCount = imageCount+1
images[imageCount] = fileName
EndIf
EndFor
Finally we print out the results, and below is the whole thing.
images = “”
imageCount = 0
startFolder = Program.Directory
Stack.PushValue(“folders”,startFolder)
While Stack.GetCount(“folders”) > 0
currentFolder = Stack.PopValue(“folders”)
folders = File.GetDirectories(currentFolder)
For i = 1 To Array.GetItemCount(folders)
Stack.PushValue(“folders”,folders[i])
EndFor
files = File.GetFiles(currentFolder)
For i = 1 To Array.GetItemCount(files)
fileName = files[i]
fileNameLower = Text.ConvertToLowerCase(fileName)
If (Text.EndsWith(fileNameLower,”.jpg”)) Then
imageCount = imageCount+1
images[imageCount] = fileName
EndIf
EndFor
EndWhile
For i = 1 To Array.GetItemCount(images)
TextWindow.WriteLine(images[i])
EndFor
http://social.technet.microsoft.com/wiki/contents/articles/15066.small-basic-stack-basics.aspx

Deja un comentario