image provider

Reading C Structs


Disclaimer

This essay does not describe an existing computer program, just one that should exist. This essay is about a suggested student project in Java programming. This essay gives a rough overview of how it might work. I have no source, object, specifications, file layouts or anything else useful to implementing this project. Everything I have prepared to help you is right here.

This project outline is not like the artificial, tidy little problems you are spoon-fed in school, when all the facts you need are included, nothing extraneous is mentioned, the answer is fully specified, along with hints to nudge you toward a single expected canonical solution. This project is much more like the real world of messy problems where it is up to you to fully the define the end point, or a series of ever more difficult versions of this project and research the information yourself to solve them.

Everything I have to say to help you with this project is written below. I am not prepared to help you implement it; or give you any additional materials. I have too many other projects of my own.

Though I am a programmer by profession, I don’t do people’s homework for them. That just robs them of an education.

You have my full permission to implement this project in any way you please and to keep all the profits from your endeavour.

Please do not email me about this project without reading the disclaimer above.

This is a project for someone who needs to share files written or read by both Java and C/C++. C usually writes great structured chunks called structs in one big record, sometimes big-endian and sometimes little endian, dotted with alignment and padding bytes.

The way you read these manually in Java is with a long series of DataInputStream. readInt calls for big endian or LEDataInputStream. readInt calls for little endian (or readDouble etc.) The code is tedious to write and hard to maintain. Your task is to write a parser that examines the source code for a C struct and produces methods in Java to either read or write such a struct in either little or big endian format. It has getters and setters for the individual fields. A clever version would have getters and setters for substructs.

One of the complications is that C permitted nested structs. Another complication is matching any padding/alignment bytes your C compiler inserts. You will need to work out the algorithms of several different compilers and make it configurable and extensible.

C strings usually have a fixed allocation size, with a terminating null byte. You will have to scan for the nulls to find the actual length of the strings. If there are no nulls and no multi-byte encodings and all fields are characters, (an unlikely event) then you could decode the struct as a block and pluck out the fields with String.substring.

This code can be useful even without C/C++ to give C-like record i/o abilities to Java.

You might simplify the project by insisting each struct live in a separate source file in a common directory with the name XXXX.c or XXXX.cpp, where XXXX in the struct name, so that you can easily find the source for any struct. You might also insist that the struct or C++ class be pruned of any source code other than field declarations. A simplication for your first version is to handle only 8-bit chars in C/C++ and presume no padding/alignment bytes. To make it really easy, you might massage your struct definitions into csv files so you don’t need a parser to get the raw data.

Learning More

Oracle’s Javadoc on DataInputStream class : available:
Oracle’s Javadoc on DataOutputStream class : available:
csv
File IO Amanuensis
LEDataStream
parser
Struct Reader Amanuensis

This page is posted
on the web at:

http://mindprod.com/project/readc.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\project\readc.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[35.170.81.33]
You are visitor number