Database in C++? Use a wrapper

January 27, 2008

Often the database itself provides an API for its access in C/C++. But it often consists of using confusing callback functions (callbacks are fun though) and the process differs from database to database. Solution? Use a wrapper..

This wrapper seems particularly interesting.. http://www.alhem.net/project/mysql/index.html

Download the wrapper source code, compile and install.

# make
# make install

Using the database using this wrapper is as easy as:

#include <mysql/mysql.h>
#include <libmysqlwrapped.h>

int main(){
StderrLog log;
Database db(“localhost”, “dbuser”, “dbpasswd”, “mydb”, &log);
if (!db.Connected()){
printf(“Database not connected – exiting\n”);
exit(-1);
}
Query q(db);
q.get_result(“select * from player”);
while (q.fetch_row()){
long num = q.getval();
std::string name = q.getstr();
printf(“#%ld: %s\n”, num, name.c_str());
}
q.free_result();
}

Good thing is, the same code can also be used for SQLite (my fav) and ODBC too (although with different wrapper and minor modifications). Another advantage is, the wrapper is cross platform, works on both win & *nix.


Print items [Assembly]

January 25, 2008

Β A very simple Assembly code to print items in a list. Hopefully the comments will explain it better. πŸ™‚

#FILE: print_it.s
#PURPOSE: This program copies the value in data_items to %eax and prints it off one by one in a loop.
#VARIABLES: The registers have the following uses:
#
# %edi – Holds the index of the data item being examined
# %eax – Current data item
#
# The following memory locations are used:
#
# data_items – contains the item data. A 0 is used
# to terminate the data

#data section. holds all important variables.
.section .data
data_items:
.long 67,3,34,-255,45,75,54,34,44,33,22,11,66,0 #values ending with a ‘0’.
printer:
.ascii “Current value of EAX is %d \n” # this format string which expands later on. Think about printf’s in C.

#text section. this is the actual code itself.
.section .text
.globl _start
_start: #every executable piece of code has _start

movl $0, %edi # move 0 into the index register (i.e., we want position 0)
movl data_items(, %edi, 4), %eax # data_items + index_register_edi_position * 4.
# (value of index_register_edi_position increments from 0 towards inifinty, see below inside start_loop)
# This is a mode of addressing memory. “4” is needed
# because it means 4-byte wide (my machine is 32-bit x86)
# and this value must be a multiple of 1,2,4,8,16 …otherwise memory mis-alignment issues will occur.
pushl %eax #push %eax into the stack
pushl $printer #push $printer (our format-string) into the stack
call printf #call printf function (this is dynamically linked to C library at run-time)

start_loop: # think of GOTO like syntax. πŸ™‚ easy heh?
cmpl $0, %eax #compare 0 with the value in %eax. to check if we hit the end of the tunnel.
je loop_exit #if they are equal, jump to loop_exit [think GOTO again]
incl %edi #increment %edi counter #or else, increment %edi by 1
movl data_items(, %edi, 4), %eax #see above.

pushl %eax #see above
pushl $printer #see above
call printf #print the format-string, see above

popl %eax #pop the contents off the stack
popl %eax #pop the contents off the stack
jmp start_loop #this is the way loops are implemented in Assembly.

loop_exit:
movl $1, %eax; #the value 1 in Linux Systems Programmer’s Manual refers to the exit system call.
# system calls are a way for programmers to call kernel and request service from it.
# In this particular instance, we need to halt the program using exit.
int $0x80; # break out and let kernel do what it does.

Assemble –

$ as print_it.s -o print_it.o

Link –

$ ld print_it.o -o print_it -lc -dynamic-linker /lib/ld-linux.so.2

Execute –

$ ./print_it </blockquote>

Put all nicely in a Makefile –

all:
as print_it.s -o print_it.o;
ld print_it.o -o print_it -lc -dynamic-linker /lib/ld-linux.so.2;
run:
./print_it;

Run the Makefile –

$ make;

Run the code –

$ make run

Note: the “-lc” means to use C library (libc.so on Gnu/Linux systems) so that we can use
printf and exit function call in our code.
The option “-dynamic-linker /lib/ld-linux.so.2” allows the program to be linked to libraries.
This builds the executable so that before executing, the OS will load the
program /lib/ld-linux.so.2 to load in external libraries and link them with
the program.

Enjoy the facade of ASM. πŸ™‚ [BTW, FYI, the executable binary is ~2k]


Implementation dilemma

January 22, 2008

Lot of people ask me what language would I chose for implementing software. The question here is not related to language. I have learnt that one should not focus merely on the language (C++ vs. Python). Each of these language has their own role and when implemented rightly can yield a useful abstraction called “software”.

I would like to emphasise that the gory feature of abstractions in C++ vs. that of Python is REALLY uncalled for and should not really be a hinder between you and your target application. What I mean by this is that, say you are in a team of 8 team person developing the next big business suite application. If 7 people are comfortable using Java, and can develop a prototype within 2 weeks, would you invest your time teaching them C++ (Only 1 person is proficient in C++)? Probably the answer is NO and in that case, your team would happily chose Java as the implementation language and move on with it.

Now, think of the same scenario but the target application being that of embedded device and not a business application. Would you still use Java given the fact that 7/8 people are comfortable with Java? Nope. The reason is because of the target environment and the constraints that the app should have to abide by. Since it’s an embedded device, the team would have to develop a stripped down executable, something that can fit within limited amount of memory and cannot have full fledged virtual machine (Python, Java are a big nada). Would even have to get some Assembly going!

So, all these rants are basically pointing to the same fact. Why not use Java? or C++ or Haskell? They got this, that. NO! They are all just a language that promotes your thought down to some form of abstraction so that machine can understand and execute it. Hence, the next time before you ask someone, what language to you/team use, it’s very important that you consider the constraints and the target environment that your app will be running in. Remember, whatever language you use for yourself of your team, at least consider the two factors; Constraints and the target environment. Whaddaya say? πŸ™‚


ABI madness

January 18, 2008

I was reading about the ELF (Executable Linkable Format) specification in Linux when I had a severe urge to learn about the concept of ABI. So, here I present some info about ABI which hopefully will explains things better.

An easy escape: It is suppose to mean the same to binary as what API means for source specification [ hoping that you understand what API is ].

More detail: Basically say you have a binary blob A somewhere in your system that B depends on for functionality. If A is modified, then B could break. To prevent this, both A and B conform to a common set of standard called Application Binary Interface or ABI in short.

There are varying cases of ABI specifications. It can range anything from an entire OS ABI to CPU ABI to Programming Language ABI. The basic premise remains the same [ i.e., they are set of standard specifications, that implementers should confirm to; be it a CPU instruction sets or a compiler specification ].


Basic Geometry learning activity

January 18, 2008

I have come up with idea for new activity for children to learn basic geometry and basic shapes. It seems too. unfair to talk about idea without even a piece of code in hand, but I want your comments and input to get the idea matured first.

The main motive is to design it as a game and with simple front end (I will post UI prototype later) but main idea behind the activity is to:

1) categorize basic shapes according to difficulty

