Thursday, October 23, 2014

Show full list of program arguments in top command

Pass the -c argument to top to display the command line arguments that were passed to the process
top -c

Monday, September 29, 2014

List all references to a table in Oracle

Got this query from this blog: http://www.dba-oracle.com/t_find_all_references_to_oracle_table.htm
select * from all_constraints
where r_constraint_name in
    (select constraint_name from all_constraints
      where table_name='TABLE_NAME');

Friday, March 8, 2013

Searching ascii code in vim

You can use %d to specify the ASCII value of the character to search. For example the following command would replace the SOH character (ASCII value 1) with a space

:%s/\%d1/ /g

Friday, January 13, 2012

Repeatedly run a command on Linux

The watch utility can be used to continuously run a command on Linux. For example in order to continuously monitor system memory via free the following command can be used:

watch -n1 free

Sunday, August 21, 2011

Azan 3.4 Released

Check out "Azan"

The latest version of Android based Azan application has been released.

It now has more than 30000(and still counting) total downloads and an average rating of more than 4 stars (out of 5). And It is being used by people from all over the world.

Friday, August 19, 2011

some sscanf magic

In the world of regular expressions and comprehensive frameworks like boost, ace etc it is justifiable if you have forgotten about poor sscan and the string parsing capabilities it provides.

Recently I had to do some rudimentary string parsing in C and didn't have external frameworks at my disposal. Thus sscanf came to the rescue and solved the problem just one line.

The following invocation of sscanf extracts from str everything uptill the first '*' and puts it in res_str.

sscanf(str, "%[^*]%*s", res_str);

Friday, August 12, 2011

Determine ringer mode on Android

The following code would let you determine if Android the device is on silent or vibrate.




private boolean isDeviceSilentOrOnVibrate(){
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
switch(am.getRingerMode()){
case AudioManager.RINGER_MODE_SILENT:
case AudioManager.RINGER_MODE_VIBRATE:
return true;
default:
return false;
}
}


Saturday, January 15, 2011

Streched ImageView in Android

If you want to have ImageViews that stretches to fit the screen size, use change the scaleType ImageView attribute to the following:

android:scaleType="fitXY"/>

Tuesday, December 21, 2010

Removing leading and trailing whitespace in C++

If you have the boost library installed, its simply a one liner

std::string line = "     hello world    ";
boost::algorithm::trim(line);


Wednesday, September 29, 2010

Azan Android Application

Just published my first Android application called: Azan

This application takes your address as input, determines your location information(latitude, longitude and timezone), look up the prayer times associated with the location and then plays Azan whenever it is due.

The app supports various calculation methods and both Shia and Sunni recitations.

Download

Search the Android market with the application's name "Azan" or my name "Sibtay" and you should find the app named Azan (Beta).


Twitter



Screen Shots




Daylight Adjustments


A manual step is required, whenever daylight times are adjusted in your locality. You just have to reconfigure your app. The adjusted timezone offset would be picked implicitly and used in prayer time calculations.

Acknowledgements

Sarah Jamal for helping in the UI design and testing.

http://www.praytime.info The app uses the excellent webservices from this website to lookup all prayer times based on latitude, longitude, timezone and date.

http://www.elegantthemes.com For icons. The icons developed by Nicholas Roach are used

http://www.styleislam.com For the beautifull Islamic icons.

Friday, June 18, 2010

Determine the endian'ess of the current platform

The following function would determine whether the current platform is little endian or not

bool is_little_endian()
{
unsigned short int word = 1;
unsigned short int mask = 0x00;

// get the most significant bytes of the word
unsigned short int msb = word>>4;

// the platform is little endian if the most significant bytes
// contains zero
if ( (msb | mask) == 0 )
{
return true;
}
return false;
}

Monday, May 10, 2010

Sleep the current thread using boost

The following code would sleep the current thread for 200 milliseconds
boost::this_thread::sleep(boost::posix_time::milliseconds(200));

Thursday, May 6, 2010

Thursday, April 8, 2010

Change lower case letters to upper case in bash

This can be done using the tr command.

For example:
echo "change my case" | tr '[a-z]' '[A-Z]'

Tuesday, March 23, 2010

Prepend string in bash using sed

You can do that using the following sed command


sed 's/^//g'

The key here is the use of ^ Which matches the start of line.

For example below is how you can prepend "foo" to a string "bar"


