Conditional Compilation in C

C provides a very useful and interesting feature called “conditional Compilation

the general structure for doing it:

#ifdef macroname

statement 1;
statement 2;
statement 3;
statement 4;
#endif

here if macroname has been #defined then the 4 statements will be compiled otherwise not.

There are 3 scenarios where I see application of this feature:

-> To “comment out” obselete lines of code. It may happen that a program undergoes a change for some reasons.
Here we may not want to delete the old code but add new code.
In this scenario, we can include the old and new code in ifdef block and control the compilation using a single #define statement.

eg:

void main()
{

#ifdef OLD
statement 1;
statement 2;
#else
statement 3;
statement 4;

#endif

}

here , if we define thee macro “OLD” then old code is compiled otherwise, the new code is compiled.

->The other (more sophisticated) use of this feature can be to make the programs more portable.
Thus, we can make use of this feature and make a program work on two completely different computers.

eg:

void main()
{
#ifdef INTEL
code for intel PC
#else
code for motorola PC
#endif
code common for both.

}

This code is pretty self explanatory.
If you want your code to run on INTEL we just need to define a macro.

->A third possible scenario is defining of custom functions.
Lets say we define a function called “my_sample_function()” in a file “file1.h”
Also, “file1.h” is included in “file2.h

If we include both the files, compiler will throw an error saying “multiple declaration of my_sample_function()

To overcome this problem, we can use the following way:

/* file1.h */

#ifndef _file
#define _file
my_sample_function()
{
/*some code */
}
#endif

Here when file1.h gets included first time, compiler knows that macro _file is not defined. Thus it gets defined and the rest of code is compiled.
Next time, since the macro stands defines, the function does not get compiled and thus there is no error.

NOTE: #ifndef is exactly opposite of #ifdef

 

Conditional compilation can also be achieved using #if ,#else #endif statements.

#if is used to evaluate whether a expression evaluated to nonzero value or not.

it is used in the same way as above:

eg:

 

void main(){

if TEST<=5

statment 1;

#else

statement 2;

#endif

}

Ways to include a file in C

There are two ways of including a file in C using #include statement:

#include “filename”

This command would look for the file in the current directory as well as the specified list of directories as mentioned in the include search path that might have been setup.

 

#include <filename>

This command would look for the file on the specified list of directories only.

 

 

 

Why not to use Strings in Cucumber

Cucumber allows you to define step definitions using strings instead of regular expressions.

This might seem simpler at first, but it has other problems, which I’ll illustrate with an example.

Here is a step definition that uses a plain string.

Given “I have 100 in my Account” do

end

We couldn’t write $100 here, because the $ has a special meaning when you define step definitions with strings.

Any $ signs—including any letter or number following it—will be interpreted as an argument, .

This step definition uses an argument:

Given “I have $amount in my Account” do |amount|

 

end

This step definition would match both of the following Gherkin steps:

Given I have 100 in my Account

Given I have $100 in my Account

 

In the first case, the Step Definition’s amount argument would have the value “100”. In the second case it would have the value “$100”. If our Step Definition expects a string with only digits, this can be problematic. We have no way to enforce a consistent way to write Gherkin steps, and the step definitions have to anticipate many kinds of input.

 

This is why using strings instead of regular expressions is not as advantageous as you might think. They give far less control over what gets matched and what arguments a step definition can receive.

HappyNess !!

Happy Moment: you solve your first sum of calculus without copying !
Next Moment :Teacher says this was very easy..any1 can do it.

Happy Moment: you see the word “passed” while seeing your result.
Next Moment: Dad says…..you scored very less….

Happy Moment: you finally complete education.
Next Moment: you are asked questions about jobs !

Happy Moment: you get your first offer letter.
Next moment: parents say your cousin got 1 lac more than you.

Happy Moment : you cook first time.
Next Moment : you are told “Tune kitchen ka kya haal kar diya ??”

Happy Moment: some1 says I love you to you.
Next Moment : He/she puts conditions for you. !!

Happy Moment : you bring sweets from your first salary.
Next Moment: they say taste is not good!

Happy Moment: You get your first visiting card.
Next Moment: No one is interested except you.

Happy Moment : you complete your project !

Next Moment: Tester says “It breaks !”

 

