Panda3D
 All Classes Functions Variables Enumerations
indent.I
1 // Filename: indent.I
2 // Created by: drose (15Feb99)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 ////////////////////////////////////////////////////////////////////
16 // Function: write_long_list
17 // Description: Writes a list of things to the indicated output
18 // stream, with a space separating each item. One or
19 // more lines will be written, and the lines will
20 // automatically be broken such that no line exceeds
21 // max_col columns if possible.
22 ////////////////////////////////////////////////////////////////////
23 template<class InputIterator>
24 void
25 write_long_list(ostream &out, int indent_level,
26  InputIterator first, InputIterator last,
27  string first_prefix, string later_prefix,
28  int max_col) {
29  if (later_prefix.empty()) {
30  later_prefix = first_prefix;
31  }
32 
33  if (first != last) {
34  // We have to use an intermediate strstream object so we can
35  // count the number of characters the item will have when it is
36  // output.
37  ostringstream item;
38  item << *first;
39  string str = item.str();
40 
41  indent(out, indent_level) << first_prefix << str;
42  int col = indent_level + first_prefix.length() + str.length();
43 
44  ++first;
45 
46  while (first != last) {
47  ostringstream item;
48  item << *first;
49  string str = item.str();
50 
51  col += 1 + str.length();
52  if (col > max_col) {
53  out << "\n";
54  indent(out, indent_level) << later_prefix << str;
55  col = indent_level + later_prefix.length() + str.length();
56 
57  } else {
58  out << " " << str;
59  }
60 
61  ++first;
62  }
63  out << "\n";
64  }
65 }