By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,866 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,866 IT Pros & Developers. It's quick & easy.

How to write a portable function

P: n/a
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

--
| Henri Schomäcker - Byteconcepts, VIRTUAL HOMES
| Datendesign für Internet und Intranet
| http://www.byteconcepts.de
| http://www.virtual-homes.de
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Henri Schomäcker wrote:
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
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 22 '05 #2

P: n/a
Peter van Merkerk wrote:

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


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

--
| Henri Schomäcker - BYTECONCEPTS, VIRTUAL HOMES
| Datendesign für Internet und Intranet
| http://www.byteconcepts.de
| http://www.virtual-homes.de
Jul 22 '05 #3

P: n/a
> 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
Jul 22 '05 #4

P: n/a
Henri Schomäcker wrote:
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/h...8h-source.html

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

- gnu x86 header file -
http://austria.sourceforge.net/dox/h...8h-source.html

Jul 22 '05 #5

P: n/a
Henri Schomäcker wrote:
Peter van Merkerk wrote:
http://boost.org/libs/filesystem/doc/index.htm

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>?

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.