Code sample



I. Programming style

NanoHTTPd programming style looks like Object-Oriented ( eg: classes & objects ). in NanoHTTPd, a class is implemented with a structure containing member attributes and member methods (pointers to functions).

Here is the declaration of the list_t structure :

typedef struct _list_t
	list_elem_t*    data;
	list_elem_t*    position;

	const   void*   (*first)(struct _list_t*);
	const   void*   (*next)(struct _list_t*);
	void	  (*add)  (struct _list_t*, const void*);

	void 	(*delete)(void*);
	void  (*delete2)(void*);
	void  (*delete_func)(void*, void(*)(void*));
} list_t;

In this structure, data & position are member attributes, and first, next are methods.

Each object of class class is instanced with a function named class_new(). This function returns a pointer to a class_t structure ( eg: a list object is instanced with list_new() ) :

	list_t* my_list = list_new();

Member functions are called thanks to the structure pointers. ( eg: the method first of the object my_list is called in this following manner: my_list->first(my_list) ). As C is not an object-oriented language, the first argument of each member functions is the object itself.

You should also notice that the "destructor" methods ( delete*** functions) always take a void* pointer as first argument. This is due to the fact that many destructors methods are used as callback functions.

II. Lists

Constructor list_t* list_new():
create a new list_t object;
Function void add(list_t* me, void* obj):
Add the object obj to the list me.
Function void* first(list_t* me):
Returns the first element of the list me,and resets the internal position pointer to this first element. Returns NULL if the list is empty.
Function void* next(list_t* me):
Returns the next element of this list me, and increment the internal position pointer. Returns NULL if the internal position pointer is at the end of the list.
Function void delete(list_t* me):
Delete the list me, and free() all elements.
Function void delete2(list_t* me):
Delete the list me.Do not free() elements.
Function void delete_cmp(list_t* me, void(*free_func)(void*)):
Delete the list me and call free_func to each element of the list.


	list_t*	my_list = list_new();
	char*	elem;
	my_list->add (my_list, "one");
	my_list->add (my_list, "two");
	my_list->add (my_list, "three");
	for ( elem = (char*) my_list->first(my_list);
			elem = (char*) my_list->next(my_list)
				printf("Hello : %s", elem);
	my_list->delete2 ( my_list);

If you run this example, you will notice that each addition to the list insert the element at the beginning of the list. The output of this example will be:

Hello three
Hello two
Hello one

III. Hash tables

Constructor hash_t* hash_new():
Create a new hash table.
Function void set(hash_t* hash, const char* key, void* obj):
Add the object obj to the hashtable hash with the key key.
Function void* get(hash_t* hash, const char* key):
Lookup in the hash table hash the object inserted with the key key. Returns NULL if no such object exists.
Function list_t* keys(hash_t* hash):
Return a list object containing all the keys in the hash table hash. You must call delete method of the resulting list to free it.
Function void delete(hash_t* hash, int free_key, int free_data, void(*free_func)(void*)):
Free the hash table hash. If free_key == 1, call free() on all keys. If free_data ==1, call free_func() on all items.