libyui  3.10.0
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 using std::string;
30 
31 
32 
33 YStringTree::YStringTree( const char * domain )
34  : _root( 0 )
35 {
36  setTextdomain( domain );
37  _root = new YStringTreeItem( YTransText( "<root>" ) );
38 }
39 
40 
42 {
43  if ( _root )
44  delete _root;
45 }
46 
47 
49 YStringTree::addBranch( const string & content,
50  char delimiter,
51  YStringTreeItem * parent )
52 {
53  YStringTreeItem * node = 0;
54 
55  if ( ! parent )
56  parent = _root;
57 
58  if ( delimiter == 0 )
59  {
60  // Simple case: No delimiter, simply create a new item for 'content'
61  // and insert it.
62 
63  node = new YStringTreeItem( YTransText( content, translate( content ) ), parent );
64  }
65  else
66  {
67  // Split 'content' into substrings and insert each subitem
68 
69  string::size_type start = 0;
70  string::size_type end = 0;
71 
72  while ( start < content.length() )
73  {
74  // Skip delimiters
75 
76  while ( start < content.length() &&
77  content[ start ] == delimiter )
78  {
79  start++;
80  }
81 
82 
83  // Search next delimiter
84 
85  end = start;
86 
87  while ( end < content.length() &&
88  content[ end ] != delimiter )
89  {
90  end++;
91  }
92 
93 
94  // Extract substring, if there is any
95 
96  if ( end > start )
97  {
98  string path_component = content.substr( start, end - start );
99  YTransText path_component_trans( path_component, translate( path_component ) );
100 
101  // Check if an entry with this text already exists
102  node = findDirectChild( parent, path_component_trans);
103 
104  if ( ! node ) // No entry with this text yet? Create one.
105  node = new YStringTreeItem( path_component_trans, parent );
106 
107  parent = node;
108  }
109 
110  start = end;
111  }
112  }
113 
114  return node;
115 }
116 
117 
118 string
119 YStringTree::translate( const string & orig )
120 {
121  string trans( dgettext( _textdomain.c_str(), orig.c_str() ) );
122 
123  return trans;
124 }
125 
126 
127 string
129  bool translated,
130  char delimiter,
131  bool startWithDelimiter )
132 {
133  string path;
134 
135  if ( item )
136  {
137  path = translated ? item->value().trans() : item->value().orig();
138 
139  while ( item->parent() && item->parent() != _root )
140  {
141  string parentPath = translated ?
142  item->parent()->value().translation() :
143  item->parent()->value().orig();
144 
145  path = parentPath + delimiter + path;
146  item = item->parent();
147  }
148 
149  }
150 
151  if ( startWithDelimiter )
152  path = delimiter + path;
153 
154  return path;
155 }
156 
157 
160  char delimiter,
161  bool startWithDelimiter )
162 {
163  if ( ! item )
164  return YTransText( "", "" );
165 
166  YTransText path = item->value();
167 
168  while ( item->parent() && item->parent() != _root )
169  {
170  path.setOrig ( item->parent()->value().orig() + delimiter + path.orig() );
171  path.setTranslation( item->parent()->value().trans() + delimiter + path.trans() );
172 
173  item = item->parent();
174  }
175 
176  if ( startWithDelimiter )
177  {
178  path.setOrig ( delimiter + path.orig() );
179  path.setTranslation( delimiter + path.translation() );
180  }
181 
182  return path;
183 }
184 
185 
186 void
188 {
189  printf( "Tree:\n" );
190  logBranch( _root, "" );
191  printf( " " );
192 }
193 
194 
195 void
196 YStringTree::logBranch( YStringTreeItem * branch, string indentation )
197 {
198  if ( branch )
199  {
200  printf( "%s%s (%s)\n", indentation.c_str(),
201  branch->value().translation().c_str(),
202  branch->value().orig().c_str() );
203 
204  YStringTreeItem * child = branch->firstChild();
205  indentation += " ";
206 
207  while ( child )
208  {
209  logBranch( child, indentation );
210  child = child->next();
211  }
212  }
213  else
214  {
215  printf( "%s<NULL>\n", indentation.c_str() );
216  }
217 }
YStringTree::logBranch
void logBranch(YStringTreeItem *branch, std::string indentation)
Debugging - dump one branch of the tree into the log file.
Definition: YStringTree.cc:196
YStringTree::setTextdomain
void setTextdomain(const char *domain)
Set the textdomain used internally for translation of pathname components.
Definition: YStringTree.h:157
YTransText::setOrig
void setOrig(const std::string &newOrig)
Set the original message.
Definition: YTransText.h:95
YStringTree::logTree
void logTree()
Debugging - dump the tree into the log file.
Definition: YStringTree.cc:187
YTransText::orig
const std::string & orig() const
Return the original message.
Definition: YTransText.h:78
SortedTreeItem::next
SortedTreeItem< PAYLOAD > * next() const
Returns this item's next sibling or 0 if there is none.
Definition: TreeItem.h:270
YStringTree::path
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:159
YStringTree::YStringTree
YStringTree(const char *textdomain)
Constructor.
Definition: YStringTree.cc:33
YStringTree::translate
std::string translate(const std::string &orig)
Translate message 'orig' using the internal textdomain.
Definition: YStringTree.cc:119
TreeItem::value
const PAYLOAD & value() const
Returns this item's value, the "payload".
Definition: TreeItem.h:113
YTransText
Helper class for translated strings: Stores a message in the original (untranslated) version along wi...
Definition: YTransText.h:37
SortedTreeItem::firstChild
SortedTreeItem< PAYLOAD > * firstChild() const
Returns this item's first child or 0 if there is none.
Definition: TreeItem.h:276
YStringTree::addBranch
YStringTreeItem * addBranch(const std::string &content, char delimiter=0, YStringTreeItem *parent=0)
Add a unique new branch with text content 'content' to the tree, beginning at 'parent' (root if paren...
Definition: YStringTree.cc:49
YTransText::translation
const std::string & translation() const
Return the translation.
Definition: YTransText.h:83
SortedTreeItem::parent
SortedTreeItem< PAYLOAD > * parent() const
Returns this item's parent or 0 if there is none.
Definition: TreeItem.h:264
SortedTreeItem
Template class for tree items that maintain sort order.
Definition: TreeItem.h:192
YTransText::trans
const std::string & trans() const
Return the translation.
Definition: YTransText.h:89
YStringTree::~YStringTree
virtual ~YStringTree()
Destructor.
Definition: YStringTree.cc:41
YStringTree::completePath
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:128
YTransText::setTranslation
void setTranslation(const std::string &newTrans)
Set the translation.
Definition: YTransText.h:100