00001 /* 00002 * lib/lex/strtreedump.h 00003 * 00004 * Expandable, indent-capable, (alloc-ahead) string buffer for tree 00005 * node dump. 00006 * 00007 * Copyright (c) 2003--2004 by Wolfgang Wieser ] wwieser (a) gmx <*> de [ 00008 * 00009 * This file may be distributed and/or modified under the terms of the 00010 * GNU General Public License version 2 as published by the Free Software 00011 * Foundation. 00012 * 00013 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00014 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00015 * 00016 */ 00017 00018 #ifndef _LIB_LEX_STRINGTREEDUMP_H_ 00019 #define _LIB_LEX_STRINGTREEDUMP_H_ 1 00020 00030 #include <vm/vmconfig.h> /* MUST be first */ 00031 #include <lib/tl/linkedlist.h> 00032 #include <lib/tl/tlstring.h> 00033 00034 #include <stdio.h> /* For FILE* in StringTreeDump::write(). */ 00035 00036 00051 class StringTreeDump 00052 { 00053 public: 00057 struct Entry : LinkedListBase<Entry> 00058 { 00059 TLString str; 00060 int indent; 00061 00063 inline Entry(int _indent) : LinkedListBase<Entry>(),str(), 00064 indent(_indent) {} 00066 inline ~Entry() {} 00067 00069 inline void append(const TLString &s) 00070 { str+=s; } 00071 00074 void RemoveEnd(char c); 00075 00079 inline void TightenSize() 00080 { str.TightenSize(); } 00081 00082 private: 00084 Entry(const Entry &); 00086 void operator=(const Entry &); 00087 }; 00088 private: 00089 // The actual dump is stored here: 00090 // We're currently appending to ents.last(). 00091 LinkedList<Entry> ents; 00092 int curr_indent; 00093 bool indent_just_added; 00094 00096 StringTreeDump(const StringTreeDump &); 00098 void operator=(const StringTreeDump &); 00099 public: 00101 StringTreeDump(); 00103 ~StringTreeDump(); 00104 00106 void AddIndent(int n=1) 00107 { curr_indent+=n; indent_just_added=1; } 00109 void SubIndent(int n=1) 00110 { curr_indent-=n; } 00112 bool IndentJustAdded() const { return(indent_just_added); } 00113 00115 void append(const TLString &str); 00117 inline StringTreeDump &operator+=(const TLString &str) 00118 { append(str); return(*this); } 00119 00121 void RemoveEnd(char c); 00122 00131 size_t write(FILE *fp,int indent_size=4,char indent_char=' '); 00132 }; 00133 00134 #endif /* _LIB_LEX_STRINGTREEDUMP_H_ */