Tuesday, August 21, 2007

Pattern Matching with PCRE Library

Pattern matching is wonderful with Perl and when it is comes to C it is nice to build wonderful applications.

#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <locale.h>
#include <errno.h>
#include <pcre.h>

int main(int argc,char *argv[])
{
pcre *re;
char cgets[250];
const char *error;
int erroffset;
int retexec;
re = pcre_compile("^(gnayiru|thingal|chevvai|budhan|viyazhan|velli|sani)$",0,&error,&erroffset,NULL);
if( re )
{
while(1){
printf("Enter the details you wish to match:");
fgets(cgets,250,stdin);
cgets[strlen(cgets)-1] = '\0';
retexec = pcre_exec(re,NULL,cgets,strlen(cgets),0,PCRE_PARTIAL,NULL,0);
switch( retexec)
{
case PCRE_ERROR_NOMATCH:
printf("No Matches\n");
break;
case PCRE_ERROR_PARTIAL:
printf("Partial Match\n");
break;
case 0:
printf("Perfect Match\n");
break;
default:
printf("Error Code: %d\n",retexec);
break;
}
}
} else {
printf("Error in Regex Compilation");
}
}


Output here:

[root@localhost 21Aug2007]# ./nkans.out
Enter the details you wish to match:thi
Partial Match
Enter the details you wish to match:thing
Partial Match
Enter the details you wish to match:thingal
Perfect Match
Enter the details you wish to match:fldjlsjfl
No Matches
Enter the details you wish to match:

Google Code checkout:

svn co http://codeoftheday.googlecode.com/svn/trunk/21Aug2007

Sunday, August 19, 2007

Getting DNS SRV Records for voip through PHP

It is important for the internet service to automatically locate the services at the moment. Just use this code to get the voip protocols service records from the DNS servers.

#!/usr/bin/php
< ?php


$domain = "pulver.com";

$voipprotocol = array("sip","iax");
$transportproto = array("udp","tcp");

foreach($voipprotocol as $voipprot)
{
foreach($transportproto as $trprot)
{
$voipdomain = "_" . $voipprot . "._" . $trprot . "." . $domain;
echo "Searching $voipdomain\n";
$rootinfo = dns_get_record($voipdomain,DNS_ALL, $nsinfo, $addinfo);
//print_r($rootinfo);
foreach($rootinfo as $srootinfo)
echo strtoupper($voipprot) . " " . strtoupper($trprot) . " HOST: " .
$srootinfo['target'] . ":" . $srootinfo['port'] . "\n";
}
}

?>

You can download using svn with the following command,

svn co http://codeoftheday.googlecode.com/svn/trunk/18Aug2007

Do Let me know if there are any improvements done, will keep updated.

Enjoy.

If you want to get this done under C, you can look into the ruli library.

Ruli - DNS SRV Resolver

Tuesday, August 7, 2007

Convert header file tree to source file tree - Shell Script

It is usual practice to write header files first and then the source code. We usually need to go back an forth to get the header file name to start to go with the coding for enterprise projects.

Using the following command, you can convert the header tree to source tree.

find ./include -ls | awk '{print($3,$11);}' | cut -c1,11- | awk '{print($2,$1)}' | sed -e 's/^\.\/include//g' | awk '{if( $2 != "" ) print($1,$2);}' | awk '{if($2 == "d") printf("\"mkdir -p ./src%s\"\n",$1); else{ sub(/\.h$/,".cpp",$1); printf("\"touch ./src%s\"\n",$1);}}' | xargs | /bin/sh

replace .cpp with the extension you want.

It looks like

./include
|----dir1
|------- dir2
|-----------file.h
etc.,

./src
|--------dir1
|-----------dir2
|------------ file.cpp

All header files will replaced with their equivalent name in .c or .cpp with the extension you supply in the command.


Enjoy.

Monday, August 6, 2007

Find and Replace string in files under linux directory tree - Linux Shell Script

This is one of most used command in linux to replace the string in files.