echo "bar" | sed 's/^/foo-/g'

Tuesday, November 24, 2009

About the new kid in town, Chrome OS

I know I don't need to explain what Chrome OS is, so I will come straight to my views about it.

Firstly, I love the fact that some OS vendor has finally come to realize that most of the user apps now are web based and are modelling their OS based on this fact. Also I like their obsession with startup speed.

Additionally since everything runs in a browser, the cost of hardware could be so cheap, that there are already rumors about google giving away the netbooks for free. Which definitely would be super exciting.

However there is one aspect which myself and some other guys might have reservations on, i.e. everything goes on the cloud. This means that you have no control over your data and as soon as you put something on it, it goes to someone else's computer.

I think they should make the cloud feature optional and allow for local storage as well.

Sunday, March 15, 2009

Implementing a non-copyable class in C++

Problem at hands

============

In the simplest of scenario, we basically want to prevent something like this:

class A

{

public:

A(){}

~A(){}

};

void foo(A a)
{
}

int main(int argc,char *argv[])

{

A a1;

A a2(a1); // directly invoking the copy constructor, should be disallowed

a2 = a1; // creating copies via assignment operator, should be disallowed

foo(a1); // and passing copies as parameters to other functions, should be disallowed


return 0;

}

Sol 1:Make the copy constructor and assignment operator private
==============================================
This is probably the first solution which will strike our minds.And apparantely, this would get the job done as well. Lets see it in action:
class A
{
private: // copy constructor and assignment operator
A(const A&){}
A& operator=(const A&) {}
public:

A(){}
~A(){}

};

void foo(A a)
{
}

int main(int argc,char *argv[])
{
A a1;
A a2(a1); // error! Copy constructor is private

A a3;
a3 = a1; // error! assignment operator is private

foo(a1); // error! copies cannot be created since the copy constructor is private

return 0;
}

All is good uptill now, but what about the following scenario?

#include <iostream>
using namespace std;

class A
{
private: // copy constructor and assignment operator
A(const A&)
{
cout<<"Copy constructor"<<endl;
}
A& operator=(const A&)
{
cout<<"Assignment operator"<<endl;
return *this;
}
public:
A(){}
~A(){}

void violate1()
{
A tmp(*this); // copy constructor will get invoked
}

void violate2()
{
A tmp;
tmp = *this; // assignment operator will get invoked
}
};

int main(int argc,char *argv[])
{
A a1;
a1.violate1(); // gets invoked successfully
a1.violate2(); // gets invoked successfully

return 0;
}



Since we only made the copy constructor and assignment operator private, we were able to invoke them from within member functions like violate1() and violate2. Similarly we should be able to invoke foo() from within a member function of class A. This leads us to Sol2

Sol2: Do not implement the copy constructor and assignment operator
==================================================

This is a smart trick to make the class uncopyable. We would just declare the functions and NOT implement them. Declaring them would prevent the compiler from generating its own generous versions of these functions and not implementing them would ensure that a linking error is generated if they are used anywhere in the program. Here is the implementation of this approach:

#include <iostream>
using namespace std;

class A
{
private:
A(const A&); // copy constructor declaration only
A& operator=(const A&); // assignment operator declaration only
public:
A(){}
~A(){}

void violate1()
{
A tmp(*this); // copy constructor will get invoked
}

void violate2()
{
A tmp;
tmp = *this; // assignment operator will get invoked
}
};

int main(int argc,char *argv[])
{
A a1;
a1.violate1(); // would get compiled, however a linker error would be generated
a1.violate2(); // would get compiled, however a linker error would be generated

return 0;
}

When I tried to compile it (using g++), I got the following linker errors
/tmp/ccUfNEtK.o: In function `A::violate1()':
cpp_code.cpp:(.text._ZN1A8violate1Ev[A::violate1()]+0x14): undefined reference to `A::A(A const&)'
/tmp/ccUfNEtK.o: In function `A::violate2()':
cpp_code.cpp:(.text._ZN1A8violate2Ev[A::violate2()]+0x20): undefined reference to `A::operator=(A const&)'
collect2: ld returned 1 exit status
We have achieved our goal! Copies of class A's object cannot be created now. But there is still room for improvement; It would definitely be more helpful if we can generate compile-time errors instead of link time errors. Sol3 will do just that

Sol3: Use inheritance
================

