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

  1. /* eadadd.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 <errno.h>
  8. #include <sys/ead.h>
  9. #include "ea.h"
  10.  
  11. int _ead_add (_ead ead, const char *name, int flags, const void *value,
  12.               int size)
  13. {
  14.   int i, len, new_size, offset;
  15.   PFEA2 dst, last;
  16.  
  17.   i = _ead_find (ead, name);
  18.   if (i >= 1)
  19.     {
  20.       if (_ead_replace (ead, i, flags, value, size) < 0)
  21.         return (-1);
  22.       return (i);
  23.     }
  24.   len = strlen (name);
  25.   new_size = _EA_SIZE1 (len, size);
  26.   if (ead->count == 0)
  27.     offset = sizeof (ULONG);
  28.   else
  29.     {
  30.       offset = ead->buffer->cbList;
  31.       last = ead->index[ead->count];
  32.       last->oNextEntryOffset = _EA_SIZE2 (last);
  33.     }
  34.   if (_ead_size_buffer (ead, offset + new_size) < 0)
  35.     return (-1);
  36.   ead->buffer->cbList = offset + new_size;
  37.   dst = (PFEA2)((char *)ead->buffer + offset);
  38.   dst->oNextEntryOffset = 0;
  39.   dst->fEA = flags;
  40.   dst->cbName = len;
  41.   dst->cbValue = size;
  42.   memcpy (dst->szName, name, len + 1);
  43.   memcpy (dst->szName + len + 1, value, size);
  44.   if (_ead_make_index (ead, ead->count + 1) < 0)
  45.     return (-1);
  46.   ++ead->count;
  47.   return (ead->count);
  48. }
  49.  
  50.  
  51. int _ead_replace (_ead ead, int index, int flags, const void *value, int size)
  52. {
  53.   PFEA2 dst;
  54.   int old_size, new_size, offset;
  55.  
  56.   if (index < 1 || index > ead->count)
  57.     {
  58.       errno = EINVAL;
  59.       return (-1);
  60.     }
  61.   dst = ead->index[index-1];
  62.   new_size = _EA_SIZE1 (dst->cbName, size);
  63.   old_size = _EA_SIZE2 (dst);
  64.   offset = (char *)dst - (char *)ead->buffer;
  65.   dst = NULL;
  66.   if (new_size > old_size)
  67.     {
  68.       if (_ead_size_buffer (ead, ead->buffer_size - old_size + new_size) < 0)
  69.         return (-1);
  70.     }
  71.   dst = (PFEA2)((char *)ead->buffer + offset);
  72.   if (new_size != old_size && index != ead->count)
  73.     memmove ((char *)dst + new_size, (char *)dst + old_size,
  74.              ead->buffer->cbList - (offset + old_size));
  75.   dst->fEA = flags;
  76.   dst->cbValue = size;
  77.   dst->oNextEntryOffset = (index == ead->count ? 0 : new_size);
  78.   ead->buffer->cbList += new_size - old_size;
  79.   memcpy (dst->szName + dst->cbName + 1, value, size);
  80.   return (_ead_make_index (ead, ead->count));
  81. }
  82.  
  83.  
  84. int _ead_delete (_ead ead, int index)
  85. {
  86.   PFEA2 ptr;
  87.   int old_size;
  88.  
  89.   if (index < 1 || index > ead->count)
  90.     {
  91.       errno = EINVAL;
  92.       return (-1);
  93.     }
  94.   ptr = ead->index[index-1];
  95.   old_size = _EA_SIZE2 (ptr);
  96.   memmove (ptr, (char *)ptr + old_size,
  97.            ead->buffer->cbList - (((char *)ptr - (char *)ead->buffer)
  98.                                   + old_size));
  99.   ead->buffer->cbList -= old_size;
  100.   if (_ead_make_index (ead, ead->count - 1) < 0)
  101.     return (-1);
  102.   --ead->count;
  103.   return (0);
  104. }
  105.