Lua Template Engine Yet Again

Introduction After writing a template engine in Lua, I found some deficiencies and wrote a few more iterations. After having used the template engine for quite a while, I’ve made a few enhancements and some fixes. This is hopefully the final version of the template engine. Newest Engine This engine is an iteration of the previous template_engine3.lua. The differences are: Bug fixes for escaping routines. An empty string will no longer cause a failure....

August 28, 2022 · John

Binary Search and Insert

Introduction When dealing with arrays it’s often necessary to keep their elements in sorted order. The easiest way is to use an insert algorithm that always puts element into the array in sorted order. That said, you don’t know where elements will end up in the array because if you already knew the order you wouldn’t need to sort in the first place. At some point you will need to pull elements back out and you will want a quick way to find a given element....

January 18, 2018 · John

Base64 Encode and Decode in C

Introduction A very popular way to encode binary data is Base64. The basis of this is an encoding table. As you might expect, there are 64 total characters that go into the tale. There are multiple implementations of base64 with slight differences. They are all the same except for the last two characters and line ending requirements. The first 62 characters are always “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789”. PEM and MIME encoding are the most common and use “+/” as the last two characters....

November 18, 2017 · John

Hex Encode and Decode in C

Introduction A very common task when working with binary data in C is, converting it to and from Hex. It’s unfortunate that C doesn’t provide a standard function that can take care of this for us. However, it’s pretty easy to implement a set of functions to handle it. Hex encoding is always twice the size of binary. Since hex is base 16 we can take any unsigned char value 0-255 and represent it in two hex digits, 0x00-0xFF....

September 24, 2017 · John

Wrapping C++ objects in C

Introduction Using C functions from C++ is very easy but going the other way isn’t. However, it can be done with a little ingenuity. Really, this isn’t as crazy as it sounds. I’ll use a simple adder for the object. It takes an integer as a starting value and has two functions for adding and getting the current value. This could be expanded later into a complex object for mathematical operations but that’s really not necessary right now....

August 18, 2017 · John

JNI is (Not) Your Friend

Introduction So far we’ve scratched the surface of using JNI when we looked at wrapping a C library and Calling Java from C. Now we’re going to look into some more complex uses. Java Class Since we’re working with JNI, we’ll need a Java class to use what we’re going to expose from C. DemoFuncs.java class DemoFuncs { static { System.loadLibrary("demo_lib"); } public enum Days { MON, TUE, WED, THU, FRI, SAT, SUN, } public static native int[] demo_array_return(); public static native int[] demo_array_copy_inc(int[] in); public static native Days demo_enum_val(); public static native Days demo_enum_field_val(); public static native void demo_exception_1(); public static native void demo_exception_2(); public static native void demo_exception_3(); public static native boolean demo_exception_4(); public static native Name demo_name(String name); } Note: This class includes an enum because one of the later examples uses them....

July 9, 2017 · John

Calling Java From C

Introduction Obviously, JNI lets you call Java functions and use Java classes in C. Typical a Java app is the one calling into C. Now Let’s say you don’t have a Java app that kicks off the process but you want your C app to still use some Java code. Okay, I know what you’re thinking, “why”? Well, there are companies out there that provide SDKs for their products only in Java....

June 17, 2017 · John

Wrapping a C library in Java

Introduction It can’t be argued that Java is popular and successful. It is consistently the number one language on TIOBE’s popularity list, above C which comes in as number two. This ranking is based on popularity and doesn’t mean Java is more used than C but that doesn’t change the fact that there is a lot of Java code out there. Unlike other languages interop with Java is not what I’d call easy....

June 6, 2017 · John

Lua Template Engine Revisited

Introduction A while ago I wrote a template engine in Lua. This engine was fairly basic and I wrote it for work as a proof of concept. It was meant as a way to see this was a viable solution. After moving forward with the project I found that the engine was usable but it needed some enhancements. One big difference with the new engine(s) vs the old one is new line handling....

May 7, 2015 · John

Extending an Application with Lua Plugins

Introduction A very common use of the Lua language (which is a very versatile) is using Lua to extend an application via plugins. Many popular games use Lua for this very purpose. Adding a plugin framework to an existing application is trivial with Lua. Also, Lua provides a very capable and easy to use language for writing plugins. To demonstrate using Lua for plugins I’m going to make very basic text editor and allow features to be added via Lua scripts (user installable plugins)....

August 16, 2014 · John