473,322 Members | 1,778 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,322 software developers and data experts.

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 2374
am*****@gmail.com 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 WorldPhoneNumber.
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 = "+1236546786524432ABC";

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.com 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
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...
7
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...
21
by: asm | last post by:
Hi All, Like typdef, does C have further support for portability? Thanks, ASM
93
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...
93
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...
239
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...
10
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...
18
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.