Skip to content


Desktop version of Kactoos

Kactoos is a social buying website. In other words, you buy a product on the list and as many other people buy the same product the price decrease.

So, to make others know about their API, they are doing a challenge. The best application developed wins a Google TV and an iPad 2.

As I want a tabled so bad, I'm developing a version of Kactoos for desktop called KaQToos.

If you want to check the project out, take a look at github's KaQToos project website.

Posted in C++, Projects.

Tagged with , , , .


Awesome C/C++ auto-completion in Emacs

Emacs is a great editor, no body doubts that. But there is no powerful auto-completion by default, like Eclipse or other IDE.

Parsing C++ is very hard and using tools like ctags works well for C but not so much for C++.
LLVM/Clang is a compiler architecture supported by Apple, Google, Adobe and others that seeks to become a serious alternative to GCC. The project started just a few years ago but it already provides a high quality C and Objective C compiler that performs better than GCC in several benchmarks. The C++ support is still not complete but good to compile boost. Clang is not just a compiler, it also has includes great tools for code analysis. Clang is BSD licensed which enables close interaction with third part applications without licensing issues.

Ok, so let's merge all these things. To accomplish that we need 3 Emacs extensions:

  • auto-complete-mode
  • yasnippet
  • auto-complete-clang

auto-complete-mode

