## Using environment variables in C++

Posted: 26th February 2014 by Tim in C++
Tags: , , , , , ,

Sometimes you need to use environment variables from within your program. There are a few ways to get the environment into your program, but the most portable way is to use the getenv function. The function will return a pointer to the null-terminated string value, or NULL if the variable is not set.

For example, the following program will print the value of $HOME if it is set. It will return 0 if the variable is set, or 1 if it’s not. #include <cstdlib> #include <iostream> int main() { char *homePath(getenv("HOME")); if (homePath == NULL) { std::cout << "$HOME is not set!" << std::endl;     }     else     {         std::cout << "$HOME is set to '" << homePath << "'" << std::endl; }  return homePath == NULL; } ## Replacing newline characters in linux Posted: 12th February 2014 by Tim in Awk, Linux Tags: , , , , , , , There are many linux tools available to do search and replace, with sed being one of the most commonly used. However, tools like sed work line-by-line. If you need to replace/remove newline characters then things get complicated. It can be done with sed, but it’s not pretty. The nicest solution I’ve seen is using awk. awk uses a Record Separator (RS) setting to determine how to split each record, and an Output Record Separator (ORS) setting to determine how to split the records as they are output. By default, RS and ORS are both set to '\n' (newline), meaning it reads in text line-by-line and outputs them in the same form. By changing ORS to something else, we can get all of the data printed on one line. The examples below will use a file named random_data.txt which contains the following data: 18838ef123e f33a244eb1e 4492b3091o9 9o7ef44b22e 77a1194g229 To replace the newline characters with a space, we can use the following: awk '{ print$0; }' RS='\n' ORS=' ' < random_data.txt 18838ef123e f33a244eb1e 4492b3091o9 9o7ef44b22e 77a1194g229

ORS does not have to be one character:

awk '{ print $0; }' RS='\n' ORS=' :: ' < random_data.txt 18838ef123e :: f33a244eb1e :: 4492b3091o9 :: 9o7ef44b22e :: 77a1194g229 :: The above commands are overly verbose, making it more obvious as to what's going on. However, both the RS value and the print$0 are default settings. RS can be omitted completely, and the print code can be replaced with the number 1. This 1 is a true condition, indicating to awk to use the default behaviour.

So to repeat the example of replacing newlines with a space, we can shorten the command to:
awk 1 ORS=' ' < random_data.txt 18838ef123e f33a244eb1e 4492b3091o9 9o7ef44b22e 77a1194g229

The shorter command is a bit more abstract but does the same job while cutting the command line length in half.

## Displaying code in LaTeX documents

Posted: 27th January 2014 by Tim in C++, Java, LaTeX
Tags: , , , , , , ,

There are a few ways to do this, but one of the simplest ways to pretty-print code in LaTeX documents is to use the listings package. The package can be configured to use specific colors for different parts of the code, with many programming languages supported.

The following document will display code for both C++ and Java, with settings provided for the most common configuration:

\documentclass{article} \usepackage{listings} \usepackage{xcolor} % for setting colors

