http://regedit.gamedev.pl/ Graphics programming, game programming, C++, games, Windows, Internet and more... |
| Main Page | Blog | Productions | Gallery | Download | Links | About me |
22:00
Tue
12
Jan 2010
Processing File Paths in C++
Paths to files and directories are special kind of strings. Sometimes they have to be processed, e.g. merged or splitted into some parts like drive letter, directory, file name and extension. Standard C library provides two function for this: makepath and splitpath. They use char* strings and operate always on all possible parts (drive, dir, fname, ext). In my opinion they are not very convenient. On the other hand, modern programming languages have more functionality in this field. For example, .NET has System.IO.Path static class with methods like GetExtension, GetFileName, GetDirectoryName.
To make operations on file paths convenient in C++, I've developed some time ago my own functions for that which operate on std::string strings. I've included them in my CommonLib library. They are similar a bit to these from Delphi :) Here is how I approach this topic: First, a common task is to extract part of a path (e.g. "C:\Windows\notepad.exe"). My functions for this are:
I also have some functions for processing paths:
Path can be absolute or relative. In Windows, absolute paths start with drive letter like "C:\" (which we can recognized by looking for colon) or network share (paths that begin with "\\"). Relative paths (where most basic example is a single file name) make sense only in some context. When we use relative paths to open real files, we address them relative to the "Current Directory" (which is a separate topic, I will write about it another time). To deal with absolute and relative paths, I've coded functions such as:
NormalizePath function preprocessess all the . and .. drectories in the path. A single dot used as directory name means "my own directory" (just does nothing) and two dots mean going up one level to the parent directory. So the path we consider here is logically equal to "C:\Windows\.\system32\..\notepad.exe". Paths like this still work, but they can look a bit unclear, so you can use NormalizePath function to clean them.
Paths in Linux are quite different. My functions also support them, although I didn't test this case in real situations yet. First of all, there are no drive letters in Linux, so absolute paths just start with '/', like "/etc/passwd". Second, a slash sign is used as a separator instead of backslash (it's interesting that slash also works in Windows). It's also more common in Linux than in Windows to meet files that don't have extension or have double extension (like "MyFile.tar.gz"). A question arises here whether what we call "extension" starts from the first or from the second dot :)
Comments (1) | Tags: algorithms c++ commonlib
| vashpan 2010-01-13 15:49:28 | It's very convenient to use forward slash in paths even on Windows. It works well, I've never met a situation when such a path syntax doesn't work. IMO it's better to write in C-like languages source: "data/img/some_image.png" instead of "data\\img\\some_image.png" And this is cross platform, works on Windows, Mac OS X and Linux. |
| [Stat] [Admin] [STAT NO AD] [pub] [Mirror] | Copyright © 2004-2010 Adam Sawicki | 0.62 s |