find . -name "*.cpp" | xargs grep -l stringtofind | xargs sed -i s/stringtofind/stringtoreplace/

Friday, August 3, 2007

Code to generate Unique Random Numbers - PHP

I was looking for a Unique Random Number with different length to use it for one of the calling card company. This PHP script just rocks. Enjoy, download the code from svn rather than copy and paste.

#!/usr/bin/php

function generateuniquenumbers($len,$count,$includezero=1)
{

if( strlen($count) > $len )
{
echo "Looks something funny in the parameter which you have supplied. \n";
return ;
}
echo "Generating " . $count . " numbers with length " . $len . "\n";
$mult = 0;
$fract = 0;
$numbers[] = "";
mt_srand(10);
if( $len > 9 )
{
$mult = (int)($len/9);
$fract = (int) $len % 9;
} else {
$fract = $len;
}
if( $fract > 0 )
{
$minfract = "";
$maxfract = "";
for($m=0; $m<$fract; $m++)
{
if($includezero)
$minfract = "0";
else
$minfract .= "1";
$maxfract .= "9";
}
}

for($numcount=0;$numcount<$count;$numcount++)
{
$curnumber = "";
for($i=0;$i<$mult;$i++)
{
if( $includezero )
$currand = mt_rand(0,999999999);
else
$currand = mt_rand(111111111,999999999);
if( strlen($currand) <>
{
$templen = strlen($currand);
for( $pad = 9 - $templen; $pad > 0; $pad--)
$curnumber .= "0";
}
$curnumber .= $currand;
}
if( $fract > 0 )
{
$currand = mt_rand($minfract,$maxfract);
if( strlen($currand) <>
{
$templen = strlen($currand);
for( $pad=strlen($maxfract)-$templen;$pad > 0;$pad--)
$curnumber .= "0";
}
$curnumber .= $currand;
}
if( !in_array($curnumber,$numbers) )
{
$numbers[] = $curnumber;
}
else
$numcount --;
}
return $numbers;
}

$ncount = 20;
$nlength = 20;

$numbers = generateuniquenumbers($nlength,$ncount,0);

for($k=1;$k<=$ncount;$k++)
echo $numbers[$k] . "\n";

?>

Here is the output:

[root@kansdevhost devel]# ./kansrand.php
Generating 20 numbers with length 20
16181070355240094632
35133279664046683582
56683089348828603637
61797018627269033641
32242637617039241045
53775119541769589123
98009706424319286130
67830750255154478737
4554441961805820434
24613857566557562252
86485415760605734578
30890354240158234429
12549747923000041677
85786735955666425870
44706552566779852177
41866325246584913132
61537972143640201927
52078343311552209047
53657534152010051142
74283518264934633688

You can download using svn with the following command,

svn co http://codeoftheday.googlecode.com/svn/trunk/03Aug2007

Do Let me know if there are any improvements done, will keep updated.

Enjoy.

Thursday, August 2, 2007

Forming main with a macro

A cute simple program forming "main" with a macro. Have fun.

#include <stdio.h>

#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)

int begin()
{
printf("hello");
return 0;
}

You can download from svn using,

svn checkout http://codeoftheday.googlecode.com/svn/trunk/02Aug2007

Enjoy.

Wednesday, August 1, 2007

Mangling with fd and /dev/null

When doing the code faced a problem with the SSL Libraries doing error output and want to ignore it. Just got a nice idea from my friend Michael Waters. Here it is.

#include <iostream>
#include <fcntl.h>

using namespace std;

int main(int argc,char *argv[])
{
int devnullfd = open("/dev/null",O_RDWR);
int stderrfd = dup(2);
dup2(devnullfd,2);
cerr << "Am I out?" <<>
dup2(stderrfd,2);
cerr << "Am I In?" <<>
close(devnullfd);
}

svn also has the example in C.

You can download from svn with the command below.

svn checkout http://codeoftheday.googlecode.com/svn/trunk/01Aug2007

Enjoy. Will try to get something out tomorrow.