tag:blogger.com,1999:blog-48071889703626382832024-03-13T22:39:21.875-07:00IMHOIn my humble opinion ....Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.comBlogger24125tag:blogger.com,1999:blog-4807188970362638283.post-23840362467803963512014-10-23T08:18:00.001-07:002014-10-23T08:18:56.105-07:00Show full list of program arguments in top command<div dir="ltr" style="text-align: left;" trbidi="on">
Pass the -c argument to top to display the command line arguments that were passed to the process<br />
<blockquote>
top -c</blockquote>
</div>
Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com0tag:blogger.com,1999:blog-4807188970362638283.post-1187503453774694702014-09-29T12:04:00.002-07:002014-09-29T12:04:31.767-07:00List all references to a table in Oracle<div dir="ltr" style="text-align: left;" trbidi="on">
Got this query from this blog: http://www.dba-oracle.com/t_find_all_references_to_oracle_table.htm<br />
<blockquote class="tr_bq">
select * from all_constraints<br />
where r_constraint_name in<br />
(select constraint_name from all_constraints<br />
where table_name='TABLE_NAME');</div>
</blockquote>
Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com0tag:blogger.com,1999:blog-4807188970362638283.post-61396300351549429762013-03-08T08:12:00.001-08:002013-03-08T08:14:22.289-08:00Searching ascii code in vim<p dir="ltr">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</p>
<p dir="ltr">:%s/\%d1/ /g</p>
Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com0tag:blogger.com,1999:blog-4807188970362638283.post-35121750920042638402012-01-13T16:01:00.000-08:002012-01-13T16:05:39.017-08:00Repeatedly run a command on Linux<div dir="ltr" style="text-align: left;" trbidi="on">
The watch utility can be used to continuously run a command on Linux. For example in order to continuously monitor system memory via <code>free</code> the following command can be used:<br />
<code></code><br />
<code>watch -n1 free</code><br />
</div>Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com0tag:blogger.com,1999:blog-4807188970362638283.post-51683444991124110412011-08-21T09:41:00.001-07:002011-08-21T09:41:08.501-07:00Azan 3.4 Released<div><p><a href="https://market.android.com/details?id=org.azan">Check out "Azan"</a></p>
<p>The latest version of Android based Azan application has been released. </p>
<p>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.</p>
</div>Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com0tag:blogger.com,1999:blog-4807188970362638283.post-12640547259207738932011-08-19T12:32:00.000-07:002011-08-19T12:33:41.346-07:00some sscanf magic<div>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.<br />
<br />
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.<br />
<br />
The following invocation of sscanf extracts from str everything uptill the first '*' and puts it in res_str.<br />
<br />
<blockquote><code> </code>sscanf(str, "%[^*]%*s", res_str);</blockquote><br />
</div>Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com0tag:blogger.com,1999:blog-4807188970362638283.post-42177305696058688312011-08-12T21:38:00.000-07:002011-08-12T21:41:33.087-07:00Determine ringer mode on AndroidThe following code would let you determine if Android the device is on silent or vibrate.<br />
<br />
<blockquote><br />
<code></code><br />
<br />
<code>private boolean isDeviceSilentOrOnVibrate(){</code><br />
<code><span class="Apple-tab-span" style="white-space: pre;"> </span>AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);</code><br />
<code><span class="Apple-tab-span" style="white-space: pre;"> </span>switch(am.getRingerMode()){</code><br />
<code><span class="Apple-tab-span" style="white-space: pre;"> </span>case AudioManager.RINGER_MODE_SILENT:</code><br />
<code><span class="Apple-tab-span" style="white-space: pre;"> </span>case AudioManager.RINGER_MODE_VIBRATE:</code><br />
<code><span class="Apple-tab-span" style="white-space: pre;"> </span>return true;</code><br />
<code><span class="Apple-tab-span" style="white-space: pre;"> </span>default:</code><br />
<code><span class="Apple-tab-span" style="white-space: pre;"> </span>return false;</code><br />
<code><span class="Apple-tab-span" style="white-space: pre;"> </span>}</code><br />
<code><span class="Apple-tab-span" style="white-space: pre;"> </span>}</code><br />
<br />
<br />
</blockquote>Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com0tag:blogger.com,1999:blog-4807188970362638283.post-64219819490242918792011-01-15T05:08:00.000-08:002011-01-15T05:08:18.176-08:00Streched ImageView in AndroidIf you want to have ImageViews that stretches to fit the screen size, use change the scaleType ImageView attribute to the following:<br />
<br />
<imageview .....="" android:id="@+id/ImageView01"><b>android:scaleType="fitXY</b>"/></imageview>Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com0tag:blogger.com,1999:blog-4807188970362638283.post-3432546717782068652010-12-21T08:12:00.000-08:002010-12-21T08:12:54.254-08:00Removing leading and trailing whitespace in C++If you have the boost library installed, its simply a one liner<br />
<br />
<blockquote><code></code>std::string line = " hello world ";<br />
boost::algorithm::trim(line);<br />
<br />
<br />
</blockquote>Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com0tag:blogger.com,1999:blog-4807188970362638283.post-22251710133786423602010-09-29T18:23:00.001-07:002010-11-07T04:18:33.569-08:00Azan Android ApplicationJust published my first Android application called: Azan<br />
<br />
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.<br />
<br />
The app supports various calculation methods and both Shia and Sunni recitations.<br />
<br />
<strong>Download</strong><br />
<br />
Search the Android market with the application's name "Azan" or my name "Sibtay" and you should find the app named Azan (Beta).<br />
<br />
<br />
<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><b>Twitter</b></div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><b><br />
</b></div><div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"><b><a href="http://twitter.com/#!/azanapp">http://twitter.com/#!/azanapp</a></b></div><div><br />
</div><br />
<strong>Screen Shots</strong><br />
<br />
<br />
<br />
<div class="separator" style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIYEDICTAcZ4OM4BNjKXP-4eQy7fKQbD7IzCwEfOqwqXyJhNtCdyAL2p4JQg0SPLs8oK734c9-hHip1VKm_oUO4TnLYC-1MyJ_VyFCAiHz2o0r2JP6lRYpwQo0plvS2M9CxY7unrDCSZA/s1600/AzanActivity.png" imageanchor="1" style="clear: left; cssfloat: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="320" px="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIYEDICTAcZ4OM4BNjKXP-4eQy7fKQbD7IzCwEfOqwqXyJhNtCdyAL2p4JQg0SPLs8oK734c9-hHip1VKm_oUO4TnLYC-1MyJ_VyFCAiHz2o0r2JP6lRYpwQo0plvS2M9CxY7unrDCSZA/s320/AzanActivity.png" width="179" /></a><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgifhwc_5ba1f1YyLTJ2fV53EqkBNQVcyTjl8USlqxuz100MAdFEfd6hUG96z0DHS57yMfznvl860-WKtpcmbPSw2F5I-lA83qj2PAyVaBWgcLTUkGdh6uyho8eF_QdHVEDyfXIKkO4O2A/s1600/ConfigureActivity.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" px="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgifhwc_5ba1f1YyLTJ2fV53EqkBNQVcyTjl8USlqxuz100MAdFEfd6hUG96z0DHS57yMfznvl860-WKtpcmbPSw2F5I-lA83qj2PAyVaBWgcLTUkGdh6uyho8eF_QdHVEDyfXIKkO4O2A/s320/ConfigureActivity.png" width="179" /></a></div><br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiqrHAELuJ2qDCAAH_UcZpZeUA0PATIRSFZQtwjVIughfcgXxxV6v9rLDENU8mMzLDj46R5faEricxxpEGk0ps-GRd3BMtrz0-_Pwgn6rm0T_FHEE1G_Qi01PBUJj9AlnTFpBPkaiXz3U/s1600/PrayerTimesActivity.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" px="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgiqrHAELuJ2qDCAAH_UcZpZeUA0PATIRSFZQtwjVIughfcgXxxV6v9rLDENU8mMzLDj46R5faEricxxpEGk0ps-GRd3BMtrz0-_Pwgn6rm0T_FHEE1G_Qi01PBUJj9AlnTFpBPkaiXz3U/s320/PrayerTimesActivity.png" width="179" /></a></div><b>Daylight Adjustments</b><br />
<b><br />
</b><br />
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.<br />
<br />
<b>Acknowledgements</b><br />
<br />
Sarah Jamal for helping in the UI design and testing.<br />
<br />
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.<br />
<br />
http://www.elegantthemes.com For icons. The icons developed by Nicholas Roach are used<br />
<br />
http://www.styleislam.com For the beautifull Islamic icons.Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com2tag:blogger.com,1999:blog-4807188970362638283.post-73409083218705664632010-06-18T06:43:00.000-07:002010-06-18T06:44:15.015-07:00Determine the endian'ess of the current platformThe following function would determine whether the current platform is little endian or not<br />
<br />
<blockquote>bool is_little_endian()<br />
{<br />
unsigned short int word = 1;<br />
unsigned short int mask = 0x00;<br />
<br />
// get the most significant bytes of the word<br />
unsigned short int msb = word>>4; <br />
<br />
// the platform is little endian if the most significant bytes<br />
// contains zero<br />
if ( (msb | mask) == 0 )<br />
{<br />
return true;<br />
}<br />
return false;<br />
}<br />
<br />
</blockquote>Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com0tag:blogger.com,1999:blog-4807188970362638283.post-16904170677618696172010-05-10T08:44:00.000-07:002010-05-10T08:44:43.513-07:00Sleep the current thread using boostThe following code would sleep the current thread for 200 milliseconds<br />
<blockquote>boost::this_thread::sleep(boost::posix_time::milliseconds(200));<br />
</blockquote>Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com0tag:blogger.com,1999:blog-4807188970362638283.post-42169966962096979302010-05-06T07:16:00.000-07:002010-05-06T07:16:14.813-07:00Get current thread id when using Boost threads<blockquote>boost::this_thread::get_id()<br />
</blockquote>Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com0tag:blogger.com,1999:blog-4807188970362638283.post-66271029201237143472010-04-08T13:19:00.000-07:002010-04-08T13:34:57.623-07:00Change lower case letters to upper case in bashThis can be done using the tr command.<br />
<br />
For example:<br />
<blockquote>echo "change my case" | tr '[a-z]' '[A-Z]'<br />
</blockquote>Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com0tag:blogger.com,1999:blog-4807188970362638283.post-15910702202106864682010-03-23T12:46:00.000-07:002010-04-08T13:10:02.182-07:00Prepend string in bash using sedYou can do that using the following sed command<br /><br /><blockquote><br />sed 's/^/<string_to_preappend>/g'<br /></blockquote><br />The key here is the use of ^ Which matches the start of line.<br /><br />For example below is how you can prepend "foo" to a string "bar"<br /><br /><blockquote><br />echo "bar" | sed 's/^/foo-/g'<br /></blockquote>Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com3tag:blogger.com,1999:blog-4807188970362638283.post-62011303595036563172009-11-24T10:59:00.001-08:002009-11-24T10:59:45.213-08:00About the new kid in town, Chrome OSI know I don't need to explain what Chrome OS is, so I will come straight to my views about it.<br /><br />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.<br /><br />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.<br /><br />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.<br /><br />I think they should make the cloud feature optional and allow for local storage as well.Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com0tag:blogger.com,1999:blog-4807188970362638283.post-66488767843812157922009-03-15T17:41:00.000-07:002009-04-19T10:11:22.411-07:00Implementing a non-copyable class in C++<p><strong>Problem at hands</strong></p><p><strong>============</strong></p><p>In the simplest of scenario, we basically want to prevent something like this:</p><code><pre><span style="color:#0000FF;">class</span> A<br /><br />{<br /><br /><span style="color:#0000FF;">public</span>:<br /><br /> A(){}<br /><br /> ~A(){}<br /><br />};<br /><br /><span style="color:#0000FF;">void</span> foo(A a)<br />{<br />}<br /><br /><span style="color:#0000FF;">int</span> main(<span style="color:#0000FF;">int</span> argc,<span style="color:#0000FF;">char</span> *argv[])<br /><br />{<br /><br />A a1;<br /><br />A a2(a1); <span style="color:#C0C0C0;">// directly invoking the copy constructor, should be disallowed<br /></span><br />a2 = a1; <span style="color:#C0C0C0;">// creating copies via assignment operator, should be disallowed<br /></span><br />foo(a1); <span style="color:#C0C0C0;">// and passing copies as parameters to other functions, should be disallowed<br /></span><br /><br /><span style="color:#0000FF;">return</span> 0;<br /><br />}<br /></pre></code><br /><strong>Sol 1:Make the copy constructor and assignment operator private<br /></strong>==============================================<br />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:<br /><code><pre><span style="color:#0000FF;">class</span> A<br />{<br /><span style="color:#0000FF;">private</span>: <span style="color:#C0C0C0;">// copy constructor and assignment operator<br /></span> A(<span style="color:#0000FF;">const</span> A&){}<br /> A& <span style="color:#0000FF;">operator</span>=(<span style="color:#0000FF;">const</span> A&) {}<br /><span style="color:#0000FF;">public</span>:<br /><br /> A(){}<br /> ~A(){}<br /><br />};<br /><br /><span style="color:#0000FF;">void</span> foo(A a)<br />{<br />}<br /><br /><span style="color:#0000FF;">int</span> main(<span style="color:#0000FF;">int</span> argc,<span style="color:#0000FF;">char</span> *argv[])<br />{<br />A a1;<br />A a2(a1); <span style="color:#C0C0C0;">// error! Copy constructor is private<br /></span><br />A a3;<br />a3 = a1; <span style="color:#C0C0C0;">// error! assignment operator is private<br /></span><br />foo(a1); <span style="color:#C0C0C0;">// error! copies cannot be created since the copy constructor is private<br /></span><br /><span style="color:#0000FF;">return</span> 0;<br />}<br /></pre></code><br />All is good uptill now, but what about the following scenario?<br /><br /><pre>#include <iostream><br /><span style="color:#0000ff;">using</span> <span style="color:#0000ff;">namespace</span> std;<br /><br /><span style="color:#0000ff;">class</span> A<br />{<br /><span style="color:#0000ff;">private</span>: <span style="color:#c0c0c0;">// copy constructor and assignment operator<br /></span> A(<span style="color:#0000ff;">const</span> A&)<br /> {<br /> cout<<<span style="color:#ff0000;">"Copy constructor"</span><<endl;<br /> }<br /> A& <span style="color:#0000ff;">operator</span>=(<span style="color:#0000ff;">const</span> A&)<br /> {<br /> cout<<<span style="color:#ff0000;">"Assignment operator"</span><<endl;<br /> <span style="color:#0000ff;">return</span> *<span style="color:#0000ff;">this</span>;<br /> }<br /><span style="color:#0000ff;">public</span>:<br /> A(){}<br /> ~A(){}<br /><br /> <span style="color:#0000ff;">void</span> violate1()<br /> {<br /> A tmp(*<span style="color:#0000ff;">this</span>); <span style="color:#c0c0c0;">// copy constructor will get invoked<br /></span> }<br /><br /> <span style="color:#0000ff;">void</span> violate2()<br /> {<br /> A tmp;<br /> tmp = *<span style="color:#0000ff;">this</span>; <span style="color:#c0c0c0;">// assignment operator will get invoked<br /></span> }<br />};<br /><br /><span style="color:#0000ff;">int</span> main(<span style="color:#0000ff;">int</span> argc,<span style="color:#0000ff;">char</span> *argv[])<br />{<br />A a1;<br />a1.violate1(); <span style="color:#c0c0c0;">// gets invoked successfully<br /></span> a1.violate2(); <span style="color:#c0c0c0;">// gets invoked successfully<br /></span><br /><span style="color:#0000ff;">return</span> 0;<br />}<br /><br /></pre><br /><br />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 <b>Sol2</b><br /><br /><strong>Sol2: Do not implement the copy constructor and assignment operator<br /></strong>==================================================<br /><br />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:<br /><br /><code><pre>#include <iostream><br /><span style="color:#0000FF;">using</span> <span style="color:#0000FF;">namespace</span> std;<br /><br /><span style="color:#0000FF;">class</span> A<br />{<br /><span style="color:#0000FF;">private</span>:<br /> A(<span style="color:#0000FF;">const</span> A&); <span style="color:#C0C0C0;">// copy constructor declaration only<br /></span> A& <span style="color:#0000FF;">operator</span>=(<span style="color:#0000FF;">const</span> A&); <span style="color:#C0C0C0;">// assignment operator declaration only<br /></span> <span style="color:#0000FF;">public</span>:<br /> A(){}<br /> ~A(){}<br /><br /> <span style="color:#0000FF;">void</span> violate1()<br /> {<br /> A tmp(*<span style="color:#0000FF;">this</span>); <span style="color:#C0C0C0;">// copy constructor will get invoked<br /></span> }<br /><br /> <span style="color:#0000FF;">void</span> violate2()<br /> {<br /> A tmp;<br /> tmp = *<span style="color:#0000FF;">this</span>; <span style="color:#C0C0C0;">// assignment operator will get invoked<br /></span> }<br />};<br /><br /><span style="color:#0000FF;">int</span> main(<span style="color:#0000FF;">int</span> argc,<span style="color:#0000FF;">char</span> *argv[])<br />{<br />A a1;<br />a1.violate1(); <span style="color:#C0C0C0;">// would get compiled, however a linker error would be generated<br /></span> a1.violate2(); <span style="color:#C0C0C0;">// would get compiled, however a linker error would be generated<br /></span><br /><span style="color:#0000FF;">return</span> 0;<br />}<br /></pre></code><br />When I tried to compile it (using g++), I got the following linker errors<br /><blockquote><pre>/tmp/ccUfNEtK.o: In function `A::violate1()':<br />cpp_code.cpp:(.text._ZN1A8violate1Ev[A::violate1()]+0x14): undefined reference to `A::A(A const&)'<br />/tmp/ccUfNEtK.o: In function `A::violate2()':<br />cpp_code.cpp:(.text._ZN1A8violate2Ev[A::violate2()]+0x20): undefined reference to `A::operator=(A const&)'<br />collect2: ld returned 1 exit status</pre></blockquote>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. <b>Sol3</b> will do just that<br /><br /><span style="font-weight: bold;">Sol3: Use inheritance</span><br />================<br /><br />Create a class (lets call it <b>uncopyable</b>) with un-implemented but declared private copy<br />constructor and assignment operator. Inherit from this class, in order to make the derived class<br />uncopyable.<br /><br /><code><pre>#include <iostream><br /><span style="color:#0000FF;">using</span> <span style="color:#0000FF;">namespace</span> std;<br /><br /><span style="color:#0000FF;">class</span> Uncopyable<br />{<br /><span style="color:#0000FF;">private</span>:<br /> Uncopyable(<span style="color:#0000FF;">const</span> Uncopyable&); <span style="color:#C0C0C0;">// copy constructor declaration only<br /></span> Uncopyable& <span style="color:#0000FF;">operator</span>=(<span style="color:#0000FF;">const</span> Uncopyable&); <span style="color:#C0C0C0;">// assignment operator declaration only<br /></span> <span style="color:#0000FF;">public</span>:<br /> Uncopyable(){};<br /> ~Uncopyable(){};<br />};<br /><br /><span style="color:#0000FF;">class</span> A:<span style="color:#0000FF;">public</span> Uncopyable<br />{<br /><span style="color:#0000FF;">public</span>:<br /> A(){}<br /> ~A(){}<br /><br /> <span style="color:#0000FF;">void</span> violate1()<br /> {<br /> A tmp(*<span style="color:#0000FF;">this</span>); <span style="color:#C0C0C0;">// error! This cannot be invoked, since base class's copy constructor is private<br /></span> }<br /><br /> <span style="color:#0000FF;">void</span> violate2()<br /> {<br /> A tmp;<br /> tmp = *<span style="color:#0000FF;">this</span>; <span style="color:#C0C0C0;">// error! assignment operator cannot be invoked, since base class has defined it as private<br /></span> }<br />};<br /><br /><span style="color:#0000FF;">int</span> main(<span style="color:#0000FF;">int</span> argc,<span style="color:#0000FF;">char</span> *argv[])<br />{<br />A a1;<br /><br />A a2(a1); <span style="color:#C0C0C0;">// error. Copy constructor cannot be invoked<br /></span><br />a1.violate1(); <span style="color:#C0C0C0;">// error (See above)<br /></span> a2.viloate2(); <span style="color:#C0C0C0;">// error (See above)<br /></span><br /><br /><span style="color:#0000FF;">return</span> 0;<br />}<br /></pre></code><br />To sum it up,<b> uncopyable</b> prevents copying of its derived classes from all possible scenarios:<br /><br /><ul><li><span style="font-weight: bold;">Outside the class:</span> Since its copy constructor and assignment operator is private</li><li><span style="font-weight: bold;">Inside member functions of the derived class</span>: 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.</li><li><strong>When passed as parameters</strong><br /></li></ul><br /><strong>ACKNOWLEDGEMENTS<br /></strong><br />This approach is being taken from Scott Meyers "Effective C++".Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com0tag:blogger.com,1999:blog-4807188970362638283.post-89016075398804888172009-02-08T05:04:00.001-08:002009-02-08T06:58:28.553-08:00ac2html: Syntax highlight your blog posts<p>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 <strong>ac2html</strong> (any code to html).</p><p><strong>ac2html </strong>takes your code and highlights it using <strong>html </strong>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.</p><p><strong>Sample run</strong></p><p><strong>========</strong></p><p>Download ac2html from <a href="http://groups.google.com/group/sibtedocs/web/AnyCode2html.tar.gz">here</a>. The code is <b>platform independent</b> and should run on all platforms (but I have only tested it on linux x86)</p><p>Untar it</p><blockquote><p><code>tar -xvzf AnyCode2html.tar.gz</code></p></blockquote><p>Compile it</p><blockquote><code>cd AnyCode2html<p>make</code></blockquote><p>Note that you might need to edit the Makefile to suit your system. </p><p>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)</p><blockquote><code><pre>#include <iostream><br />using namespace std;<br /><br />int main(int argc, char* argv[])<br />{<br />// print a test message<br />cout<<endl<<"Hello world"<<endl;<br />return 0;<br />}</pre></code></blockquote><br /><p>Ugly isn't it? Now run ac2html</p><blockquote><code>./ac2html cpp_keywords cpp_code.cpp output.html</code></blockquote><p>Before we show the results lets analyze the arguments that we just passed. All the arguments that we passed are required by <b>ac2html</b>. </p><ul><li><b>cpp_keywords</b> is the keywords file which <b>ac2html</b> uses to identify keywords.</li><li><b>cpp_code.cpp</b> is the file that contains my c++ code</li><li><b>output.html</b> is the file which the results should be saved</li></ul><p>Now copy the the contents of <b>output.html</b> and paste it to the blogger's compose window. Publish your post and you will get this:<br /><blockquote><code><pre>#include <iostream><br /><span style="color:#0000FF;">using</span> <span style="color:#0000FF;">namespace</span> std;<br /><br /><span style="color:#0000FF;">int</span> main(<span style="color:#0000FF;">int</span> argc, <span style="color:#0000FF;">char</span>* argv[])<br />{<br /> <span style="color:#C0C0C0;">// print a test message<br /></span> cout<<endl<<<span style="color:#FF0000;">"Hello world"</span><<endl;<br /> <span style="color:#0000FF;">return</span> 0;<br />}</pre></code></blockquote><br />Beautiful, isn't it :-)<br /><p><strong>ACKNOWLEDGEMENTS</strong><br /></p><p><strong>================</strong></p><p>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.</p>Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com1tag:blogger.com,1999:blog-4807188970362638283.post-18825214059329210022009-01-16T23:23:00.000-08:002011-01-09T12:21:09.675-08:00A Perl SDL based gameJust finished writing a small Perl SDL based game. I call it invade. Its a conventional space invader type of game.<br />
Download it from <a href="http://sibtedocs.googlegroups.com/web/invade-dist.tar.gz?gda=IkSGQEQAAAC_dDAWiTf4XKfkL0dzq3VBA-lCaKBBqI4jSf2Ut20BRZHtx3dl8Mh4fmPDQiJahBVV6u9SiETdg0Q2ffAyHU-dzc4BZkLnSFWX59nr5BxGqA">here </a><br />
Untar it and voila!<br />
Before running it, make sure that you have SDL and SDL perl libraries installed.<br />
<br />
<br />
<b>Acknowledgements</b><br />
<br />
<br />
Thanks to my wife Sarah, for designing the background and for the excellent QA :)<br />
<br />
<br />
<object height="430" width="480"><param name="allowfullscreen" value="true"><param name="allowscriptaccess" value="always"><param name="movie" value="http://www.facebook.com/v/62397587999"><embed src="http://www.facebook.com/v/62397587999" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="430" height="430"></embed></object>Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com4tag:blogger.com,1999:blog-4807188970362638283.post-23824676809694946402008-10-17T08:23:00.000-07:002008-10-17T18:33:37.711-07:00C++ mutable, finally makes senseAfter being a long time basher of C++'s <code>mutable</code> type specifiers, I have finally come to embrace this controversial concept (but its still in beta state :-)<br /><br />This happened while implementing the <code>FixSession</code> class for my <b>FIXTransit</b> project. I was passing <code>FixSession</code> 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 <code>Session::createHeader()</code> whose job is to simply return a FIX message Header for that Session, would result in incrementing the <code>SessionState::seqNum</code> property.<br /><br />So now that the rationale behind the divine concept had occured to me, I very proudly declared the <code>SessionState</code> data member as <code>mutable</code> and continued to pass the Session objects as logically constant references :-)<br /><br />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.Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com0tag:blogger.com,1999:blog-4807188970362638283.post-37494902384703634012008-05-30T15:41:00.000-07:002008-05-30T23:08:26.405-07:00Touch WindowsAs you might have heard by now, Microsoft is introducing touch screen technology in Windows 7; Microsoft's next operating system release<br /><br /><a href="http://news.bbc.co.uk/2/hi/technology/7422924.stm">http://news.bbc.co.uk/2/hi/technology/7422924.stm</a><br /><br />Well this is an interesting concept and Bill Gates being a great envisioner, is I think right in predicting that in future the ways we interact with our PCs would change.I also expect(and hope) this release of windows to get a better reception than Vista, which was a TOTAL flop.<br /><br />Also don't get fooled by Ballmer's comment of selling 150 million copies of Windows Vista. This number does'nt show that it was successfull or people liked it. Those number just show that how powerfull Microsoft is and on the contrary how helpless the rest of the world is. Vista sucked; everyone knew it but still they had no other option but to buy and use it. The reason being that all new laptops were getting shipped with Vista. You actually had to customize your laptop, pay extra and even wait more to get Windows XP installed. Unfortunately even if Windows 7 is crapy, people would still buy, use and eventually like it. The reason being that In the PC market there is simply no alternative to Windows (Apple is too expensive and Linux is too geeky)<br /><br />Fortunately, being a Gnu Linux and open source software user I don't fall under this helpless category. Whenever I buy a laptop the first thing I do is that I repartition, remove Windows install Linux and other free software.Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com2tag:blogger.com,1999:blog-4807188970362638283.post-80518447379219715812008-05-20T16:47:00.000-07:002008-05-24T07:21:09.221-07:00A simple but EFFECTIVE debugging technique<span style=";font-family:arial;font-size:100%;" ><span style="font-weight: bold;">Debugging with printf:</span><br /><span style="font-weight: bold;">================<br /><br /></span>Even with all the sophisticated debugging tools out there, as developers I guess all of us will agree that <code>printf</code> still remains one of the most convenient and heavily used debugging aids. Whether you are writting new code or trying to debug existing one, there is nothing like good old <code>printf :) </code><br /></span><span style=";font-family:arial;font-size:100%;" ><span style="font-weight: bold;"><br />printf not acceptable in production:<br />=========================<br /></span></span><br /><span style=";font-family:arial;font-size:100%;" >Having said that, we all would agree that nobody would want printfs or fprintf's in their production code. A few reasons for that being:<br /><span style="font-weight: bold;"></span><br />1. printf messages clutters the stdout and considering that these messages are coming straight from developers hearts, you would DEFINITELY not want to expose them to your clients<br /><br />2. You must be thinking, "well I can use fprintf instead and redirect the debug messages to a file and add a secret command line parameter to run the program in debug mode". There are still two issues with this;<br />(i) Your source code will be cluttered with alot of if statements. Even worst the program will have to perform the <code>if (mode == DEBUG)</code> type of tests where ever you have printed debug message(s). If your application is large and runs in real time these tests could prove to be a considerable performance bottleneck.<br />(ii) (i) is bad but it isn't the worst part. The worst is that the if statements and their corresponding bodies will contribute to the binary size, thus killing the performance of your program (see 3 for details)<br /><br />3. Debug messages will increase the size of the code section part of the binary, which means that they would considerably effect performance.<br /><br /><span style="font-weight: bold;">Preprocessor directives to the rescue:</span><br /><span style="font-weight: bold;">===========================</span><br /><br /></span>Thus the question is <b>"how do I add debug statements to my program, but at the same time not add debug statements to my program :)"</b>. Well the answer to this dilemma is, the use of preprocessor directives (<code>#if</code>). Add the debugging code under a <b>preprocessor directive</b> and compile it conditionally.<br /><blockquote><code><br />#if DEBUG<br />/* print crazy debug messages or perform some other debugging operations */<br /><br />#endif<br /></code></blockquote><br />Now whenever I need debug messages I can turn them on by compiling my program with the <code>DEBUG</code> macro defined.<br /><br />The following small program demonstrates the use of this technique:<br /><blockquote><code><br />3<br />4 #define DEBUG 1<br />5<br />6 int main(int argc, char *argv[])<br />7 {<br />8 printf("Hello World\n");<br />9 #if DEBUG<br />10 printf("Debugging is enabled\n");<br />11 #endif<br />12 return 0;<br />13 }<br /></code></blockquote><br /><br />Note how we are defining <code>DEBUG</code> at <code>line 4</code>. Also note how we are using it in a preprocessor directive at <code>line 9</code><br /><blockquote><code><br />9 #if DEBUG<br />10 printf("Debugging is enabled\n");<br />11 #endif<br /></code></blockquote><br /><br />Compiling and running the program would yield the following output<br /><blockquote><code><br />Hello World<br />Debugging is enabled<br /></code></blockquote><br /><br />Now undefine <code>DEBUG</code> by updating <code>line 4</code> as follows<br /><blockquote><code><br />4 #define DEBUG 0<br /></code></blockquote><br /><br />Compile and execute the program. You should get the following output<br /><blockquote><code><br />Hello World<br /></code></blockquote><br /><br />Not only did the execution of what ever was between <code>#if DEBUG ... #endif</code> was avoided, but it was not even included in the compiled binary. Hence we achieved our goal, i.e. <bo>"we added debug statements to our program, but at the same time did not add debug statements" :)<br /><br />There is a much elegant want to define the <code>DEBUG</code> marco; Remove line 4 of the above program and compile it with gcc's -D option:<br /></bo><blockquote><code><br />gcc -o test <b>-DDEBUG=1</b> test.c<br /></code></blockquote><br /><br />You can now include the debug messages in your production level code and commit it to your SCM's release branch(however DO make sure to write it under preprocess directives).<br /><br />Please note that I am in no way denying the importance of debuggers, they are a VERY important component of developing stable software, however there are scenarios where outputing messages are more convenient and that is where this approach comes handy.Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com0tag:blogger.com,1999:blog-4807188970362638283.post-81907539578024431092008-05-18T11:10:00.000-07:002008-05-18T14:31:32.409-07:00Simple Makefile TutorialThe purpose of this tutorial is to create and understand a simple Makefile without the use of autotools (autoconf, automake etc).<br /><br /><span style="font-weight: bold;">Make Utility<br /></span><span style="font-weight: bold;">=========<br /><br /></span>From the make manpage <span style="font-style: italic;">"The purpose of the make utility is to determine automatically which pieces of a large program need to be recompiled, and issue </span><span style="font-style: italic;">the commands to recompile them"<br /></span><span style="font-style: italic;"><br /></span>Normally, you would use the make utility when your program's source code includes more than one file. By virtue of this utility, if you make changes to a particular module, you will not be required to recompile the whole program. It will recompile only the changed modules and the modules that dependent on them.<br /><br /><span style="font-weight: bold;">The Makefile<br /></span><span style="font-weight: bold;">==========<br /><br /></span>The <code>make</code> utility takes a <span style="font-style: italic;">Makefile</span> or <span style="font-style: italic;">description file </span>as input. In this file you specify the modules to build and their corresponding dependencies.<span style="font-style: italic;"><br /><br /></span>When invoked, the <code>make</code> utility expects a file named <span style="font-style: italic;">Makefile </span>to be present<span style="font-style: italic;"> </span>in the same directory and uses it as its description file.<br /><br /><span style="font-weight: bold;">Makefile Format<br />============<br /><br /></span>The dependencies in the <span style="font-style: italic;">Makefile </span>are specified using the following syntax:<br /><blockquote><code><br />target: components<br />TAB rule<br /></code></blockquote><br /><br />In the first line you specify the <span style="font-style: italic;">target</span> name and its <span style="font-style: italic;">dependencies</span> whereas the second line comprises of the <span style="font-style: italic;">rule(s) </span>to build the target. Note that in the rules line the <code>TAB</code> is mandatory, otherwise <code>make</code> will not be able to parse correctly and an error will be generated.<br /><br /><span style="font-weight: bold;">Invoking make<br />==========<br /><br /></span><code>make</code> is invoked simply by entering <code>make</code> on the command line. It should be invoked from the same directory where you have defined your Makefile.<br /><br /><blockquote><code>make</code></blockquote><br />By default <code>make</code> will start building the first target defined in your Makefile. However you can also instruct <code>make</code> to build a specific target via the following syntax:<br /><blockquote><code><br />make <span style="font-style: italic;">target-name</span><br /></code></blockquote><br /><br /><span style="font-weight: bold;"></span><span style="font-weight: bold;">Example<br />======<br /><br /></span>Lets jump straight to an example and implement what we have learned. Below is a simple Makefile. It builds a program named test:<br /><blockquote><code><br />test: test.o<br /> gcc -o test test.o<br /><br />test.o: test.c<br /> gcc -c test.c<br /></code></blockquote><br /><span style="font-weight: bold;"></span><span style="font-weight: bold;"></span><br />The target <code>test</code> depends on <code>test.o</code> and it is built by issuing the command <code>gcc -o test test.o</code>. If <code>test.o</code> is modified then make will build <code>test</code> again. Similarly <code>test.o</code> is itself another target which depends on <code>test.c</code>. If <code>test.c</code> is modified then <code>test.o</code> is rebuilt using its corresponding rule.<br /><br />In the above scenario we were only dealing with one source file. We would have been good by simply using gcc as well. Lets take a slightly more complicated example:<br /><br />Suppose you are implementing a <code>stack</code> and you want to write a program; <code>stacktest</code> that tests this implementation. The stack is being implemented by wrapping around another container called <code>vector</code>.<br /><br />Hence, <code>stacktest</code> depends on <code>stack</code> and <code>stack</code> depends on <code>vector</code>. The corresponding Makefile can be written as follows:<br /><br /><blockquote><code><br />stacktest: stacktest.o stack.o vector.o<br /> gcc -o stacktest stacktest.o stack.o vector.o <br /><br />stacktest.o: stacktest.c stack.h<br /> gcc -c stacktest.c <br /><br />stack.o: stack.c stack.h vector.h<br /> gcc -c stack.c<br /><br />vector.o: vector.c vector.h<br /> gcc -c vector.c<br /></code></blockquote><br /><span style="font-weight: bold;">Adding the clean target<br />=================<br /><br /><span style="font-weight: bold;"></span></span>Traditionally each Makefile comes with a <code>clean</code> target. The purpose of this target is to remove all the object files and targets so that the next time you invoke <code>make</code> everything is re-compiled. Lets add a <code>clean</clean> target in our <code>stacktest</code> example:<br /><blockquote><code><br />clean:<br /> rm -f *.o<br /> rm -f stacktest<br /></code></blockquote><br />As you can see, in our <code>clean</code> target we are removing all the object files and the <code>stacktest</code> binary. This target can be invoked as follows:<br /><blockquote></code><br />make clean<br /></code></blockquote><br /><br />Hope you found this helpfull!<span style="font-weight: bold;"><br /></span>Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com2tag:blogger.com,1999:blog-4807188970362638283.post-58314779361139373952008-05-14T12:30:00.000-07:002008-05-14T15:46:45.760-07:00Function overloading In C<span style="font-weight: bold;">Background:<br />=========<br /><br /></span><span>A few days back, me and my friend were discussing if it would be possible to implement function overloading in C. We discussed a few approaches but none seemed to provide a complete solution. So as you would expect from a developer, as soon as I came back home, I started working on it. Consequently after 2 days worth of work I came up with <a href="http://sibtedocs.googlegroups.com/web/coverload.h?gda=3GbcojwAAADMugtGhzrCGYXSnwoocpjgeZkjNj8zagacdOSjruo3V2G1qiJ7UbTIup-M2XPURDSvh9GcHY23x3jU-UWF6ByY">coverload.h</a> . Just include this file in your code and start overloading in C :)<br /><blockquote><code>#include "coverload.h"</code></blockquote><br />By using the macros defined in this file you can imitate function overloading. I would not say that its a 100% solution, but <b><i>very</i></b> close.<br /></span><span style="font-weight: bold;"><br />Basic Idea:<br />========<br /><br /></span><span>As in C++, we rely on name decoration (or name mangling) to mangle the names of functions. For example:<br /><br /><code>void foo(int x,float y)</code> will be name mangled to something like <code>void <span style="font-weight: bold;">foo</span>int_float(int x,float y)</code><br /></span><span style="font-weight: bold;"><br /></span><span style="font-weight: bold;">Function Declaration:<br />===============<br /></span><br />All functions should be declared using the <code>DECL_FUNC</code> macro:<br /><blockquote><code><br />DECL_FUNC(<span style="font-style: italic;">function_name</span>, <span style="font-style: italic;">return_type</span>, <span style="font-style: italic;">parameters</span>)<br /></code></blockquote><br />Where:<br /><blockquote><br /> <span style="font-style: italic;">function_name</span>: name of the function<br /> <span style="font-style: italic;">return_type</span>: is the return type of the function (DAH!!!)<br /> <span style="font-style: italic;">parameters</span>: A comma seperated list of parameters. Each parameter is specified via the PARAM(type,name) macro<br /></blockquote><br /><span style="font-weight: bold;">For Example:</span><br />Here is how you can declare a function named foo, having return type int and taking double and long parameters:<br /><blockquote><code><br /> DECL_FUNC(foo,int,PARAM(double,dparam),PARAM(long,lparam))<br /> {<br /> .....<br /> }</code></blockquote><br />And this is how you will overload the above function:<br /><blockquote><code><br /> DECL_FUNC(foo,int,PARAM(int,iparam))<br /> {<br /> ......<br /> }</code></blockquote><br /><br /><span style="font-weight: bold;"> Function Invocation:</span><span style="font-weight: bold;"><br />===============</span><br /><br />To invoke a function, the <code> INVOKE_FUNC </code> macro should be used:<br /><blockquote><code><br />INVOKE_FUNC(FUNCNAME_FROM_TYPES(<span style="font-style: italic;">func_name</span><function_name>,<span style="font-style: italic;">arg_types</span>),<arg_types><span style="font-style: italic;"></span><span style="font-style: italic;">arg1,arg2,arg3,</span>...)<br /></arg_types></function_name></code></blockquote><br />Where:<blockquote><br /> <function_name><span style="font-style: italic;">func_name</span>: name of the function<br /><span style="font-style: italic;">arg_types:</span> a command seperated list of arg types<br /> <arg_types><span style="font-style: italic;">arg1,arg2... </span>: a comma seperated list of arg values</arg_types></function_name></blockquote><br /><br /><br />Note that we need to invoke the <code>FUNCNAME_FROM_TYPES</code> arguments to construct the name of the<br />actual function to invoke.<br /><br /><span style="font-weight: bold;">For Example:</span><br /> In order to invoke a function named foo having a double and long parameter do the following:<br /><blockquote><code><br /> INVOKE_FUNC(FUNCNAME_FROM_TYPES(foo,double,long),4.5,20L)<br /></code></blockquote><function_name><arg_types><function_name><arg_types><br /><br />Below we invoke the overloaded version of foo, that takes one int parameter:<br /></arg_types></function_name></arg_types></function_name><blockquote><code><br /><function_name><arg_types><function_name><arg_types> INVOKE_FUNC(FUNCNAME_FROM_TYPES(foo,int),4)<br /></arg_types></function_name></arg_types></function_name></code></blockquote><br /><br /><span style="font-weight: bold;">Limitations:<br />========<br /></span> <ul><li>I have'nt done any real testing for now (will do it at my earliest and publish the bug fixes)</li><li>Right now this can only work with 5 function parameters. However as you can see from the macro definitions, adding support for more parameters is very easy (mere copy paste) now. Also I am currently working on a script that will auto-generate the <code><b>coverload.h</b></code> file. It will be published as soon as it gets completed.</li><li>I have'nt tested the code with pointers. So for now you cannot use pointers with "*". However if you really want to use pointers, there is a workaround; If you typedef your pointers , you should be fine.<br /></li></ul><span style="font-weight: bold;">Examples:<br /><br /></span>You can find some usage examples in <a href="http://sibtedocs.googlegroups.com/web/overload.c?gda=Hw7mTjsAAADMugtGhzrCGYXSnwoocpjgFxHxE6Q5sfqL9cH5w47l3mG1qiJ7UbTIup-M2XPURDQvQIFLeWrVyIfGJ19GdP49">overload.c</a><br /><br /><span style="font-weight: bold;"></span>Syed Sibte Shahhttp://www.blogger.com/profile/14618039405658483594noreply@blogger.com2