kmediad 0.5.0a
A cross-platform Web-based audio player
Functions
klib_assocarray.c File Reference

Implementation of the klib_AssocArray class. More...

#include <malloc.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include "klib_log.h"
#include "klib_object.h"
#include "klib_string.h"
#include "klib_list.h"
#include "klib_assocarray.h"
#include "klib_nvpair.h"

Functions

klib_Stringklib_assocarray_tostring (klib_AssocArray *self)
 Get a string representation of the whole array.
void klib_assocarray_construct (klib_AssocArray *self, const char *class_name)
 Standard klib constructor.
klib_AssocArrayklib_assocarray_new (void)
 Create a new empty array.
void klib_assocarray_dispose (klib_AssocArray *self)
 Standard klib destructor.
klib_NVPairklib_assocarray_get_nvpair_by_key (const klib_AssocArray *self, const char *key)
void klib_assocarray_put (klib_AssocArray *self, const char *key, klib_Object *value)
 Set the value associated with the specified key.
klib_Objectklib_assocarray_get (const klib_AssocArray *self, const char *key)
 Get the value associated with the specified key, or NULL if the key can not be found.
int klib_assocarray_length (const klib_AssocArray *self)
 Get the current length of the array.
const char * klib_assocarray_get_key_by_index (const klib_AssocArray *self, int index)
 Get the key at the specified position.
klib_Objectklib_assocarray_get_value_by_index (const klib_AssocArray *self, int index)
 Get the value at the specified position.

Detailed Description

Implementation of the klib_AssocArray class.


Function Documentation

void klib_assocarray_construct ( klib_AssocArray self,
const char *  class_name 
)

Standard klib constructor.

  {
  if (klib_object_trace_flags & KLIB_OBJECT_TRACE_CONSTRUCTION)
    klib_log (KLIB_LOG_TRACE, "Called constructor for AssocArray\n");

  klib_object_construct ((klib_Object *)self, class_name);
  ((klib_Object *)self)->dispose_func = (klib_Object_DisposeFunc) 
     klib_assocarray_dispose;
  ((klib_Object *)self)->tostring_func = (klib_Object_TostringFunc) 
     klib_assocarray_tostring;

  self->priv = (klib_AssocArray_priv *) malloc (sizeof (klib_AssocArray_priv));
  memset (self->priv, 0, sizeof (klib_AssocArray_priv));

  self->priv->pairs = klib_list_new ();
  }
void klib_assocarray_dispose ( klib_AssocArray self)

Standard klib destructor.

  {
  if (klib_object_trace_flags & KLIB_OBJECT_TRACE_CONSTRUCTION)
    klib_log (KLIB_LOG_TRACE, "Called destructor for AssocArray\n");

  if (self->priv)
    {
    if (self->priv->pairs)
      {
      klib_object_unref ((klib_Object *)self->priv->pairs);
      self->priv->pairs = NULL;
      }
    free (self->priv);
    self->priv = NULL;
    }

  klib_object_dispose ((klib_Object *)self);
  }
klib_Object* klib_assocarray_get ( const klib_AssocArray self,
const char *  key 
)

Get the value associated with the specified key, or NULL if the key can not be found.

By implication, NULL is not a valid value to associate with a key, as it can't be distinguished from a non-set key.

  {
  const klib_NVPair *pair = klib_assocarray_get_nvpair_by_key (self, key);
  if (pair)
    {
    return klib_nvpair_get_value (pair);
    }
  else
    return NULL;
  }
const char* klib_assocarray_get_key_by_index ( const klib_AssocArray self,
int  index 
)

Get the key at the specified position.

  {
  klib_NVPair *pair = klib_assocarray_get_nvpair_by_index (self, index);
  return klib_nvpair_get_key (pair); 
  }
klib_NVPair* klib_assocarray_get_nvpair_by_key ( const klib_AssocArray self,
const char *  key 
)
  {
  int i, l = klib_list_length (self->priv->pairs);
  for (i = 0; i < l; i++)
    {
    klib_NVPair *pair = (klib_NVPair *) klib_list_get (self->priv->pairs, i);
    const char *trykey = klib_nvpair_get_key (pair);
    if (strcmp (trykey, key) == 0) return pair;
    }
  return NULL;
  }
klib_Object* klib_assocarray_get_value_by_index ( const klib_AssocArray self,
int  index 
)

Get the value at the specified position.

  {
  klib_NVPair *pair = klib_assocarray_get_nvpair_by_index (self, index);
  return klib_nvpair_get_value (pair); 
  }
int klib_assocarray_length ( const klib_AssocArray self)

Get the current length of the array.

  {
  return klib_list_length (self->priv->pairs);
  }
klib_AssocArray* klib_assocarray_new ( void  )

Create a new empty array.

  {
  if (klib_object_trace_flags & KLIB_OBJECT_TRACE_CONSTRUCTION)
    klib_log (KLIB_LOG_TRACE, "Called new for AssocArray\n");

  klib_AssocArray *self = (klib_AssocArray *) malloc (sizeof (klib_AssocArray));
  memset (self, 0, sizeof (klib_AssocArray));
  klib_assocarray_construct (self, "AssocArray"); 

  return self;
  }
void klib_assocarray_put ( klib_AssocArray self,
const char *  key,
klib_Object value 
)

Set the value associated with the specified key.

  {
  klib_NVPair *pair = klib_assocarray_get_nvpair_by_key (self, key);
  if (pair)
    {
    // Key already exists
    klib_nvpair_set_value (pair, value);
    }
  else
    {
    klib_NVPair *pair = klib_nvpair_new (key, value);
    klib_list_append (self->priv->pairs, (klib_Object *)pair);
    klib_object_unref ((klib_Object *)pair);
    }
  }
klib_String* klib_assocarray_tostring ( klib_AssocArray self)

Get a string representation of the whole array.

  {
  klib_String *s = klib_string_new_empty();
  int i, l = klib_list_length (self->priv->pairs);
  for (i = 0; i < l; i++)
    {
    klib_Object *pair = klib_list_get (self->priv->pairs, i);
    klib_String *ss = klib_object_tostring (pair);
    klib_string_append (s, ss->str);
    klib_object_unref ((klib_Object *)ss);
    }
  return s;
  }