• 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#include <tdeversion.h>
34
36// Namespace URLUtil
38
39TQString URLUtil::filename(const TQString & name) {
40 int slashPos = name.findRev("/");
41 return slashPos<0 ? name : name.mid(slashPos+1);
42}
43
45
46TQString URLUtil::directory(const TQString & name) {
47 int slashPos = name.findRev("/");
48 return slashPos<0 ? TQString("") : name.left(slashPos);
49}
50
52
53TQString URLUtil::getRelativePath(const TQString& basepath, const TQString& destpath)
54{
55 TQString relpath = ".";
56 if (!TQFile::exists(basepath) ||
57 !TQFile::exists(destpath))
58 return "";
59 TQStringList basedirs = TQStringList::split(TQString( TQChar( TQDir::separator() ) ),basepath);
60 TQStringList destdirs = TQStringList::split(TQString( TQChar( TQDir::separator() ) ),destpath);
61
62 int maxCompare=0;
63 if (basedirs.count()>=destdirs.count())
64 maxCompare=destdirs.count();
65 else
66 maxCompare=basedirs.count();
67 int lastCommonDir=-1;
68 for (int i=0; i<maxCompare; i++)
69 {
70 if (basedirs[i] != destdirs[i])
71 break;
72 lastCommonDir=i;
73 }
74 for (uint i=0;i<basedirs.count()-(lastCommonDir+1); i++)
75 relpath += TQString( TQChar( TQDir::separator() ) )+TQString("..");
76 for (int i=0; i<lastCommonDir+1; i++)
77 destdirs.pop_front();
78 if (destdirs.count())
79 relpath += TQString( TQChar( TQDir::separator() ) )+destdirs.join( TQChar( TQDir::separator() ) );
80 return TQDir::cleanDirPath(relpath);
81}
82
84
85TQString URLUtil::relativePath(const KURL & parent, const KURL & child, uint slashPolicy) {
86 bool slashPrefix = slashPolicy & SLASH_PREFIX;
87 bool slashSuffix = slashPolicy & SLASH_SUFFIX;
88 if (parent.equals(child,true))
89 return slashPrefix ? TQString("/") : TQString("");
90
91 if (!parent.isParentOf(child)) return TQString();
92 int a=slashPrefix ? -1 : 1;
93 int b=slashSuffix ? 1 : -1;
94 return child.path(b).mid(parent.path(a).length());
95}
96
98
99TQString URLUtil::relativePath(const TQString & parent, const TQString & child, uint slashPolicy) {
100 return relativePath(KURL(parent), KURL(child), slashPolicy);
101}
102
104
105TQString URLUtil::upDir(const TQString & path, bool slashSuffix) {
106 int slashPos = path.findRev("/");
107 if (slashPos<1) return TQString();
108 return path.mid(0,slashPos+ (slashSuffix ? 1 : 0) );
109}
110
112
113KURL URLUtil::mergeURL(const KURL & source, const KURL & dest, const KURL & child) {
114
115 // if already a child of source, then fine
116 if (source.isParentOf(child) || source.equals(child,true)) return child;
117
118 // if not a child of dest, return blank URL (error)
119 if (!dest.isParentOf(child) && !dest.equals(child,true)) return KURL();
120
121 // if child is same as dest, return source
122 if (dest.equals(child,true)) return source;
123
124 // calculate
125 TQString childUrlStr = child.url(-1);
126 TQString destStemStr = dest.url(1);
127 TQString sourceStemStr = source.url(1);
128 return KURL(sourceStemStr.append( childUrlStr.mid( destStemStr.length() ) ) );
129
130}
131
133
134TQString URLUtil::getExtension(const TQString & path) {
135 int dotPos = path.findRev('.');
136 if (dotPos<0) return TQString("");
137 return path.mid(dotPos+1);
138}
139
141
142TQString URLUtil::extractPathNameRelative(const KURL &baseDirUrl, const KURL &url )
143{
144 TQString absBase = extractPathNameAbsolute( baseDirUrl ),
145 absRef = extractPathNameAbsolute( url );
146 int i = absRef.find( absBase, 0, true );
147
148 if (i == -1)
149 return TQString();
150
151 if (absRef == absBase)
152 return TQString( "." );
153 else
154 return absRef.replace( 0, absBase.length(), TQString() );
155}
156
158
159TQString URLUtil::extractPathNameRelative(const TQString &basePath, const KURL &url )
160{
161 KURL baseDirUrl = KURL::fromPathOrURL( basePath );
162 return extractPathNameRelative( baseDirUrl, url );
163}
164
166
167TQString URLUtil::extractPathNameRelative(const TQString &basePath, const TQString &absFilePath )
168{
169 KURL baseDirUrl = KURL::fromPathOrURL( basePath ),
170 fileUrl = KURL::fromPathOrURL( absFilePath );
171 return extractPathNameRelative( baseDirUrl, fileUrl );
172}
173
175
176TQString URLUtil::extractPathNameAbsolute( const KURL &url )
177{
178 if (isDirectory( url ))
179 return url.path( +1 ); // with trailing "/" if none is present
180 else
181 {
182 // Ok, this is an over-tight pre-condition on "url" since I hope nobody will never
183 // stress this function with absurd cases ... but who knows?
184 /*
185 TQString path = url.path();
186 TQFileInfo fi( path ); // Argh: TQFileInfo is back ;))
187 return ( fi.exists()? path : TQString() );
188 */
189 return url.path();
190 }
191}
192
194
195bool URLUtil::isDirectory( const KURL &url )
196{
197 return isDirectory( url.path() );
198}
199
201
202bool URLUtil::isDirectory( const TQString &absFilePath )
203{
204 return TQDir( absFilePath ).exists();
205}
206
208
209void URLUtil::dump( const KURL::List &urls, const TQString &aMessage )
210{
211 if (!aMessage.isNull())
212 {
213 kdDebug(9000) << aMessage << endl;
214 }
215 kdDebug(9000) << " List has " << urls.count() << " elements." << endl;
216
217 for (size_t i = 0; i<urls.count(); ++i)
218 {
219 KURL url = urls[ i ];
220// kdDebug(9000) << " * Element = " << url.path() << endl;
221 }
222}
223
225
226TQStringList URLUtil::toRelativePaths( const TQString &baseDir, const KURL::List &urls)
227{
228 TQStringList paths;
229
230 for (size_t i=0; i<urls.count(); ++i)
231 {
232 paths << extractPathNameRelative( baseDir, urls[i] );
233 }
234
235 return paths;
236}
237
239
240TQString URLUtil::relativePathToFile( const TQString & dirUrl, const TQString & fileUrl )
241{
242 if (dirUrl.isEmpty() || (dirUrl == "/"))
243 return fileUrl;
244
245 TQStringList dir = TQStringList::split("/", dirUrl, false);
246 TQStringList file = TQStringList::split("/", fileUrl, false);
247
248 TQString resFileName = file.last();
249 file.remove(file.last());
250
251 uint i = 0;
252 while ( (i < dir.count()) && (i < (file.count())) && (dir[i] == file[i]) )
253 i++;
254
255 TQString result_up;
256 TQString result_down;
257 TQString currDir;
258 TQString currFile;
259 do
260 {
261 i >= dir.count() ? currDir = "" : currDir = dir[i];
262 i >= file.count() ? currFile = "" : currFile = file[i];
263 //tqWarning("i = %d, currDir = %s, currFile = %s", i, currDir.latin1(), currFile.latin1());
264 if (currDir.isEmpty() && currFile.isEmpty())
265 break;
266 else if (currDir.isEmpty())
267 result_down += file[i] + "/";
268 else if (currFile.isEmpty())
269 result_up += "../";
270 else
271 {
272 result_down += file[i] + "/";
273 result_up += "../";
274 }
275 i++;
276 }
277 while ( (!currDir.isEmpty()) || (!currFile.isEmpty()) );
278
279 return result_up + result_down + resFileName;
280}
281
283
284//TODO: remove for KDE4
285TQString URLUtil::canonicalPath( const TQString & path )
286{
287 TQDir dir(path);
288 return dir.canonicalPath();
289}
290
292
293//written by "Dawit A." <adawit@kde.org>
294//borrowed from his patch to KShell
295TQString URLUtil::envExpand ( const TQString& str )
296{
297 uint len = str.length();
298
299 if (len > 1 && str[0] == '$')
300 {
301 int pos = str.find ('/');
302
303 if (pos < 0)
304 pos = len;
305
306 char* ret = getenv( TQConstString(str.unicode()+1, pos-1).string().local8Bit().data() );
307
308 if (ret)
309 {
310 TQString expandedStr ( TQFile::decodeName( ret ) );
311 if (pos < (int)len)
312 expandedStr += str.mid(pos);
313 return expandedStr;
314 }
315 }
316
317 return str;
318}
319
URLUtil::envExpand
TQString envExpand(const TQString &variable)
Performs environment variable expansion on variable.
Definition urlutil.cpp:295
URLUtil::filename
TQString filename(const TQString &pathName)
Definition urlutil.cpp:39
URLUtil::directory
TQString directory(const TQString &pathName)
Definition urlutil.cpp:46
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:209
URLUtil::canonicalPath
TQString canonicalPath(const TQString &path)
Same as TQDir::canonicalPath in later versions of TQt.
Definition urlutil.cpp:285
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:142
URLUtil::relativePathToFile
TQString relativePathToFile(const TQString &dirUrl, const TQString &fileUrl)
Definition urlutil.cpp:240
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:113
URLUtil::isDirectory
bool isDirectory(const KURL &url)
Definition urlutil.cpp:195
URLUtil::toRelativePaths
TQStringList toRelativePaths(const TQString &baseDir, const KURL::List &urls)
Definition urlutil.cpp:226
URLUtil::extractPathNameAbsolute
TQString extractPathNameAbsolute(const KURL &url)
Definition urlutil.cpp:176
URLUtil::getRelativePath
TQString getRelativePath(const TQString &base, const TQString &dest)
Definition urlutil.cpp:53
URLUtil::relativePath
TQString relativePath(const KURL &parent, const KURL &child, uint slashPolicy=SLASH_PREFIX)
Definition urlutil.cpp:85
URLUtil::getExtension
TQString getExtension(const TQString &path)
Definition urlutil.cpp:134
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:105
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.9.8
This website is maintained by Timothy Pearson.