Microsoft Small Basic

Program Listing: JQK779
' Get Absolute Path from Relative Path 0.1b
' Copyright (c) 2014 Nonki Takahashi. The MIT License.
' Program ID
' TODO add \ to C:
' TODO show error for too many ..\..\..\..
'
curDir = Program.Directory ' current directory
relPath = "."
While relPath <> ""
TextWindow.WriteLine("Current directory = " + curDir)
TextWindow.Write("Relative path? ")
relPath = TextWindow.Read()
GetAbsPath()
TextWindow.WriteLine("Absolute path = " + absPath)
TextWindow.WriteLine("")
EndWhile
Sub GetAbsPath
' param curDir - current directory
' param relPath - relative path
' return absPath - absolute path
If Text.IsSubText(relPath, ":") Or Text.StartsWith(relPath, "\") Then
absPath = relPath
Else
absPath = curDir + "\" + relPath
RemoveDots()
EndIf
EndSub
Sub RemoveDots
' param absPath - with dots
' return absPath - without dots
path = absPath
folder = ""
n = 0
While Text.IsSubText(path, "\")
n = n + 1
p = Text.GetIndexOf(path, "\")
folder[n] = Text.GetSubText(path, 1, p - 1)
path = Text.GetSubTextToEnd(path, p + 1)
EndWhile
If path <> "" Then
n = n + 1
folder[n] = Text.GetSubText(path, 1, p - 1)
EndIf
While Array.ContainsValue(folder, ".") Or Array.ContainsValue(folder, "..")
For i = 1 To n
If folder[i] = "." Then
RemoveEntry()
i = n + 1 ' break
ElseIf folder[i] = ".." Then
i = i - 1
RemoveEntry()
RemoveEntry()
i = n + 1 ' break
EndIf
EndFor
EndWhile
absPath = folder[1]
For i = 2 To n
absPath = absPath + "\" + folder[i]
EndFor
EndSub
Sub RemoveEntry
' param folder - entry array
' param i - entry index
' return folder - updated entry array
' return n
For _i = i To n - 1
folder[_i] = folder[_i + 1]
EndFor
folder[n] = ""
n = n - 1
EndSub