473,395 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to write a portable function

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
5 1997
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
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
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Thomas | last post by:
What's the quickest way to write and read 10.000 integer values ( or more ) to and from a file? Using struct somehow? The example in the docs shows how to handle to or three arguments, but is the...
24
by: Charles Ulrich | last post by:
Greetings, I hope my greenness isn't showing too bad by asking this, but I ran across this trivial program today that left me flabbergasted: #define MESSAGE "This account is currently not...
12
by: John Leslie | last post by:
I need to write a string to a file in EBCDIC. Do I need to do it character by character using a translation table, or is there a function to translate the whole string? (I am aware that I can...
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
24
by: Chen Shusheng | last post by:
Hello, I want to write a time delay function like "Timedelay(float time_lenth){}". When execute it, it will delay some seconds as long as "time_lenth" indicating. Could you help on how to write...
1
by: skillzero | last post by:
Is there a portable way to pass a va_list as a parameter to another function taking a variable argument list? I have a function that takes a printf-like format string and I'd like to use...
63
by: Bill Cunningham | last post by:
I don't think I can do this without some help or hints. Here is the code I have. #include <stdio.h> #include <stdlib.h> double input(double input) { int count=0,div=0; double...
65
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.