home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / anwor032.zip / antiword.0.32 / stylelist.c < prev    next >
C/C++ Source or Header  |  2000-10-26  |  4KB  |  185 lines

  1. /*
  2.  * stylelist.c
  3.  * Copyright (C) 1998-2000 A.J. van Os
  4.  *
  5.  * Description:
  6.  * Build, read and destroy a list of Word style information
  7.  */
  8.  
  9. #include <stdlib.h>
  10. #include <ctype.h>
  11. #include "antiword.h"
  12.  
  13.  
  14. /* Variables needed to write the Style Information List */
  15. static style_desc_type    *pAnchor = NULL;
  16. static style_desc_type    *pStyleLast = NULL;
  17. /* Variable needed to read the Style Information List */
  18. static style_desc_type    *pStyleCurrent = NULL;
  19.  
  20.  
  21. /*
  22.  * vDestroyStyleInfoList - destroy the Style Information List
  23.  */
  24. void
  25. vDestroyStyleInfoList(void)
  26. {
  27.     style_desc_type    *pCurr, *pNext;
  28.  
  29.     DBG_MSG("vDestroyStyleInfoList");
  30.  
  31.     /* Free the Style Information List */
  32.     pCurr = pAnchor;
  33.     while (pCurr != NULL) {
  34.         pNext = pCurr->pNext;
  35.         pCurr = xfree(pCurr);
  36.         pCurr = pNext;
  37.     }
  38.     pAnchor = NULL;
  39.     /* Reset all control variables */
  40.     pStyleLast = NULL;
  41.     pStyleCurrent = NULL;
  42. } /* end of vDestroyStyleInfoList */
  43.  
  44. /*
  45.  * ucChooseListCharacter - choose our list character
  46.  */
  47. static unsigned char
  48. ucChooseListCharacter(unsigned char ucListType, unsigned char ucListChar)
  49. {
  50.     if (isprint(ucListChar)) {
  51.         return ucListChar;
  52.     }
  53.     if (ucListType == LIST_BULLETS) {
  54.         switch (ucListChar) {
  55.         case 0xa8:
  56.             return OUR_DIAMOND;
  57.         case 0xb7:
  58.             return OUR_BULLET;
  59.         case 0xde:
  60.             return '=';
  61.         case 0xe0:
  62.             return 'o';
  63.         case 0xfe:
  64.             return ' ';
  65.         default:
  66.             DBG_HEX(ucListChar);
  67.             return OUR_BULLET;
  68.         }
  69.     }
  70.     return '.';
  71. } /* end of ucChooseListCharacter */
  72.  
  73. /*
  74.  * vAdd2StyleInfoList - Add an element to the Style Information List
  75.  */
  76. void
  77. vAdd2StyleInfoList(const style_block_type *pStyleBlock)
  78. {
  79.     static BOOL    bMustStore = FALSE;
  80.     style_desc_type    *pListMember;
  81.  
  82.     fail(pStyleBlock == NULL);
  83.     fail(pStyleBlock->lFileOffset < -1);
  84.  
  85.     NO_DBG_MSG("bAdd2StyleInfoList");
  86.  
  87.     if (pStyleBlock->lFileOffset < 0) {
  88.         return;
  89.     }
  90.  
  91.     switch (pStyleBlock->ucStyle) {
  92.     case 1: case 2: case 3:
  93.     case 4: case 5: case 6:
  94.     case 7: case 8: case 9:
  95.         /* These styles are the nine header levels */
  96.         break;
  97.     default:
  98.         if (pStyleBlock->sLeftIndent > 0 ||
  99.             pStyleBlock->sRightIndent < 0 ||
  100.             pStyleBlock->ucAlignment != 0x00 ||
  101.             bMustStore) {
  102.             break;
  103.         }
  104.         /*
  105.          * When reached here, the information is not useful to
  106.          * the current implementation, so we save memory by
  107.          * not storing them.
  108.          */
  109.         return;
  110.     }
  111.  
  112.     NO_DBG_HEX(pStyleBlock->lFileOffset);
  113.     NO_DBG_DEC_C(pStyleBlock->sLeftIndent != 0,
  114.         pStyleBlock->sLeftIndent);
  115.     NO_DBG_DEC_C(pStyleBlock->sRightIndent != 0,
  116.         pStyleBlock->sRightIndent);
  117.     NO_DBG_DEC_C(pStyleBlock->bInList, pStyleBlock->bInList);
  118.     NO_DBG_DEC_C(pStyleBlock->bUnmarked, pStyleBlock->bUnmarked);
  119.     NO_DBG_DEC_C(pStyleBlock->ucStyle != 0, pStyleBlock->ucStyle);
  120.     NO_DBG_DEC_C(pStyleBlock->ucAlignment != 0, pStyleBlock->ucAlignment);
  121.     NO_DBG_DEC_C(pStyleBlock->bInList, pStyleBlock->ucListType);
  122.     NO_DBG_HEX_C(pStyleBlock->bInList, pStyleBlock->ucListCharacter);
  123.  
  124.     /*
  125.      * Always store the information about the paragraph
  126.      * following a member of a list.
  127.      */
  128.     bMustStore = pStyleBlock->bInList;
  129.  
  130.     if (pStyleLast != NULL &&
  131.         pStyleLast->tInfo.lFileOffset == pStyleBlock->lFileOffset) {
  132.         /*
  133.          * If two consecutive styles share the same
  134.          * offset, remember only the last style
  135.          */
  136.         fail(pStyleLast->pNext != NULL);
  137.         pStyleLast->tInfo = *pStyleBlock;
  138.         return;
  139.     }
  140.  
  141.     /* Create list member */
  142.     pListMember = xmalloc(sizeof(style_desc_type));
  143.     /* Fill the list member */
  144.     pListMember->tInfo = *pStyleBlock;
  145.     pListMember->pNext = NULL;
  146.     /* Correct the values where needed */
  147.     if (pListMember->tInfo.sLeftIndent < 0) {
  148.         pListMember->tInfo.sLeftIndent = 0;
  149.     }
  150.     if (pListMember->tInfo.sRightIndent > 0) {
  151.         pListMember->tInfo.sRightIndent = 0;
  152.     }
  153.     if (pListMember->tInfo.ucListLevel > 8) {
  154.         pListMember->tInfo.ucListLevel = 8;
  155.     }
  156.     pListMember->tInfo.ucListCharacter =
  157.         ucChooseListCharacter(pListMember->tInfo.ucListType,
  158.                 pListMember->tInfo.ucListCharacter);
  159.     /* Add the new member to the list */
  160.     if (pAnchor == NULL) {
  161.         pAnchor = pListMember;
  162.         pStyleCurrent = pListMember;
  163.     } else {
  164.         fail(pStyleLast == NULL);
  165.         pStyleLast->pNext = pListMember;
  166.     }
  167.     pStyleLast = pListMember;
  168. } /* end of vAdd2StyleInfoList */
  169.  
  170. /*
  171.  * Get the next item in the Style Information List
  172.  */
  173. const style_block_type *
  174. pGetNextStyleInfoListItem(void)
  175. {
  176.     const style_block_type    *pItem;
  177.  
  178.     if (pStyleCurrent == NULL) {
  179.         return NULL;
  180.     }
  181.     pItem = &pStyleCurrent->tInfo;
  182.     pStyleCurrent = pStyleCurrent->pNext;
  183.     return pItem;
  184. } /* end of pGetNextStyleInfoListItem */
  185.