Making MiniZip Easier to Use

Introduction Zip files are one of the most popular archive formats out there, and there are a lot of things you can do with them. While working with the ePub ebook format I spent a lot of time working with zip files. Thankfully, the standards committee for ePub used zip as the container format instead of designing their own. If you’ve ever done anything with Android you’ve dealt with zip files and probably don’t even realize it....

September 8, 2019 · John

Read Write File C Helpers

Introduction Reading and writing files in C isn’t as difficult as it sounds. A few simple loops are all you really need. That said, it’s nice to have a few helper functions ready to drop into a project. Before we write anything we need to think about the choice between fopen and open. The major differences are fopen is portable, part of the C standard, and unbuffered. While open is technically not portable it’s ubiquitous across *nix systems....

August 18, 2019 · John

Recursive Create Directory in C Revisited

Awhile back I wrote a function to recursively create directories in C. It used a string builder to split the parts and rebuild the path. The way mkdir works is by taking a single directory that does not exist and creates it. If there are multiple path parts that don’t exit it will error. Hence needing to split the string into parts and create each part of the path separately. Earlier parts of the path must exist before trying to add a later part....

July 10, 2019 · John

Unsigned Count Down

Introduction Something that comes up surprisingly often is traversing an array backwards. Maybe you’re emptying a queue. How about my personal favorite, reversing the order of elements. Counting in a for loop is so common you just don’t think about it. But counting backwards can lead to issues if you don’t do it right. The Wrong Way When you count down to and include 0 with an unsigned integer you’d think you could do something like this:...

June 2, 2019 · John

Thread Pool in C

Introduction When I was writing Poddown I needed a thread pool and I needed one that is cross platform. Since it’s a lightweight app I didn’t want to include a big third party threading library. So I wrote my own. Why a Thread Pool Creating threads can be quite expensive. Typically each thread is going to do essentially the same thing so it’s good to keep reusing them. Threads are actually quite heavy and creating or destroying threads takes time away from what you’re trying to accomplish....

April 12, 2019 · John

Cross Platform Thread Wrapper

Introduction There are many open source applications which use threading and are limited to either *nix or Windows because Windows handles threading a bit differently than *nix. I develop on macOS so pthreads is my go to but using it effectively locks me out of Windows because Windows doesn’t implement pthread. Instead it has it’s own thread API. There is a pthread implementation that works on Windows but it’s big, and heavy....

April 5, 2019 · John

Looping Through Bytes to Check for Bits

Checking for bits in 1 byte is easy. Checking in 2 bytes is also easy. Checking an odd number of bits in a variable number bytes isn’t so easy. The hard part is dealing with the boundary between bytes where we need to move from one to the next. Lets say we have 3 bytes. We need to count the number of bits set for the first 19 bits. First we need the block of bytes we want to look at....

March 22, 2019 · John

Mergesort in C

Introduction Quicksort is most people’s go to sort function and that’s not a bad thing because it’s a really good general purpose sorting algorithm. A good implementation is really fast and, being an in place algorithm, it uses very little memory. The big drawback of Quicksort is that it’s non-stable. This means there are some situations where using Quicksort a deal breaker and can’t be used. Enter Mergesort which is stable....

December 5, 2018 · John

General Comparison Functions in C

Introduction qsort, heapsort, mergesort, bsearch, and many more search and sort functions all take a compar argument to determine the sorting order of elements in an array. The compar function takes the form int (*compar)(const void *, const void *). The compar function’s parameters are the address of two of the elements in the array. The parameters will need to be dereferenced before they can be compared. Any type of value can have a compar function written for it and it can be used to sort an array of any type....

November 27, 2018 · John

Quicksort in C

Introduction Quicksort is one of the most common sorting algorithms and one of the most efficient. It’s so common that it’s part of C89. That said, it’s still good to know how it works, its strengths, and it’s weaknesses. It takes as a divide and conquer approach to sorting. An element is selected as a pivot point. The elements are sorted against the pivot so all elements less than the pivot are on the left side and greater than elements are on the right....

June 17, 2018 · John