Cateogry: programming

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 targes 5.1. All the Lua projects I deal... Continue reading

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... Continue reading

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 <code... Continue reading

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.... Continue reading

Byte Swapping in C



Introduction Some times you need to swap bytes. Sometimes you don’t. But right now we do. Well, we don’t but if we were implementing some sorting functions we’d need a good swap function. Sorting typically doesn’t move pointers around, instead it moves the bytes pointed to by the pointer.... Continue reading

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... Continue reading

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... Continue reading

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.... Continue reading

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... Continue reading

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... Continue reading

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... Continue reading

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... Continue reading

Recursive Create Directory in C



C has a very large gap when it comes to working with files and directories. It is C, so all the building blocks are there. Thankfully, it’s pretty easy to put together something useful. In a project I’ve been working on I needed to create a directory. This is pretty... Continue reading

Poddown a simple podcast downloader



I have been listing to podcasts for a long time and many years ago I wrote a podcast downloader. Back then I had a media center and I wanted my favorite podcasts to automatically download each night. At that time there wasn’t anything that really did that. Well, there... Continue reading

String Splitting in C



For a project I’ve been working on I needed to split a string into it’s component parts. There is strtok which I find useless for pretty much any task. It is not thread-safe, nor is it re-entrant, which makes it impossible to parse two strings (in a loop) at... Continue reading

Constant Time String Comparison in C



Comparing strings in C is typically handled with strncmp. This is fine in most cases but if you need to compare sensitive information, such as a message digest, it’s a really bad choice. strncmp is susceptible to timing attacks because it will stop comparing once the first... Continue reading

Efficient C String Builder



One task that always annoys me when I work with C is building strings. snprintf is all well and good if I know exactly the format I want. It falls down with anything that needs to be build iteratively and dynamically. Other languages that have built in strings will... Continue reading

Lua-perforator a Lua Profiling Module



When I wrote the fixed width integer part of my Lua number library there were some performance issues. Even though I kept it simple and I knew a it wasn’t going to be quick, I did find there were a few places that needed to be optimized. To this... Continue reading

Lua-hashings a Lua Hash Library



A while back I wrote lua-nums and said it was working on a project that needed its features. Well, this is that project! lua-hashings is a pure Lua cryptographic hash library. This all started with crc32 and a work project. I was working with a third party API... Continue reading

Lua-nums a Lua Number Library



One area that I’ve always found problematic in Lua is its number type. It’s a one size fits all solution and there are things I want to do that doesn’t really work with it. I’m mainly a C programmer and I’ve found unsigned integer wrapping to be useful in many... Continue reading

iOS Keyboard Dismiss



One of my biggest pet peeve when it comes to iOS is how the keyboard stays on screen by default. Basically, when you add a TextField the keyboard pops up and won’t go away. Pretty much every app out there has the keyboard dismiss if you tap outside of it.... Continue reading

iOS Money TextField



Most financial apps at some point need to have a user input a monetary amount. A very common way this is handled is a widget that accepts only numeric input. As the user types, it adds the given digit to the cents field. For example, the user types 1 and... Continue reading

iOS App Install Request in App



Recently I was faced with an issue where I was writing an app that could call another app using iOS’s URL scheme. An issue I ran into was how to handle the calling app not being installed. Basically, when openURL failed, I needed to ask the user if... Continue reading

iOS Activity Overlay



I was recently working on an iOS project and was implementing a login view. As usual, the user enters their credentials, the credentials are sent to a server for verification and, if successful, the user is allowed into the app. The problem I ran into is that the asynchronous account... Continue reading

Interop Objective-C Objects In C Using ARC



Introduction Using C functions from Objective-C is very easy but going the other way isn’t so easy. Especially with ARC which can destroy the object out from under you because C code is outside of ARC. With ARC Objective-C objects are no longer allowed in C structs for this... Continue reading

Preparing Music for Manual Copying Through iTunes



Like many people I prefer to manage my music collection myself instead of leaving it up to something like iTunes. Thankfully iTunes (which you must use if you have certain devices) allows manual copying of music to a device. The one real problem I have is most of my music... Continue reading

Binary Headers



