kbgoffice again working(sort of)

The code for kbgoffice is working already on my machine. It is not really ported to Qt4, because right now it relays on the qt3support which will not be there and for good ;) . There are also some annoying bugs(I will see where they came from, so keep calm :P ). The instructions to compile the code(notice there is no target to install it for now) are in the INSTALL file. To get the code you will need subversion. Search google if you don’t know how to get it. The command to checkout the code is

svn co http://svn.assembla.com/svn/kbgoffice/trunk kbgoffice;cd kbgoffice

now

cat INSTALL

and follow the instructions. You will also need the dictionary files which you can find over here

kbgoffice fun

Today at the Bulgarian linux forum someone asked to port the kbgoffice program to KDE4. I have used the damn thing pretty much so I decided to go for it. After 2 hours of fighting with compiler errors it finally compiled but most of the code is just commented out so I got a prototype… I started to look at the classes this night and I see some bad code :( . Not sure why but many people still write ugly C++ code. The idea of the whole program is rather nice and the design is cool but there are some things that drive me nuts. For example the author reads the config file everytime he needs to save/read a property which is not efficient at all. I will try to fix some of the problems and I hope I will have the time for that. I will post a snippet which bit me today while I was working on the damn thing.

 1 #include <iostream>  2 #include <cstdlib>  3 #include <ifstream>  4 #include <string>  5 #include <map>  6  7  8 int main (int argc, char const* argv[])  9 { 10     std::map<std::string, std::string> myMap; 11     std::ifstream iofile(“example.txt”); 12     std::string key; 13     std::string value; 14     while(! iofile.eof()){ 15         getline(iofile,key); 16         int position = key.find(“=”); 17         if (position == std::string::npos)  18             break; 19         value = key; 20         value.erase(0,(position+1)); 21         key.erase(key.begin()+position, key.end()); 22         std::cout << “The key is “ << key << ” and the value is “ << value << std::endl; 23     } 24     return 0; 25 } 26 //    std::string home = getenv(“HOME”); 27 //    std::ofstream file(“example.txt”, std::ios::app | std::ios::out); 28 //    file << “WithSplash=0\n”; 29 //    file.close();

Well where is the problem with that you ask :? The answer is here so we change the code to this:

 1 #include <iostream>  2 #include <cstdlib>  3 #include <fstream>  4 #include <string>  5 #include <map>  6  7  8 int main (int argc, char const* argv[])  9 { 10     std::map<std::string, std::string> myMap; 11     std::ifstream iofile(“example.txt”); 12     std::string key; 13     std::string value; 14     while( getline(iofile,key) ){ 15         int position = key.find(“=”); 16         if (position == std::string::npos)  17             break; 18         value = key; 19         value.erase(0,(position+1)); 20         key.erase(key.begin()+position, key.end()); 21         std::cout << “The key is “ << key << ” and the value is “ << value << std::endl; 22     } 23     return 0; 24 } 25 //    std::string home = getenv(“HOME”); 26 //    std::ofstream file(“example.txt”, std::ios::app | std::ios::out); 27 //    file << “WithSplash=0\n”; 28 //    file.close();

and we are ready to continue.

Problem with file endings

Today I had the same problem with mixed file endings. It seems that many editors are still dealing bad with that on different platforms, so you get different line endings. This times instead of manually dealing with the problem I googled and it seems that there is a nice workaround, you just need to type

:%s/\r//

and you are done. This will take care of you, I also have ff=unix in most of my files and in my vimrc.

NERDTree

Hmm today i finally found a nice TreeExplorer for vim. I was looking for something like this for so long.

    • It had to be plugable to the UI and not clutter it

 

  • It had to list the files in a directories of my project

 

 

  • I wanted to be able to open the files from there to the right side of the window as antoher buffer/tab

 

 

There are many plugins out there that solve some of the problems/requirements in the list, but they were not complete. I have tried project(this one was good), the built in explorer, some other explorers, which names i cannot remember :D .

Enter NERDTree. That is one of the plugins that saves your day. I just put

map <F6> :NERDTreeToggle

in my vimrc and now when i hit F6 i have a list of the files in my home directory. I go to that window with either the mouse or <C-W> w and now i can use the neat features of the plugin.

? - get helpC - make the directory your ROOTo - open/close the directory(this shows the files also in the     directory as a list

When you finish don’t forget to hit F6 again :D

Don’t trust garbage collectors

I recently read This blog post and the author has a nice point about garbage collectors. When we program in languages with that feature we should not blindly trust the automatic memory management for everything. Most of the time it is ok, but sometimes you have to think about memory management in those languages too.
In the blog post Tahseen Ur Rehman gives an example with a thread which was holding some references to the objects so they could not be deleted, so the users were getting OutOfMemoryErrors. The author also warn us that we should not trust the lie

“In some programming languages, memory management is the programmer’s responsibility.
… …
An alternate approach to memory management that is now commonly utilized, especially by most modern object-oriented languages, is automatic management by a program called a garbage collector“

There is also a nice example in Effective Java with the same point. So next time when we want to do something smart with the language, we should think if it is ok for the garbage collector and it will be able to do it’s job. I find that most of the time those kind of errors are found in concurrent programs and hacky arrays.

A little benchmark with C++ and Java in the game.

Today i just wanted to make a benchmark and see which is faster java or C++. There are rumors over the internet that java is or will be faster than c++ some day. So today I decided to try compare the speed of those languages. We all know arrays and how bad they can be sometimes if you are not careful with them in C++, because

    • They don’t know it’s size

 

  • You cannot extend it if you reach the limit

 

 

  • The name of an array converts to a pointer to its first element at the slightest provocation(there are nice examples for this point here)

 

 

So with one word arrays are not nice. Java provides some fixes for those, because there an array know it’s size and there are no pointers :) . But what if we use a higher level abstractions from STL? Will std::vector beat the Java arrays? Here is the code:
main.cpp:


#include <vector>

#include <iostream>

int main(int argc, char* argv[]){

std::vector<int> Vec(10000);

for(int x = 0;x < Vec.size();x++){

Vec[x] =x;

std::cout << Vec[x] << std::endl;

}

}

Main.java


class Main{

public static void main(String []args){

int Vec[] = new int[10000];

for (int x = 0;x < Vec.length;x++){

Vec[x] = x;

System.out.println(x);

}

}

}

So we compile with

g++ -O2 -fomit-frame-pointer main.cpp && javac Main.java

and let’s run them through time :)

