Archive for the Programming Category

static functions in pthread_create() using solaris compiler

Posted in Programming on April 9, 2008 by aghoras

Create a C function to basically do the conversion from static to extern “C” for you:

extern “C” int CreateThread( pthread_t*& pThread, CVoidFunction pVoidFunction, void* pArgument )
{
// Create the thread
const int nResult = pthread_create( pThread,
(const pthread_attr_t*)0,
(void*(*)(void*))pVoidFunction, // cast to extern “C” void* (*)( void* )
pArgument );

return nResult;
}

GCC Compiler defines

Posted in Programming on April 8, 2008 by aghoras

gcc -E -Wp,-dD hello.c |grep \#define |sort

Big Endian Little Endian bit fields

Posted in Programming on February 21, 2008 by aghoras

For big endian machines (e.g. Motorola) :

typedef union{
   struct{
      uint32_t MSB       :1;
      .
      .
      .
      uint32_t LSB       :1;
   }_bit;
   uint32_t _word
} typename_t;

For little endian machines (e.g. Intel):

typedef union{
   struct{
      uint32_t LSB       :1;
      .
      .
      .
      uint32_t MSB       :1;
   }_bit;
   uint32_t _word
} typename_t;

Save Bash History

Posted in Programming on December 24, 2007 by aghoras

Use the following command to force Bash to save command history:

history -w

VB Hex Function used for Bit Decoding in Excel

Posted in Programming on September 12, 2007 by aghoras
Attribute VB_Name = "HexFunctions"
' Returns the a bit range in a given hex
Function Bits(HexNum As String, startBit As Integer, endBit As Integer) As String
    Dim num, mask, startDivider, Results As Long

    num = CLng("&H" & HexNum)
    mask = 0

    ' play this silly trick to avoid overflows when doing things with 2^31
    If endBit = 31 Then
        endBit = 30
        OrVal = &H80000000
    Else
        OrVal = 0
    End If

    'create the mask 1s
    For i = startBit To endBit
        mask = mask Or (2 ^ i)
    Next

    mask = mask Or OrVal

    Test = Hex(mask)

    Results = (num And mask)
    Test = Hex(Results)
    ' shift to the right
    For i = 1 To startBit
        ' overcome the stupid sign extension
        Results = Results \\ 2
        If (Results < 0) Then
            Results = Results And &H7FFFFFFF
        End If
        Test = Hex(Results)
    Next

    Bits = Hex(Results)
End Function
' Pads the given hex number with leading zeros
Function PadZeros(HexNum As String, size As Integer) As String
    Results = HexNum
    For i = 1 To size - Len(HexNum)
        Results = "0" + Results
    Next

    PadZeros = Results
End Function

'Converts a hex number to long
Function HexToDecimal(HexNum As String) As Long
    HexToDecimal = CLng("&H" & HexNum)
End Function

'Converts Decimal To Hex
Function DecimalToHex(num As Long) As String
    DecimalToHex = Hex(num)
End Function

Handling spaces in bash scripts

Posted in Programming on August 15, 2007 by aghoras

To handle spaces in bash scripts, set the IFS variable to $’\n’. e.g

#!/usrbin/bash
export IFS=$’\n’
for i in `find . -type f -name *.mp3`; do
mp3info -p “%a,%l,%n,%t,%F\n” “$i”
done

Exporting and Importing Data into Postgres

Posted in Programming on June 22, 2007 by aghoras

To export a postgres database

 pg_dump mydb > db.sql

To reload such a script into a (freshly created) database named newdb:

$ psql -d newdb -f db.sql

If statements in SQL

Posted in Programming on June 22, 2007 by aghoras

To have a conditional statement in a SELECT statement, use CASE WHEN…THEN….ELSE.

create view logbook as
        select  "Date",
                "N Number",
                "From","To",
                "Number of Instrument Approaches",
                "Number of Night Landings",
                "Total Flight Duration" - "Night" as "Day",
                "Night",
                "Simulator",
                "Simulated Instrument",
                "Actual Instrument",
                 case when "IsCrossCountry" = true  then "Total Flight Duration"  else 0.0 end as "Cross Country"
         from flights;

A simple file IO program in Perl

Posted in Programming on June 20, 2007 by aghoras

This program searches the instructions.txt file line by line to and print the lines immediately after the ones matching the Patter /Read.*RegAddr:\ 0×3/.

#!/usr/bin/perl
open INSTR_FILE, "< Instructions.txt" or die "Cannot open instruciton file $!";
print "## Register 3n";
while ($line = <INSTR_FILE>){
        if ($line =~ /Read.*RegAddr: 0x3/){
                $line = <INSTR_FILE>;
                @fields = split /t/,$line;
                print $fields[2],"n";
        }
}
close INSTR_FILE;

a2ps

Posted in Programming on May 22, 2007 by aghoras

To print code in color
a2ps –pro=color [files]

To generate a color PDF of code
a2ps –pro=color -Ppdf [files]

When using a2ps use the following to fix the cropped margins on a Deskjet printer:
a2ps -M letterdj counter_test.vh

In Cygwin, to convert things to a PDF
a2ps -r -M letterdj -o - Parity_Gen.vhd | ps2pdf - foo.pdf

 Another good way to print a bunch of files

a2ps  –toc -l 120 -r -M letterdj -o -  *.vhd |ps2pdf -  foo.pdf