auto-complete-mode is an awesome extension for Emacs. It provides an intelligent auto-completion. To install, just follow these steps:

  1. Download it at http://cx4a.org/software/auto-complete/
  2. Copy all .el to ~/.emacs.d or other personal folder that you keep your Emacs extensions
  3. Add the following code to your ~/.emacs:
    (require 'auto-complete-config)
    (add-to-list 'ac-dictionary-directories "~/.emacs.d/ac-dict")

    Note: change the ac-dictionary-directories as specified in step 2.

yasnippet

yasnippet is a template system for Emacs. Installation process:

  1. Download the yasnippet-bundle at http://code.google.com/p/yasnippet/
  2. Uncompress it at ~/.emacs.d or other personal folder that you keep your Emacs extensions
  3. Add the following code to your ~/.emacs:
    (require 'yasnippet-bundle)

auto-complete-clang

auto-complete-clang unites the power of auto-complete, clang and yasnippet.

  1. Download the .el file at https://github.com/brianjcj/auto-complete-clang
  2. Copy it at ~/.emacs.d or other personal folder that you keep your Emacs extensions
  3. Add the following code to your ~/.emacs:
  4. (require 'auto-complete-clang)
  5. Since auto-complete-clang calls clang to every auto-complete command, we need to trigger it with some key. In my example here I'm using C-Tab
  6. (setq ac-auto-start nil)
    (setq ac-quick-help-delay 0.5)
    (define-key ac-mode-map  [(control tab)] 'auto-complete)
  7. Setting some properties:
  8. (defun my-ac-config ()
    (setq ac-clang-flags (split-string "-I/path/to/headers"))
    (setq-default ac-sources '(ac-source-abbrev ac-source-dictionary ac-source-words-in-same-mode-buffers))
    (add-hook 'emacs-lisp-mode-hook 'ac-emacs-lisp-mode-setup)
    (add-hook 'c-mode-common-hook 'ac-cc-mode-setup)
    (add-hook 'ruby-mode-hook 'ac-ruby-mode-setup)
    (add-hook 'css-mode-hook 'ac-css-mode-setup)
    (add-hook 'auto-complete-mode-hook 'ac-common-setup)
    (global-auto-complete-mode t))
    (defun my-ac-cc-mode-setup ()
    (setq ac-sources (append '(ac-source-clang ac-source-yasnippet) ac-sources)))
    (add-hook 'c-mode-common-hook 'my-ac-cc-mode-setup)
    (my-ac-config)

    There is basically one important thing here, the ac-clang-flags. You must pass all your include paths in order to clang parses everything correctly. Even the stdc++ headers. For example:

    (setq ac-clang-flags (split-string "-I/usr/include/c++/4.5 -I/usr/include -I/usr/local/include -I/usr/local/qt4/include -I/usr/local/qt4/include/QtCore -I/usr/local/qt4/include/QtGui"))
  9. Since my background color is black, I had to change the ac-cursor-color in auto-complete.el. To do that find the first occurrence of ac-cursor-color and change nil to the desired color. For example:
  10. (defvar ac-cursor-color "blanched almond"
    "Old cursor color.")

Troubleshoot

If you get some error with bits/c++config.h in *clang error* buffer, like:
fatal error: 'bits/c++config.h' file not found
You might need to create a symbolic link from the machine hardware name(i686, etc) c++ bits includes to the c++ bits:
# ln -s /usr/include/c++/4.5/`uname -m`-linux-gnu/bits/* /usr/include/c++/4.5/bits

To inspire you, here is my .emacs file:

; auto-complete-mode
; --------------
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/Dropbox/elisp/ac-dict")
 
; yasnippet
; --------------
(require 'yasnippet-bundle)
 
; auto-complete-clang
; --------------
(require 'auto-complete-clang)
 
(setq ac-auto-start nil)
(setq ac-quick-help-delay 0.5)
;; (ac-set-trigger-key "TAB")
(define-key ac-mode-map  [(control tab)] 'auto-complete)
(defun my-ac-config ()
  (setq ac-clang-flags (split-string "-I/usr/include/c++/4.5 -I/usr/include -I/usr/local/include -I/usr/local/kdchart4/include -I/usr/local/rog/include -I/usr/local/qt4/include -I/usr/local/qt4/include/QtCore -I/usr/local/qt4/include/QtGui"))
  (setq-default ac-sources '(ac-source-abbrev ac-source-dictionary ac-source-words-in-same-mode-buffers))
  (add-hook 'emacs-lisp-mode-hook 'ac-emacs-lisp-mode-setup)
  (add-hook 'c-mode-common-hook 'ac-cc-mode-setup)
  (add-hook 'ruby-mode-hook 'ac-ruby-mode-setup)
  (add-hook 'css-mode-hook 'ac-css-mode-setup)
  (add-hook 'auto-complete-mode-hook 'ac-common-setup)
  (global-auto-complete-mode t))
(defun my-ac-cc-mode-setup ()
  (setq ac-sources (append '(ac-source-clang ac-source-yasnippet) ac-sources)))
(add-hook 'c-mode-common-hook 'my-ac-cc-mode-setup)
;; ac-source-gtags
(my-ac-config)

Working with Qt 1

 

Working with Qt 2

 

It also works with the C++ Standard Library

 

That's it.

Posted in C, C++, Miscellaneous, Tutoriais.

Tagged with , , .


String replace in C

I had this "homework" to make a string compressor using some specific hashes. Than it comes the necessity to create a function that replaces all matches of a string to another string.

Well, it turns out that this function is quite interesting. Lemme show my implementation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
void string_replace_all(const char *old, const char new, char *str)
{
	int i, j;
	int old_len = strlen(old);
	int str_len = strlen(str);
	int k = -1;
	char sub1[BUF];
 
	for (i = 0, j = 0; i < str_len; ++i) {
		if (str[i] == old[j]) {
			if (k == -1) {
				k = i;
			}
			++j;
		} else {
			if (str[i] == old[0]) {
				k = i;
				j = 1;
			} else {
				k = -1;
				j = 0;
			}
		}
		if (j == old_len) {
 
 
			memcpy(sub1, str, k);
 
			sub1[k] = new;
			sub1[k + 1] = '\0';
 
			strcat(sub1, (str + (k + j)));
 
			strcpy(str, sub1);
 
			string_replace_all(old, new, (str + (k + 1)));
		}
	}
}

Off course there is more interesting implementations. Why don't you show us yours?

Posted in Algorithms, C.

Tagged with , .


Using find + awk to create a .tar.gz

What if you want to easily create a .tar.gz with a bunch of files and some conditions?
I had a similar problem, for example, to create a .tar.gz with a bunch of CMakeLists.txt files with 'lib' in its directory name. To do this I simply used find + awk + tar.

Here is the code:
find project/ -name CMakeLists.txt | awk '{a=1;while(a<=NF){if(match($a, "lib")){print $a}; ++a}}' | xargs tar cvfz cmake.tar.gz

AWK is an awesome tool to process texts and really useful in command line.

Posted in AWK, Miscellaneous, Tutoriais.

Tagged with , .


Do you really know what a vector is?!

I know that almost anyone knows what's a vector. Some say that is an arrow. Others that is an object that has both a length and direction. Well, let's examine that in depth.

First of all, a vector is a logical definition from the Set Theory.

Let R be an equivalence relation for a set K. We define the Quotient Set, denoted by the symbol K/R, as:

K/R := \{ [X]_R | X \in K \}

Results that K/R is a partition of K, i.e., K/R is a class of non-empty subsets of K, pairwise disjoint and whose union is the class K.

In summary, we have the following important result:
If R an equivalence relation on a set K, than, the quotient set of K for R is a partition of K.

R is a relation defined in class K from all oriented segments in the plane, as follows:

xy\ R\ uz if, and only if, the length of xy is same as uz, the line which support xy is parallel to uz and xy has the same direction as uz.
Results that R is an equivalence in K. Therefore:

K/R = \{ [PQ]_R | PQ \in K \} is a partition of K. So,

PQ \ R\  P'Q' \leftrightarrow [PQ]_R = [P'Q']_R

Remembering that [X]_R := \{ Y \in K | Y\ R\ X \} \subset K. If you want to know more about, check the Equivalence Class.

So, a vector from the plane is, by definition, any one element of the quotient set K/R. Thus we have,

PQ\ R\ P'Q' \leftrightarrow \vec{PQ} = \vec{P'Q'}

If you think this is too much. There is an easy approach.
Vector is a set of all equipolent oriented segments together. i.e.

\vec{AB} = \{ A'B' | A'B' \sim AB \}

You have never wondered why it is correct to use equal sign (=) for "different but similar" vectors and it is incorrect to use equal sign for "different but similar" oriented segments? The truth is, since you are comparing sets(vectors) with the same elements(oriented segments), it is true when you say \vec{a} = \vec{b}. Now, if a and b were oriented segments, you will only be able to use the equal sign if a and b are really the same element in the plane(the same set of points).

Yes, logics is much more than computer algorithm or stuff like that.

Posted in Mathematics, Nerd.

Tagged with .


Getting back to open source

Hi everyone,

Yesterday I began to set up my KDE environment to start contribute again to the KDE project.

It's very easy. If you want to help just click here!

Soon I will post more news about KDE, RobotQt and stuff.

Posted in KDE, Miscellaneous.

Tagged with .


looong #fail no GCC

O uso do long long como tipo de dado foi introduzido no padrão ISO C99. Isso acabou gerando uma revolta na comunidade acadêmica que achava o cúmulo ter de usar long long ao invéz de um nome específico, argumentando "qual será a próxima? long long long, long long long long ... ".

Bom, o que parece é que o pessoal do GCC também não foi muito com a cara desse long long não.

long long long no gcc

long long long #fail no gcc

#FAIL para a ISO C99 ou pro GCC? Fica a dúvida.

Posted in C, Miscellaneous, Nerd.

Tagged with , .