libyui  3.3.1
YStringTree.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YStringTree.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #include <stdio.h>
27 #include "YStringTree.h"
28 
29 
30 
31 
32 YStringTree::YStringTree( const char * domain )
33  : _root( 0 )
34 {
35  setTextdomain( domain );
36  _root = new YStringTreeItem( YTransText( "<root>" ) );
37 }
38 
39 
41 {
42  if ( _root )
43  delete _root;
44 }
45 
46 
48 YStringTree::addBranch( const std::string & content,
49  char delimiter,
50  YStringTreeItem * parent )
51 {
52  YStringTreeItem * node = 0;
53 
54  if ( ! parent )
55  parent = _root;
56 
57  if ( delimiter == 0 )
58  {
59  // Simple case: No delimiter, simply create a new item for 'content'
60  // and insert it.
61 
62  node = new YStringTreeItem( YTransText( content, translate( content ) ), parent );
63  }
64  else
65  {
66  // Split 'content' into substrings and insert each subitem
67 
68  std::string::size_type start = 0;
69  std::string::size_type end = 0;
70 
71  while ( start < content.length() )
72  {
73  // Skip delimiters
74 
75  while ( start < content.length() &&
76  content[ start ] == delimiter )
77  {
78  start++;
79  }
80 
81 
82  // Search next delimiter
83 
84  end = start;
85 
86  while ( end < content.length() &&
87  content[ end ] != delimiter )
88  {
89  end++;
90  }
91 
92 
93  // Extract substring, if there is any
94 
95  if ( end > start )
96  {
97  std::string path_component = content.substr( start, end - start );
98  YTransText path_component_trans( path_component, translate( path_component ) );
99 
100  // Check if an entry with this text already exists
101  node = findDirectChild( parent, path_component_trans);
102 
103  if ( ! node ) // No entry with this text yet? Create one.
104  node = new YStringTreeItem( path_component_trans, parent );
105 
106  parent = node;
107  }
108 
109  start = end;
110  }
111  }
112 
113  return node;
114 }
115 
116 
117 std::string
118 YStringTree::translate( const std::string & orig )
119 {
120  std::string trans( dgettext( _textdomain.c_str(), orig.c_str() ) );
121 
122  return trans;
123 }
124 
125 
126 std::string
128  bool translated,
129  char delimiter,
130  bool startWithDelimiter )
131 {
132  std::string path;
133 
134  if ( item )
135  {
136  path = translated ? item->value().trans() : item->value().orig();
137 
138  while ( item->parent() && item->parent() != _root )
139  {
140  std::string parentPath = translated ?
141  item->parent()->value().translation() :
142  item->parent()->value().orig();
143 
144  path = parentPath + delimiter + path;
145  item = item->parent();
146  }
147 
148  }
149 
150  if ( startWithDelimiter )
151  path = delimiter + path;
152 
153  return path;
154 }
155 
156 
159  char delimiter,
160  bool startWithDelimiter )
161 {
162  if ( ! item )
163  return YTransText( "", "" );
164 
165  YTransText path = item->value();
166 
167  while ( item->parent() && item->parent() != _root )
168  {
169  path.setOrig ( item->parent()->value().orig() + delimiter + path.orig() );
170  path.setTranslation( item->parent()->value().trans() + delimiter + path.trans() );
171 
172  item = item->parent();
173  }
174 
175  if ( startWithDelimiter )
176  {
177  path.setOrig ( delimiter + path.orig() );
178  path.setTranslation( delimiter + path.translation() );
179  }
180 
181  return path;
182 }
183 
184 
185 void
187 {
188  printf( "Tree:\n" );
189  logBranch( _root, "" );
190  printf( " " );
191 }
192 
193 
194 void
195 YStringTree::logBranch( YStringTreeItem * branch, std::string indentation )
196 {
197  if ( branch )
198  {
199  printf( "%s%s (%s)\n", indentation.c_str(),
200  branch->value().translation().c_str(),
201  branch->value().orig().c_str() );
202 
203  YStringTreeItem * child = branch->firstChild();
204  indentation += " ";
205 
206  while ( child )
207  {
208  logBranch( child, indentation );
209  child = child->next();
210  }
211  }
212  else
213  {
214  printf( "%s<NULL>\n", indentation.c_str() );
215  }
216 }
SortedTreeItem< PAYLOAD > * firstChild() const
Returns this item&#39;s first child or 0 if there is none.
Definition: TreeItem.h:276
void setTextdomain(const char *domain)
Set the textdomain used internally for translation of pathname components.
Definition: YStringTree.h:157
void logTree()
Debugging - dump the tree into the log file.
Definition: YStringTree.cc:186
const std::string & trans() const
Return the translation.
Definition: YTransText.h:89
Template class for tree items that maintain sort order.
Definition: TreeItem.h:191
YStringTree(const char *textdomain)
Constructor.
Definition: YStringTree.cc:32
std::string translate(const std::string &orig)
Translate message &#39;orig&#39; using the internal textdomain.
Definition: YStringTree.cc:118
const std::string & translation() const
Return the translation.
Definition: YTransText.h:83
YTransText path(const YStringTreeItem *item, char delimiter, bool startWithDelimiter=true)
Construct a complete path (both original and translated) for the specified tree item.
Definition: YStringTree.cc:158
SortedTreeItem< PAYLOAD > * parent() const
Returns this item&#39;s parent or 0 if there is none.
Definition: TreeItem.h:264
void setTranslation(const std::string &newTrans)
Set the translation.
Definition: YTransText.h:100
YStringTreeItem * addBranch(const std::string &content, char delimiter=0, YStringTreeItem *parent=0)
Add a unique new branch with text content &#39;content&#39; to the tree, beginning at &#39;parent&#39; (root if paren...
Definition: YStringTree.cc:48
void setOrig(const std::string &newOrig)
Set the original message.
Definition: YTransText.h:95
const PAYLOAD & value() const
Returns this item&#39;s value, the "payload".
Definition: TreeItem.h:113
const std::string & orig() const
Return the original message.
Definition: YTransText.h:78
std::string completePath(const YStringTreeItem *item, bool translated, char delimiter, bool startWithDelimiter)
Construct a complete original or translated path for the specified tree item.
Definition: YStringTree.cc:127
Helper class for translated strings: Stores a message in the original (untranslated) version along wi...
Definition: YTransText.h:36
void logBranch(YStringTreeItem *branch, std::string indentation)
Debugging - dump one branch of the tree into the log file.
Definition: YStringTree.cc:195
virtual ~YStringTree()
Destructor.
Definition: YStringTree.cc:40