using config files in C

Requirement is to move the data out of C programs and let the program use a config file for the data. say we have a file called data.ini.

$ cat data.ini
position:{
x = 50;
y = 100;
}

$ cat prog.c
#include <libconfig.h>
int main(){
  long int x = 0;
  config_t* config;

  config_init(config);
  // read config file
  config_read_file(config, "data.ini");

  // get the value
  if( config_lookup_int(config, "position.x", &x) == CONFIG_FALSE ){
    printf("failed to lookup - position.x\n");
    return 1;
  }

  printf("x = %d\n", (int)x);
  return 0;
}

Compile the program using the option “-lconfig”

$ gcc -lconfig prog.c

You may need libconfig-dev,
$ sudo apt-get install libconfig-dev

Reference:
libconfig manual
Configuration file (wikipedia)

Advertisements

gnome-do plugin [part 1]: create an action

I started trying out gnome-do when I read an article about the upcoming ontology based tools (gnome-do and ubiquity) in Linux for you. There are many plug-ins available for gnome-do (official and community). I am using Debian(lenny) and had a hard time in setting it up. Setting up do from source was easy, but setting up the plug-ins from source was hard. Do plug-ins (0.8.3) depends on banshee (>=1.4.3), but debian lenny had a lesser version. Then I tried to set up banshee from source which again asked for gstreamer-* > 0.10.3 which was not available in Lenny. Moreover the first step for building banshee in Lenny has failed (apt-get build-dep banshee). Finally, I migrated to debian testing and got gnome-do and its plug-ins from debian repository without any problem. Its time to start our own plug-in, before proceeding to this step, I tried to build a plug-in( EOG-Slideshow ) that is available already in launchpad. All you have to do is,

1. run gnome-do from terminal

2. open the project file in monodevelop

3. build it from Monodevelop.

4. Now, open the plug-ins list by clicking on do in system tray and selecting the preferences. The list of plug-ins available are listed here.

5. We have to drag the dll (which we had compiled just now) and drop it in.

6. Terminal should show the status that the plug-in is getting installed. once it is installed then we shall enable it and start using.

Now, lets start writing the plug-in, the simplest plug-in that I found on net is Google-Search. There are few changes required (we need to include .addin.xml for the plug-in and we should inherit Act etc..).

HelloWorld.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using Do.Universe;
namespace HelloWorld
{
class HelloWorldAction : Act
{
public override string Name {
get { return "HelloWorld"; }
}
public override string Description {
get { return "Say Hello World."; }
}
public override string Icon {
get { return "search"; }
}
public override IEnumerable SupportedItemTypes {
get { return new Type[] { typeof (ITextItem) }; }
}
public override IEnumerable Perform (IEnumerable items, IEnumerable modItems) {
string query = (items.First() as ITextItem).Text;
System.Diagnostics.Process.Start("/usr/bin/xmessage", " -center -default okay hello, " + query);
return null;
}
}
}

HelloWorld.addin.xml:

<Addin
id="HelloWorld"
namespace= "Do"
version="0.1"
name="HelloWorld"
description="Say Hello World."
author="SatheeshKumar Mohan"
category="Community"
>

how to compile it?

$ gmcs -target:library -r:/usr/lib/gnome-do/Do.Universe.dll HelloWorld.cs "/res:HelloWorld.addin.xml"
That’s it, add the plugin to gnome-do as we did earlier and enable it. Just launch do, type anything say, sathyz and select the action “hello world”, it will display an xmessage saying “Hello, sathyz”.

The principle is this action is supported for any text item, (so the source here is text) and action to perform is show a dialog saying hello, # that text#. In the next part l am trying to get data from a specific source, say the data for Rhythmbox comes from rhythmbox.db,for ssh connect it is ~/.ssh/config.

form resubmission in spring

we used SimpleFormController for handling forms, in which we had onSuccess mapped to url say, ‘/uploadSuccess’. The problem with this was, after submitting the form the user would be taken to “/uploadSuccess”. if user presses refresh in browser or if the user goes to some page and comes back using the back button the form would be resubmitted. To avoid this, use ‘redirect:/uploadSuccess’.

[python] using doctest

doctests – looks on doc strings for usage specified like interactive python sessions. The main use of doctest is to update the docstrings as the implementation changes.

def say_hello(name=None):
    """A comment
    >>> say_hello()
    Hello
    >>> say_hello("Sathyz")
    Hello Sathyz
    >>> say_hello(1234)
    1234
    """
    from exceptions import TypeError
    if name is None:
        print "Hello"
    elif isinstance(name,str):
        print "Hello",name
    else:
        raise TypeError

if __name__=='__main__':
    import doctest
    doctest.testmod()
    print "Tests done" 

Executing this will give output like,

Z:\Spike\i-python>python say_hi.py
**********************************************************************
File "say_hi.py", line 7, in __main__.say_hello
Failed example:
    say_hello(1234)
Exception raised:
    Traceback (most recent call last):
      File "E:\Python25\lib\doctest.py", line 1228, in __run
        compileflags, 1) in test.globs
      File "", line 1, in 
        say_hello(1234)
      File "say_hi.py", line 16, in say_hello
        raise TypeError
    TypeError
**********************************************************************
1 items had failures:
   1 of   3 in __main__.say_hello
***Test Failed*** 1 failures.
Tests done
Z:\Spike\i-python>

[Ruby] Sending mails in ruby

To Send emails In ruby
I have followed the steps from Ian Purthon
It goes..

skumar@nomad:~$ cat /tmp/email1.rb
require ‘net/smtp’

def send_email(from, from_alias, to, to_alias, subject, message)
msg = <<END_OF_MESSAGE
From: #{from_alias} <#{from}>
To: #{to_alias} <#{to}>
Subject: #{subject}
#{message}
END_OF_MESSAGE
        Net::SMTP.start(‘localhost’) do |smtp|
                smtp.send_message msg, from, to
                end
end

send_email(‘skumar@spikesource.com’,’skumar’,’skumar@mailbucket.org’,’skumar’,’Send email from ruby’, ‘This is a test mail’)
skumar@nomad:~$

If you get some errors like,

skumar@nomad:~$ ruby /tmp/email.rb
/usr/lib/ruby/1.8/net/protocol.rb:206:in `initialize’: Connection refused – connect(2) (Errno::ECONNREFUSED)
        from /usr/lib/ruby/1.8/net/protocol.rb:206:in `new’
        from /usr/lib/ruby/1.8/net/protocol.rb:206:in `old_open’
        from /usr/lib/ruby/1.8/timeout.rb:56:in `timeout’
        from /usr/lib/ruby/1.8/timeout.rb:76:in `timeout’
        from /usr/lib/ruby/1.8/net/protocol.rb:206:in `old_open’
        from /usr/lib/ruby/1.8/net/smtp.rb:393:in `do_start’
        from /usr/lib/ruby/1.8/net/smtp.rb:378:in `start’
        from /usr/lib/ruby/1.8/net/smtp.rb:316:in `start’
        from /tmp/email.rb:11
skumar@nomad:~$

means, We need a smtp server installed in the local machine. I had postfix server installed.

Blogged with Flock

Tags:

email1.rb