473,811 Members | 3,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Portability

Is there any open source C++ library that performs platform independent
binary file I/O? Major concerns being endianness,
structure alignment, size of data types on different platforms, 32-bit
vs 64-bit architectures and format of floating point numbers. Can
someone give a clear cut explanation on how this could be achieved
using some snippet if possible? A preliminary demonstration of
serializing and deserializing a float could be a good starter ...

Thanks
Amol

Jul 25 '05 #1
3 2427
am*****@gmail.c om wrote:
Is there any open source C++ library that performs platform independent
binary file I/O? Major concerns being endianness,
structure alignment, size of data types on different platforms, 32-bit
vs 64-bit architectures and format of floating point numbers. Can
someone give a clear cut explanation on how this could be achieved
using some snippet if possible? A preliminary demonstration of
serializing and deserializing a float could be a good starter ...

Thanks
Amol


Qt library's QDataStream class will do this

http://doc.trolltech.com/4.0/qdatastream.html

Qt 4.0 is available on win/mac/linux/unix under the GPL

Jul 26 '05 #2
Hello Amol,

On Mon, 25 Jul 2005 22:18:15 UTC, "am*****@gmail. com" <am*****@gmail. com>
wrote:
Is there any open source C++ library that performs platform independent
binary file I/O?
There are libraries that help with that sort of thing. They aren't
a requirement for platform independance though.
Major concerns being endianness,
Byte order is a fairly simple concept to encode correctly. The problem
is determining what format the data is in. There are a few simple coding
techniques that can be used to determine the endianness and wordsize of
your target platform. It is much easier to simply establish a standard
binary format and be able to translate between that and your current
machine architecture.
structure alignment,
This varies by platform and can be difficult for some platforms to
manipulate. Various alignments are easy on 80x86 architecture but
somewhat harder on other platforms. Again, it is establishing a standard
format and allowing that some architectures may have trouble encoding
that format.
size of data types on different platforms, 32-bit
vs 64-bit architectures and format of floating point numbers. Can
someone give a clear cut explanation on how this could be achieved
using some snippet if possible? A preliminary demonstration of
serializing and deserializing a float could be a good starter ...
Okay, let's say we decide to use a given floating point format
as our standard and that standard defines that values are encoded
in a four (eight-bit) byte in a certain byte order. All you need
is to write a small routine to: A-decide what byte order to read/write
the data, and B-to read/write a floating point value. Floating point
values could be problematic if your machine supported a floating
point encoding format that was fundamentally different than what your
data file format would be.
Thanks
Amol


The ACE library has some code to help with this. So do the standard
TCP/IP libraries. You might also try looking up references for
"network byte order".

Many years ago I needed to load/store and communicate with a large
tree of data that some might use XML for today. It used TLD or
Tag-Length-Data formatting. Basically you write a tag that states
what comes next, its length, and the data. It is just a binary form
of XML-like encoding.

The byte encoding was defined for all related programs. The underlying
tree structure was written exactly once with an in-memory object oriented
access mechanism. Every object (class) also had to include its own
encode/decode (<< and >>) operators. The code for this data structure
was written entirely in portable code. I also mandated that no other code
be added to this particular module/library. Thus, I had all access
mechanisms
to the data, binary encode/decode, and data validation all in one library.

The low level data started with Flags, Bytes, Strings, Integers, and
so on. Then I'd derive from String and create something like NumericString.
Tha might be derived from and called NADPPhoneNumber . (NADP stands for
North American Dialing Plan). Another derivation was for WorldPhoneNumbe r.
Various friendships and conversions were allowed so that you could easily
convert from one similar format to another. Validation errors were
mandatory
so something like

NADPPhoneNumber MyPhoneNumber = "+1236546786524 432ABC";

was gauranteed to throw an exception if the action wasn't legal. The
general
goal of the library was to encode all the information for processing
an automated or manual phone call and recording the processed results. It
could be used for file storage, passing a call-in-process from an automated
call handler to a manual operator, and so on.

A second library was then developed that duplicated this object-tree
structure that was UI specific. So there was a self describing GUI with
editors for all of the data classes. Validation at this level was somewhat
more meaningful and the example above would be flagged as having an error
and the error text could be read. A simple arrangement of GUI design
made it easy to build complex and user-friendly editors.

