libyui
3.0.10
|
00001 /* 00002 Copyright (C) 2000-2012 Novell, Inc 00003 This library is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU Lesser General Public License as 00005 published by the Free Software Foundation; either version 2.1 of the 00006 License, or (at your option) version 3.0 of the License. This library 00007 is distributed in the hope that it will be useful, but WITHOUT ANY 00008 WARRANTY; without even the implied warranty of MERCHANTABILITY or 00009 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00010 License for more details. You should have received a copy of the GNU 00011 Lesser General Public License along with this library; if not, write 00012 to the Free Software Foundation, Inc., 51 Franklin Street, Fifth 00013 Floor, Boston, MA 02110-1301 USA 00014 */ 00015 00016 00017 /*-/ 00018 00019 File: YStringTree.cc 00020 00021 Author: Stefan Hundhammer <sh@suse.de> 00022 00023 /-*/ 00024 00025 00026 #include <stdio.h> 00027 #include "YStringTree.h" 00028 00029 00030 00031 00032 YStringTree::YStringTree( const char * domain ) 00033 : _root( 0 ) 00034 { 00035 setTextdomain( domain ); 00036 _root = new YStringTreeItem( YTransText( "<root>" ) ); 00037 } 00038 00039 00040 YStringTree::~YStringTree() 00041 { 00042 if ( _root ) 00043 delete _root; 00044 } 00045 00046 00047 YStringTreeItem * 00048 YStringTree::addBranch( const std::string & content, 00049 char delimiter, 00050 YStringTreeItem * parent ) 00051 { 00052 YStringTreeItem * node = 0; 00053 00054 if ( ! parent ) 00055 parent = _root; 00056 00057 if ( delimiter == 0 ) 00058 { 00059 // Simple case: No delimiter, simply create a new item for 'content' 00060 // and insert it. 00061 00062 node = new YStringTreeItem( YTransText( content, translate( content ) ), parent ); 00063 } 00064 else 00065 { 00066 // Split 'content' into substrings and insert each subitem 00067 00068 std::string::size_type start = 0; 00069 std::string::size_type end = 0; 00070 00071 while ( start < content.length() ) 00072 { 00073 // Skip delimiters 00074 00075 while ( start < content.length() && 00076 content[ start ] == delimiter ) 00077 { 00078 start++; 00079 } 00080 00081 00082 // Search next delimiter 00083 00084 end = start; 00085 00086 while ( end < content.length() && 00087 content[ end ] != delimiter ) 00088 { 00089 end++; 00090 } 00091 00092 00093 // Extract substring, if there is any 00094 00095 if ( end > start ) 00096 { 00097 std::string path_component = content.substr( start, end - start ); 00098 YTransText path_component_trans( path_component, translate( path_component ) ); 00099 00100 // Check if an entry with this text already exists 00101 node = findDirectChild( parent, path_component_trans); 00102 00103 if ( ! node ) // No entry with this text yet? Create one. 00104 node = new YStringTreeItem( path_component_trans, parent ); 00105 00106 parent = node; 00107 } 00108 00109 start = end; 00110 } 00111 } 00112 00113 return node; 00114 } 00115 00116 00117 std::string 00118 YStringTree::translate( const std::string & orig ) 00119 { 00120 std::string trans( dgettext( _textdomain.c_str(), orig.c_str() ) ); 00121 00122 return trans; 00123 } 00124 00125 00126 std::string 00127 YStringTree::completePath( const YStringTreeItem * item, 00128 bool translated, 00129 char delimiter, 00130 bool startWithDelimiter ) 00131 { 00132 std::string path; 00133 00134 if ( item ) 00135 { 00136 path = translated ? item->value().trans() : item->value().orig(); 00137 00138 while ( item->parent() && item->parent() != _root ) 00139 { 00140 std::string parentPath = translated ? 00141 item->parent()->value().translation() : 00142 item->parent()->value().orig(); 00143 00144 path = parentPath + delimiter + path; 00145 item = item->parent(); 00146 } 00147 00148 } 00149 00150 if ( startWithDelimiter ) 00151 path = delimiter + path; 00152 00153 return path; 00154 } 00155 00156 00157 YTransText 00158 YStringTree::path( const YStringTreeItem * item, 00159 char delimiter, 00160 bool startWithDelimiter ) 00161 { 00162 if ( ! item ) 00163 return YTransText( "", "" ); 00164 00165 YTransText path = item->value(); 00166 00167 while ( item->parent() && item->parent() != _root ) 00168 { 00169 path.setOrig ( item->parent()->value().orig() + delimiter + path.orig() ); 00170 path.setTranslation( item->parent()->value().trans() + delimiter + path.trans() ); 00171 00172 item = item->parent(); 00173 } 00174 00175 if ( startWithDelimiter ) 00176 { 00177 path.setOrig ( delimiter + path.orig() ); 00178 path.setTranslation( delimiter + path.translation() ); 00179 } 00180 00181 return path; 00182 } 00183 00184 00185 void 00186 YStringTree::logTree() 00187 { 00188 printf( "Tree:\n" ); 00189 logBranch( _root, "" ); 00190 printf( " " ); 00191 } 00192 00193 00194 void 00195 YStringTree::logBranch( YStringTreeItem * branch, std::string indentation ) 00196 { 00197 if ( branch ) 00198 { 00199 printf( "%s%s (%s)\n", indentation.c_str(), 00200 branch->value().translation().c_str(), 00201 branch->value().orig().c_str() ); 00202 00203 YStringTreeItem * child = branch->firstChild(); 00204 indentation += " "; 00205 00206 while ( child ) 00207 { 00208 logBranch( child, indentation ); 00209 child = child->next(); 00210 } 00211 } 00212 else 00213 { 00214 printf( "%s<NULL>\n", indentation.c_str() ); 00215 } 00216 }