% set the default code style \lstset{     frame=tb, % draw a frame at the top and bottom of the code block     tabsize=4, % tab space width     showstringspaces=false, % don't mark spaces in strings     numbers=left, % display line numbers on the left     commentstyle=\color{green}, % comment color     keywordstyle=\color{blue}, % keyword color     stringstyle=\color{red} % string color }

\begin{document}

\begin{lstlisting}[language=C++, caption={C++ code using listings}] #include <iostream> int main() {     // print hello to the console     std::cout << "Hello, world!" << std::endl;     return 0; } \end{lstlisting}

\begin{lstlisting}[language=Java, caption={Java code using listings}] public class Hello {     public static void main(String[] args)     {         // print hello to the console         System.out.println("Hello, world!");     } } \end{lstlisting}

\end{document}

This will produce the following document:

The package is much more flexible than the example above shows; see the full documentation for more details.

## C/C++ gotcha – ternary operator casting

Posted: 13th January 2014 by Tim in C, C++
Tags: , , , , , ,

C and C++ have what is known as a ternary operator; syntax which allows you to do conditional operations inline. This is done using syntax similar to:

const int b = (<condition> ? 10 : 100);

This will set b to 10 if <condition> is true, or 100 otherwise. Ternary operators allow code to be written more concisely, and allows you to do things like populating const variables conditionally as shown above, which cannot be done in a normal  if (<condition>) {...} else {...} block. However, there is (at least) one thing to watch out for: casting.

Ternary operators return a value, and the data type must be the same for both results (ie: you cannot return an integer in one case and a string in another). If you attempt to return two compatible types of different sizes, such as a float and a double, the smaller value will be upcast (in this case, the float will be cast to a double). Why would that be a problem, you ask? Consider the following program:

#include <iostream>

int main() {     std::cout << (true ? 'a' : 100) << std::endl;     return 0; }

Running the program will print:

97

So what’s going on here? This simple program should print the letter ‘a’ if true == true (ie: always). However, the other return value is 100 which is an int, not a char. The ternary operator can only return one type, so the char ‘a’ (ASCII character 97) is upcast to an int, and the number 97 is printed instead.

## Command line arguments with Perl

Posted: 27th December 2013 by Tim in Perl
Tags: , , , , ,

Command line arguments in perl are stored in the $ARGV array, and the number of arguments can be deduced from the size of that array: $#ARGV + 1. One way to access this data is to access the data by index: $ARGV[0] for the first argument for example. Unlike C, the program name is not passed as the first argument. For example, the following Perl program will print all arguments passed to it: print "There are " . ($#ARGV + 1) . " arguments:\n"; for (my $i = 0;$i <= $#ARGV; ++$i) {     print "  \$ARGV[" .$i . "] = " . $ARGV[$i] . "\n"; }

$perl <script_name> first second third There are 3 arguments:$ARGV[0] = first   $ARGV[1] = second$ARGV[2] = third

## Lattice multiplication method

Posted: 12th December 2013 by Tim in Mathematics
Tags: , , , , , , ,

The lattice multiplication method is a way of multiplying two numbers in a simple, concise form. It works a lot like the traditional method taught in schools but can be easier and faster for multiplying large numbers.

Let’s go through it step by step, using the example of 64 x 17:

1) Draw a box, with one number written horizontally along the top and the other number written vertically down the right-hand side.

2) Split the box into smaller boxes so that each number on the top edge has it’s own column and each number on the side has it’s own row.

3) Split each box diagonally from top-right to bottom-left.

4) Multiply each row by each column and write the answer in the associated box. Tens values go to the left of the diagonal line, unit values go to the right.

and so on until you have populated the entire box.

5) Sum up each diagonal from right to left. Carry tens over to the diagonal on the left.

And you’re done! The answer is written along the edge of the box, from top-left down and across to bottom right.

Using this concise method, we’ve calculated that 64 x 17 = 1088. This can be used for numbers of any size; simply use a smaller or larger box as necessary.

## Namespace Alias in C++

Posted: 26th November 2013 by Tim in C++
Tags: , , , , , ,

Sometimes you may find yourself working with namespaces which are really long to type. Writing out the whole namespace can be tedious and make the code harder to read, and using use namespace can sometimes make the code ambiguous (and is discouraged by some coding guidelines). The solution to this is to create a namespace alias, which allows you to reference the full namespace using a shorter name. You can create a namespace alias with the following syntax:

namespace <alias> = <full::namespace::reference>

<alias> can now be used where you would usually use <full::namespace::reference>. For example:

#include <iostream>