The base structure only had 300 or so classes and never required any
fixes. Classes were added as the project evolved.

As layers were added, so was a drag-and-drop GUI editor for constructing
the phone call. The goal of all this was to take a shop that basically
programmed every call into real code and convert it to one that could
allow end users the ability to construct their own phone call systems
reusing the companies automated systems and operators. Since the system
supported validation at all levels, the end user knew if the description
was correct. Behind the scenes valdiation also allowed incorrect
descriptions to be rereouted back to customers. It was a very productive
nine month project that drastically changed a well known telephone company.
New phone call ideas could be created and implemented based on interpreting
the demands of the lower level call handling structure and passing the
results between machines as needed. Eventually the system added a few
more bells and whistles and it also build web pages to go along with the
automated attendants and operator centers. It was a great inbound only
call center solution that allowed salespersons to make and sell call
center solutions from their laptops in just a few hours or minutes.
....they wanted an outbound verson but I was able to squash that idea
by changing the "Answer" portion of the product name to "Bother".

Sorry to be so long winded. Yes, binary objects can easily be
defined and used. They are often very useful and reusable.
Proper separation of your layers (e.g. data and UI) can also
be very useful.

David
Jul 26 '05 #3
Ian
am*****@gmail.c om wrote:
Is there any open source C++ library that performs platform independent
binary file I/O? Major concerns being endianness,
structure alignment, size of data types on different platforms, 32-bit
vs 64-bit architectures and format of floating point numbers. Can
someone give a clear cut explanation on how this could be achieved
using some snippet if possible? A preliminary demonstration of
serializing and deserializing a float could be a good starter ...

The simplest solution is to stream them as ASCII. This is the only
truly portable format.

Even with text, you will still have issues with 64 bit into 32 bit values.

Ian
Jul 26 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
2182
by: Tim Tyler | last post by:
I just ported one of the apps I wrote using PHP 5 under Windows to PHP 4 under unix. I knew I wanted to deploy in this environment - and wrote my code in anticipation of running in the other environment. A brief summary follows: Portability was not too bad. However there were several problems I did not anticipate.
7
1623
by: fabio de francesco | last post by:
Hi, I'm not a professional programmer, but I've been writing C/C++ and Ada programs for a few years on GNU/Linux without ever concerning on standards and portability to other OSs. I've always noted that when I write code I must use lots of platform specific system calls (POSIX and X/OPEN). Often I found myself using threads and concurrent processes with some sort of IPC. Some time I need some socket API. When I just want to open a...
21
1931
by: asm | last post by:
Hi All, Like typdef, does C have further support for portability? Thanks, ASM
93
3690
by: roman ziak | last post by:
I just read couple articles on this group and it keeps amazing me how the portability is used as strong argument for language cleanliness. In my opinion, porting the program (so you just take the source code and recompile) is a myth started 20-30 years ago when world consisted of UNIX systems. Well, world does not consist of UNIX systems anymore, but there are 100s of different systems running in cell-phones, DVD players, game consoles...
93
4020
by: jacob navia | last post by:
In this group there is a bunch of people that call themselves 'regulars' that insist in something called "portability". Portability for them means the least common denominator. Write your code so that it will compile in all old and broken compilers, preferably in such a fashion that it can be moved with no effort from the embedded system in the coffe machine to the 64 bit processor in your desktop.
239
10345
by: Eigenvector | last post by:
My question is more generic, but it involves what I consider ANSI standard C and portability. I happen to be a system admin for multiple platforms and as such a lot of the applications that my users request are a part of the OpenSource community. Many if not most of those applications strongly require the presence of the GNU compiling suite to work properly. My assumption is that this is due to the author/s creating the applications...
10
3827
by: Lionel B | last post by:
Greetings, I have some code that is to read unformatted data from disc and interpret it as blocks of unsigned integers. In an attempt to achieve efficiency (it is pretty essential for my application that the code be speed optimized ) I use reinterpret_cast to alias a block of chars read in from disc as a block of integer "words". My existing code (see simplified code below) appears to work well enough on the platforms available to me,...
18
409
by: jacob navia | last post by:
One of the holy cows here is this "portability" stuff. In practice, portability means: 1) Use the least common denominator of all the supported systems. 2) Between usability / good user experience and portability always choose portability since this minimizes programming effort
0
10647
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
10384
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...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7667
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
5553
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...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4338
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.