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

Lua For Loop Scope

Introduction One odd thing about Lua when compared to other programming languages is, everything is global by default. Take this for example: function func_a() q_var = 7 end func_a() print(q_var) Once func_a is called q_var is available and set to 7. Even outside of func_a. You can mark a variable or function as local so it’s not global and pretty much anything written in Lua that’s mildly complex will use local a lot....

May 20, 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

Lua-stater a Lua State Machine

At work I use state machines extensively in one of our applications. I’ve found state machines to be very powerful when working with messaging. They are especially useful when combined with event based processing, where an event comes in, the current state can process and move to the next state based on the data received. People have made a few different Lua state machines but, they don’t work quite like I’d like....

March 4, 2019 · John

Lua Ternary

Lua doesn’t have true ternary operator but there is a way to mimic the behavior. This is one of the biggest issues people who aren’t used to Lua have when reading Lua code. For the most part Lua is simple and straight forward to understand. Assuming a blocky and easy to understand style is used. However, the pseudo ternary is often used and trips up people. So let’s look at it in some detail....

February 20, 2019 · John

Lua Case Insenstive Table

Introduction This is something I wrote for PenLight but alas it wasn’t merged. The problem they had is the use if __pairs which isn’t present in Lua 5.1. The project wants to maintain compatibility with 5.1 and LuaJIT which targets 5.1. All the Lua projects I deal with are Lua 5.3 and there isn’t a clean way to make a case insensitive table without using __pairs. So I’m posting this here because I find it useful....

February 12, 2019 · John