Happy Moment: you get in a running train!

Next Moment: you see there is no place to sit.!!
Happy Moment: you see your kids face for the first time.
Next moment: hospital presents you with a bill !

Happy moment: you finally leave the world.
Next Moment: people fight over your money in front of your dead body !

Happyness my friends never last long !
Its the memories of these happy moments that we must cherish !!

Installing and setting up GIT

INSTALLING GIT
Installing from Source:
In short, on a Unix-based system, you can download the Git source code from the Git Download Page, and then run
something along the lines of :

$ make prefix=/usr all ;# as yourself
$ make prefix=/usr install ;# as root

You will need the expat, curl, zlib, and openssl libraries installed – though with the possible exception of expat, these will normally already be there.

Linux
If you are running Linux, you can likely install Git easily via your native package management system:

$ yum install git-core
$ apt-get install git-core

Mac 10.4:
In both Mac 10.4 and 10.5, you can install Git via MacPorts, if you have that installed. If not, you can install it from here.
Once MacPorts is installed, all you should have to do is:

$ sudo port install git-core

If you prefer to install from source, these articles may be helpful:
Article: Installing Git on Tiger
Article: Installing Git and git-svn on Tiger from source

Mac 10.5
With Leopard, you can also install via MacPorts, but here you have the additional option of using a nice installer, which you can download from here: Git OSX Installer
If you prefer to install it from source, these guides may be particularly helpful to you :
Article: Installing Git on OSX Leopard
Article: Installing Git on OS 10.5

SETUP AND INITIALIZATION
Git Config
The first thing you’re going to want to do is set up your name and email address for Git to use to sign your commits.

$ git config –global user.name “Rushabh hathi”
$ git config –global user.email “rushabhhathi@gmail.com”

That will set up a file in your home directory which may be used by any of your projects. By default that file is ~/.gitconfig
and the contents will look like this:
[user]
name = Rushabh Hathi
email = rushabhhathi@gmail.com

If you want to override those values for a specific project (to use a work email address, for example), you can run the git config command without the –global option while in that project. This will add a [user] section like the one shown above to the .git/config file in your project’s root directory.

Resizing the image without chaning its dimensions using paperclip

Hi,

In a recent application, the need of the hour was to reduce the file size of an image without changing its dimensions.

Now, this can be acheived by reducing the quality of the image.

ImageMagick library has a “convvert” command which does exactly the same..

for eg(on the terrminal) : convert image1.jpg -quality 10 image2.jpg

image1.jpg—>original image

image2.jpg—->final image

 

According to the paperclip wiki there is a :quality parameter which is allowed.

I tried that but for some reasons , I was not able to get it running.

I also tried using :convert_options but it did not work too.

 

Thus , I wrote my own processor and got the stuff working. Here is the model and processor code :

#######Model code###########

class Passet < ActiveRecord::Base
attr_accessible :caption, :markup, :media_passet, :pcontent_id
has_attached_file :media_passet,
:styles => {
: original => {
:geometry => “256×256<“,
:processors => [:qresize]
}
}
belongs_to :pcontent
end

 

###################Processor code########################

make a file called qresize.rb in #{Rails.root}/lib/papaerclip_processors

The path is mentioned because this path is loaded automatically….

