Category Archives: Code-Snippets

PHP like str_replace function in C

This post is part of a series of posts where I want to document a bunch of C functions I use for my projects that I have written, collected and modified over the past few years. One of the functions that I use regularly is a string replacement function like the str_replace in PHP. This function replaces all occurences of a particular substring with a replacement pattern. The code for this was borrowed from here. In the original code, a newly allocated string is returned after all the substrings are replaced. I have modified it so that the replacements are made in place. Continue reading

Modify argv to hide sensitive information like passwords etc

A lot of programs require you to pass sensitive information like passwords etc as arguments to the program. However if you pass passwords as arguments, it will be visible through the /proc filesystem or the ps ax output while the program is in execution. To avoid the possibility of anybody prying on sensitive information, programmers should modify the memory location where the input parameters are stored (argv array), so that it is not visible to any other users, who might have the access levels to see what processes you’re running. Jotted below is some sample code which modifies its input parameters to hide it from the proc file system. Continue reading

Simple, Fast and Light-Weight Banner Advertisement Rotator using Server Side Includes (SSI)

Here’s a code snippet which will let you rotate multiple banner advertisements for all the ad spaces you have on your HTML pages. I wrote this because I didn’t want to use an ad server and wanted a light weight yet elegant solution. What the below code does is, it creates a SSI variable ‘slot’ which can have possible values of 1-12. Each value corresponds to a 5 second slot. Source this file at the start of your HTML file and then use the slot variable anywhere in your HTML code to rotate data according to time based slots. Continue reading

Retreive your Latest Twitter Update (Tweet) using Python

I used to display my latest tweet using PHP code as part of my WordPress theme’s function.php. However that did not work reliably and used to slow down page loading a wee bit. Hence I decided to generate the html code for the latest tweet as a background/cron process and just read the generated data in WordPress. Here’s how I do it. Continue reading

Detect Mobile Browser using Server Side Includes (SSI)

There’s a website dedicated for open source code in various programming languages to detect mobile browsers. However they didn’t have any code to detect a mobile browser using SSI. Thats what I needed to make the HTML pages on my website mobile friendly. Using the <!--#if expr SSI conditional expression, I wrote up a small code snippet to detect the most common smart phones and set a variable when a mobile phone is detected. I use this variable in other parts of HTML to control what is show and what is not to make it suitable for a mobile browser. Continue reading

Indirectly reference bash variable

If you know the name of a variable which contains a value of interest, you need indirect referencing to retrieve the value, this is how you do it.

#!/bin/bash

j=1 #variable containg a value
i=j #variable containing the name of the variable which holds the value
eval k=\$$i #k now contains the value of j which is 1
echo $k
j=2
k=${!i}  #another way of indirectly referencing
echo $k

Bash based Busy Handler for Sqlite

Sqlite is a serverless database and hence doesn’t allow multiple synchronous write requests. Sqlite API for most programming languages have a busy handler functionality which lets you define a busy handler function or a timeout value, thus enabling your program to wait for a certain amount of time and then access the database again.
Continue reading

XSL template for Generating Per Page TOC for Docbook HTML Output

Sometime back I worked on this upcoming Ruby On Rails Book and helped the author convert his docbook to various formats like HTML, PDF, EPUB, Kindle etc. While working with the HTML output, I figured that the standard output spewed out by the Docbook XSL stylesheets is quite boring. So I improvised a lil, added per page TOC, pretty css, icons etc. I spent quite some time on the per page TOC feature. Here’s the XSL template that I wrote for the TOC. Continue reading