473,785 Members | 2,297 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2020
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.merke rk(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(st d::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%c gl%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_VE R)
... 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_atomi c.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(st d::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<cha r*> instead of
std::vector<std ::string>?

--
Peter van Merkerk
peter.van.merke rk(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
3087
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 fastest way to write and read just a simple : while data: f.write(struct.pack(format, integer-value)) or is there another, quicker and nicer way to do it????
24
5867
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 available.\n" int main(int argc, char *argv) {
12
4648
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 convert a whole file using Unix utilities, but this file will have only a few header records in EBCDIC)
1
4311
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 time after routine process A, the text file is named "mytext.dat" in following format with "#####" as separator. The maximum entries of them is 5. When reaching the fifth entry, it will delete the very first entry.
131
6230
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 portable C code - *not only* because, in a 'C sense', I
24
16138
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 such a function? ---------------- lovely newsgroup ----------------
1
9476
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 something like %V to pass in another format string and a va_list to allow nesting. It happens to work on my compiler, but I wasn't sure if it's portable to use va_list's as parameters to a variable argument function because va_list isn't always just a...
63
3267
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 mean=0,linput=0; FILE *fpr, *fpw;
65
5104
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 got errors of Failed to open file ./outdir/mytestout.txt. Below is the code: #include <stdio.h>
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9489
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10162
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10100
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.