module Paperclip
# handles compression of image by reducing its quality
class Qresize < Processor
def initialize file, options = {}, attachment = nil
super
@format = File.extname(@file.path)
@basename = File.basename(@file.path, @format)
end
def make
src = @file
dst = Tempfile.new([@basename, @format])
dst.binmode
begin
parameters = []
parameters << “:source”
parameters << “-quality 10”
parameters << “:dest”
parameters = parameters.flatten.compact.join(” “).strip.squeeze(” “)
success = Paperclip.run(“convert”, parameters, :source => “#{File.expand_path(src.path)}[0]”, :dest => File.expand_path(dst.path))
rescue PaperclipCommandLineError => e
raise PaperclipError, “There was an error during the Size Reduction  for #{@basename}” if @whiny
end
dst
end
end
end

 

This is a general code for image resizing and probably at this stage, we may not require to write a processor.

But the advantage of doing this is that we can extend this to do a lot more as and when requirement comes.

Also, we can handle our video and audio processing by this easily.

The other option is to pass a block(lambda or Proc) to :styles hash but I personally feel this is a much cleaner approach.

 

Hope it helps.

PreProcessing in Paperclip

Hi.

If at all you want to do any kind of prepreocessing on attachments using paperclip then here is a simple(not wen i did it for the first time…) way of getting a TempFile object and doing whatever you want.

Here I set the size of image (actually reduce the original size into half)

 

class Passet < ActiveRecord::Base
attr_accessible :caption, :markup, :media_passet, :pcontent_id
has_attached_file :media_passet,
:styles => {

: original => {
:geometry => Proc.new { |instance| instance.set_size },
:quality => 10
}
}

belongs_to :pcontent

def set_size

ori_img =Paperclip::Geometry.from_file(self.media_passet.queued_for_write[:original].path)

“#{ori_img.width/2}x#{ori_img.height/2}”

end
end

Here I reduce the dimensions if the image and reduce the quality too.

This way, I can save a 1.1MB image as 39.2KB image,

It it what I required,and I hope this will give you a hint about your own application.

 

Hope it helps.

PS: there are many other(mayb elegant solutions) which can be found on paperclip github page whose link I have posted above.

Installing sun (oracle) jdk in precise pangolin..

Finally, after doing many things unsuccessfully,

this method works for me:

sudo rm /var/lib/dpkg/info/oracle-java7-installer* 
sudo apt-get purge oracle-java7-installer* 
sudo rm /etc/apt/sources.list.d/*java* 
 sudo apt-get update 
sudo add-apt-repository ppa:webupd8team/java 
 sudo apt-get update 
sudo apt-get install oracle-java7-installer

The new Fingerprinting technique in Rails 3.1

In the new rails 3.1, I heard about the new fingerprinting technique used by them.Looking at the concept I mist say that its hats off the the core team for developing such a great concept.

Here is a quick but thorough overview :

Fingerprinting is a technique whereby the filenames of content that is static or infrequently updated is altered to be unique to the content contained in the file.

When a filename is unique and based on its content, HTTP headers can be set to encourage caches everywhere (at ISPs, in browsers) to keep their own copy of the content. When the content is updated, the fingerprint will change and the remote clients will request the new file. This is generally known as cachebusting.

The most effective technique is to insert a hash of the content into the name, usually at the end. For example a CSS file global.css is hashed and the filename is updated to incorporate the hash.

global.css => global-908e25f4bf641868d8683022a5b62f54.css

This is the strategy adopted by the Rails asset pipeline.

Rails’ old strategy was to append a query string to every asset linked with a built-in helper. In the source the generated code looked like this:

/stylesheets/global.css?1309495796

 

This has several disadvantages:

  1. Not all caches will cache content with a query string
    Steve Souders recommends, “…avoiding a querystring for cacheable resources”. He found that in these case 5-20% of requests will not be cached.
  2. The file name can change between nodes in multi-server environments.
    The query string in Rails is based on the modification time of the files. When assets are deployed to a cluster, there is no guarantee that the timestamps will be the same, resulting in different values being used depending on which server handles the request.

The other problem is that when static assets are deployed with each new release of code, the mtime of all these files changes, forcing all remote clients to fetch them again, even when the content of those assets has not changed.

Fingerprinting avoids all these problems by ensuring filenames are consistent based on their content.

Fingerprinting is enabled by default for production and disabled for all the others environments. You can enable or disable it in your configuration through the config.assets.digest option.

 

The key differences in W3C event model and Microsoft event model

Recently I came across the key differences in the w3c event model and the event model followed by microsoft( its for the IE ghost which haunts many !!)

I am listing the major differences here :

Points W3C event Model Microsoft event model
Nomenclature It is called the W3C event model It is called JScript event model
API used : object.addEventListener(event,function,useCapture) object.attachEvent(event,function)
The this object this object points to the object
to which event is attached to
this points to the window object
event object It allows event object to be passedas first argument of the listener functionthus we can do $event.preventDefault(); It provides a window.eventobject.Thus to do the same thing you have to setthe returnValue property of window.event object to false
Scope of event object The event object is not global The event object is always global
Standardized methods for event object the event object provides many standardized methods the event object does not provide the same standardized methods
Compatibility !! It is supported by all browsers Supported by microsoft browsers only !