?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  1. /* l_pre.c - prerequisites 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_pre.h"
  16.  
  17. void
  18.     prerequisite_entry_clear
  19.     (
  20.         struct prerequisite_entry_t *self
  21.     )
  22. {
  23.     list_entry_clear (&self->list_entry);
  24.     self->prerequisite = NULL;
  25. }
  26.  
  27. void
  28.     prerequisite_entry_free
  29.     (
  30.         struct prerequisite_entry_t *self
  31.     )
  32. {
  33.     list_entry_free (&self->list_entry);
  34.     if (self->prerequisite)
  35.         free (self->prerequisite);
  36.     prerequisite_entry_clear (self);
  37. }
  38.  
  39. void
  40.     prerequisites_clear
  41.     (
  42.         struct prerequisites_t *self
  43.     )
  44. {
  45.     list_clear (&self->list);
  46. }
  47.  
  48. bool
  49.     prerequisites_add
  50.     (
  51.         struct prerequisites_t *self,
  52.         const char *prerequisite,
  53.         struct prerequisite_entry_t **result
  54.     )
  55. {
  56.     bool ok;
  57.     struct prerequisite_entry_t *p;
  58.     char *p_prerequisite;
  59. #if DEBUG == 1
  60.     unsigned i;
  61. #endif  // DEBUG == 1
  62.  
  63.     ok = false;
  64.     p = (struct prerequisite_entry_t *) NULL;
  65.     p_prerequisite = (char *) NULL;
  66.  
  67.     if (!self || !prerequisite)
  68.     {
  69.         _DBG ("Bad arguments.");
  70.         goto _local_exit;
  71.     }
  72.  
  73.     p = malloc (sizeof (struct prerequisite_entry_t));
  74.     if (!p)
  75.     {
  76.         _perror ("malloc");
  77.         goto _local_exit;
  78.     }
  79.     p_prerequisite = strdup (prerequisite);
  80.     if (!p_prerequisite)
  81.     {
  82.         _perror ("strdup");
  83.         goto _local_exit;
  84.     }
  85.  
  86.     p->list_entry.next = NULL;
  87.     p->prerequisite = p_prerequisite;
  88.  
  89. #if DEBUG == 1
  90.     i = self->list.count;
  91. #endif  // DEBUG == 1
  92.     list_add_entry ((struct list_t *) self,  (struct list_entry_t *) p);
  93.  
  94.     _DBG_ ("Added new prerequisite #%u: '%s'", i, p->prerequisite);
  95.  
  96.     ok = true;
  97.  
  98. _local_exit:
  99.     if (!ok)
  100.     {
  101.         if (p)
  102.         {
  103.             free (p);
  104.             p = (struct prerequisite_entry_t *) NULL;
  105.         }
  106.         if (p_prerequisite)
  107.             free (p_prerequisite);
  108.     }
  109.     if (result)
  110.         *result = p;
  111.     return !ok;
  112. }
  113.  
  114. bool
  115.     prerequisites_print
  116.     (
  117.         struct prerequisites_t *self,
  118.         FILE *stream
  119.     )
  120. {
  121.     const struct prerequisite_entry_t *p;
  122.     bool padding;
  123.  
  124.     if (!self || !stream)
  125.     {
  126.         _DBG ("Bad arguments.");
  127.         return true;
  128.     }
  129.  
  130.     padding = false;
  131.     for (p = (struct prerequisite_entry_t *) self->list.first; p;
  132.          p = (struct prerequisite_entry_t *) p->list_entry.next)
  133.     {
  134.         if (fprintf (stream, padding ? " %s" : "%s", p->prerequisite) < 0)
  135.         {
  136.             _perror ("fprintf");
  137.             return true;
  138.         }
  139.         padding = true;
  140.     }
  141.  
  142.     return false;
  143. }
  144.  
  145. void
  146.     prerequisites_free
  147.     (
  148.         struct prerequisites_t *self
  149.     )
  150. {
  151.     struct prerequisite_entry_t *p, *n;
  152.  
  153.     p = (struct prerequisite_entry_t *) self->list.first;
  154.     while (p)
  155.     {
  156.         n = (struct prerequisite_entry_t *) p->list_entry.next;
  157.         prerequisite_entry_free (p);
  158.         free (p);
  159.         p = n;
  160.     }
  161.     prerequisites_clear (self);
  162. }
  163.