The Lazy Programmer

March 17, 2010

.NET DeflateStream/zlib compatibility

Filed under: /NET,Programming — ferruccio @ 3:37 pm
Tags:

This is just a quick post to describe how to format compressed data with the .NET DeflateStream class so that it can be read back in a C or C++ program using zlib. Compressing data with DeflateStream is extremely simple. The original code I was using looked like this: (more…)

Advertisements

Building Boost 1.42 with zlib 1.2.4 support

Filed under: Programming — ferruccio @ 7:09 am
Tags:

I am currently using Boost.IOStreams to read some compressed data out of a database. When I tried to user a zlib_decompressor object to deflate the incoming stream, it failed to compile. It turns out that you have to add zlib to the boost build process,  so I grabbed a copy of the latest zlib sources and made the necessary change to my boost build script (added -sZLIB_SOURCE=… so that boost.build could find the zlib sources).

Unfortunately the build failed. Bjam complained that it was missing a dependency (gzio.c). After a bit of trial and error with various boost.build options, I found the problem. I checked the zlib change log and it turns out that in version 1.2.3.9 they removed the gzio.c file from the zlib sources.  Unfortunately, this was a very recent change so boost.build has not yet been updated to reflect the current zlib. (more…)

March 2, 2010

Berkeley DB Viewer

Filed under: Database,Programming — ferruccio @ 11:27 am
Tags: , ,

I’m currently working on a project which uses Berkeley DB (BDB) as it’s data storage engine. I can’t say enough good things about BDB. It has proven to be a very fast and flexible way to store and retrieve data, it is very easy to use and the documentation is absolutely top notch.

One issue I ran into, though, is that there is no good way to examine the databases for debugging purposes. Initially, I used the provided db_dump command-line tool, which was fine for small databases. Db_dump dumps the entire contents of a database, which was OK when I was dealing with databases with only a few records. But now I am working with databases with thousands and soon millions of records. Db_dump just won’t do. (more…)

September 19, 2009

OpenDiff/SVN command line shortcut

Filed under: OSX,Programming,SVN — ferruccio @ 8:18 am
Tags: ,

For cross-platform projects, I switched from using XCode to TextMate and CMake. I found this to be a more productive environment for me but I miss the OpenDiff integration that’s built into XCode. Most of the time svn diff is all I need, but for more complex changes the visualization provided by OpenDiff makes life so much easier. (more…)

June 15, 2009

Dynamic C++

Filed under: C#,Dynamic-Typing,Programming — ferruccio @ 10:01 pm
Tags:

A while back, I started building a PDF parser in C++. I had been using the Adobe PDF IFilter to extract text from PDF files in order to index the content, but I wanted to be able to be able to also extract formatting information so I dug into the PDF format. The PDF format itself is fairly easy to parse, but the contents can be quite complex.

The PDF format consists of a series of objects, expressed in a simple syntax based on PostScript. There are primitives such as strings and numbers, and there are collections (arrays and dictionaries) which can contain both primitives and containers. You can see how things quickly become complicated when you have dictionaries containing arrays containing other complex objects.

(more…)

April 5, 2009

A Python snippet for reading binary data

Filed under: Programming,Python — ferruccio @ 7:31 pm
Tags: ,

I’ve been experimenting using Python to read data from binary files and started to notice the following pattern in my code.

  1. Read a block of binary data.
  2. Use struct.unpack() to break out individual fields.
  3. Create a dictionary from those fields using the appropriate key names.

(more…)

January 5, 2009

Returning multiple values from a function in C++

Filed under: C#,Programming — ferruccio @ 10:22 pm
Tags: , ,

Ideally all functions should return just one value. There are many times, however, when returning more than one value makes a function so much more convenient. The classic example of this convenience is file input. When we read data from a file we want to know two things: Did we reach the end of the file? and if not, what is the next piece of data from the file.

Sometimes, we can encode multiple return values into one. For many of us, the first C idiom we learned from K&R is processing input a character at a time:

int ch;
while ((ch = getchar()) != EOF) {
    // do character processing...
}

This works because the EOF macro was set to something outside the range of valid characters (usually -1). While this approach can work fairly well for simple cases, it quickly breaks down as the types we wish to return get more complex.

(more…)

November 18, 2008

Functional C++

Filed under: C#,Programming — ferruccio @ 9:56 pm
Tags: , ,

It’s been awhile since my last post. It was near the end of a product shipping cycle and, well, you know how that goes. Then I got a bit addicted to stackoverflow and spent way too much time each night reading and answering  questions. Eventually, I started a couple of side-projects which may eventually yield something interesting to write about.

Anyway, a little over a month ago, I answered a question on Stack Overflow titled “What is the one programming skill you always wanted to master but haven’t had time?” I didn’t have to think much to come up with an answer to that: Functional Programming.

I understand some of the fundamental concepts behind functional programming and occasionally I dabble a bit with LISP or I read a bit more of SICP but the practical applications of FP have been elusive to me.

(more…)

August 10, 2008

Adding a lock() statement to C++

Filed under: C#,Multi-threading,Programming — ferruccio @ 9:43 am
Tags: , ,

One of the things I like about C# that I miss in C++ is the lock() statement. It provides a simple way to control access to an object across multiple threads.

When I first started writing Windows apps, I used the Windows critical section API directly. After getting tired of EnterCriticalSection/LeaveCriticalSection, (I suspect like everybody else) I created a CriticalSection class that was a simple wrapper around the raw API.

Eventually, I got around to using mutexes from the Boost Thread class. You can create a scope guard whose destructor releases its referenced object, but it still lacks the simple elegance and clarity of being able to say:

lock (some-object) {
    do something to some-object
}

(more…)

August 3, 2008

Formatting Output with Boost

Filed under: C#,Programming — ferruccio @ 9:20 pm
Tags: , ,

Sometimes a GUI is overkill for a project. You just need a simple tool to do some task. Perhaps it needs to be scripted. So you whip up a console mode program and you eventually have to output something. At this point, many developers will simply ignore the C++ iostreams library and reach for good old printf(). I can certainly understand why. The iostreams objects are easy enough to use for simple formatting tasks. However, when you need to do something more sophisticated, you will often find yourself digging through reference material, muttering "this should be easy…"

(more…)

Next Page »

Create a free website or blog at WordPress.com.