Coding example for thrift in C, using files - c

I've started using thrift for C. I managed to generated the .c and .h files via the compiler. I'm looking to write to a file, preferably JSON. However there are no examples on the apache thrift site. A internet search turns up next to nothing useful. Does anybody have any sample code that I can use? I essentially have a struct has a bunch of ints and char *.

However there are no examples on the apache thrift site. A internet search turns up next to nothing useful.
That's simply not true, we have a great tutorial covering a great part of the languages. You can find them quite easily via Google. The tutorial code can be found in the release tarball or in the Git repository as a top-level directory named tutorial.
Since you are looking specifically for JSON, I recommend to have a look at the cross-language test client/server, which can be found under test or lib (a bit inconsistent right now, we are about cleaning that up). AFAIK for plain C there is no JSON available, but for C++ there is.
In order to store things into a file, you basically choose a stream or file transport and the protocol of your choice from the available protocols. It's as simple as (pseudocode)
var data = InitializeMyDataStructure();
var trans = new TFileTransport("myfile");
var prot = new TJSONProtocol(trans);
data.write(prot);
The support for plain C is somewhat limited yet, but there is all kinds of transports/protocols for C++.

Related

How would I read the NTFS master file table in C (*not* C++)?

I need a simple, lightweight way to read the NTFS MFT on a Windows server, using only C. My goal is to return a collection of directories and their permissions in a programmatic way for an application my company is building.
Every other answer I've researched on StackOverflow and other places have involved using C++ or other languages, and are typically very bloated. I'm pretty sure that what I want can be done in just a few lines of code, using the Windows API to call CreateFile (to get a handle to the root volume) and DeviceIoControl (to read the MFT). But I can't find a simple C solution for doing this.
Note that, although I've been a C#/.NET developer for many years (and also know other languages including Java and Python), I am fairly new to low-level C programming and Windows API calls. I also realize that there is a free too, Mft2Csv, that does exactly this. But the actual source code isn't available for me to reverse-engineer (GitHub has only the executable and supporting files).
I also realize I could just parse the directory tree using C# the .NET namespaces System.IO and System.Security.AccessControl. But this is way too slow for my purposes.

How to manage thrift source file in version control?

We are currently exploring using thrift in our project. One of my question is how to handle the thrift source files and specific language generated files in version control (git) ?
Let's say I have project server A implemented using java, and project B an mobile application using Objective-C, and project C which is the thrift files. What currently on top of my mind is having all project as different git project, and project C as submodule of project A and project B. The pro is we can have consistent thrift source, and we don't need to put generated source files into git project.
Then let's say I have another thrift file that differs with project C, and being used only with project A and project D. Should I also put those files in project C? How project B knows that some files in project C not for him in case we put those files in project C?
Another approach might be committing the generated source files into each project. Or maybe another approach that I don't know.
Thanks!
Irrespective of the musings about whether to split the projects in your highly specific case into Git submodules or not, these are the general guidelines that apply to all kinds of generated code, including Thrift, but not limited to it.
General rules
The source documents (IDL, DSL, ...) belong into the repository.
Any code that can be easily generated out of these sources does not.
Exceptions
As with nearly every rule, there are exceptions. The exceptions come in various flavours, such as
the generated artifacts are not easily producable (for whatever reason)
the generated code needs hotfixes to work around bugs 1)
etc.
Additional notes
Strive to have one source, and one source only (of course not counting branches etc.) for these files. A good idea could be to set it up as a library to be used across projects. Treat that library like you would do with any other 3rdparty module, including proper versioning and all. If you use Git, indeed Git submodules may be an approach to achieve that.
Then let's say I have another thrift file that differs with project C
If it differs, it is either a entirely different IDL, or a different version of the IDL in question.
1) In the real world, such things may happen. Unfortunately.

Wireshark tcap dissector inside my program

I'm working on SS7 project and reached a point where I need to create my tcap dissector/parser, So I was wondering to using wireshark dev files inside my source.
Is that possible? if yes? how can I do it? is there any tutorial available?
http://www.tcpdump.org/ has all needed information.
You'll need to use libpcap as described here: http://www.tcpdump.org/pcap3_man.html
I guess pcap_open_offline is a good start, you can then use the related functions to get the structured data contained in the dump file. Using the same library, you could also capturing directly from your application.
On a related note, wireshark and tshark allow to export a pcap file to xml, you could also use this format instead of the binary pcap if you'd like to.
Like most things to do with software it is possible. However a more valid question might be what use can you make of the Wireshark source code?
Some disadvantages of using Wireshark are:
it is a general purpose tool built for all protocols. All implemented protocol dissectors plug into its general framework. So if you want to reuse just a particular protocol you need some way of implementing or stubbing out the framework code.
it is designed only for dissecting and describing protocol components. It has no encoding functionality.
it is licensed using the copy left GPL license. This means that any software you build from Wireshark must also be licensed in this way.
Having said that it can be invaluable to just browse the source code to get a starting point. The main tcap dissecting source file is at epan/dissectors/packet-tcap.c. Wireshark uses a ASN.1 decompiler to parse the TCAP message. The definition file it uses can be found in asn1/tcap/tcap.asn.

Does a C language client library for Cassandra exist?

I wish I could use Cassandra for a project where only C language is used.
I tried to find a mature C client library for Cassandra, with no luck.
Shall I try a C++ one instead, like http://github.com/posulliv/libcassandra, or does one exist?
At least in Thrift trunk, there is C glib support, which means it's possible to make a C client for Cassandra. It's probably not well tested yet.
posulliv/libcassandra was never completed and only supports Cassandra 0.6. It lacks several features like super column support, for example.
I also started a C++ client that supports Cassandra 0.7, but it's still half-baked at the moment. Most of the Cassandra API is supported. At the very least, it can be a good example of working with the Thrift API. Of course, if you're capable of contributing to the project or starting a C client, I think many people would appreciate that.
I've created a very basic proof-of-concept C client for Cassandra and put it up on github. It relies on the barely-documented C language support in Thrift (as mentioned in previous answers). This client is not mature by any means, but demonstrates how to use the C files generated by Thrift, and offers some basic functionality; it might be a useful stepping stone towards a more serious C client.
I managed generating C code from the Cassandra trunk Thrift file.
I obtained the following files :
cassandra.c
cassandra.h
cassandra_types.c
cassandra_types.h
No main() function there, I guess these should be linked and used as a library, but I found no API documentation or whatsoever.
The samples in thrift/lib/c_glib/test do not show any client test source...
Will look into this more deeply !
I came across this and hopefully this helps -
http://datastax.github.io/cpp-driver/
There is already a C++ cassandra API called libQtCassandra
http://snapwebsites.org/project/libqtcassandra#Download

Where can I find an example unzipper using zlib?

I'm looking for a bare bones simple example C app for unpacking a zip file using zlib. It must support fairly new version of .zip and must have source right down to the zlib calls.
The zlib-bin source package on my system (linux) has some example programs called "minizip" and "miniunzip" which shows just that.
The zpipe.c example on the zlib.net web site is pretty straight forward. There is also a pretty good description of what it does.

Resources