Standard String Library functions in string.h in C

FUNCTION USE
strlen Finds the length of the string.
strlwr Converts string to lower case.
strupr Converts string to upper case.
strcat Appends one string at end of another.
strncat Appends first n characters of a string at end of another.
strcpy Copies a string to another.
strncpy Copies first n characters of a string into another.
strcmp Compares two strings
strncmp Compares first n characters of two strings.
strcmpi Compares two strings without regards to case.
stricmp Compares two strings without regards to case.
strnicmp Compares first n characters of a string without regards to case.
strdup Duplicates a string.
strchr Finds first occurrence of a given character in a string.
strrchr Finds last occurrence of a given character in a string.
strstr Finds first occurrence of a given string in a string.
strset Sets all characters of a string to given character.

 

strnset Sets first n characters of a string to given character.
strrev Reverses the string.
   

Adding Functions to Library in C

This might be known to many but  I recently tried it and got it working.

Suppose you make a function in C(for example here we take a simple function which calculates the square of a number).

Its a fairly simple one and I want to add in in a library thus making its reuse possible.

(Please note I am taking a very simple scenario because the actual program is not the point of focus here, but the procedure is)

The function:

int square(int n)

{

return n*n;

}

Compile the file:

Compile the file in which you have written this function. Lets call it “sqr.c”. A new file called “sqr.obj” is created contained compiled code.

Add the function to Library:

Add the function to the library “maths.lib” using the following command.

C:\>tlib maths.lib + c:\sqr.obj

Please note the paths. Your paths may be different than mine!

here, maths.lib is the library and + is a switch which indicates that we want to add a new function to the library and the object file is at the given path.

Header file:

Add the prototype of square() function in a header file (lets say “square.h”) This file should be included in the program where we want to use the function. we can use it as:

#include “c:\square.h”

Thats all !

Use the function and enjoy !!

Hope it helps.

Paperclip Resizing options…

Paperclip allows us to specify image dimensions in the  model iteself. This makes life very simple in cases where we only want image resizing.

Here is a list of things we can do to get the best out of it….

Please note : it applies to image magick also.

The basic specification is <width>x<height> in pixels, optionally followed by a modifier. In some cases you can omit either width or height.

    • 256×256
      This specifies the Maximum dimensions for the image, while preserving the aspect ratio of the image. So if your image were actually 512×256 it would be resized to 256×128 in order to fit inside the specified size.
    • 256×256!
      This specifies the Exact image size, so a 512×256 image would be changed into 256×256, losing the aspect ratio.
    • 256x
      This specifies the desired width of the target image, leaving the height to be set so as to preserve the aspect ratio.

 

    • x256
      This specifies the desired height of the target image, while preserving the aspect ratio.
    • 256×256^
      This specifies the Minimum size for the target image, so the resized image may be larger than these dimensions.
    • 256×256>
      This specifies that the image will be resized only if it is Larger than the dimensions.
    • 256×256<
      This specifies that the image will be resized only if it is Smaller than the dimensions.

Deploying with Pasenger and apache

Recently had come accross the task of deploying a rails application with apache and passenger.

I did everything as prescribed in the way I used to but for some reasons I was shown the directory listing of the application instead of application being executed.

The change I had taken from my conventional way of giving the paths to using symbolic links.

Finally i figured out the solution as an issue with passenger and the way it treats symlinks:

 

here is what you need to put between the <directory> tags to get stuff working:

 

PassengerResolveSymlinksInDocumentRoot on

 

 

Hope it helps !

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.

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.

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