home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / anwor032.zip / antiword.0.32 / propmod.c < prev    next >
C/C++ Source or Header  |  2001-09-25  |  2KB  |  111 lines

  1. /*
  2.  * propmod.c
  3.  * Copyright (C) 2001 A.J. van Os; Released under GPL
  4.  *
  5.  * Description:
  6.  * Build, read and destroy a list (array) of Word property modifiers
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "antiword.h"
  12.  
  13. #if defined(DEBUG)
  14. #define ELEMENTS_TO_ADD     3
  15. #else
  16. #define ELEMENTS_TO_ADD    30
  17. #endif /* DEBUG */
  18.  
  19. /* Variables needed to write the property modifier list */
  20. static unsigned char    **ppAnchor = NULL;
  21. static int        iNextFree = 0;
  22. static int        iMaxElements = 0;
  23.  
  24.  
  25. /*
  26.  * vDestroyPropModList - destroy the property modifier list
  27.  */
  28. void
  29. vDestroyPropModList(void)
  30. {
  31.     int    iIndex;
  32.  
  33.     DBG_MSG("vDestroyPropModList");
  34.  
  35.     /* Free all the elements of the list */
  36.     for (iIndex = 0; iIndex < iNextFree; iIndex++) {
  37.         ppAnchor[iIndex] = xfree(ppAnchor[iIndex]);
  38.     }
  39.     /* Free the list itself */
  40.     ppAnchor = xfree(ppAnchor);
  41.     /* Reset all control variables */
  42.     iNextFree = 0;
  43.     iMaxElements = 0;
  44. } /* end of vDestroyPropModList */
  45.  
  46. /*
  47.  * vAdd2PropModList - add an element to the property modifier list
  48.  */
  49. void
  50. vAdd2PropModList(const unsigned char *pucPropMod)
  51. {
  52.     size_t    tSize, tLen;
  53.  
  54.     fail(pucPropMod == NULL);
  55.  
  56.     DBG_MSG("vAdd2PropModList");
  57.  
  58.     if (iNextFree >= iMaxElements) {
  59.         iMaxElements += ELEMENTS_TO_ADD;
  60.         tSize = iMaxElements * sizeof(unsigned char **);
  61.         ppAnchor = xrealloc(ppAnchor, tSize);
  62.     }
  63.     DBG_DEC(iNextFree);
  64.  
  65.     tLen = 2 + (size_t)usGetWord(0, pucPropMod);
  66.     DBG_HEX(tLen);
  67.     DBG_PRINT_BLOCK(pucPropMod, tLen);
  68.     ppAnchor[iNextFree] = xmalloc(tLen);
  69.     memcpy(ppAnchor[iNextFree], pucPropMod, tLen);
  70.     iNextFree++;
  71. } /* end of vAdd2PropModList */
  72.  
  73. /*
  74.  * pucGetPropModListItem - get an item of the property modifier list
  75.  */
  76. const unsigned char *
  77. pucReadPropModListItem(unsigned short usPropMod)
  78. {
  79.     int    iIndex;
  80.     static unsigned char    aucBuffer[4];
  81.  
  82.     if (usPropMod == IGNORE_PROPMOD) {
  83.         /* This Properties Modifier must be ignored */
  84.         return NULL;
  85.     }
  86.  
  87.     if (!odd(usPropMod)) {
  88.         /* Variant 1: The information is in the input ifself */
  89.         aucBuffer[0] = 2;
  90.         aucBuffer[1] = 0;
  91.         aucBuffer[2] = (unsigned char)((usPropMod & 0x00fe) >> 1);
  92.         aucBuffer[3] = (unsigned char)((usPropMod & 0xff00) >> 8);
  93.         return aucBuffer;
  94.     }
  95.  
  96.     if (ppAnchor == NULL) {
  97.         /* No information available */
  98.         return NULL;
  99.     }
  100.  
  101.     /* Variant 2: The input contains an index */
  102.     iIndex = (int)(usPropMod >> 1);
  103.     if (iIndex < 0 || iIndex >= iNextFree) {
  104.         DBG_HEX(usPropMod);
  105.         DBG_DEC(iIndex);
  106.         DBG_DEC(iNextFree);
  107.         return NULL;
  108.     }
  109.     return ppAnchor[iIndex];
  110. } /* end of pucGetPropModListItem */
  111.