Conceptually directory structures are trees, and paths are lists of nodes that represent traversal(1). When you think of things that way instead of strings with slashes, life becomes easier, and common tools tend to just work. Pushing to the boundaries of your application as much representational decoration as possible, and internally working with more native data structure types is a principal that doesn't only apply to data serialization formats, it also applies to filesystem paths.
Your algorithm should be working with lists of directory names and file names, and converting to slash-delimited strings at the ingress and egress boundaries.
(1) Actually many file systems are directed acyclic graphs, thanks to concepts such as hard linking, but conceptually we can treat them like heirarchical data structures, keeping in mind that it's possible for a node in the tree to point to any other node anywhere else in the tree. ...so the tree concept does sort of break down, but is a useful white lie.
|