• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • KDevelop Utility Library
 

KDevelop Utility Library

  • lib
  • util
urlutil.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003 Julian Rockey <linux@jrockey.com>
3 Copyright (C) 2003 Alexander Dymo <cloudtemple@mksat.net>
4 Copyright (C) 2003 Mario Scalas <mario.scalas@libero.it>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21#include <tqstringlist.h>
22
23#include <tqdir.h>
24#include <tqfileinfo.h>
25#include <kdebug.h>
26
27#include <unistd.h>
28#include <limits.h>
29#include <stdlib.h>
30
31#include "urlutil.h"
32
33
35// Namespace URLUtil
37
38TQString URLUtil::filename(const TQString & name) {
39 int slashPos = name.findRev("/");
40 return slashPos<0 ? name : name.mid(slashPos+1);
41}
42
44
45TQString URLUtil::directory(const TQString & name) {
46 int slashPos = name.findRev("/");
47 return slashPos<0 ? TQString("") : name.left(slashPos);
48}
49
51
52TQString URLUtil::getRelativePath(const TQString& basepath, const TQString& destpath)
53{
54 TQString relpath = ".";
55 if (!TQFile::exists(basepath) ||
56 !TQFile::exists(destpath))
57 return "";
58 TQStringList basedirs = TQStringList::split(TQString( TQChar( TQDir::separator() ) ),basepath);
59 TQStringList destdirs = TQStringList::split(TQString( TQChar( TQDir::separator() ) ),destpath);
60
61 int maxCompare=0;
62 if (basedirs.count()>=destdirs.count())
63 maxCompare=destdirs.count();
64 else
65 maxCompare=basedirs.count();
66 int lastCommonDir=-1;
67 for (int i=0; i<maxCompare; i++)
68 {
69 if (basedirs[i] != destdirs[i])
70 break;
71 lastCommonDir=i;
72 }
73 for (uint i=0;i<basedirs.count()-(lastCommonDir+1); i++)
74 relpath += TQString( TQChar( TQDir::separator() ) )+TQString("..");
75 for (int i=0; i<lastCommonDir+1; i++)
76 destdirs.pop_front();
77 if (destdirs.count())
78 relpath += TQString( TQChar( TQDir::separator() ) )+destdirs.join( TQChar( TQDir::separator() ) );
79 return TQDir::cleanDirPath(relpath);
80}
81
83
84TQString URLUtil::relativePath(const KURL & parent, const KURL & child, uint slashPolicy) {
85 bool slashPrefix = slashPolicy & SLASH_PREFIX;
86 bool slashSuffix = slashPolicy & SLASH_SUFFIX;
87 if (parent.equals(child,true))
88 return slashPrefix ? TQString("/") : TQString("");
89
90 if (!parent.isParentOf(child)) return TQString();
91 int a=slashPrefix ? -1 : 1;
92 int b=slashSuffix ? 1 : -1;
93 return child.path(b).mid(parent.path(a).length());
94}
95
97
98TQString URLUtil::relativePath(const TQString & parent, const TQString & child, uint slashPolicy) {
99 return relativePath(KURL(parent), KURL(child), slashPolicy);
100}
101
103
104TQString URLUtil::upDir(const TQString & path, bool slashSuffix) {
105 int slashPos = path.findRev("/");
106 if (slashPos<1) return TQString();
107 return path.mid(0,slashPos+ (slashSuffix ? 1 : 0) );
108}
109
111
112KURL URLUtil::mergeURL(const KURL & source, const KURL & dest, const KURL & child) {
113
114 // if already a child of source, then fine
115 if (source.isParentOf(child) || source.equals(child,true)) return child;
116
117 // if not a child of dest, return blank URL (error)
118 if (!dest.isParentOf(child) && !dest.equals(child,true)) return KURL();
119
120 // if child is same as dest, return source
121 if (dest.equals(child,true)) return source;
122
123 // calculate
124 TQString childUrlStr = child.url(-1);
125 TQString destStemStr = dest.url(1);
126 TQString sourceStemStr = source.url(1);
127 return KURL(sourceStemStr.append( childUrlStr.mid( destStemStr.length() ) ) );
128
129}
130
132
133TQString URLUtil::getExtension(const TQString & path) {
134 int dotPos = path.findRev('.');
135 if (dotPos<0) return TQString("");
136 return path.mid(dotPos+1);
137}
138
140
141TQString URLUtil::extractPathNameRelative(const KURL &baseDirUrl, const KURL &url )
142{
143 TQString absBase = extractPathNameAbsolute( baseDirUrl ),
144 absRef = extractPathNameAbsolute( url );
145 int i = absRef.find( absBase, 0, true );
146
147 if (i == -1)
148 return TQString();
149
150 if (absRef == absBase)
151 return TQString( "." );
152 else
153 return absRef.replace( 0, absBase.length(), TQString() );
154}
155
157
158TQString URLUtil::extractPathNameRelative(const TQString &basePath, const KURL &url )
159{
160 KURL baseDirUrl = KURL::fromPathOrURL( basePath );
161 return extractPathNameRelative( baseDirUrl, url );
162}
163
165
166TQString URLUtil::extractPathNameRelative(const TQString &basePath, const TQString &absFilePath )
167{
168 KURL baseDirUrl = KURL::fromPathOrURL( basePath ),
169 fileUrl = KURL::fromPathOrURL( absFilePath );
170 return extractPathNameRelative( baseDirUrl, fileUrl );
171}
172
174
175TQString URLUtil::extractPathNameAbsolute( const KURL &url )
176{
177 if (isDirectory( url ))
178 return url.path( +1 ); // with trailing "/" if none is present
179 else
180 {
181 // Ok, this is an over-tight pre-condition on "url" since I hope nobody will never
182 // stress this function with absurd cases ... but who knows?
183 /*
184 TQString path = url.path();
185 TQFileInfo fi( path ); // Argh: TQFileInfo is back ;))
186 return ( fi.exists()? path : TQString() );
187 */
188 return url.path();
189 }
190}
191
193
194bool URLUtil::isDirectory( const KURL &url )
195{
196 return isDirectory( url.path() );
197}
198
200
201bool URLUtil::isDirectory( const TQString &absFilePath )
202{
203 return TQDir( absFilePath ).exists();
204}
205
207
208void URLUtil::dump( const KURL::List &urls, const TQString &aMessage )
209{
210 if (!aMessage.isNull())
211 {
212 kdDebug(9000) << aMessage << endl;
213 }
214 kdDebug(9000) << " List has " << urls.count() << " elements." << endl;
215
216 for (size_t i = 0; i<urls.count(); ++i)
217 {
218 KURL url = urls[ i ];
219// kdDebug(9000) << " * Element = " << url.path() << endl;
220 }
221}
222
224
225TQStringList URLUtil::toRelativePaths( const TQString &baseDir, const KURL::List &urls)
226{
227 TQStringList paths;
228
229 for (size_t i=0; i<urls.count(); ++i)
230 {
231 paths << extractPathNameRelative( baseDir, urls[i] );
232 }
233
234 return paths;
235}
236
238
239TQString URLUtil::relativePathToFile( const TQString & dirUrl, const TQString & fileUrl )
240{
241 if (dirUrl.isEmpty() || (dirUrl == "/"))
242 return fileUrl;
243
244 TQStringList dir = TQStringList::split("/", dirUrl, false);
245 TQStringList file = TQStringList::split("/", fileUrl, false);
246
247 TQString resFileName = file.last();
248 file.remove(file.last());
249
250 uint i = 0;
251 while ( (i < dir.count()) && (i < (file.count())) && (dir[i] == file[i]) )
252 i++;
253
254 TQString result_up;
255 TQString result_down;
256 TQString currDir;
257 TQString currFile;
258 do
259 {
260 i >= dir.count() ? currDir = "" : currDir = dir[i];
261 i >= file.count() ? currFile = "" : currFile = file[i];
262 //tqWarning("i = %d, currDir = %s, currFile = %s", i, currDir.latin1(), currFile.latin1());
263 if (currDir.isEmpty() && currFile.isEmpty())
264 break;
265 else if (currDir.isEmpty())
266 result_down += file[i] + "/";
267 else if (currFile.isEmpty())
268 result_up += "../";
269 else
270 {
271 result_down += file[i] + "/";
272 result_up += "../";
273 }
274 i++;
275 }
276 while ( (!currDir.isEmpty()) || (!currFile.isEmpty()) );
277
278 return result_up + result_down + resFileName;
279}
280
282
283//TODO: remove for KDE4
284TQString URLUtil::canonicalPath( const TQString & path )
285{
286 TQDir dir(path);
287 return dir.canonicalPath();
288}
289
291
292//written by "Dawit A." <adawit@kde.org>
293//borrowed from his patch to KShell
294TQString URLUtil::envExpand ( const TQString& str )
295{
296 uint len = str.length();
297
298 if (len > 1 && str[0] == '$')
299 {
300 int pos = str.find ('/');
301
302 if (pos < 0)
303 pos = len;
304
305 char* ret = getenv( TQConstString(str.unicode()+1, pos-1).string().local8Bit().data() );
306
307 if (ret)
308 {
309 TQString expandedStr ( TQFile::decodeName( ret ) );
310 if (pos < (int)len)
311 expandedStr += str.mid(pos);
312 return expandedStr;
313 }
314 }
315
316 return str;
317}
318
URLUtil::envExpand
TQString envExpand(const TQString &variable)
Performs environment variable expansion on variable.
Definition urlutil.cpp:294
URLUtil::filename
TQString filename(const TQString &pathName)
Definition urlutil.cpp:38
URLUtil::directory
TQString directory(const TQString &pathName)
Definition urlutil.cpp:45
URLUtil::dump
void dump(const KURL::List &urls, const TQString &aMessage=TQString())
Dumps the list of KURL urls on standard output, eventually printing aMessage if it is not null.
Definition urlutil.cpp:208
URLUtil::canonicalPath
TQString canonicalPath(const TQString &path)
Same as TQDir::canonicalPath in later versions of TQt.
Definition urlutil.cpp:284
URLUtil::extractPathNameRelative
TQString extractPathNameRelative(const KURL &baseDirUrl, const KURL &url)
Given a base directory url in baseDirUrl and the url referring to the sub-directory or file,...
Definition urlutil.cpp:141
URLUtil::relativePathToFile
TQString relativePathToFile(const TQString &dirUrl, const TQString &fileUrl)
Definition urlutil.cpp:239
URLUtil::mergeURL
KURL mergeURL(const KURL &source, const KURL &dest, const KURL &child)
'Merges' URLs - changes a URL that starts with dest to start with source instead.
Definition urlutil.cpp:112
URLUtil::isDirectory
bool isDirectory(const KURL &url)
Definition urlutil.cpp:194
URLUtil::toRelativePaths
TQStringList toRelativePaths(const TQString &baseDir, const KURL::List &urls)
Definition urlutil.cpp:225
URLUtil::extractPathNameAbsolute
TQString extractPathNameAbsolute(const KURL &url)
Definition urlutil.cpp:175
URLUtil::getRelativePath
TQString getRelativePath(const TQString &base, const TQString &dest)
Definition urlutil.cpp:52
URLUtil::relativePath
TQString relativePath(const KURL &parent, const KURL &child, uint slashPolicy=SLASH_PREFIX)
Definition urlutil.cpp:84
URLUtil::getExtension
TQString getExtension(const TQString &path)
Definition urlutil.cpp:133
URLUtil::SLASH_PREFIX
@ SLASH_PREFIX
URL has slash as a prefix.
Definition urlutil.h:38
URLUtil::SLASH_SUFFIX
@ SLASH_SUFFIX
URL has slash as a suffix.
Definition urlutil.h:39
URLUtil::upDir
TQString upDir(const TQString &path, bool slashSuffix=false)
Definition urlutil.cpp:104
urlutil.h
Utility functions to operate on URLs.

KDevelop Utility Library

Skip menu "KDevelop Utility Library"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

KDevelop Utility Library

Skip menu "KDevelop Utility Library"
  • buildtools
  •   lib
  •     base
  •     parsers
  •       autotools
  •       qmake
  •     widgets
  •   api
  • languages
  •   lib
  •     debugger
  •     designer_integration
  •     interfaces
  • lib
  •   catalog
  •   interfaces
  •     extensions
  •     external
  •     extras
  •   util
  •   widgets
  •     propeditor
  • parts
  •   documentation
  •     interfaces
  • src
  •   profileengine
  •     lib
Generated for KDevelop Utility Library by doxygen 1.15.0
This website is maintained by Timothy Pearson.