?login_element?

Subversion Repositories NedoOS

Rev

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

  1. /* l_ifile.c -- include files 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 <limits.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "debug.h"
  14. #include "platform.h"
  15. #include "l_list.h"
  16. #include "l_ifile.h"
  17.  
  18. void
  19.     included_file_entry_clear
  20.     (
  21.         struct included_file_entry_t *self
  22.     )
  23. {
  24.     list_entry_clear (&self->list_entry);
  25.     self->line = 0;     // invalid
  26.     self->flags = 0;
  27.     self->name = NULL;
  28. }
  29.  
  30. void
  31.     included_file_entry_free
  32.     (
  33.         struct included_file_entry_t *self
  34.     )
  35. {
  36.     list_entry_free (&self->list_entry);
  37.     if (self->name)
  38.         free (self->name);
  39.     included_file_entry_clear (self);
  40. }
  41.  
  42. void
  43.     included_files_clear
  44.     (
  45.         struct included_files_t *self
  46.     )
  47. {
  48.     list_clear (&self->list);
  49. }
  50.  
  51. bool
  52.     included_files_add
  53.     (
  54.         struct included_files_t *self,
  55.         unsigned line,
  56.         unsigned flags,
  57.         const char *name,
  58.         struct included_file_entry_t **result
  59.     )
  60. {
  61.     bool ok;
  62.     struct included_file_entry_t *p;
  63.     char *p_name;
  64. #if DEBUG == 1
  65.     unsigned i;
  66. #endif  // DEBUG == 1
  67.  
  68.     ok = false;
  69.     p = (struct included_file_entry_t *) NULL;
  70.     p_name = (char *) NULL;
  71.  
  72.     if (!self || !name)
  73.     {
  74.         _DBG ("Bad arguments.");
  75.         goto _local_exit;
  76.     }
  77.  
  78.     p = malloc (sizeof (struct included_file_entry_t));
  79.     if (!p)
  80.     {
  81.         _perror ("malloc");
  82.         goto _local_exit;
  83.     }
  84.     p_name = strdup (name);
  85.     if (!p_name)
  86.     {
  87.         _perror ("strdup");
  88.         goto _local_exit;
  89.     }
  90.  
  91.     included_file_entry_clear (p);
  92.     p->line = line;
  93.     p->flags = flags;
  94.     p->name = p_name;
  95.  
  96. #if DEBUG == 1
  97.     i = self->list.count;
  98. #endif  // DEBUG == 1
  99.     list_add_entry ((struct list_t *) self, (struct list_entry_t *) p);
  100.  
  101.     _DBG_ ("Added new included file #%u:", i);
  102.     _DBG_ ("Included file #%u line = %u", i, p->line);
  103.     _DBG_ ("Included file #%u flags = 0x%X", i, p->flags);
  104.     _DBG_ ("Included file #%u user file = '%s'", i, p->name);
  105.  
  106.     ok = true;
  107.  
  108. _local_exit:
  109.     if (!ok)
  110.     {
  111.         if (p)
  112.         {
  113.             free (p);
  114.             p = (struct included_file_entry_t *) NULL;
  115.         }
  116.         if (p_name)
  117.             free (p_name);
  118.     }
  119.     if (result)
  120.         *result = p;
  121.     return !ok;
  122. }
  123.  
  124. bool
  125.     included_files_find
  126.     (
  127.         struct included_files_t *self,
  128.         const char *name,
  129.         struct included_file_entry_t **result
  130.     )
  131. {
  132.     bool ok;
  133.     struct included_file_entry_t *p;
  134.     unsigned i;
  135.  
  136.     ok = false;
  137.     p = (struct included_file_entry_t *) NULL;
  138.  
  139.     if (!self || !name)
  140.     {
  141.         _DBG ("Bad arguments.");
  142.         goto _local_exit;
  143.     }
  144.  
  145.     p = (struct included_file_entry_t *) self->list.first;
  146.     i = 0;
  147.     while (p)
  148.     {
  149.         if (!strcmp (p->name, name))
  150.         {
  151.             // Success
  152.             _DBG_ ("Found included file '%s' at #%u.", p->name, i);
  153.             ok = true;
  154.             goto _local_exit;
  155.         }
  156.         p = (struct included_file_entry_t *) p->list_entry.next;
  157.         i++;
  158.     }
  159.  
  160.     // Fail
  161.     //p = (struct included_file_entry_t *) NULL;
  162.     _DBG_ ("Failed to find included file '%s'.", name);
  163.  
  164. _local_exit:
  165.     if (result)
  166.         *result = p;
  167.     return !ok;
  168. }
  169.  
  170. void
  171.     included_files_free
  172.     (
  173.         struct included_files_t *self
  174.     )
  175. {
  176.     struct included_file_entry_t *p, *n;
  177.  
  178.     p = (struct included_file_entry_t *) self->list.first;
  179.     while (p)
  180.     {
  181.         n = (struct included_file_entry_t *) p->list_entry.next;
  182.         included_file_entry_free (p);
  183.         free (p);
  184.         p = n;
  185.     }
  186.     included_files_clear (self);
  187. }
  188.