 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Matthias Hofmann Guest
|
Posted: Tue Sep 06, 2005 4:07 pm Post subject: Portable way to retrieve path separator |
|
|
Hello everyone!
I wonder if there is a portable way to retrieve the platform-dependant path
separator. Windows uses '' while Linux uses '/', and other platforms might
still use other characters. The standard says nothing about path separators,
and I found no information on the internet either.
If there is no way to tell the path separator, then can anyone please tell
me a portable way to make clients of my code aware of the problem? What I
have in mind is something like this:
#ifdef _WIN32
#define PATH_SEPARATOR "\"
#else
#pragma message ( "Could not determine path separator." )
#endif
However, as far as I know the message pragma is not portable.
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Klomanager
http://www.anvil-soft.de - Die Macher des Klomanagers
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Sep 07, 2005 12:56 am Post subject: Re: Portable way to retrieve path separator |
|
|
Matthias Hofmann wrote:
| Quote: | I wonder if there is a portable way to retrieve the platform-dependant path
separator. Windows uses '' while Linux uses '/', and other platforms might
still use other characters. The standard says nothing about path separators,
and I found no information on the internet either.
|
The Standard says nothing because the system doesn't have to have any
"paths" and hence nothing like "path separator" can be standardized.
| Quote: | If there is no way to tell the path separator, then can anyone please tell
me a portable way to make clients of my code aware of the problem?
|
Tell them to supply it.
| Quote: | What I
have in mind is something like this:
#ifdef _WIN32
#define PATH_SEPARATOR "\"
#else
#pragma message ( "Could not determine path separator." )
#endif
However, as far as I know the message pragma is not portable.
|
Right, it's not.
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Russell Hind Guest
|
Posted: Wed Sep 07, 2005 12:57 am Post subject: Re: Portable way to retrieve path separator |
|
|
Matthias Hofmann wrote:
| Quote: | Hello everyone!
I wonder if there is a portable way to retrieve the platform-dependant path
separator. Windows uses '' while Linux uses '/', and other platforms might
still use other characters. The standard says nothing about path separators,
and I found no information on the internet either.
If there is no way to tell the path separator, then can anyone please tell
me a portable way to make clients of my code aware of the problem? What I
have in mind is something like this:
|
Have you looked at boost::filesystem library?
http://www.boost.org/libs/filesystem/doc/index.htm
Cheers
Russell
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Wed Sep 07, 2005 1:05 am Post subject: Re: Portable way to retrieve path separator |
|
|
Matthias Hofmann wrote:
| Quote: | #ifdef _WIN32
#define PATH_SEPARATOR "\"
#else
#pragma message ( "Could not determine path separator." )
#endif
|
#if defined(WIN32) || defined(_WIN32)
# define PATH_SEPARATOR "\"
#else
# error "Could not determine path separator."
#endif
Uli
--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Wed Sep 07, 2005 1:06 am Post subject: Re: Portable way to retrieve path separator |
|
|
In article <431d82af$0$2110$9b4e6d93 (AT) newsread2 (DOT) arcor-online.net>,
Matthias Hofmann <hofmann (AT) anvil-soft (DOT) com> wrote:
| Quote: | Hello everyone!
I wonder if there is a portable way to retrieve the platform-dependant path
separator. Windows uses '' while Linux uses '/', and other platforms might
still use other characters. The standard says nothing about path separators,
and I found no information on the internet either.
If there is no way to tell the path separator, then can anyone please tell
me a portable way to make clients of my code aware of the problem? What I
have in mind is something like this:
#ifdef _WIN32
#define PATH_SEPARATOR "\"
#else
#pragma message ( "Could not determine path separator." )
#endif
However, as far as I know the message pragma is not portable.
replace #pragma messsage with |
#error Could not determine path separator
is easiest solution.
#error generates a complier error with the text after #error on the
same line.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Wed Sep 07, 2005 9:58 am Post subject: Re: Portable way to retrieve path separator |
|
|
Matthias Hofmann wrote:
| Quote: | Hello everyone!
I wonder if there is a portable way to retrieve the platform-dependant path
separator. Windows uses '' while Linux uses '/', and other platforms might
still use other characters. The standard says nothing about path separators,
and I found no information on the internet either.
If there is no way to tell the path separator, then can anyone please tell
me a portable way to make clients of my code aware of the problem? What I
have in mind is something like this:
#ifdef _WIN32
#define PATH_SEPARATOR "\"
#else
#pragma message ( "Could not determine path separator." )
#endif
However, as far as I know the message pragma is not portable.
|
Actually, Windows will accept the slash character in either direction
as a file system delimiter (see below). But the issue is not
necessarily OS-dependent since the delimiter could depend on the format
of the volume being accessed
The good news is that there there is a portable way to specify file
path names, just not one that C++ provides. As far as I know #include
directives are about the only place where file system paths would have
much bearing on a C++ program. But the C++ Standard leaves the
interpretation of an include directive open. It simply states that the
mapping between the the contents of the include directive and the local
file system is implementation-defined.
But as long as the C++ program can check the system APIs for POSIX
compliance at runtime, then the program can call POSIX routines with
POSIX compliant path names (which look like traditional UNIX paths).
Most of the popular OSes including Windows, Linux, and MacOS X do have
POSIX APIs for accessing their file systems; so a portable
implementation in C++ should be possible.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
ThosRTanner Guest
|
Posted: Wed Sep 07, 2005 10:08 am Post subject: Re: Portable way to retrieve path separator |
|
|
Matthias Hofmann wrote:
| Quote: | Hello everyone!
I wonder if there is a portable way to retrieve the platform-dependant path
separator. Windows uses '' while Linux uses '/', and other platforms might
still use other characters. The standard says nothing about path separators,
and I found no information on the internet either.
Acutally, windows32 will let you use / as a path separator, so as long |
as you intend to use only windows or unix, you needn't worry.
| Quote: | If there is no way to tell the path separator, then can anyone please tell
me a portable way to make clients of my code aware of the problem? What I
have in mind is something like this:
#ifdef _WIN32
#define PATH_SEPARATOR "\"
#else
#pragma message ( "Could not determine path separator." )
#endif
However, as far as I know the message pragma is not portable.
To what precisely? |
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Wed Sep 07, 2005 10:08 am Post subject: Re: Portable way to retrieve path separator |
|
|
Matthias Hofmann wrote:
| Quote: | I wonder if there is a portable way to retrieve the
platform-dependant path separator. Windows uses '' while
Linux uses '/', and other platforms might still use other
characters.
|
Windows uses both. If you're parsing pathnames, you have to
accept both '/' and '' as path separators. If you're
displaying pathnames, '' is preferred.
| Quote: | The standard says nothing about path separators, and I found
no information on the internet either.
If there is no way to tell the path separator, then can anyone
please tell me a portable way to make clients of my code aware
of the problem? What I have in mind is something like this:
#ifdef _WIN32
#define PATH_SEPARATOR "\"
#else
#pragma message ( "Could not determine path separator." )
#endif
However, as far as I know the message pragma is not portable.
|
Never heard of it. And what happens when you have twenty
different systems, with twenty different attributes.
The usual solution is to create a system dependant include file
for each system, which contains all of this sort of
information. Things like:
static char const GB_optId = '-' ; // - under UNIX
static char const GB_altOptId = '+' ; // + under UNIX
static char const GB_asciiEsc = '\' ; // under UNIX
static char const GB_preferredPathSep = '/' ; // / under UNIX
static char const GB_allowedPathSep[] = "/" ;
static bool const GB_ignoreCase = false ; // in filenames
only.
static char const GB_stdinName[] = "-" ;
static int const GB_exitSuccess = 0 ;
static int const GB_exitWarning = 1 ;
static int const GB_exitError = 2 ;
static int const GB_exitFatal = 3 ;
static int const GB_exitInternal = 4 ;
This version is for Unix based systems. For Windows,
GB_allowedPathSep becomes "/\".
Put the version for each system in a separate directory, and use
-I to select at compile time. (Your compiler invocation is
pretty system dependant anyway, so adding a system dependant
option to it won't change much.)
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Matthias Hofmann Guest
|
Posted: Wed Sep 07, 2005 5:14 pm Post subject: Re: Portable way to retrieve path separator |
|
|
"Greg Herlihy" <greghe (AT) pacbell (DOT) net> schrieb im Newsbeitrag
news:1126048768.497680.4010 (AT) g14g2000cwa (DOT) googlegroups.com...
| Quote: | Most of the popular OSes including Windows, Linux, and MacOS X do have
POSIX APIs for accessing their file systems; so a portable
implementation in C++ should be possible.
|
What I actually want to do is take the __FILE__ predefined macro and cut off
the path in front of the filename, so that
"C:MyDocumentsMyProjectmain.cpp" turns into "main.cpp". In order to
achieve this, I usually write code like the following:
char* cutoff_path( char* p )
{
char* _p;
if ( ( _p = strrchr( p, '\' ) ) != NULL )
p = _p + 1;
return p;
}
However, it seems to be a bad idea to hard code the backslash if you want to
be portable.
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Klomanager
http://www.anvil-soft.de - Die Macher des Klomanagers
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Matthias Hofmann Guest
|
Posted: Wed Sep 07, 2005 5:16 pm Post subject: Re: Portable way to retrieve path separator |
|
|
"ThosRTanner" <ttanner2 (AT) bloomberg (DOT) net> schrieb im Newsbeitrag
news:1126082481.385997.154230 (AT) g47g2000cwa (DOT) googlegroups.com...
| Quote: | However, as far as I know the message pragma is not portable.
To what precisely?
|
Actually to any platform that supports C++, although I mainly use Windows.
Nevertheless, I want to make my code available to other people as well, and
they might use systems of any kind.
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Klomanager
http://www.anvil-soft.de - Die Macher des Klomanagers
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Matthias Hofmann Guest
|
Posted: Wed Sep 07, 2005 5:19 pm Post subject: Re: Portable way to retrieve path separator |
|
|
"Carl Barron" <cbarron413 (AT) adelphia (DOT) net> schrieb im Newsbeitrag
news:060920051629433708%cbarron413 (AT) adelphia (DOT) net...
| Quote: | In article <431d82af$0$2110$9b4e6d93 (AT) newsread2 (DOT) arcor-online.net>,
However, as far as I know the message pragma is not portable.
replace #pragma messsage with
#error Could not determine path separator
is easiest solution.
#error generates a complier error with the text after #error on the
same line.
|
Is #error a platform independent directive? It is not even listed in the
MSDN-Library, but maybe I simply oversaw it.
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Klomanager
http://www.anvil-soft.de - Die Macher des Klomanagers
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Matthias Hofmann Guest
|
Posted: Wed Sep 07, 2005 5:20 pm Post subject: Re: Portable way to retrieve path separator |
|
|
"Russell Hind" <rh_gmane (AT) mac (DOT) com> schrieb im Newsbeitrag
news:dfkfjh$p0l$1$8300dec7 (AT) news (DOT) demon.co.uk...
No, but this looks very interesting, thank you for this link. However, it
would be great if my code would not require separate libraries.
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Klomanager
http://www.anvil-soft.de - Die Macher des Klomanagers
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Matthias Hofmann Guest
|
Posted: Wed Sep 07, 2005 5:21 pm Post subject: Re: Portable way to retrieve path separator |
|
|
"Ulrich Eckhardt" <doomster (AT) knuut (DOT) de> schrieb im Newsbeitrag
news:3o6db5F4fdbtU2 (AT) uni-berlin (DOT) de...
| Quote: | Matthias Hofmann wrote:
#ifdef _WIN32
#define PATH_SEPARATOR "\"
#else
#pragma message ( "Could not determine path separator." )
#endif
#if defined(WIN32) || defined(_WIN32)
# define PATH_SEPARATOR "\"
#else
# error "Could not determine path separator."
#endif
|
Thank you, that looks promising. Is there a list of macros for other
systems, like Linux or Mac?
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Klomanager
http://www.anvil-soft.de - Die Macher des Klomanagers
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Matthias Hofmann Guest
|
Posted: Wed Sep 07, 2005 5:21 pm Post subject: Re: Portable way to retrieve path separator |
|
|
"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> schrieb im Newsbeitrag
news:bqjTe.33701$Tf5.22212 (AT) newsread1 (DOT) mlpsca01.us.to.verio.net...
| Quote: | The Standard says nothing because the system doesn't have to have any
"paths" and hence nothing like "path separator" can be standardized.
|
The standard also uses locales to support internationalization, though the
system does not have to have formats for the display of date and time. All
that would be required is a function that returns a character, which would
be the implementation-dependent path separator.
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Klomanager
http://www.anvil-soft.de - Die Macher des Klomanagers
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Bjorn Reese Guest
|
Posted: Wed Sep 07, 2005 7:09 pm Post subject: Re: Portable way to retrieve path separator |
|
|
Matthias Hofmann wrote:
| Quote: | Thank you, that looks promising. Is there a list of macros for other
systems, like Linux or Mac?
|
http://predef.sourceforge.net/
--
mail1dotstofanetdotdk
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|