views:

178

answers:

6

I have a program that I need to run under *nix and windows. because the program takes file paths from files the issue is what to do about the \ vs / issue.

My current thought is to put in a regex that converts the wrong one to the right one depending on what system I'm on. This will have the effect of letting either type work on either system. Aside from the fact that now I have two problems, does anyone see any other problems?

(Other better solutions are more than welcome)

Edit: the primary issue is getting windows paths to work on unix rather than the other way around.

+5  A: 

The / is fully supported in win32 too.

Also see this related question

Jonas Gulle
+1  A: 

Windows will generally accept either \ or /,so standardizing on / may make your problem simpler as long as you have complete control over the filenames.

Mark Ransom
good point, but I don't :(
BCS
+1  A: 

Have you considered creating a "file manager" class that will handle all of the file pathing issues for you? That way in your mail application, when you're loading a data file, you can call something like this.

LoadApplicationData(FileManager.GetDataFilePath)

Then your file manager will detect the environment that it is in and return the proper file path option. That way you can also accomodate for Win32 vs. Unix locatio standards (like Program Files vs /usr or whatever) as well.

Dillie-O
That's more or less equivalent to what I plan to do.
BCS
Looking up at your edit, by creating the file manager class, you won't have to worry about getting your windows paths to work on UNIX, you can keep each one separate, you'll just need to instruct the user to install to the proper directory if no installer is used.
Dillie-O
How does that follow? In a file I have the text "./Foo/Bar.txt" and I need to open that file. No mater how I do it, on Unix I need to convert the / to \. Am I missing something?
BCS
In pseudo code, your file manager would do something like this:If Environment.IsWindows Then Return Environment.WinUserDirectory + "/" + AppName + "/Bar.txt"Else Return "/usr/" + UserName + "/AppName/Bar.txt"End If
Dillie-O
That won't do me any good because it ignores what the current dir is. The path I need to process is a relative path. You seem to be solving a different problem than I'm looking for.
BCS
+1  A: 

Note that Win32 paths are complex when you consider drive letters (no analog on Unix) and the special 'forks' (MacOS pre-X term - likewise no analog on Unix in general, though MacOS X has them - surprise, surprise) that can be provided. Be careful.

Jonathan Leffler
A: 

Create a parser for your input to create a tree structure of nodes representing directories. Then you can 'save' by walking the tree and writing whatever delimiters you want or optionally doing different things, like checking if the directory exists or writing meta files. This is actually something that I am just now thinking would be useful for my own application :-)

Klathzazt
An ambitous approach.
BCS
A: 

You didn't say what language you are using, so I'm going to selfishly assume c/c++. boost, if you are willing to use it, has a filesystem library. Of course, if you are using a dynamic language, FS abstraction libraries probably already exist there too (e.g. in perl, File::Spec is quite standard).

Mike Ellery
.NET, but only if it is insanely trivial. Otherwise I will need to write my own.
BCS