Create a class (lets call it uncopyable) with un-implemented but declared private copy
constructor and assignment operator. Inherit from this class, in order to make the derived class
uncopyable.

#include <iostream>
using namespace std;

class Uncopyable
{
private:
Uncopyable(const Uncopyable&); // copy constructor declaration only
Uncopyable& operator=(const Uncopyable&); // assignment operator declaration only
public:
Uncopyable(){};
~Uncopyable(){};
};

class A:public Uncopyable
{
public:
A(){}
~A(){}

void violate1()
{
A tmp(*this); // error! This cannot be invoked, since base class's copy constructor is private
}

void violate2()
{
A tmp;
tmp = *this; // error! assignment operator cannot be invoked, since base class has defined it as private
}
};

int main(int argc,char *argv[])
{
A a1;

A a2(a1); // error. Copy constructor cannot be invoked

a1.violate1(); // error (See above)
a2.viloate2(); // error (See above)


return 0;
}

To sum it up, uncopyable prevents copying of its derived classes from all possible scenarios:

  • Outside the class: Since its copy constructor and assignment operator is private
  • Inside member functions of the derived class: because of private copy constructor and assignment operator, the derived classes do not have access to them. And we know that the default copy constructor invokes the copy constructor of its base class. Moreover the default assignment operator also invokes the assignment operator of its base class. Since both are private, hence a compile time error would be generated if such an attempt made.
  • When passed as parameters

ACKNOWLEDGEMENTS

This approach is being taken from Scott Meyers "Effective C++".

Sunday, February 8, 2009

ac2html: Syntax highlight your blog posts

If you are a developer and you post your code on blogger (or other blogs) then you are gonna love this small tool which I call ac2html (any code to html).

ac2html takes your code and highlights it using html thus allowing you to generate a syntax highlighted version of your code, which you can post on blogger or any place where html is accepted.

Sample run

========

Download ac2html from here. The code is platform independent and should run on all platforms (but I have only tested it on linux x86)

Untar it

tar -xvzf AnyCode2html.tar.gz

Compile it

cd AnyCode2html

make

Note that you might need to edit the Makefile to suit your system.

Now write some c++ code or pick an existing one. I am going to use a sample c++ file which has the following contents (copied and pasted from my source file)

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
// print a test message
cout<<endl<<"Hello world"<<endl;
return 0;
}

Ugly isn't it? Now run ac2html

./ac2html cpp_keywords cpp_code.cpp output.html

Before we show the results lets analyze the arguments that we just passed. All the arguments that we passed are required by ac2html.

  • cpp_keywords is the keywords file which ac2html uses to identify keywords.
  • cpp_code.cpp is the file that contains my c++ code
  • output.html is the file which the results should be saved

Now copy the the contents of output.html and paste it to the blogger's compose window. Publish your post and you will get this:

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
 // print a test message
cout<<endl<<"Hello world"<<endl;
 return 0;
}

Beautiful, isn't it :-)

ACKNOWLEDGEMENTS

================

I got this idea from Adrian's post at http://codecentrix.blogspot.com/2007/05/blogs-and-c-syntax-highlighting.html. However his tool was too windows and c++ centric, hence I decided to rewrite a generic one.

Friday, January 16, 2009

A Perl SDL based game

Just finished writing a small Perl SDL based game. I call it invade. Its a conventional space invader type of game.
Download it from here
Untar it and voila!
Before running it, make sure that you have SDL and SDL perl libraries installed.


Acknowledgements


Thanks to my wife Sarah, for designing the background and for the excellent QA :)


Friday, October 17, 2008

C++ mutable, finally makes sense

After being a long time basher of C++'s mutable type specifiers, I have finally come to embrace this controversial concept (but its still in beta state :-)

This happened while implementing the FixSession class for my FIXTransit project. I was passing FixSession objects as constant references to other classes, but realized that this wouldn't be possible because some logically-read-only functions might end up modifying the SessionState data member. For example Session::createHeader() whose job is to simply return a FIX message Header for that Session, would result in incrementing the SessionState::seqNum property.

So now that the rationale behind the divine concept had occured to me, I very proudly declared the SessionState data member as mutable and continued to pass the Session objects as logically constant references :-)

Now I might end up in revisiting and changing this design altogether, however I feel that I have learned an important lesson from this. Also I would still maintain that this is not an ideal approach and should be adopted in very rare situations.