PandA-2024.02
indented_output_stream.cpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
44 
45 #include "exceptions.hpp"
46 
47 #include <cstddef>
48 #include <fstream>
49 #include <iostream>
50 
52 extern size_t indentation;
53 
54 IndentedOutputStream::IndentedOutputStream(char o, char c, unsigned int d)
55  : indent_spaces(0), opening_char(o), closing_char(c), delta(d), is_line_start(true)
56 {
57 }
58 
60 {
61 #ifndef NDEBUG
62  if(indent_spaces != 0)
64  {
65  std::cerr << "Not all indentations have been closed: " << indentation << std::endl;
66  }
67 #endif
68 }
69 
70 void IndentedOutputStream::Append(const std::string& str)
71 {
72  std::string::const_iterator it_end = str.end();
75  bool needToInd = false;
76 
77  if(*(str.begin()) == closing_char)
78  {
79 #if 0
80  if(delta > indent_spaces)
81  {
82  WriteFile("Error");
83  THROW_UNREACHABLE("Indentation becomes negative");
84  }
85 #endif
86  Deindent();
87  }
88 
89  if(is_line_start)
90  {
91  AppendIndent();
92  }
93 
94  for(std::string::const_iterator it = str.begin(); it != it_end; ++it)
95  {
96  is_line_start = false;
97  if(*it == '\n')
98  {
99  output_stream << *it;
100  if((it + 1) != it_end)
101  {
102  if(*(it + 1) != closing_char)
103  {
104  AppendIndent();
105  }
106  else
107  {
108  needToInd = true;
109  }
110  }
111  else
112  {
113  is_line_start = true;
114  }
115  }
116  else if(*it == opening_char)
117  {
118  indent_spaces += delta;
120  {
121  AppendChar(*it);
122  }
123  }
124  else if(*it == closing_char)
125  {
126  if(*(str.begin()) != closing_char and it != str.begin())
127  {
128  Deindent();
129  }
130  if(needToInd)
131  {
132  AppendIndent();
133  needToInd = false;
134  }
136  {
137  AppendChar(*it);
138  }
139  }
140  else
141  {
142  AppendChar(*it);
143  }
144  }
145 }
146 
148 {
149  output_stream << str;
150 }
151 
153 {
154  for(unsigned int i = 0; i < indent_spaces; i++)
155  {
156  output_stream << " ";
157  }
158 }
159 
161 {
162  output_stream << c;
163 }
164 
166 {
167  indent_spaces += delta;
168 }
169 
171 {
172  THROW_ASSERT(indent_spaces >= indent_spaces - delta, "Indentation has become negative");
173  indent_spaces -= delta;
174 }
175 
177 {
178  std::string ret;
179  ret = output_stream.str();
180  return ret;
181 }
182 
183 void IndentedOutputStream::WriteFile(const std::string& file_name)
184 {
185  std::ofstream file_out(file_name.c_str(), std::ios::out);
186  file_out << output_stream.str() << std::endl;
187  file_out.close();
188  output_stream.str(std::string());
189 }
void WriteFile(const std::string &file_name)
Write the indented output on a file.
size_t indentation
In global_variables.hpp.
void AppendIndent()
Append the indent spaces.
unsigned int delta
delta indent space
IndentedOutputStream(char o='{', char c='}', unsigned int d=3)
constructor
void AppendIndented(const std::string &str)
Append a pre-indented string to the output.
void Indent()
Manually increase the indenting of the code.
exceptions managed by PandA
Class to print indented code.
char closing_char
char that increments the indent space by a delta
const std::string WriteString()
Write the indented output on a string.
#define THROW_UNREACHABLE(str_expr)
helper function used to specify that some points should never be reached
Definition: exceptions.hpp:292
void Append(const std::string &str)
Append a string to the output.
char opening_char
char that increments the indent space by a delta
unsigned int indent_spaces
number of spaces used to indent after a new line print
std::ostringstream output_stream
The actual stream.
#define STD_OPENING_CHAR
STD include.
#define STD_CLOSING_CHAR
Special closing character used to close the current nested level.
char str[25]
Definition: fixedptc.c:8
void AppendChar(const char &c)
Append the current char.
void Deindent()
Manually reduce the indenting of the code.
#define THROW_ASSERT(cond, str_expr)
helper function used to check an assert and if needed to throw an error in a standard way ...
Definition: exceptions.hpp:289

Generated on Mon Feb 12 2024 13:02:56 for PandA-2024.02 by doxygen 1.8.13