December 21, 2011

Converting a space delimited sentence into a sequence of null terminated strings

The title says it all, you are given a string which consists of words separated by spaces, here is a snippet of code for converting the same into a sequence of null terminated strings and then later printing them. Returning the indices in the original string where the words begin is the key here :




Printing the strings is pretty trivial as :




Quick code, picked up the hack from a certain shell command parsing code in Xinu.

December 20, 2011

Intents and Activities in Android

The android UI is built around the concept of Activities. Each screen you see when you use an android app is an activity. And as usual, each activity has a layout that in programming methodology can only be accessed by the thread which set/created the layout. Android however has better method for creating activities and getting back information from them than purely language oriented methods (like global variables, shared objects etc). Android defines the notion of an intent, which can be used to give some information to an Activity and also get back information from an activity as an intent. The general code flow looks like the following :

create an intent and start the activity :

 final int result = 1;
Intent intent = new Intent(VoipChat.this, UserSettings.class);
startActivityForResult(intent, result);

Add code in the event handler when an activity returns ( you need to check for the specific return code which is the same as result)


   public void onActivityResult(int requestCode, int resultCode, Intent data) {
                 if (resultCode == Activity.RESULT_OK && requestCode == 1) {
                                    //Do some work here
                   }
   }

In the activity class which is started, make sure you put the necessary data in a new intent before calling the finish() method. The code in UserSettings.java for example would look something like :

   Bundle bundle = new Bundle();
Log.d("USERSETTINGS", "Returning : "+userName.getText().toString());
bundle.putString("UserName", userName.getText().toString());
data.putExtras(bundle);
this.setResult(RESULT_OK, data);
finish();

Accordingly the code inside the if block above would look something like :
                 
   String result = data.getExtras().getString("UserName");

note that data is the intent variable in the arguments for the onActivityResult() method.

NOTE : This blog post contains very specific information about the android sdk and included libraries. Unlike most of the other posts on this blog, it may not be of interest to the general readers.

December 06, 2011

Disabling auto indentation for code pasting in vim

If you are a vim user and have automatic indentation enabled, then you have sure been through the trouble of trying to paste indented code and messing up the indentation completely. There is however a fix for this , you can temporarily disable code indentation when you are pasting text. Add the following to your vim configuration file (mostly ~/.vimrc) :

set pastetoggle=<key sequence>

You can set whatever key sequence you want and if you do not have any other mapping for that key sequence, you just need to hit the key sequence and then "i" for insert and paste the text. Insert will generally show you the mode, for example if you have paste mode enabled, you will see

-- INSERT (paste) --

You can disable the paste mode using the same sequence as above. On my machine I have the sequence  set to <leader><C-p>, (my leader key is bound to the default ",")

set pastetoggle=<leader><C-p>

So hitting, "," and then Ctrl-p toggles the mode on vim on my machine.