?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /* l_err.c - errors list structure.
  2.  
  3.    This is free and unencumbered software released into the public domain.
  4.    For more information, please refer to <http://unlicense.org>. */
  5.  
  6. #include "defs.h"
  7.  
  8. #include <stdbool.h>
  9. #include <stdarg.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "debug.h"
  14. #include "l_list.h"
  15. #include "l_err.h"
  16.  
  17. void
  18.     error_entry_clear
  19.     (
  20.         struct error_entry_t *self
  21.     )
  22. {
  23.     list_entry_clear (&self->list_entry);
  24.     self->msg = NULL;
  25. }
  26.  
  27. void
  28.     error_entry_free
  29.     (
  30.         struct error_entry_t *self
  31.     )
  32. {
  33.     list_entry_free (&self->list_entry);
  34.     if (self->msg)
  35.         free (self->msg);
  36.     error_entry_clear (self);
  37. }
  38.  
  39. void
  40.     errors_clear
  41.     (
  42.         struct errors_t *self
  43.     )
  44. {
  45.     list_clear (&self->list);
  46. }
  47.  
  48. bool
  49.     errors_add
  50.     (
  51.         struct errors_t *self,
  52.         const char *msg,
  53.         struct error_entry_t **result
  54.     )
  55. {
  56.     bool ok;
  57.     struct error_entry_t *p;
  58.     char *p_msg;
  59.  
  60.     ok = false;
  61.     p = (struct error_entry_t *) NULL;
  62.     p_msg = (char *) NULL;
  63.  
  64.     if (!self || !msg)
  65.     {
  66.         _DBG ("Bad arguments.");
  67.         goto _local_exit;
  68.     }
  69.  
  70.     p = malloc (sizeof (struct error_entry_t));
  71.     if (!p)
  72.     {
  73.         _perror ("malloc");
  74.         goto _local_exit;
  75.     }
  76.  
  77.     p_msg = strdup (msg);
  78.     if (!p)
  79.     {
  80.         _perror ("strdup");
  81.         goto _local_exit;
  82.     }
  83.  
  84.     error_entry_clear (p);
  85.     p->msg = p_msg;
  86.  
  87.     list_add_entry (&self->list, &p->list_entry);
  88.     ok = true;
  89.  
  90. _local_exit:
  91.     if (!ok)
  92.     {
  93.         if (p)
  94.         {
  95.             free (p);
  96.             p = (struct error_entry_t *) NULL;
  97.         }
  98.         if (p_msg)
  99.             free (p_msg);
  100.     }
  101.     if (result)
  102.         *result = p;
  103.     return !ok;
  104. }
  105.  
  106. bool
  107.     errors_add_vfmt
  108.     (
  109.         struct errors_t *self,
  110.         struct error_entry_t **result,
  111.         unsigned bufsize,
  112.         const char *format,
  113.         va_list ap
  114.     )
  115. {
  116.     bool ok;
  117.     char *s;
  118.  
  119.     ok = false;
  120.     s = (char *) NULL;
  121.  
  122.     if (!self || !bufsize || !format)
  123.     {
  124.         _DBG ("Bad arguments.");
  125.         goto _local_exit;
  126.     }
  127.  
  128.     s = malloc (bufsize);
  129.     if (!s)
  130.     {
  131.         _perror ("malloc");
  132.         goto _local_exit;
  133.     }
  134.     vsnprintf (s, bufsize, format, ap);
  135.     if (!errors_add (self, s, result))
  136.         ok = true;
  137.  
  138. _local_exit:
  139.     if (s)
  140.         free (s);
  141.     if (!ok)
  142.         if (result)
  143.             *result = (struct error_entry_t *) NULL;
  144.     return !ok;
  145. }
  146.  
  147. bool
  148.     errors_add_fmt
  149.     (
  150.         struct errors_t *self,
  151.         struct error_entry_t **result,
  152.         unsigned bufsize,
  153.         const char *format,
  154.         ...
  155.     )
  156. {
  157.     bool ok;
  158.     va_list ap;
  159.  
  160.     va_start (ap, format);
  161.  
  162.     ok = false;
  163.  
  164.     if (!self || !bufsize || !format)
  165.     {
  166.         _DBG ("Bad arguments.");
  167.         goto _local_exit;
  168.     }
  169.  
  170.     if (!errors_add_vfmt (self, result, bufsize, format, ap))
  171.         ok = true;
  172.  
  173. _local_exit:
  174.     va_end (ap);
  175.     if (!ok)
  176.         if (result)
  177.             *result = (struct error_entry_t *) NULL;
  178.     return !ok;
  179. }
  180.  
  181. void
  182.     errors_free
  183.     (
  184.         struct errors_t *self
  185.     )
  186. {
  187.     struct error_entry_t *p, *n;
  188.  
  189.     p = (struct error_entry_t *) self->list.first;
  190.     while (p)
  191.     {
  192.         n = (struct error_entry_t *) p->list_entry.next;
  193.         error_entry_free (p);
  194.         free (p);
  195.         p = n;
  196.     }
  197.     errors_clear (self);
  198. }
  199.