nikolavp@Nikolavp:~$ time ./a.out > /dev/null

real    0m0.010suser    0m0.004ssys     0m0.004snikolavp@Nikolavp:~$ time java Main > /dev/null

real    0m0.196suser    0m0.140ssys     0m0.052s

So C++ with vector which is supposed to be a higher level abbstraction outperformed Java’s “safe” arrays. The next time you want to write something in Java think a little bit is it really worth the machines ;) . Don’t get me wrong Java have it’s place and it made the whole world a better place, but don’t convince me, that it will be faster(or is faster now) than C++… It just never will.

#include
using namespace std;
int main(){
}


for(var i=0;i{
go("http://orkut.com")
enter("Email","myemail@gmail.com")
enter("password","secret")
sleep(600)
click("sign in")
sleep(600)
go("http://www.orkut.com/GLogin.aspx?cmd=logout")
sleep(60)
}

Nice way to keep the lines indented in vim

Here is one more vim tipp. I just put a new keybinding to my .vimrc file, thanks to \amethyst from #vim@freenode.net :) . Here it is

inoremap <expr> <CR> (col("$")==col(".") ? "\<ESC>=a{\<C-O>o" : "\<CR>")

This will indent the whole block before the line if you hit <CR> on the end of the code you are writing in insert mode. If you are not at the end of the line you will get the default behaviour of <CR>. I was not sure how to implement that, but \amethyst really helped me on IRC. Once more this shows the power of chatting with experienced people who know the project very well. Viva open source :) .

Making abbreviations persistent in vim

If you are like me, most of the time you make more mistakes in vim, because your mind is little slower than you fingers. Enter abbreviations which is a really neat feature in vim :) . You just have to type

:iabbrev wrongstring wantedstring

in command mode and the next time you type the word wrong vim will fix it. So where is the problem? The problem is that when you type that in command mode it is not persistent between sessions. I really wanted to save those save somehow in a file. So here is the solution :)

function! Abbreviate(input, output) python << EOFdef appendAbb():    import os    import vim    home = os.getenv("USERPROFILE") or os.getenv("HOME")    home += "/myvimfiles/abbs.vim"    Abbs = open(home, "a")    inputstr = vim.eval("a:input")    outputstr = vim.eval("a:output")    abbreviation = "iabbrev " + str(inputstr) + " " + str(outputstr)+ "\n"    Abbs.write(abbreviation)EOF:py appendAbb()let abbrevation = "iabbrev ". expand(a:input). " ". expand(a:output)execute abbrevationendfunctioncommand! -nargs=+ Abb :call Abbreviate(<f-args>)

Put that in your ~/.vimrc file and you are done :) . You should change /myvimfiles/abbs.vim to the file with abbreviations you would like to use. If it is in a special directory not in your runtimepath you should also put source “path/to/the/file” in vimrc :) . Now next time you want to correct a type just type

:Abb wrongstring correctstring

and that’s all ;)
Happy vimming

Renaming a variable in gvim

Some of you may be missing the nice IDE’s and their neat features when you are editing in gvim. I was myself a long time Eclipse user, but I found that it doesn’t make me more productive, but just eat the resources of my machine. I missed the renaming variable feature in gvim for some time now. Today this is over I was reading the manual of vim and found what I was looking for. We all know the substitute command, but I was not sure how to replace the variable with one command for all files in the project. So here is what I have now in my .vimrc:

fun! BuffersRenameVar(cword, newvar)        let word = a:cword        let newvar = a:newvar        let replace = ":bufdo %s/\\\

You just hit \brn for buffer rename and have to change the “nvname”
with the new name of the variable and that’s all :) . This will ask you
to rename every appearance of the variable in all open buffers. Don’t
forget to open all all your files in vim. This will also put a global
mark named “R” so you can continue your work. Happy vimming