PDA

View Full Version : Moving strings into an array



ThePenguinMaster
07-03-2009, 06:18 AM
Ok so there is a concept I’m trying to work out here, I’m not sure how to go about this but I want to use the fsrw object to get a listing of all of the files on an SD card. I can get the file names by using the fStore.nextfile function, but I am having trouble storing the value into an array. What I have so far:





CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
tvPin = 24
cardPin = 20
obj
term: "tv_text"
fStore: "fsrw"

var
byte tbuf
long fileList[20]

pub start | r, sta, bytes, i
term.start(tvPin)
' grab the file list up to 20 items for now

GetFileList

i:=0
repeat while not fileList == 0
term.str(fileList)
term.out($0D)
i++


pub GetFileList|i
fStore.mount(cardPin)
fStore.opendir
i:=0
repeat while 0 == fStore.nextfile(@tbuf)
LONG[ fileList ][ i ] := @tbuf
i++





When I run the code, I get no output. If I change “LONG[ fileList ][ i ] := @tbuf” to “fileList][ i ] := @tbuf” it will work but tbuf is rewritten every time. I made the array a set of longs although this is maybe no the right way to store a string. Tbuf is a pointer to the location that contains the file name, and I need to store that into an array and not the pointer value to the string. Is there a somewhat short way around this? Or do I need to modify the fsrw to buffer the file names and return an address to the buffer? I would much prefer to have fsrw remain unmodified and be able to take the string from the memory location it returns and store it into an array. Also, I do not know how many items may be in the array, basically, there are X amount of files. Any help would be appreciated. This is a simple core concept and I’m sure I could use the concept of storing strings into an array in a few applications. Any help would be appreciated.

Timmoore
07-03-2009, 06:32 AM
The simplest I can think of, is since you dont have long filenames, each filename is 8 chars + 1 terminator (0). So make the filelist array 20 *9 bytes. then do a movbyte of 9 bytes from @tbuf to @filelist[9*i]. Then thee are upto 20 names in filelist, the start of each name is 9 bytes apart.

ThePenguinMaster
07-03-2009, 07:38 AM
ok so i made some changes, and it is still not responding how i expect, it acts like i am still moving an address. For my file listing i get:

.mp3
1.mp3

the files i have are:
newfile.txt
1.mp3

here is the updated code:


CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
tvPin = 24
cardPin = 20
obj
term: "tv_text"
fStore: "fsrw"

var
byte tbuf
byte fileList[20*13]
long numFiles
pub start | r, sta, bytes, i
term.start(tvPin)
' grab the file list up to 20 items for now

GetFileList

i:=0
repeat while i < numFiles
print(@fileList[13*i])
i++


pub GetFileList|i
fStore.mount(cardPin)
fStore.opendir
i:=0
print(string("entering loop"))
repeat while 0 == fStore.nextfile(@tbuf)
bytemove(@fileList[13*i],@tbuf,13)
i++
numfiles:=i

pub Print(text)
term.str(text)
term.out($0D)





I don't know why this is so difficult, it seems like such a simple concept. any help would be appreciated.
now if I do this:



pub GetFileList|i
fStore.mount(cardPin)
fStore.opendir
i:=0
print(string("entering loop"))
repeat while 0 == fStore.nextfile(@tbuf)
bytemove(@fileList[13*i],@tbuf,13)
print(@fileList[13*i])
i++
numfiles:=i



then i will get my full file listing printed out. So for some reason the buffer changes and i am still not receiving the values from the array, but the pointer to a changing tbuf.

Post Edited (ThePenguinMaster) : 7/2/2009 11:43:35 PM GMT

localroger
07-03-2009, 07:42 AM
fsrw reports filenames in name dot ext null format, so they can be 8+1+3+1 or 13 bytes long. You need an array of n * 13 bytes where n is the number of filenames you want to save. You then use something like fstore.nextfile(@tbuf + n * 13) where n is your file counter. You then retrieve them from @tbuf+n*13 for each value of n as you walk the list.

localroger
07-03-2009, 07:45 AM
PenguinMaster: tbuf must itself be a 13 byte array the way you're using it. But you could substitute your filelist for where I used tbuf in my last example and eliminate the need for it entirely, just tell fstore to write straight into the array.

ThePenguinMaster
07-03-2009, 08:09 AM
lol yes I see what your saying.. I way over complicated that using the array lol . It is as simple as passing the section of the array into the nextfile function. I knew it seemed more complex than it had to be. I almost feel embarrassed that I had such a problem with that. Its been a while since I programmed the prop. so I'm sure I will have a lot more noobish questions, but thank you for helping me. Well here is the fixed code:





CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
tvPin = 24
cardPin = 20
obj
term: "tv_text"
fStore: "fsrw"

var
byte tbuf
byte fileList[20*13]
long numFiles
pub start | r, sta, bytes, i
term.start(tvPin)
GetFileList

i:=0
repeat while i < numFiles
print(@fileList[13*i])
i++

pub GetFileList|i
fStore.mount(cardPin)
fStore.opendir
i:=0
repeat while 0 == fStore.nextfile(@fileList[13*i++])
numfiles:=i

pub Print(text)
term.str(text)
term.out($0D)