How to write a portable function

  • Thread starter Henri =?ISO-8859-1?Q?Schom=E4cker?=
  • Start date
H

Henri =?ISO-8859-1?Q?Schom=E4cker?=

Hi folks,

I have a quite big class, which I want to use on UNIX-like systems and on
win32.
Until now, everything is absolutely portable. But now I need to read a
directory and use the os dependent functions.

So need to define and include different libraries and functions for both
OS-types. How can I do this using e.g. compiler #define's that all
compilers understand? And can I do this also inside of a function?

I'm using VC++ 6 on win32. Is there a special define set by the IDE like
e.g. __WIN32 or so that I could use?

With UNIX-like OS I'm using the typical combination of gcc, libtool and
autotools.

It's only the dir-read function so I don't want to write seperate
sourcefiles for both OS types.

Many thanks in advance,
yours Henri
 
P

Peter van Merkerk

Henri said:
Hi folks,

I have a quite big class, which I want to use on UNIX-like systems and on
win32.
Until now, everything is absolutely portable. But now I need to read a
directory and use the os dependent functions.

So need to define and include different libraries and functions for both
OS-types. How can I do this using e.g. compiler #define's that all
compilers understand? And can I do this also inside of a function?

I'm using VC++ 6 on win32. Is there a special define set by the IDE like
e.g. __WIN32 or so that I could use?

With UNIX-like OS I'm using the typical combination of gcc, libtool and
autotools.

It's only the dir-read function so I don't want to write seperate
sourcefiles for both OS types.

http://boost.org/libs/filesystem/doc/index.htm
 
H

Henri =?ISO-8859-1?Q?Schom=E4cker?=

Peter said:

Fist of all: Many thanks for the reply :)
I know the boost library and make use of it in my other linux projects, but
in this project, I only need the dir-read functions and have the code
already finished for win32 (This is the version I got paid for first).
Because it's just a few lines, I would like to use something in my code
like:

Simplified ;-)
8<--------8<--------8<--------8<--------8<--------8<--------
int readDiretory(std::vector<char*> &filelist)
{
...

#if defined(...)
win32 code
#endif
#if defined(...)
UNIX-type code
#endif

return 1;
}
8<--------8<--------8<--------8<--------8<--------8<--------

I put the required win32 #define <windows.h> into StdAfx.h, so only the few
lines in the source have to be dependent by the OS type.

Again, many thanks in advance,
yours Henri
 
G

Gianni Mariani

Henri said:
Hi folks,

I have a quite big class, which I want to use on UNIX-like systems and on
win32.
Until now, everything is absolutely portable. But now I need to read a
directory and use the os dependent functions.

So need to define and include different libraries and functions for both
OS-types. How can I do this using e.g. compiler #define's that all
compilers understand? And can I do this also inside of a function?

I'm using VC++ 6 on win32. Is there a special define set by the IDE like
e.g. __WIN32 or so that I could use?

With UNIX-like OS I'm using the typical combination of gcc, libtool and
autotools.

It's only the dir-read function so I don't want to write seperate
sourcefiles for both OS types.

Apart from using a third party library (as suggested by Peter in a
previous reply), when writing portable code, try not to litter your code
with conditionals. Code with lots of

#if defined(_MSC_VER)
... win32 stuff ...
#elif defined(unix)
... unix stuff ...
#endif

is very unreadable and difficult to maintain hence prone to error.


The alternative I use is to have a single "os.h" file which contains a
minimal amount of conditional stuff and to use the "#include MACRO"
trick. Within the system dependant headers, define exactly the same
interface but implemented in different files for different systems.

e.g.

in - at_os.h

00132 #if defined(WIN32) || defined(_WIN32)
00133
00134 #define AT_ATOMIC_H "at_win32_atomic.h"
00135
00136 #else
00137
00138 #define AT_ATOMIC_H "at_gx86_atomic.h"
00139
00140 #endif

Then in the "atomic.h" header, simply include AT_ATOMIC_H.

e.g.

in atomic.h

#include AT_ATOMIC_H

(system dependant header)

Do not include "windows.h" or any system header file in portable code
header files (with very few exceptions). If you have to, use the PIMPL
idiom to eliminate dependantcies.

An example is in the Austria library:
- portable header file -
http://austria.sourceforge.net/dox/html/at__os_8h-source.html

- win32 header file -
http://austria.sourceforge.net/dox/html/at__win32__atomic_8h-source.html

- gnu x86 header file -
http://austria.sourceforge.net/dox/html/at__gx86__atomic_8h-source.html
 
G

Gernot Frisch

I know the boost library and make use of it in my other linux
projects, but
in this project, I only need the dir-read functions and have the code
already finished for win32 (This is the version I got paid for first).
Because it's just a few lines, I would like to use something in my code
like:
....

There's a x-platform dirent.h availiable that let's you use the unix
directory comamnds for x-platforms.

If you want to do it on your own, define macros as:

IS_WINDOWS, IS_LINUX and so on... and define these in the top of the
header. So if you encounter a problem (WIN32 is defined, however
WIN_CE has not full support for all Win API command e.g.) you can
change the way the macros are defined in one place, not at every
location you used them.
Just a thought...

-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
 
P

Peter van Merkerk

Henri said:
Fist of all: Many thanks for the reply :)
I know the boost library and make use of it in my other linux projects, but
in this project, I only need the dir-read functions and have the code
already finished for win32 (This is the version I got paid for first).
Because it's just a few lines, I would like to use something in my code
like:

Simplified ;-)
8<--------8<--------8<--------8<--------8<--------8<--------
int readDiretory(std::vector<char*> &filelist)
{
...

#if defined(...)
win32 code
#endif
#if defined(...)
UNIX-type code
#endif

return 1;
}
8<--------8<--------8<--------8<--------8<--------8<--------

I put the required win32 #define <windows.h> into StdAfx.h, so only the few
lines in the source have to be dependent by the OS type.

Normally MSVC6 defines the WIN32 macro which you could for conditional
compilation. Alternatively you could use the WINVER macro which is
defined when you include directly or indirectly the <windows.h> header
file (which is extemely likely when you use the Windows API). Of course
you could also define your own macro's to control which path is taken
during compilation.

When you design the interface of those directory functions, make sure
that the interface is suitable for other platforms as well. This may not
be as trivial as it seems. If you get it wrong platform dependant stuff
may trickle through the rest of your code.

BTW. Why are you using std::vector<char*> instead of
std::vector<std::string>?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top