Shop OBEX P1 Docs P2 Docs Learn Events
Improving my spin? — Parallax Forums

Improving my spin?

jmspaggijmspaggi Posts: 629
edited 2010-09-07 06:18 in Propeller 1
Hi dear spin experts folks.

I did a spin methode, and I think it can be improved. I'm "spinning" since only few months, so I'm not very good with that.

Just to learn, is there a way to improve this method?

The goal of this method is to call changeDirectory for each token is the received path.

Like if we receive /pathA/pathB, we need to call changeDirectory (pathA) then changeDirectory (pathB).

The received parameter can start or and with a / or a \

So we can receive pathA\pathB\ as well as \pathA\pathB

So far, it alway go from the root. But maybe the 1st one should go from the currend directory only.

So is there anywhere we can improve the Spin code below?

Thanks,

JM
PUB changePath(path) | i
  result := 0
  if byte[path] == "/" or byte[path] == "\"
    result := 1
  i := result
  changeDirectory(string("/"))
  repeat until result == strsize(path)
    if ((byte[path+result] == "/") or (byte[path+result] == "\"))
      bytemove (@changePathNameBuffer, path+i, result-i)
      byte[@changePathNameBuffer+result-i] := 0
      changeDirectory(@changePathNameBuffer)
      i := result+1
    result++
  return listWorkingDirectory

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2010-09-05 12:00
    In your examples you call the functions with "pathA\pathB\" or with "/pathA/pathB". My guess is that the second one won't change the directory to pathB because you only call changeDirectory when you found a "/" or a "\" after the sub-tree-name.

    So in fact your path has to end with a "/" or a "\". Or you have to change the code to also call changeDirectory in case the loop is exited and i is not equal to result+1.

    Another improvement would be to use the passed path instead of doing a bytemove. You can simply replace the found "\" or "/" with a $00. Then you have a valid string that you can use directly. Youd then call changeDirectory with path+i as you do it in the bytemove. If the path string should not be destroyed you can later on replace the $00 with "\" or "/".
  • jmspaggijmspaggi Posts: 629
    edited 2010-09-07 06:18
    Good idea to use the received path! Like just changing all the "/" and "\" by #00 and giving all the tokens one by one to change directory... I will try that.

    Also, you're right. Look like it's not working if the path is not ending by "/" and "\". I will have to fix that too!

    Thanks,

    JM
Sign In or Register to comment.