urlutil.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <qstringlist.h>
00022
00023 #include <qdir.h>
00024 #include <qfileinfo.h>
00025 #include <kdebug.h>
00026 #include <qdir.h>
00027
00028 #include <unistd.h>
00029 #include <limits.h>
00030 #include <stdlib.h>
00031
00032 #include "urlutil.h"
00033
00034 #include <kdeversion.h>
00035 #if (KDE_VERSION_MINOR==0) && (KDE_VERSION_MAJOR==3)
00036 #include <kdevkurl.h>
00037 #endif
00038
00040
00042
00043 QString URLUtil::filename(const QString & name) {
00044 int slashPos = name.findRev("/");
00045 return slashPos<0 ? name : name.mid(slashPos+1);
00046 }
00047
00049
00050 QString URLUtil::directory(const QString & name) {
00051 int slashPos = name.findRev("/");
00052 return slashPos<0 ? QString("") : name.left(slashPos);
00053 }
00054
00056
00057 QString URLUtil::relativePath(const KURL & parent, const KURL & child, uint slashPolicy) {
00058 bool slashPrefix = slashPolicy & SLASH_PREFIX;
00059 bool slashSuffix = slashPolicy & SLASH_SUFFIX;
00060 if (parent.cmp(child,true))
00061 return slashPrefix ? QString("/") : QString("");
00062
00063 if (!parent.isParentOf(child)) return QString();
00064 int a=slashPrefix ? -1 : 1;
00065 int b=slashSuffix ? 1 : -1;
00066 return child.path(b).mid(parent.path(a).length());
00067 }
00068
00070
00071 QString URLUtil::relativePath(const QString & parent, const QString & child, uint slashPolicy) {
00072 return relativePath(KURL(parent), KURL(child), slashPolicy);
00073 }
00074
00076
00077 QString URLUtil::upDir(const QString & path, bool slashSuffix) {
00078 int slashPos = path.findRev("/");
00079 if (slashPos<1) return QString::null;
00080 return path.mid(0,slashPos+ (slashSuffix ? 1 : 0) );
00081 }
00082
00084
00085 KURL URLUtil::mergeURL(const KURL & source, const KURL & dest, const KURL & child) {
00086
00087
00088 if (source.isParentOf(child) || source.cmp(child,true)) return child;
00089
00090
00091 if (!dest.isParentOf(child) && !dest.cmp(child,true)) return KURL();
00092
00093
00094 if (dest.cmp(child,true)) return source;
00095
00096
00097 QString childUrlStr = child.url(-1);
00098 QString destStemStr = dest.url(1);
00099 QString sourceStemStr = source.url(1);
00100 return KURL(sourceStemStr.append( childUrlStr.mid( destStemStr.length() ) ) );
00101
00102 }
00103
00105
00106 QString URLUtil::getExtension(const QString & path) {
00107 int dotPos = path.findRev('.');
00108 if (dotPos<0) return QString("");
00109 return path.mid(dotPos+1);
00110 }
00111
00113
00114 QString URLUtil::extractPathNameRelative(const KURL &baseDirUrl, const KURL &url )
00115 {
00116 QString absBase = extractPathNameAbsolute( baseDirUrl ),
00117 absRef = extractPathNameAbsolute( url );
00118 int i = absRef.find( absBase, 0, true );
00119
00120 if (i == -1)
00121 return QString();
00122
00123 if (absRef == absBase)
00124 return QString( "." );
00125 else
00126 return absRef.replace( 0, absBase.length(), QString() );
00127 }
00128
00130
00131 QString URLUtil::extractPathNameRelative(const QString &basePath, const KURL &url )
00132 {
00133 #if (KDE_VERSION_MINOR!=0) || (KDE_VERSION_MAJOR!=3)
00134 KURL baseDirUrl = KURL::fromPathOrURL( basePath );
00135 #else
00136 KURL baseDirUrl = KdevKURL::fromPathOrURL( basePath );
00137 #endif
00138 return extractPathNameRelative( baseDirUrl, url );
00139 }
00140
00142
00143 QString URLUtil::extractPathNameRelative(const QString &basePath, const QString &absFilePath )
00144 {
00145 #if (KDE_VERSION_MINOR!=0) || (KDE_VERSION_MAJOR!=3)
00146 KURL baseDirUrl = KURL::fromPathOrURL( basePath ),
00147 fileUrl = KURL::fromPathOrURL( absFilePath );
00148 #else
00149 KURL baseDirUrl = KdevKURL::fromPathOrURL( basePath ),
00150 fileUrl = KdevKURL::fromPathOrURL( absFilePath );
00151 #endif
00152 return extractPathNameRelative( baseDirUrl, fileUrl );
00153 }
00154
00156
00157 QString URLUtil::extractPathNameAbsolute( const KURL &url )
00158 {
00159 if (isDirectory( url ))
00160 return url.path( +1 );
00161 else
00162 {
00163
00164
00165
00166
00167
00168
00169
00170 return url.path();
00171 }
00172 }
00173
00175
00176 bool URLUtil::isDirectory( const KURL &url )
00177 {
00178 return isDirectory( url.path() );
00179 }
00180
00182
00183 bool URLUtil::isDirectory( const QString &absFilePath )
00184 {
00185 return QDir( absFilePath ).exists();
00186 }
00187
00189
00190 void URLUtil::dump( const KURL::List &urls, const QString &aMessage )
00191 {
00192 if (!aMessage.isNull())
00193 {
00194 kdDebug(9000) << aMessage << endl;
00195 }
00196 kdDebug(9000) << " List has " << urls.count() << " elements." << endl;
00197
00198 for (size_t i = 0; i<urls.count(); ++i)
00199 {
00200 KURL url = urls[ i ];
00201
00202 }
00203 }
00204
00206
00207 QStringList URLUtil::toRelativePaths( const QString &baseDir, const KURL::List &urls)
00208 {
00209 QStringList paths;
00210
00211 for (size_t i=0; i<urls.count(); ++i)
00212 {
00213 paths << extractPathNameRelative( baseDir, urls[i] );
00214 }
00215
00216 return paths;
00217 }
00218
00220
00221 QString URLUtil::relativePathToFile( const QString & dirUrl, const QString & fileUrl )
00222 {
00223 if (dirUrl.isEmpty() || (dirUrl == "/"))
00224 return fileUrl;
00225
00226 QStringList dir = QStringList::split("/", dirUrl, false);
00227 QStringList file = QStringList::split("/", fileUrl, false);
00228
00229 QString resFileName = file.last();
00230 file.remove(file.last());
00231
00232 uint i = 0;
00233 while ( (i < dir.count()) && (i < (file.count())) && (dir[i] == file[i]) )
00234 i++;
00235
00236 QString result_up;
00237 QString result_down;
00238 QString currDir;
00239 QString currFile;
00240 do
00241 {
00242 i >= dir.count() ? currDir = "" : currDir = dir[i];
00243 i >= file.count() ? currFile = "" : currFile = file[i];
00244 qWarning("i = %d, currDir = %s, currFile = %s", i, currDir.latin1(), currFile.latin1());
00245 if (currDir.isEmpty() && currFile.isEmpty())
00246 break;
00247 else if (currDir.isEmpty())
00248 result_down += file[i] + "/";
00249 else if (currFile.isEmpty())
00250 result_up += "../";
00251 else
00252 {
00253 result_down += file[i] + "/";
00254 result_up += "../";
00255 }
00256 i++;
00257 }
00258 while ( (!currDir.isEmpty()) || (!currFile.isEmpty()) );
00259
00260 return result_up + result_down + resFileName;
00261 }
00262
00264
00265
00266 QString URLUtil::canonicalPath( const QString & path )
00267 {
00268 QString r;
00269 char cur[PATH_MAX+1];
00270 if ( ::getcwd( cur, PATH_MAX ) )
00271 {
00272 char tmp[PATH_MAX+1];
00273 if( ::realpath( QFile::encodeName( path ), tmp ) )
00274 {
00275 r = QFile::decodeName( tmp );
00276 }
00277
00278 ::chdir( cur );
00279 }
00280 return r;
00281 }
00282
00284
00285
00286
00287 QString URLUtil::envExpand ( const QString& str )
00288 {
00289 uint len = str.length();
00290
00291 if (len > 1 && str[0] == '$')
00292 {
00293 int pos = str.find ('/');
00294
00295 if (pos < 0)
00296 pos = len;
00297
00298 char* ret = getenv( QConstString(str.unicode()+1, pos-1).string().local8Bit().data() );
00299
00300 if (ret)
00301 {
00302 QString expandedStr ( QFile::decodeName( ret ) );
00303 if (pos < (int)len)
00304 expandedStr += str.mid(pos);
00305 return expandedStr;
00306 }
00307 }
00308
00309 return str;
00310 }
00311
This file is part of the documentation for KDevelop Version 3.1.2.