home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / EMXLIB8F.ZIP / EMX / LIB / IO / EAD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-02  |  3.2 KB  |  175 lines

  1. /* ead.c (emx+gcc) -- Copyright (c) 1993 by Eberhard Mattes */
  2.  
  3. #define INCL_DOSFILEMGR
  4. #include <os2.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <io.h>
  8. #include <errno.h>
  9. #include <sys/ead.h>
  10. #include "ea.h"
  11.  
  12. _ead _ead_create (void)
  13. {
  14.   _ead p;
  15.  
  16.   p = malloc (sizeof (*p));
  17.   if (p == NULL)
  18.     {
  19.       errno = ENOMEM;
  20.       return (NULL);
  21.     }
  22.   p->count = 0;
  23.   p->max_count = 0;
  24.   p->total_value_size = 0;
  25.   p->total_name_len = 0;
  26.   p->buffer_size = 0;
  27.   p->index = NULL;
  28.   p->buffer = NULL;
  29.   return (p);
  30. }
  31.  
  32.  
  33. void _ead_destroy (_ead ead)
  34. {
  35.   if (ead->buffer != NULL) free (ead->buffer);
  36.   if (ead->index != NULL) free (ead->index);
  37.   free (ead);
  38. }
  39.  
  40.  
  41. void _ead_clear (_ead ead)
  42. {
  43.   ead->count = 0;
  44.   ead->total_value_size = 0;
  45.   ead->total_name_len = 0;
  46. }
  47.  
  48.  
  49. int _ead_count (_ead ead)
  50. {
  51.   return (ead->count);
  52. }
  53.  
  54.  
  55. int _ead_value_size (_ead ead, int index)
  56. {
  57.   if (index == 0)
  58.     return (ead->total_value_size);
  59.   else if (index < 1 || index > ead->count)
  60.     {
  61.       errno = EINVAL;
  62.       return (-1);
  63.     }
  64.   else
  65.     return (ead->index[index-1]->cbValue);
  66. }
  67.  
  68.  
  69. int _ead_name_len (_ead ead, int index)
  70. {
  71.   if (index == 0)
  72.     return (ead->total_name_len);
  73.   else if (index < 1 || index > ead->count)
  74.     {
  75.       errno = EINVAL;
  76.       return (-1);
  77.     }
  78.   else
  79.     return (ead->index[index-1]->cbName);
  80. }
  81.  
  82.  
  83. const char *_ead_get_name (_ead ead, int index)
  84. {
  85.   if (index < 1 || index > ead->count)
  86.     {
  87.       errno = EINVAL;
  88.       return (NULL);
  89.     }
  90.   else
  91.     return ((const char *)ead->index[index-1]->szName);
  92. }
  93.  
  94.  
  95. const void *_ead_get_value (_ead ead, int index)
  96. {
  97.   if (index < 1 || index > ead->count)
  98.     {
  99.       errno = EINVAL;
  100.       return (NULL);
  101.     }
  102.   else
  103.     return ((const void *)
  104.             ead->index[index-1]->szName + ead->index[index-1]->cbName + 1);
  105. }
  106.  
  107.  
  108. int _ead_get_flags (_ead ead, int index)
  109. {
  110.   if (index < 1 || index > ead->count)
  111.     {
  112.       errno = EINVAL;
  113.       return (-1);
  114.     }
  115.   else
  116.     return (ead->index[index-1]->fEA);
  117. }
  118.  
  119.  
  120. int _ead_find (_ead ead, const char *name)
  121. {
  122.   int i;
  123.  
  124.   for (i = 0; i < ead->count; ++i)
  125.     if (strcmp (name, ead->index[i]->szName) == 0)
  126.       return (i+1);
  127.   errno = ENOENT;
  128.   return (-1);
  129. }
  130.  
  131.  
  132. int _ead_make_index (struct _ead_data *ead, int new_count)
  133. {
  134.   int i;
  135.   PFEA2 pfea;
  136.  
  137.   if (new_count > ead->max_count)
  138.     {
  139.       ead->max_count = new_count;
  140.       ead->index = realloc (ead->index, ead->max_count * sizeof (*ead->index));
  141.       if (ead->index == NULL)
  142.         {
  143.           ead->max_count = 0;
  144.           ead->count = 0;
  145.           errno = ENOMEM;
  146.           return (-1);
  147.         }
  148.     }
  149.   pfea = &ead->buffer->list[0];
  150.   for (i = 0; i < new_count; ++i)
  151.     {
  152.       ead->index[i] = (PFEA2)pfea;
  153.       pfea = (PFEA2)((char *)pfea + pfea->oNextEntryOffset);
  154.     }
  155.   return (0);
  156. }
  157.  
  158.  
  159. int _ead_size_buffer (struct _ead_data *ead, int new_size)
  160. {
  161.   if (new_size > ead->buffer_size)
  162.     {
  163.       ead->buffer_size = new_size;
  164.       ead->buffer = realloc (ead->buffer, ead->buffer_size);
  165.       if (ead->buffer == NULL)
  166.         {
  167.           ead->buffer_size = 0;
  168.           ead->count = 0;
  169.           errno = ENOMEM;
  170.           return (-1);
  171.         }
  172.     }
  173.   return (0);
  174. }
  175.