namespace My { namespace Really { namespace Long { namespace Name { namespace Space {

void sayHello() {     std::cout << "Hello!" << std::endl; }

} // namespace Space } // namespace Name } // namespace Long } // namespace Really } // namespace My

namespace MyNS = My::Really::Long::Name::Space;

int main() {     MyNS::sayHello();     return 0; }

Will print:
Hello!

## Using FIFOs in Python

Posted: 11th November 2013 by Tim in Python
Tags: , , , , , , , , ,

FIFOs are a very simple tool for communicating between processes. And using them in python is very easy. Simply call os.mkfifo(<path>) and treat the FIFO like any other file.

For example, we can create two simple python scripts, one for sending and one for receiving.

###### sender.py

import os

path = "/tmp/my_program.fifo" os.mkfifo(path)

fifo = open(path, "w") fifo.write("Message from the sender!\n") fifo.close()

import os import sys

path = "/tmp/my_program.fifo" fifo = open(path, "r") for line in fifo:     print "Received: " + line, fifo.close()

By running the programs in separate terminal windows, the receiver will print the following:
Received: Message from the sender!

## Conditional INSERT in SQL

Posted: 23rd October 2013 by Tim in SQL
Tags: , , , , , ,

Sometimes you want to run an INSERT statement in SQL only if some condition is met. There are a few methods available to do this, but not all of them are supported by all database systems. One method which is supported on all systems the use of a SELECT statement to return the row values, with the condition set in that SELECT statement:

INSERT INTO <table> (<col1>, <col2>) SELECT <val1>, <val2> WHERE <condition>

One practical example of this is inserting a row only if it does not already exist. This can be done like so:

Table definition:
CREATE TABLE person (person_id int PRIMARY KEY, name varchar(20));

Row Insert:
INSERT INTO person (person_id, name) SELECT 1, 'Me' WHERE NOT EXISTS (SELECT 1 FROM person WHERE person_id = 1);

Running the row insert query for the first time will result in the row being inserted. If run a second time, no row is inserted because a row with person_id = 1 already exists.

## C++ – Count the number of occurrences of a character in a string

Posted: 13th October 2013 by Tim in C++
Tags: , , , , , , ,

Counting the number of occurrences of a given character in a std::string can be done using one function from the STL library: the std::count(...) function. This function takes three parameters: two iterators (the beginning and end of the desired search), and the item you wish to count. This function can be used for any STL container which uses iterators, such as vector and set.

For example, this full program:

#include <algorithm> #include <iostream> #include <string>

int main() {     std::string myString("The bubble sort algorithm is a good algorithm "                          "for students to learn, but is usually too slow "                          "for real world applications");     std::cout << "The string '" << myString << "' has "               << std::count(myString.begin(), myString.end(), 'a')               << " occurrences of the letter 'a'" << std::endl;

    return 0; }

Will print the following:

The string 'The bubble sort algorithm is a good algorithm for students to learn, but is usually too slow for real world applications' has 8 occurrences of the letter 'a'

## Running Perl regex from the command line

Posted: 26th September 2013 by Tim in Linux, Perl
Tags: , , , , , ,

Perl regular expressions are slightly different from grep (or egrep) regular expressions. grep is sufficient most of the time, but sometimes you may need the extra flexibility of Perl regular expressions, or may just want to test out a regular expression that you will use later in Perl code. This can be done on the command line like so:

perl -ne 'print if /my.regular.expression/' < file

For example, let’s take a file full of random data, named random_data.txt which contains the following:

18838ef123e f33a244eb1e 4492b3091o9 9o7ef44b22e 77a1194g229

We can write a regular expression to print out the lines which are valid hexadecimal values. The following Perl regular expression can be used for this:

^[\da-fA-F]+$ Using the syntax above, we can print the lines in the file which are valid hexadecimal numbers like so: perl -ne 'print if /^[\da-fA-F]+$/' < random_data.txt

Which will print:

18838ef123e f33a244eb1e

Similarly, text can be cat to the perl command like so:

cat random_data.txt | perl -ne 'print if /^[\da-fA-F]+$/' ## Do until script succeeds in bash Posted: 12th September 2013 by Tim in Bash, Linux Tags: , , , , , , Occasionally you want to continue retrying a script or program until it succeeds. In bash, this can be done using the until ... do ... done syntax, like so: until (<command>); do echo "FAIL!"; sleep 10; done For example, if you wanted to halt processing until a program.log file was created, you could do something like this: until (ls program.log >/dev/null 2>&1); do echo 'Log not ready yet. Will try again in 5 seconds.'; sleep 5; done; echo 'Ready!'; Or, on multiple lines: until (ls program.log >/dev/null 2>&1); do echo 'Log not ready yet. Will try again in 5 seconds.'; sleep 5; done; echo 'Ready!'; (There are much better ways to check if a file exists in bash. I’ve used this method here to show how to use until in this way) This script will print something like: Log not ready yet. Will try again in 5 seconds. Log not ready yet. Will try again in 5 seconds. Ready! ## Math product symbol in LaTeX Posted: 28th August 2013 by Tim in LaTeX Tags: , , , , , In a similar manner to the summation symbol, the product symbol can easily be added to a LaTeX document using the \prod notation. The sybmol can be compressed to fit on one line (useful for small equations displayed within a text block), or enlarged to make it more readable. For example, to print the formula for the product of the square of numbers 1 to 10, we could use the following notations: \prod_{n=1}^{10} n^{2} (compressed format) \displaystyle\prod_{n=1}^{10} n^{2} (full format) Remember that, like all math syntax, the code must be in math tags. ie:$\sum…$, $\sum...$, etc. ## Custom signal handlers in C++ Posted: 16th August 2013 by Tim in C++ Tags: , , , , , , , , , , , Using the signal(...) routine (defined in the csignal or signal.h headers), you can define how various signals are handled by your program. The routine takes two arguments; the first is the signal you wish to handle, and the second is the handling routine itself. The handling routine can be specified as SIG_DFL (default handling), SIG_IGN (ignore signal) or any (non-member) function which takes one int argument and returns void. signal(...) will return the handling routine specified before the call. That sounds a bit confusing, so let’s consider the following program: #include <csignal> #include <iostream> #include <unistd.h> // for sleep bool keep_running = true; void sigintHandler(int signum) { keep_running = false; } int main() { // force all SIGINT signals to be handled by sigintHandler signal(SIGINT, &sigintHandler);  std::cout << "Starting program (press ctrl-C to stop)" << std::endl;  while (keep_running) { std::cout << "." << std::flush; sleep(1); }  std::cout << std::endl; std::cout << "Exiting program cleanly. Goodbye!" << std::endl;  return 0; } The program above doesnt’ do much; it just prints a . character every second. The print loop uses the keep_running global boolean as an exit condition, continuing while the variable is true. Above the loop, we have set SIGINT, the interrupt signal sent when you ctrl-C out of a program, to run the sigintHandler routine. This routine stops the loop, allowing the program to exit cleanly. Running the program will result in something like this: Starting program (press ctrl-C to stop) ......^C Exiting program cleanly. Goodbye! ## Editing a file in hex mode in Vim Posted: 27th July 2013 by Tim in Linux, Vim Tags: , , , , , , Native Vim doesn’t have a hex editing mode built in, however it is possible to edit a file in hex by converting the file back and forth using xxd. To do this, open your file in Vim and run :%!xxd. From here you can change the hex values and, when you’re done, run :%!xxd -r to convert back. For example, in a terminal, create a file with the word “HeX” in it like so: echo "HeX" > my_file Next, open it in vim and run :%!xxd. Vim should display this:   0000000: 4865 580a HeX.   The first column, 00000000: is the character number for the first character in this line, zero indexed. In other words, the preceding 4 is the first character in the file (index 0). Don’t change this. Similarly, the Hex. at the end is the string representation of the line data. Don’t touch this either. The file contents are the middle bit: 4865 580a. Files with more data may contain multiple lines with 16 hex characters on each line. Run xxd on a larger file to see what I mean. Let’s change the ‘X’ into ‘x’. ‘X’ in hex is 0×58 and is represented by the fifth and sixth hex numbers in the file. ‘x’ in hex is 0×78. So, using normal Vim commands, replace 58 with 78 so that your file looks like so:   0000000: 4865 780a HeX.   Finally, convert the file back to text using :%!xxd -r. You will see that the file now contains the string “Hex”. ## Internal Write (sprintf) in Fortran 90 Posted: 12th July 2013 by Tim in Fortran Tags: , , , , , If you need to generate a string which is made up of different variables (integers, floats, other strings, etc), you could use the sprintf routine in C. To achieve the same results in Fortran 90, you can use an Internal Write. Internal writes are very simple in Fortran. You constuct the write statement as if you were printing to the terminal, but instead of specifying the logical unit number (or ‘*’) as the first argument in the parentheses, provide the variable name you wish to store the result to instead. For example: ! a simple program which demonstrates internal writes program sprintf  character*100 logfile ! path to the file used for logging integer*1 instnum ! program instance number  1 write (*,*) 'Please enter a number between 1 and 9:' read (*,*) instnum  if (instnum.lt.1.or.instnum.gt.9) then write (*,*) 'Number must be between 1 and 9!' goto 1 endif ! generate the log file path using the instnum value provided write(logfile,'(a,i1,a)') '/tmp/myprog',instnum,'.log'  write (*,*) 'Using logfile: ',logfile ! program logic goes here  end This program will provide the following output:  Please enter a number between 1 and 9: 1 Using logfile: /tmp/myprog1.log As you can see, the input number is used to generate a log file path, which is stored in the variable logfile. This full path is then printed to the terminal, and may be used later on in the program as required. ## Template Metaprogramming in C++ Posted: 27th June 2013 by Tim in C++ Tags: , , , , , , , , , One frequently used feature of C++ is templates. Templates can be applied to classes and functions to make your work more generic without sacraficing runtime performance. It’s a fantastic feature that brought with it one unexpected technology: template metaprogramming. Template metaprogramming essentially means doing some of your computation at compile time by taking advantage of the way templates are used in C++. This could be done to improve your program’s runtime performance (in exchange for slower compile times) or simply to make the programmer’s work easier. Take this code as an example: // Generic exponent compile-time calculations. Pow<2,3>::result == 2^3 template <unsigned long B, unsigned long E> struct Pow { static const unsigned long result = B * Pow<B, E-1>::result; }; template <unsigned long B> struct Pow<B, 0> { static const unsigned long result = 1; }; This code will allow you to calculate exponents at compile time, provided you also know the base and exponent at compile time. This recursive algorithm is computed during compilation to provide a constant value for result. The template specialization on E = 0 is the exit point for the algorithm; without this the algorithm will never complete. Sure, if the programmer knows the exponent which needs to be calculated, he or she could calculate this themselves and write the value in the code. However, sometimes it’s clearer and/or easier to show the calculation in your code. Or you may want to do something more complex. Take for example the slightly longer example below, which builds on our Pow<B, E> example above: #include <iostream> #include <limits> // Generic exponent compile-time calculations. Pow<2,3>::result == 2^3 template <unsigned long B, unsigned long E> struct Pow { static const unsigned long result = B * Pow<B, E-1>::result; }; template <unsigned long B> struct Pow<B, 0> { static const unsigned long result = 1; }; // Bailey-Borwein-Plouffe formula for calculating pi // http://en.wikipedia.org/wiki/Bailey-Borwein-Plouffe_formula template <unsigned long N> struct CalculatePi { static const double pi = ( 1.0/Pow<16,N>::result * ( 4.0/(8*N + 1.0) - 2.0/(8*N + 4.0) - 1.0/(8*N + 5.0) - 1.0/(8*N + 6.0) ) ) + CalculatePi<N-1>::pi; }; template <> struct CalculatePi<-1> { static const double pi = 0; }; // main program. Print pi, calculated from 10 iterations of // the BBP formula above int main() { std::cout.precision(std::numeric_limits<double>::digits10); std::cout << "pi: " << CalculatePi<10>::pi << std::endl;  return 0; } This will print: pi: 3.14159265358979 This complete program prints the value of π, calulated from 10 iterations of the following formula: All calculations are done at compile time; at runtime the program simply reads the precalculated value from memory and prints it. Calculating the value with more iterations will slow down the compilation, but will not affect the runtime performance. There are other things which can be done with template metaprogramming, such as calculating typenames, which I will cover in a later post. ## Functors in C++ Posted: 12th June 2013 by Tim in C++ Tags: , , , , , , , One neat feature of C++ is Functors, or function objects. Essentially, functors are just like normal objects, but they can be ‘called’ like any other function; they can take any number of arguments of any type and return a value of any type. However, because functors are objects, they can have state (without resorting to static variables) and can utilize other class features like inheritance and access restrictions (private, public, protected). You can also use multiple versions of the same functor with different states, which is something you cannot do easily with functions. The syntax is very similar to other class operator overloading, except you can define any number of parameters (or none at all): <return type> operator()(<parameter list>){...} For example, consider the following full program which provides a functor for multiplying a number by two, but with a twist: #include <iostream> class TimesTwo { private: unsigned int times_called_; public: TimesTwo() : times_called_(0) { }  unsigned int operator()(unsigned int value) { // Every third time someone calls this function, return // the maximum unsigned int value. Just for fun. if (++times_called_ % 3 == 0) { return ~0; }  return value * 2; }  unsigned int timesCalled() const { return times_called_; } }; int main() { TimesTwo tt; for (int x = 0; x < 10; ++x) { std::cout << x << " times 2 equals " << tt(x) << std::endl; }  std::cout << "TimesTwo called " << tt.timesCalled() << " time(s)" << std::endl;  return 0; } As you can see, we have a TimesTwo functor which takes a number and multiplies it by two. However, unlike a normal multiplication routine, it keeps track of how many times it has been called, and returns a bogus value to every third caller. The number of times the function has been called is simply a private class attribute, the value of which may be read (but not written) via class accessors. This program will output: 0 times 2 equals 0 1 times 2 equals 2 2 times 2 equals 4294967295 3 times 2 equals 6 4 times 2 equals 8 5 times 2 equals 4294967295 6 times 2 equals 12 7 times 2 equals 14 8 times 2 equals 4294967295 9 times 2 equals 18 TimesTwo called 10 time(s) It’s not a very useful functor, but it’s behaviour would be difficult to replicate fully using traditional functions. ## Splitting a string with a given delimeter in python Posted: 28th May 2013 by Tim in Python Tags: , , , , , Strings in python can be split using any given delimeter. Unlike other languages, the delimeter can be a string of any length; it’s not limited to one character. To do this, you can use the <string>.split(<delimeter>) function. Similarly, you can join an array of strings back into one string, using any defined string to join them. This is done using <joining_string>.join(<array>). For example, you can use the following code to split a given sentence into an array of words, broken up by the space character, and recreate the sentence with underscores between the words like so: original = "There are 206 bones in the typical adult human skeleton" words = original.split(" ") rebuilt = "_".join(words) print "Original: " + original print "Split: " + str(words) print "Rebuilt: " + rebuilt This will print: Original: There are 206 bones in the typical adult human skeleton Split: ['There', 'are', '206', 'bones', 'in', 'the', 'typical', 'adult', 'human', 'skeleton'] Rebuilt: There_are_206_bones_in_the_typical_adult_human_skeleton ## <string> contains <substring> in bash Posted: 13th May 2013 by Tim in Bash, Linux Tags: , , , , , , , , , There are a few ways to find out if a string contains a substring using bash. Below are a couple of ways this can be done without invoking any other processes. ###### Star Wildcard One very simple method is to match strings using the * character to denote any number of other characters. For example: if [[ "$string" == *"$substring"* ]]; then echo "'$string' contains '$substring'"; else echo "'$string' does not contain '$substring'"; fi If you want to find if a string begins with substring, only use the * character after substring. ie: [[ "$string" == "$substring"* ]]. Similarly, omit the first * character to match strings ending with substring. ###### Regular Expressions More advanced pattern matching can be done using regular expressions. In it’s most basic form, we can do simple substring matching like so: if [[ "$string" =~ "$substring" ]]; then echo "'$string' contains '$substring'"; else echo "'$string' does not contain '$substring'"; fi This method is computationally slower than the first method, but it can be expanded with other regular expression syntax to do more powerful pattern matching, such as matching strings which contain the substring on word boundaries. See http://linux.die.net/Bash-Beginners-Guide/sect_04_01.html#sect_04_01_02 for more details on bash regular expression syntax. ## Reversing a string in Python Posted: 25th April 2013 by Tim in Python Tags: , , , , , , , , If you need to reverse a string in python, the easiest way is to do use the my_string[::-1] syntax. For example, to print a reversed string, you could write: my_string = "ABCDE" print my_string[::-1] So what is this doing? The square bracket syntax is used for returning a substring. This works like so: [<start>:<end>:<step>]. If <start> and/or <end> are not specified, as in the example above, the start and end of the string is used instead. For example, my_string[:3] would return ABC. The last parameter, <step> is optional. If provided, the substring will use every step‘th character when building the string. For example, my_string[0:5:2] would return ACE; the 1st, 3rd and 5th character. If a negative number is provided for <step>, the string will be constructed from the end instead of the beginning. So for our example program, my_string[::-1] constructs a substring of ABCDE using all characters (beginning to end), traversed backwards over every character. In other words, it reverses the string. ## Running C++ code from a C program Posted: 11th April 2013 by Tim in C, C++ Tags: , , , , , , , , There are times where you have a C program but would like to call code written in C++. This is quite easy to do, and if done right will work on all (compliant) compilers on multiple platforms. To achieve this, you need to declare your function(s) to look like C functions in a header file. By that I mean that they can only use types recognised by C (int, double, etc). The function definition can use C++ to it’s heart’s content, but from the outside it needs to look like C. The functions need to be declared as extern "C" when compiled under C++, as shown in the example below. Next, you need to define your function(s) in a separate .cc or .cpp file. This file is pure C++ and can contain any C++ syntax you like. Finally, the calling code simply needs to include the header file and call the routine. To build your program, first compile the C++ code into an object (.o) file using a C++ compiler, then build your main program with the .o file included like you would with any other .o file. Let’s look at small example. In this example I perform factorial calculations on integers and floats. I didn’t want to duplicate the code for this work, so I wrote a templated function in C++ and provided C-style wrappers to allow this to be called from C. ###### cplusplus.h // the two #ifdef blocks allow extern "C" to be used when // compiling as C++ but not when compiling as C #ifdef __cplusplus extern "C" { #endif int factorial_int(int x); float factorial_float(float f); #ifdef __cplusplus } // extern "C" #endif ###### cplusplus.cc #include "cplusplus.h" template <typename T> int calcFactorial(T number) { if (number <= 1) { return 1; } return number * calcFactorial(number - 1); } // wrapper function for int calculations int factorial_int(int x) { return calcFactorial(x); } // wrapper function for float calculations float factorial_float(float f) { return calcFactorial(f); } ###### main.c #include "cplusplus.h" #include <stdio.h> int main() { int n = 10; printf("%d! = %d\n", n, factorial_int(n));  float f = 10; printf("%f! = %f\n", f, factorial_float(f));  return 0; } You can then build this in gcc: g++ -c cplusplus.cc -o cc.o gcc -o prog_name main.c cc.o And you’re done! Running the program will result in this output: 10! = 3628800 10.000000! = 3628800.000000 ## How to find the UUID of a disk in Linux Posted: 25th March 2013 by Tim in Linux Tags: , , , , , , , , , , , , If you need to find the UUID of a hard disk in Linux, to add an entry to fstab for example, you can use the blkid command. There are many other methods available, but blkid prints the UUID with the label and disk type, and doesn't require you to enter the path to the disk. Note that this command must be run as root. For example, I can find the UUID of the "stuff" disk like so: $ sudo blkid /dev/sdb1: UUID="9b2b12c4-9893-4b76-ae0f-faff2d1625ee" TYPE="ext4" /dev/sda1: LABEL="stuff" UUID="4a4bac24-6bb2-49df-baed-2e0aa9a6ad6b" TYPE="ext4" /dev/sda2: LABEL="home" UUID="06000775-b63c-41b1-896a-476c3990f784" TYPE="ext4" /dev/sdc1: UUID="29bd570f-95bc-4a93-b7c4-2c1b9bf29cca" TYPE="ext3" /dev/sdd1: LABEL="Elements" UUID="5420AA2320AA0BD4" TYPE="ntfs"

From here it's easy to see that the "stuff" disk is /dev/sda1 and has the UUID 4a4bac24-6bb2-49df-baed-2e0aa9a6ad6b.

## Creating candlestick charts with gnuplot

Posted: 10th March 2013 by Tim in Gnuplot
Tags: , , , , , , , , ,

Gnuplot can create candlestick charts out of the box. These charts are often used for graphing financial data, with the middle block representing the difference between the open and close, and the high and low represented with lines protruding from the top and bottom of this box. The bars are colored differently depending on if the security’s price increased or decreased over this period of time. In gnuplot, losses are represented with filled bars.

Creating the graph is relatively easy. First you need a data file. This can be in a number of formats, but the following format is fairly common:

<date>,<open_price>,<high_price>,<low_price>,<close_price>

Once you have your data file, creating the graph is as simple as:

plot '<data_file>' using 1:2:4:3:5 with candlesticks

The 1:2:4:3:5 is the field order for date, open, low, high and close prices in the file. If your file has data in a different order, you will need to rearrange these numbers.

The following is a complete working example. We have two files; bhp.dat which contains the price data for BHP on the Australian Stock Exchange, and candlestick.gnuplot which contains the gnuplot instructions.

###### bhp.dat

# BHP (Australian Stock Exchange) # Date,Open,High,Low,Close 2013-02-15,38.75,38.84,38.43,38.59 2013-02-18,38.76,38.94,38.59,38.67 2013-02-19,38.73,39.00,38.62,39.00 2013-02-20,39.00,39.34,38.43,38.65 2013-02-21,37.45,37.64,37.17,37.17 2013-02-22,36.73,37.18,36.51,36.87 2013-02-25,36.75,37.19,36.56,36.90 2013-02-26,36.27,36.69,36.12,36.35

###### candlestick.gnuplot

set terminal pngcairo font "arial,10" size 500,500 set output 'candlestick.png' set xdata time set timefmt"%Y-%m-%d" set xrange ["2013-02-14":"2013-02-27"] set yrange [*:*] set datafile separator "," plot 'bhp.dat' using 1:2:4:3:5 notitle with candlesticks

With these files, we can generate the graph on the command line with:

\$ gnuplot candlestick.gnuplot

Which will generate candlestick.png which will look like this:

## Converting to uppercase / lowercase in sed

Posted: 24th February 2013 by Tim in Bash, Linux
Tags: , , , , , , , ,

Sed is a useful tool for editing strings on the command line. Changing characters to uppercase or lowercase can be done easily with this tool by simply adding one or more of the following to your substitution string:

* \L – convert all proceeding characters to lowercase
* \U – convert all proceeding characters to uppercase
* \E – leave all proceeding characters in their current case

For example:

First letter capitalized:
echo "aBcDe fGh" | sed 's/^./\U&\E/'
will print ABcDe fGh

First word capitalized:
echo "aBcDe fGh" | sed 's/^[^ ]*/\U&\E/'
will print ABCDE fGh

Whole string lower case:
echo "aBcDe fGh" | sed 's/./\L&/g'
will print abcde fgh