File path transfer
I have a program that I need to run under * nix and windows. because the program takes file paths from files, the problem is what to do with the \
vs problem /
.
My current thought is to insert a regex that converts the wrong one to the right one, depending on which system I am on. This will cause any type to work on any of these systems. Besides the fact that I now have two problems , does anyone see any other problems?
(Other better solutions are more than welcome)
Edit: The main problem is that Windows paths work for unix and not the other way around.
source to share
Have you considered creating a file manager class that will handle all file problems? So, in the mail app, when you download the data file, you can call something like this.
LoadApplicationData(FileManager.GetDataFilePath)
Then your file manager will detect the environment it is in and return the path to the correct file path. This way you can also host the Win32 and Unix locatio standards (e.g. Program Files vs / usr or whatever).
source to share
Create an input parser to create a tree structure of nodes representing directories. You can then "save" by traversing the tree and writing down whatever delimiters you want, or perhaps doing different things like checking if a directory exists or writing metafiles. This is actually what I currently think would be useful for my own application :-)
source to share
You didn't say which language you are using, so I am selfishly assuming c / C ++. boost , if you want to use it, the filesystem library . Of course, if you are using a dynamic language, the FS abstraction libraries probably already exist there (for example, in perl, File :: Spec is pretty standard).
source to share
You did not tell us which files you are viewing. I'm going to assume that they are config files. There are many ways in this case, IMHO, the correct answer is to design your program to avoid being able to manipulate paths if possible. I posted an answer here: fooobar.com/questions/286570 / ... which matters.
ways:
- Add a command line parameter that allows the user to specify the path in question instead of reading it from the config file.
- Add a command line parameter so that the user can specify the base path. The paths in the config file will be interpreted as being under this base path.
- Divide the config file into three. One file will have cross platform configuration, another file will have Windows only configuration, and the final file will have Linux only configuration. Then the user can specify the correct path for Windows and Linux. On windows, your program will read the cross-platform config file and only the Windows-only config file. On Linux, it will read the cross platform file and only the Linux config file.
- Add preprocessing to the parsing of your config file. This will allow you to have a single config file where the user can force your program to ignore certain lines in the file depending on which OS the program is running on. This way, the user can specify the file path twice. Once for Linux and once for Windows.
- Redesign so that the files are always in the same directory as your executable - then the user only specifies the filenames in the config file, not the file paths.
- Use a simple function that switches "/" to "\". Then document to the user that they need to specify paths as Linux paths and this transformation will be applied to windows.
- Create your own minilanguage for this and write it down to the user. For example: "/" - specifies the directory separator, {root} - expands to the root of the file system, {cwd} - expands to the current directory, {app} - expands to the path to your application, etc. The user can then provide file paths, for example: {root} /myfiles/bob.txt on both platforms.
- Some paths will work on both platforms. For example: relative paths such as .. /my/bill.txt files. Limit your application to only work with these paths. Document this limitation and how your application handles user paths.
source to share