Introduction Earlier in the year I wrote a Lua implementation of xxd -i (it’s at the bottom under the “Embedding” section). I wrote this because a few platforms I was working with didn’t support xxd. I didn’t want to attempt compiling and installing it. I had Lua installed... Continue reading

s2n Memory Hardening Analysis



Recently Amazon.com introduced s2n as a new TLS implementation. The idea is to have a small and simplified TLS library. Looking at it I noticed it’s very Linux centric. It cannot be compiled on Windows. There are patches to make it work on OS X. There is a report that... Continue reading

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... Continue reading

Effective Threading Using Qt



Effective Threading Using Qt Introduction Over the years using Qt I’ve seen a lot of difficulty using threads with Qt. Threads can be difficult and Qt provides a lot of ways to make threads easy to work with. Still basic / direct / low level threading (I’ll just call... Continue reading

Why I Love and Hate Linux Distros



The Good Parts I’m a big open source advocate and spend quite a bit of my spare time contributing to various open source projects. Everything form managing full projects such as Sigil, and KDocker. Also, heavily contributing to projects such as calibre. As well as doing... Continue reading

Client Side Session Cache in OpenSSL



Building on Server Side Session Cache in OpenSSL we need to deal with the Client side. The OpenSSL documentation for SSL_CTX_set_session_cache_mode has an option for client caching. However, it states that, “the application must select the session to be reused by using the SSL_set_session(3) function.” It also states... Continue reading

Server Side Session Cache in OpenSSL



At work (information posed with permission from my employer) we’ve been looking into session caching with OpenSSL. We started this by looking at the server and found that by default OpenSSL will enable and use a session cache when acting as the server. However, there are two major things we... Continue reading

Enable DH and ECDH in OpenSSL (Server)



Recently at work we were looking into Forward Secrecy (FS). We were using Qualys SSL Server Test and noticed that Forward Secrecy was showing as NO. We decided to look into this because we want to use the most robust security we can. What we found was none... Continue reading

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... Continue reading

My SixAxis Pairing Tool



I was looking for a way to use a PlayStation Dual Sock 3 SixAxis controller (that is a mouthful of a name) with my Android phone. I found Dancing Pixel Studios Sixaxis Controller app which looked like the only real solution. It requires root which isn’t a problem... Continue reading

Using Lua as a Templating Engine



Introduction There are a lot of templating engines to choose from. The vast majority of which are primarily geared toward the web. Meaning they’re specially designed for outputting HTML/XML documents. Lua provides more flexibility and can easily be used as a general templating engine. Also, Lua is very easy... Continue reading

Wrapping a Lua module in C



Introduction So far I’ve covered wrapping a C library in Lua, writing a Lua module, and calling Lua from C. The next step is using this knowledge to wrap a Lua module in C. Basically, the opposite of wrapping a C library in Lua for use... Continue reading

Calling Lua From C



Introduction One very useful feature of Lua is how easy it is to work with from C. Unlike other scripting languages Lua’s C support goes to and from. Meaning you not only can call C from Lua but you can call Lua from C. Often you’ll see applications written... Continue reading

Writing Lua Modules



Introduction With Lua 5.2 the recommended way to write modules changed. Instead of using module(“mymodule”, package.seeall) the recommendation now is to create a local table, put all of the module’s functions in it and return the table. The big difference is a local table does not register the module... Continue reading

Wrapping a C library in Lua



Introduction An often overlooked feature of Lua is the C API. The most common use I’ve seen is to allow Lua scripts to use existing C libraries instead of reimplementing existing functionality in pure Lua (which is not always feasible). Fortunately, Lua has very strong integration with C and... Continue reading

Calling Functions in EXE From Plugins in Windows



When writing an application that uses plugins sometimes it’s necessary to have the plugin call functions from the application. This is fairly easy to do on pretty much any OS but on Windows it requires the plugin to link to the application. Sometimes explicit linking is unreasonable. One situation where... Continue reading

Wrapping a C library in COM/ActiveX



Introduction Not very long ago my boss walked up to my desk, put a pile of books down beside me and said “Not it”. The books were all related to COM programming. The project he gave me was to create a COM wrapper for an existing C library we... Continue reading

Retrieve Formatting Set by QSyntaxHighlighter



I have been working on adding inline spell check to Sigil recently and ran into a quirk on Qt that isn’t immediately obvious. I ended up having to look though the Qt source code to understand exactly what was happening. When dealing with a QPlainTextEdit you can get the QTextCursor... Continue reading

Amazon APNX file format



Coming with the Kindle 3.1 firmware is the ability to have real page numbers. Getting ready for this Amazon has put out a preview release of the 3.1 firmware and has started adding the necessary information to Kindle books to show the page numbers. The page numbers themselves map to... Continue reading

Duplicity Backup Script



I’ve started using duplicity combined with Amazon’s S3 to backup this server. Duplicity is an amazing application that makes backups simple. Some of the features I like best about duplicity are: it encrypts the data, compresses it, splits it into manageable chunks, does incremental backups, and can backup... Continue reading

lebookread 0.2



I’ve made a new release of lebookread. This version supports the following formats: palmdoc, ztxt, epub, tcr, rb, mobi, and fb2. The library is usable but still needs a lot of work. Unit testing, examples, more code comments and more formats to name a few things. Continue reading

Qt Remove Directory and Its Contents



When dealing with directories, Qt has a large number of functions to make manipulating them easy. However, it does not include a way to delete a non-empty directory. This little omission is easily solved. Following is a recursive function that will delete a directory along with all of it’s contents.... Continue reading

C++ Derived Classes and Object Destruction



While working on lebookread I realized that the the destructor for my reader classes would never be called. lebook read has a base class (FormatReader) that exports all of the necessary functionality for use by applications using the library. All of the readers are a subclass of FormatReader. The library... Continue reading

lebookread



I have been taking a short break from blogging again. The pressure at work has only increased and is eating into a lot of my time. I haven’t been motivated to work on personal projects because well they are work. However, this has recently changed a bit. I’ve started a... Continue reading

Sending WM_DELETE_WINDOW client messages



In my X11 Intercept Window Close Event post I left out one very important piece of information. How to actually close the embedded window after intercepting the WM_DELETE_WINDOW message. The best thing to do is send our own WM_DELETE_WINDOW message to the embedded window. This will keep the embedded... Continue reading

X11 Intercept Window Close Event



Note Nov, 8 2009: a few additions have been made to the code samples to make them more complete. Specifically subscribing to X11 events. A feature request for KDocker was made a few days ago for docking when closed. Basically the person wants the window to iconify when the... Continue reading

Unidecoder



A while back I made a post about ASCIIizing Text. With it was a simple python application that would convert Unicode characters to ASCII equivalents. It doesn’t do a basic conversion but also Latinizes the characters when they are outside of the ASCII range. The uni2ascii package I... Continue reading

QPlainTextEdit With In Line Spell Check



Update: Simplified Highlighter.highlightBlock function One thing Qt lacks is an integrated spell check in the text entry components. For a project I’m working on this is necessary. Using python-enchant and the QSyntaxHighlighter I was able to implement this functionality. Here is how to add an in line spell check support... Continue reading

Better QPlainTextEdit With Line Numbers



My last post was an implementation of a Qt widget which displays text with line numbers. I found that it has a few limitations. The biggest was a performance penalty when dealing with large documents. I’ve since re-factored and rewritten the class to make the performance acceptable. I’ve also cleaned... Continue reading

QTextEdit With Line Numbers



Here is a Qt4 widget written in Python that allows for line numbers next to a QTextEdit. Similar to what is seen in a number of text editors such as gedit and kate. from PyQt4.Qt import QFrame, QWidget, <span... Continue reading

ASCIIize Text



One pet peeve of I have with my Cybook Gen 3 is its inability to properly display unicode characters in plain text files. I don’t need anything fancy like Japanese characters just simple things like “ and ” (as opposed to “ and “). To solve this problem I’ve been... Continue reading

History Drop Down With Model



Following is a bit of python code that illustrates how to create a QComboBox that attaches to a model for listing history items. The main features of this code are items entered in the text area of the combo are added to the history. Selected items and items entered that... Continue reading

QCompleter and Comma-Separated Tags



Here is a python script demonstrating how to use QCompleter to complete multiple tags in a QLineEdit. A few features of this script are: removing tags from the drop down that already appear in the QLineEdit, caching the tags, and inserting a , after completion to ease adding more tags.... Continue reading

Changing Single Quotation Marks to Double in eBooks



As a person living in the USA I highly prefer double quotation marks to single quotes when denoting speech. Some authors use the single quote for effect but mostly it’s just a style choice. I find UK authors generally use the two interchangeably. Tolkien books are a good example. I... Continue reading

More PDF Work



Other than little fixes here and there I’ve mainly focused this week on getting PDF output complete. It now supports profiles as well as custom page sizes. A little work is still needed on the processing rules for the html produced by the PDFInput. Otherwise, PDF Input/Output is complete. Though,... Continue reading

Calibre Work - April 4



This has been a busy week for Calibre. The new conversion pipeline is complete. Part of this change is there is a new framework for command line options. I’ve spent most of this past week moving the PDF input/output and TXT input/output over to use the new framework. The other... Continue reading

Calibre work



I’ve been working on a few new features for Calibre recently. They will appear once pluginize is turned into trunk. All of the recent features that I though were going to be part of the 0.5 release are currently in pluginize. I was under the impression that pluginize was going... Continue reading

Bookmark Manager in Calibre 0.5



Calibre 0.5 is gearing up and I’ve had some more work accepted into Kovid’s pluginize branch. This time it’s a basic bookmark manager for the ebook-viewer app. It allows editing, deleting, exporting, and importing bookmarks for the opened ebook. The GUI isn’t all that great but I’m not a great... Continue reading

Printing Support and PDF Conversion in Calibre 0.5



Support for printing will in fact be coming in version 0.5. I’ve committed (to my branch) a new printing framework that fixes the bugs in the old one. Also, I’ve committed some PDF changes. There is now an any2pdf app which will allow for conversion of any supported ebook format... Continue reading

Printing Support in Calibre



Calibre will be getting support to print books in the ebook viewer. The code has been checked into my branch already and is just waiting for the the upcoming 0.5 release. UPDATE Due to some unfortunate bugs the printing support won’t be coming for some time. Continue reading

Kindle Detection Bug in Calibre



The Kindle has been very tricky to get working in Calibre. However, it is finally working correctly (at least it should be). The latest issue with the Kindle driver was in the usbms core. The main memory location was never being set. On Windows the driver loops over all attached... Continue reading

Cybook t2b Format Specification



The Cybook Gen 3 uses it’s own image file for thumbnails. The t2b file is generated by the device based upon the image found in the ebook. If there is no image a default one with the file name written across it is created. One of the feature request for... Continue reading

Calibre and Cybook



The usbms driver is mostly done. Everything is implemented and it works. Testing is all that’s needed now. The Cybook is working and Kindle support should be committed soon. Continue reading

Calibre and New Driver Code



This Christmas Santa was very nice to me. He gave me a Cybook Gen 3 ebook reader. I’ve really been needing a new one since Tati has been monopolizing (this isn’t a bad thing) the Sony Prs505 we already had. Sadly the ebook management application I often use (Calibre) didn’t... Continue reading

Multiple Input with Single Input Apps



The few eBook formating tools I’ve posted all share one major flaw. They can only handle a single file as input. This is a problem when you want to run it on all or a number of different eBooks. If you are able to use Bash there is a simple... Continue reading

Building the eBook Tools



It’s come to my attention that while I’ve posted a few eBook formating tools I wrote and use I never posted how to build them. Since I’m using Qt the easiest way to build them is to use qmake and make. The build process is simple. Create a pro file... Continue reading

eBook Adding Empty Lines At End of File



Continuing my work to clean up my eBooks I’ve written another little tool to help. I like for my eBooks to have two blank lines at the end of the file. The only major caveat of this one is it assumes Unix end of lines. Meaning a single n character.... Continue reading

eBook Paragraph Formating



Today I wrote two simple programs to help me clean up my ebooks. I prefer to keep my ebook collection as plain text files with paragraphs separated by a blank line. The first program reflows the paragraphs to put each on a single line. The second removes extraneous whitespace from... Continue reading

json and blogger



Today I decided to learn about json. To help me with this I coded a little python script I call blogger-updates.py. It takes the name of a blogger blog and optionally a number designating the number of entries to reterieve. I used Google’s blogger api to get the data.... Continue reading

Projects



Added a projects page. Now I need to rethink the theme, the page navigation isn’t very intuitive… Continue reading