1 /******************************************************************************
2  *
3  *
4  *
5  * Copyright (C) 1997-2015 by Dimitri van Heesch.
6  *
7  * Permission to use, copy, modify, and distribute this software and its
8  * documentation under the terms of the GNU General Public License is hereby
9  * granted. No representations are made about the suitability of this software
10  * for any purpose. It is provided "as is" without express or implied warranty.
11  * See the GNU General Public License for more details.
12  *
13  * Documents produced by Doxygen are derivative works derived from the
14  * input used in their production; they are not affected by this license.
15  *
16  */
17 
18 #include <stdexcept>
19 
20 #include <stdlib.h>
21 
22 #include "doxygen.h"
23 #include "outputgen.h"
24 #include "message.h"
25 #include "portable.h"
26 
OutputGenerator(const QCString & dir)27 OutputGenerator::OutputGenerator(const QCString &dir) : m_t(nullptr), m_dir(dir)
28 {
29   //printf("OutputGenerator::OutputGenerator()\n");
30 }
31 
~OutputGenerator()32 OutputGenerator::~OutputGenerator()
33 {
34   //printf("OutputGenerator::~OutputGenerator()\n");
35 }
36 
OutputGenerator(const OutputGenerator & og)37 OutputGenerator::OutputGenerator(const OutputGenerator &og) : m_t(nullptr)
38 {
39   m_dir = og.m_dir;
40   // we don't copy the other fields.
41   // after copying startPlainFile() should be called
42   if (og.m_t.stream()!=nullptr)
43   {
44     throw std::runtime_error("OutputGenerator copy constructor called while a file is processing");
45   }
46 }
47 
operator =(const OutputGenerator & og)48 OutputGenerator &OutputGenerator::operator=(const OutputGenerator &og)
49 {
50   m_dir = og.m_dir;
51   // we don't copy the other fields.
52   // after assignment startPlainFile() should be called
53   if (og.m_t.stream()!=nullptr)
54   {
55     throw std::runtime_error("OutputGenerator assignment operator called while a file is processing");
56   }
57   return *this;
58 }
59 
startPlainFile(const QCString & name)60 void OutputGenerator::startPlainFile(const QCString &name)
61 {
62   //printf("startPlainFile(%s)\n",qPrint(name));
63   m_fileName=m_dir+"/"+name;
64   m_file = Portable::fopen(m_fileName.data(),"wb");
65   if (m_file==0)
66   {
67     term("Could not open file %s for writing\n",qPrint(m_fileName));
68   }
69   m_t.setFile(m_file);
70 }
71 
endPlainFile()72 void OutputGenerator::endPlainFile()
73 {
74   m_t.flush();
75   m_t.setStream(nullptr);
76   Portable::fclose(m_file);
77   m_fileName.resize(0);
78 }
79 
dir() const80 QCString OutputGenerator::dir() const
81 {
82   return m_dir;
83 }
84 
fileName() const85 QCString OutputGenerator::fileName() const
86 {
87   return m_fileName;
88 }
89 
pushGeneratorState()90 void OutputGenerator::pushGeneratorState()
91 {
92   m_genStack.push(isEnabled());
93   //printf("%p:pushGeneratorState(%d) enabled=%d\n",this,genStack->count(),isEnabled());
94 }
95 
popGeneratorState()96 void OutputGenerator::popGeneratorState()
97 {
98   //printf("%p:popGeneratorState(%d) enabled=%d\n",this,genStack->count(),isEnabled());
99   if (!m_genStack.empty())
100   {
101     bool lb = m_genStack.top();
102     m_genStack.pop();
103     if (lb) enable(); else disable();
104   }
105 }
106 
enable()107 void OutputGenerator::enable()
108 {
109   if (!m_genStack.empty())
110   {
111     m_active=m_genStack.top();
112   }
113   else
114   {
115     m_active=true;
116   }
117 }
118 
disable()119 void OutputGenerator::disable()
120 {
121   m_active=false;
122 }
123 
enableIf(OutputGenerator::OutputType o)124 void OutputGenerator::enableIf(OutputGenerator::OutputType o)
125 {
126   if (o==type()) enable();
127 }
128 
disableIf(OutputGenerator::OutputType o)129 void OutputGenerator::disableIf(OutputGenerator::OutputType o)
130 {
131   if (o==type()) disable();
132 }
133 
disableIfNot(OutputGenerator::OutputType o)134 void OutputGenerator::disableIfNot(OutputGenerator::OutputType o)
135 {
136   if (o!=type()) disable();
137 }
138 
isEnabled(OutputGenerator::OutputType o)139 bool OutputGenerator::isEnabled(OutputGenerator::OutputType o)
140 {
141   return (o==type() && m_active);
142 }
143 
get(OutputGenerator::OutputType o)144 OutputGenerator *OutputGenerator::get(OutputGenerator::OutputType o)
145 {
146   return (o==type()) ? this : 0;
147 }
148 
149