This short article will show how to use the Apache Portable Runtime (APR) with it’s built in hashtable.

Introduction

If you are in need to place data in a memory structure to access it quickly, you are probably best with a hash table data type. The Apache Portable Runtime (APR) luckily, along some others like e.g. apr_table_t or apr_array_header_t (both defined in apr_tables.h), provides such a data type to you.

Hashtables do have the big advantage that you can supply them any data type you like. The next big thing with hashtables is that they are usually very efficient if the number of elements that they are holding grows.

You can download the file here.

Let’s hash

In order to demonstrate hashtables we are using a very simple data structure which holds a date, a username and a password. We then set some values onto this data structure and save the created structure into the hashtable.

After storing that object into the hashtable we are reading the objects back from the hashtable and output it’s contents.

The c0de

Now that you do have some overview of what we are actually doing here, it’s time to show you some code. I think it pretty much speaks for itself.

You should however, if you are using strings as keys, use the special APR_HASH_KEY_STRING value to indicate a string valued key to APR. This will use strlen(key) to compute the length (NUL terminator is not included there).

#include <apr.h>
#include <apr_hash.h>
#include <apr_time.h>
#define APR_WANT_STDIO
#include <apr_want.h>

struct userRecord
{
  apr_time_t creationDate;
  char username[30];
  char password[30];
};

void print(struct userRecord *user)
{
  apr_pool_t *p = NULL;
  char *timeReadable = NULL;
  apr_pool_create(&p, NULL);
  int i = 0;

  timeReadable = apr_palloc(p, APR_RFC822_DATE_LEN);
  apr_rfc822_date(timeReadable, user->creationDate);

  printf("Username:\t\t %s\n", user->username);
  printf("Password:\t\t %s\n", user->password);
  printf("CreationDate:\t\t %s\n", timeReadable);

  apr_pool_destroy(p);
  return;
}

/* Compile with
 * $> export APR_LIBS="`apr-1-config --cflags --cppflags --includes --ldflags --link-ld --libs`"
 * $> export APU_LIBS="`apu-1-config --includes --ldflags --link-ld --libs`"
 * $> gcc $APR_LIBS $APU_LIBS aprHashtable.c -o aprHashtable
 */
int main(int argc, const char* const* argv)
{
  apr_status_t rv;
  apr_pool_t *p = NULL;
  struct userRecord *cUser = NULL;
  struct userRecord *readUser = NULL;
  int i = 0;
  char countName[12];
  apr_hash_index_t *hidx = NULL;


  apr_app_initialize(&argc, &argv, NULL);
  atexit(apr_terminate);

  apr_pool_create(&p, NULL);

  apr_hash_t *ht = apr_hash_make(p);

  apr_cpystrn(countName, "Jens Frey  ", 12);

  for (i=0; i < 26; i++) {
    countName[10] = (char) i + 65;
    cUser = apr_palloc(p, sizeof(struct userRecord));
    apr_cpystrn(cUser->username, countName, strlen(countName) + 1);
    apr_cpystrn(cUser->password, "secret", strlen("secret") + 1);
    cUser->creationDate = apr_time_now();

    apr_hash_set(ht, cUser->username, APR_HASH_KEY_STRING, cUser);

    print(cUser);
  }

  for (hidx = apr_hash_first(p, ht); hidx; hidx = apr_hash_next(hidx)) {
    apr_hash_this(hidx, NULL, NULL, (void*) &readUser);
    print(readUser);
  }

  readUser = apr_hash_get(ht, "Jens Frey G", APR_HASH_KEY_STRING);
  printf("\n\n");
  print(readUser);

  printf("Hashtable size: %d bytes\n",
     apr_hash_count(ht) * sizeof(struct userRecord)
     + apr_hash_count(ht) * sizeof(readUser->username));

  apr_terminate();
  return rv;
}

Like this post? Share on: TwitterFacebookEmail


Jens Frey Avatar Jens Frey is the creator of the datapile blog.

Keep Reading


Published

Category

Programming

Tags

Stay in Touch