1 /* textoc.c */
2 /*****************************************************************************/
3 /* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only                     */
4 /*                                                                           */
5 /* AS                                                                        */
6 /*                                                                           */
7 /* TeX-->ASCII/HTML Converter: Table Of Contents                             */
8 /*                                                                           */
9 /*****************************************************************************/
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "strutil.h"
16 #include "textoc.h"
17 
18 /*--------------------------------------------------------------------------*/
19 
20 typedef struct sTocSave
21 {
22   struct sTocSave *pNext;
23   char *pTocName;
24 } tTocSave, *tpTocSave;
25 
26 /*--------------------------------------------------------------------------*/
27 
28 static tpTocSave pFirstTocSave;
29 
30 /*--------------------------------------------------------------------------*/
31 
32 /*!------------------------------------------------------------------------
33  * \fn     InitToc(void)
34  * \brief  initialize TOC
35  * ------------------------------------------------------------------------ */
36 
InitToc(void)37 void InitToc(void)
38 {
39   pFirstTocSave = NULL;
40 }
41 
42 /*!------------------------------------------------------------------------
43  * \fn     AddToc(const char *pLine, unsigned Indent)
44  * \brief  add TOC entry
45  * \param  pLine title of TOC entry
46  * \param  Indent indentation of title
47  * ------------------------------------------------------------------------ */
48 
AddToc(const char * pLine,unsigned Indent)49 void AddToc(const char *pLine, unsigned Indent)
50 {
51   tpTocSave pNewTocSave, pRunToc;
52 
53   pNewTocSave = (tpTocSave) malloc(sizeof(*pNewTocSave));
54   pNewTocSave->pNext = NULL;
55   pNewTocSave->pTocName = (char *) malloc(1 + Indent + strlen(pLine));
56   strcpy(pNewTocSave->pTocName, Blanks(Indent));
57   strcat(pNewTocSave->pTocName, pLine);
58   if (!pFirstTocSave)
59     pFirstTocSave = pNewTocSave;
60   else
61   {
62     for (pRunToc = pFirstTocSave; pRunToc->pNext; pRunToc = pRunToc->pNext);
63     pRunToc->pNext = pNewTocSave;
64   }
65 }
66 
67 /*!------------------------------------------------------------------------
68  * \fn     PrintToc(char *pFileName)
69  * \brief  dump TOC to file
70  * \param  pFileName where to write
71  * ------------------------------------------------------------------------ */
72 
PrintToc(char * pFileName)73 void PrintToc(char *pFileName)
74 {
75   tpTocSave pRun;
76   FILE *pFile = fopen(pFileName, "w");
77 
78   if (!pFile)
79     perror(pFileName);
80 
81   for (pRun = pFirstTocSave; pRun; pRun = pRun->pNext)
82     fprintf(pFile, "%s\n\n", pRun->pTocName);
83   fclose(pFile);
84 }
85 
86 /*!------------------------------------------------------------------------
87  * \fn     FreeToc(void)
88  * \brief  free TOC list
89  * ------------------------------------------------------------------------ */
90 
FreeToc(void)91 void FreeToc(void)
92 {
93   while (pFirstTocSave)
94   {
95     tpTocSave pOld = pFirstTocSave;
96     pFirstTocSave = pOld->pNext;
97     free(pOld->pTocName);
98     free(pOld);
99   }
100 }
101