PandA-2024.02
simple_indent.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  */
45 #include <iostream>
46 #include <string>
47 
48 #include "dbgPrintHelper.hpp"
50 #include "exceptions.hpp"
52 #include "simple_indent.hpp"
53 
54 simple_indent::simple_indent(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 simple_indent::operator()(std::ostream& os, 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  deindent();
80  }
81 
82  if(is_line_start)
83  {
84  write_indent(os);
85  }
86 
87  for(std::string::const_iterator it = str.begin(); it != it_end; ++it)
88  {
89  is_line_start = false;
90  if(*it == '\n')
91  {
92  os << *it;
93  if((it + 1) != it_end)
94  {
95  if(*(it + 1) != closing_char)
96  {
97  write_indent(os);
98  }
99  else
100  {
101  needToInd = true;
102  }
103  }
104  else
105  {
106  is_line_start = true;
107  }
108  }
109  else if(*it == opening_char)
110  {
111  indent();
113  {
114  write_char(os, *it);
115  }
116  }
117  else if(*it == closing_char)
118  {
119  if(*(str.begin()) != closing_char and it != str.begin())
120  {
121  deindent();
122  }
123  if(needToInd)
124  {
125  write_indent(os);
126  needToInd = false;
127  }
129  {
130  write_char(os, *it);
131  }
132  }
133  else
134  {
135  write_char(os, *it);
136  }
137  }
138 }
139 
141 {
142  indent_spaces += delta;
143 }
144 
146 {
147  THROW_ASSERT(indent_spaces >= indent_spaces - delta, "Indentation has become negative");
148  indent_spaces -= delta;
149 }
150 
151 void simple_indent::write_indent(std::ostream& os)
152 {
153  for(unsigned int i = 0; i < indent_spaces; i++)
154  {
155  os << " ";
156  }
157 }
158 
159 void simple_indent::write_char(std::ostream& os, const char& c)
160 {
161  os << c;
162 }
unsigned int delta
delta indent space
File containing functions and utilities to support the printing of debug messagges.
char closing_char
char that increments the indent space by a delta
Simple pretty print functor.
exceptions managed by PandA
char opening_char
char that increments the indent space by a delta
void write_char(std::ostream &os, const char &c)
Write the current char.
void deindent()
Manually reduce the indenting of the code.
~simple_indent()
destructor
simple_indent(char o, char c, unsigned int d)
constructor
void indent()
Manually increase the indenting of the code.
void operator()(std::ostream &os, const std::string &str)
pretty print functor
#define STD_OPENING_CHAR
STD include.
unsigned int indent_spaces
number of spaces used to indent after a new line print
#define STD_CLOSING_CHAR
Special closing character used to close the current nested level.
size_t indentation
The current indentation for debug messages.
char str[25]
Definition: fixedptc.c:8
void write_indent(std::ostream &os)
Write the indent spaces.
#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