Imported from antiword-0.37.tar.gz.
[antiword.git] / sectlist.c
blob016d1b89ac6f68f6340e916c1cc795b48eb5320a
1 /*
2 * sectlist.c
3 * Copyright (C) 2001-2004 A.J. van Os; Released under GNU GPL
5 * Description:
6 * Build, read and destroy list(s) of Word section information
7 */
9 #include <stddef.h>
10 #include <string.h>
11 #include "antiword.h"
15 * Private structure to hide the way the information
16 * is stored from the rest of the program
18 typedef struct section_mem_tag {
19 section_block_type tInfo;
20 ULONG ulCharPos;
21 struct section_mem_tag *pNext;
22 } section_mem_type;
24 /* Variables needed to write the Section Information List */
25 static section_mem_type *pAnchor = NULL;
26 static section_mem_type *pSectionLast = NULL;
30 * vDestroySectionInfoList - destroy the Section Information List
32 void
33 vDestroySectionInfoList(void)
35 section_mem_type *pCurr, *pNext;
37 DBG_MSG("vDestroySectionInfoList");
39 /* Free the Section Information List */
40 pCurr = pAnchor;
41 while (pCurr != NULL) {
42 pNext = pCurr->pNext;
43 pCurr = xfree(pCurr);
44 pCurr = pNext;
46 pAnchor = NULL;
47 /* Reset all control variables */
48 pSectionLast = NULL;
49 } /* end of vDestroySectionInfoList */
52 * vAdd2SectionInfoList - Add an element to the Section Information List
54 void
55 vAdd2SectionInfoList(const section_block_type *pSection, ULONG ulCharPos)
57 section_mem_type *pListMember;
59 fail(pSection == NULL);
61 /* Create list member */
62 pListMember = xmalloc(sizeof(section_mem_type));
63 /* Fill the list member */
64 pListMember->tInfo = *pSection;
65 pListMember->ulCharPos = ulCharPos;
66 pListMember->pNext = NULL;
67 /* Add the new member to the list */
68 if (pAnchor == NULL) {
69 pAnchor = pListMember;
70 } else {
71 fail(pSectionLast == NULL);
72 pSectionLast->pNext = pListMember;
74 pSectionLast = pListMember;
75 } /* vAdd2SectionInfoList */
78 * vGetDefaultSection - fill the section struct with default values
80 void
81 vGetDefaultSection(section_block_type *pSection)
83 (void)memset(pSection, 0, sizeof(*pSection));
84 pSection->bNewPage = TRUE;
85 } /* end of vGetDefaultSection */
88 * vDefault2SectionInfoList - Add a default to the Section Information List
90 void
91 vDefault2SectionInfoList(ULONG ulCharPos)
93 section_block_type tSection;
95 vGetDefaultSection(&tSection);
96 vAdd2SectionInfoList(&tSection, ulCharPos);
97 } /* end of vDefault2SectionInfoList */
100 * pGetSectionInfo - get the section information
102 const section_block_type *
103 pGetSectionInfo(const section_block_type *pOld, ULONG ulCharPos)
105 const section_mem_type *pCurr;
107 if (pOld == NULL || ulCharPos == 0) {
108 if (pAnchor == NULL) {
109 /* There are no records, make one */
110 vDefault2SectionInfoList(0);
111 fail(pAnchor == NULL);
113 /* The first record */
114 NO_DBG_MSG("First record");
115 return &pAnchor->tInfo;
118 NO_DBG_HEX(ulCharPos);
119 for (pCurr = pAnchor; pCurr != NULL; pCurr = pCurr->pNext) {
120 NO_DBG_HEX(pCurr->ulCharPos);
121 if (ulCharPos == pCurr->ulCharPos ||
122 ulCharPos + 1 == pCurr->ulCharPos) {
123 NO_DBG_HEX(pCurr->ulCharPos);
124 return &pCurr->tInfo;
127 return pOld;
128 } /* end of pGetSectionInfo */
131 * tGetNumberOfSections - get the number of sections
133 size_t
134 tGetNumberOfSections(void)
136 const section_mem_type *pCurr;
137 size_t tCounter;
139 for (tCounter = 0, pCurr = pAnchor;
140 pCurr != NULL;
141 tCounter++, pCurr = pCurr->pNext)
142 ; /* Empty */
143 return tCounter;
144 } /* end of tGetNumberOfSections */
147 * ucGetSepHdrFtrSpecification - get the Heder/footer specification
149 UCHAR
150 ucGetSepHdrFtrSpecification(size_t tSectionNumber)
152 const section_mem_type *pCurr;
153 size_t tIndex;
155 for (tIndex = 0, pCurr = pAnchor;
156 tIndex < tSectionNumber && pCurr != NULL;
157 tIndex++, pCurr = pCurr->pNext)
158 ; /* Empty */
159 if (pCurr == NULL) {
160 DBG_DEC(tSectionNumber);
161 DBG_FIXME();
162 return 0x00;
164 return pCurr->tInfo.ucHdrFtrSpecification;
165 } /* end of ucGetSepHdrFtrSpecification */