Shop OBEX P1 Docs P2 Docs Learn Events
Moving strings into an array — Parallax Forums

Moving strings into an array

ThePenguinMasterThePenguinMaster Posts: 89
edited 2009-07-03 00:09 in Propeller 1
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[noparse][[/noparse]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[i] == 0 
      term.str(fileList[i])
      term.out($0D) 
      i++ 

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

[/i][/i]



When I run the code, I get no output. If I change “LONG[noparse][[/noparse] fileList ][noparse][[/noparse] i ] := @tbuf” to “fileList][noparse][[/noparse] 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.

Comments

  • TimmooreTimmoore Posts: 1,031
    edited 2009-07-02 22:32
    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[noparse][[/noparse]9*i]. Then thee are upto 20 names in filelist, the start of each name is 9 bytes apart.
  • ThePenguinMasterThePenguinMaster Posts: 89
    edited 2009-07-02 23:38
    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[noparse][[/noparse]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[noparse][[/noparse]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[noparse][[/noparse]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[noparse][[/noparse]13*i],@tbuf,13)
            print(@fileList[noparse][[/noparse]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
  • localrogerlocalroger Posts: 3,452
    edited 2009-07-02 23:42
    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.
  • localrogerlocalroger Posts: 3,452
    edited 2009-07-02 23:45
    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.
  • ThePenguinMasterThePenguinMaster Posts: 89
    edited 2009-07-03 00:09
    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[noparse][[/noparse]20*13]
        long numFiles
    pub start | r, sta, bytes, i
       term.start(tvPin)       
       GetFileList
    
       i:=0
       repeat while i < numFiles
          print(@fileList[noparse][[/noparse]13*i])
          i++ 
                         
    pub GetFileList|i
       fStore.mount(cardPin)    
       fStore.opendir
       i:=0
       repeat while 0 == fStore.nextfile(@fileList[noparse][[/noparse]13*i++])
       numfiles:=i        
    
    pub Print(text)
            term.str(text)
            term.out($0D)
    
    
    
Sign In or Register to comment.