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

Implementation of the klib_Error 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_error.h"

Functions

const char * klib_error_default_perror (int code)
 The default code-to-string mapping function.
klib_Stringklib_error_tostring (klib_Error *self)
klib_Stringklib_error_full_message (const klib_Error *self)
 Get the full message including the interpreted error code.
void klib_error_construct (klib_Error *self, const char *class_name, int code, klib_Error_PErrorFunc perror_func, const char *msg)
 Standard klib constructor.
klib_Errorklib_error_new (int code, klib_Error_PErrorFunc perror_func, const char *fmt,...)
 Create a new klib_Error with a specified code and handler function, and a text message formatted as for printf().
void klib_error_dispose (klib_Error *self)
 Standard klib destructor.

Detailed Description

Implementation of the klib_Error class.


Function Documentation

void klib_error_construct ( klib_Error self,
const char *  class_name,
int  code,
klib_Error_PErrorFunc  perror_func,
const char *  msg 
)

Standard klib constructor.

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

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

  self->code = code;
  self->perror_func = perror_func;
  if (!self->perror_func) 
    self->perror_func = klib_error_default_perror;
  self->msg = klib_string_new (msg);
  }
const char* klib_error_default_perror ( int  code)

The default code-to-string mapping function.

  {
  if (code == KLIB_ERROR_OPEN_WRITE)
    return "Can't open file for writing";
  if (code == KLIB_ERROR_OPEN_READ)
    return "Can't open file for reading";
  if (code == KLIB_ERROR_STAT)
    return "stat() called failed -- can't get file info";
  if (code == KLIB_ERROR_NOTREG)
    return "Not a regular file";
  if (code == KLIB_ERROR_MEMORY)
    return "Out of memory";
  if (code == KLIB_ERROR_NOT_DIR)
    return "Not a valid directory";
  if (code == KLIB_ERROR_LS_DIR)
    return "Can't list directory";
  if (code == KLIB_ERROR_OPEN_SOCKET)
    return "Can't open socket";
  if (code == KLIB_ERROR_GETHOSTBYNAME)
    return "Unknown host";
  if (code == KLIB_ERROR_CONNECT)
    return "Can't connect";
  if (code == KLIB_ERROR_HTTPNOTOK)
    return "Received an error HTTP error response";
  if (code == KLIB_ERROR_JSON_SYNTAX)
    return "Malformed JSON object syntax";
  return "Unknown error";
  }
void klib_error_dispose ( klib_Error self)

Standard klib destructor.

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

  if (self->msg)
    {
    klib_object_unref ((klib_Object *)self->msg);
    self->msg = NULL;
    }

  klib_object_dispose ((klib_Object *)self);
  }
klib_String* klib_error_full_message ( const klib_Error self)

Get the full message including the interpreted error code.

The caller must free the string returned

  {
  return klib_error_tostring ((klib_Error *)self);
  }
klib_Error* klib_error_new ( int  code,
klib_Error_PErrorFunc  error_func,
const char *  fmt,
  ... 
)

Create a new klib_Error with a specified code and handler function, and a text message formatted as for printf().

error_func may be NULL, and a default will be used.

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

  klib_Error *self = (klib_Error *) malloc (sizeof (klib_Error));
  memset (self, 0, sizeof (klib_Error));

  va_list ap;
  va_start (ap, fmt);
  klib_String *sss = klib_string_new_empty();
  klib_string_vprintf (sss, fmt, ap);
  va_end (ap);


  klib_error_construct (self, "Error", code, perror_func, sss->str); 
  klib_object_unref ((klib_Object *)sss);

  return self;
  }
klib_String* klib_error_tostring ( klib_Error self)
  {
  klib_String *s = klib_string_new_empty();
  klib_string_printf (s, "Error %d", self->code);
  if (self->perror_func)
    {
    klib_string_append (s, ": ");
    klib_string_append (s, self->perror_func (self->code));
    }
  if (!klib_string_is_null_or_empty (self->msg))
    {
    klib_string_append (s, ": ");
    klib_string_append (s, self->msg->str);
    }
  return s;
  }