2) show one shape (say with middle difficulty) to child for few second

3) ask him to imitate (redraw) the shape on canvas

4) check the accuracy of shape to its original *

5) show difference to child

6) give next shape from similar or different category according to his ability **

7) wrapΒ  whole thing as exciting and interactive game

* I have written simple NN program to recognize basic shapes and alphabet, I am plannin to reuse the code.

** The idea is to first start with difficulty level tuned to child ability, giving him enough practice and increasing difficulty afterward

I will be writing this activity for XO (OLPC Laptop) and will host code soon on repository, please help me by suggesting and commenting about this idea.


Welcome to /lib/nepal!

January 18, 2008

Hello freedom lovers! πŸ™‚

This is a small adventurous idea some of us have put into. We’ll be sharing some technological ideas, codes, HOWTOs over the coming weeks/months/years into lot of stuffs.

We’ll also make sure that everyone’s voice is also heard at the same time!
So, don’t hesitate to communicate to through the comments. We’ll also keep you updated on latest gizmos. How’s that? O.o

Enjoy!


Programmer’s block

January 18, 2008

I have been trying for days to do some programming on the google android. It just seems harder and harder to do when you have a huge thing to write and you don’t have a milestone set. It’s the difference between coding and real engineering. Coding is generally for small, perhaps class projects, assignments with a teacher looking after you but when you have a giant research type thing that has all the design aspects, it starts getting scary.
I was just having this discussion with himanshu about the difference between a writer’s block and a programmer’s block. It’s easy to dismiss them as being similar. No offence for I’m a writer too and perhaps I can say it better too. Often writer’s block is about ideas and creative aspects, programmer’s block involves that and so much more. It can be about debugging this nasty bug while being not-quite sure about the implementation of the algorithm and still having the doubts if you are compiling it right. πŸ™‚ Most of the times, concepts are major problems for they are at least half of the thing. However, its often easy to underestimate the other half. The dreaded coding portion.
I love programming and more I find that I’m unable to do it for sometimes days on end and every so often something just pops in my head and I begin hacking into the night/morning/next morning… I’m not too sure if I’m making sense right now but I invite you up for your experiences with this and maybe we can come up with a solution: sort of – to be more productive as developers!


Design a site like this with WordPress.com
Get started