Archive for August, 2006

C++ — Convert int to string

August 30, 2006

In order to convert an int (or any other numeric type, e.g., float, double, etc.) to string, you can use:

#include <sstream>

int i = 5;
std::string s;
std::stringstream out;
out << i;
s = out.str();

Other C/C++ not so frequently asked questions.

Emacs – tabs

August 28, 2006

To make an emacs mode forget about the indentation rules and insert tabs whenever you press the TAB key, add this to your .emacs file:

(define-key html-mode-map (kbd “TAB”) ’self-insert-command); # only in html-mode

If you don’t really want tabs, but spaces to be inserted whenever you press the TAB key, add this to your .emacs file:

(define-key html-mode-map (kbd “TAB”) ‘tab-to-tab-stop); # only in html-mode

Make sure to call the above function in a mode hook.

You might also want to edit the size of each tab, so use:

M-x edit-tab-stops

For more info:
http://www.student.northpark.edu/pemente/emacs_tabs.htm

C/C++ — printf — Number of chars to output

August 25, 2006

If you want to output a specified number of chars from a char array using a printf-like function, you can use:

char buffer[] = “Hi there!”;
int n = 2;
printf(“%.*s”, n, buffer); // outputs “Hi”

It will output only the first n number of characters form buffer.

Unix – read “man” pages from info

August 25, 2006

In order to read “man” pages from info, you can use:

info man_page
e.g., info read

If you need a specific section (e.g., man 2 read), you can use:

info “man_page(page_no)”
e.g., info “read(2)”

If you are inside info and you want to read a “man” page, you can use:

M-x man RET man_page or
M-x man RET “man_page(page_no)”

C/C++ – non blocking file descriptor

August 24, 2006

If you got an input file descriptor by using open() (<unistd.h>) or other function that creates a file descriptor and you want to set the descriptor to be non blocking on read, you can do:

fcntl(file_desc, F_SETFL, O_NONBLOCK); // <fcntl.h>

C++ – convert object to char*

August 23, 2006

To easily convert an object to char* you can write an operator like:

class String {
char str[80];

operator char *() { return str; } // convert to char*
}

And the you can use it like this:

char *buf;
String s;
buf =s;

More here:
http://www.ee.cooper.edu/courses/course_pages/past_courses/EID151/CPP.html

C/C++ – symbols in object files

August 22, 2006

In order to check if a certain symbol (e.g., function) is defined in a certain object file you can use:

nm object_file | grep symbol_name

Databases/SQL — SQLite — Dump database

August 20, 2006

In order to dump a SQLite database you can use the following command:

sqlite database .dump > database.sql

To put the data into a SQLite database from a sql file use:

sqlite database < database.sql

Emacs – M-x shell color

August 18, 2006

> Any idea how to set the color of the shell prompt in emacs?
>
> Not sure how much of this has to do with my windowing system (Windows
> XP) and shell (putty) or version of emacs and host.

I don’t think you’ll get ANSI color in your Emacs terminal unless you have this:

;; Add color to a shell running in emacs ‘M-x shell’
(autoload ‘ansi-color-for-comint-mode-on “ansi-color” nil t)
(add-hook ’shell-mode-hook ‘ansi-color-for-comint-mode-on)

Then set the colors:

(custom-set-faces
;; custom-set-faces was added by Custom — don’t edit or cut/paste it!
;; Your init file should contain only one such instance.
‘(comint-highlight-prompt ((t (:foreground “white”)))))

C++ – funtion template

August 18, 2006

Here is an example of a function template:

template <class T>
T Add(T a, T b)
{
return a+b;
}