summaryrefslogtreecommitdiffstats
path: root/lib/util/urlutil.cpp
blob: 12b000658e3eef8673b897a6f3da6b6a6e24f077 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* This file is part of the KDE project
   Copyright (C) 2003 Julian Rockey <linux@jrockey.com>
   Copyright (C) 2003 Alexander Dymo <cloudtemple@mksat.net>
   Copyright (C) 2003 Mario Scalas <mario.scalas@libero.it>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/
#include <tqstringlist.h>

#include <tqdir.h>
#include <tqfileinfo.h>
#include <kdebug.h>

#include <unistd.h>
#include <limits.h>
#include <stdlib.h>

#include "urlutil.h"

#include <tdeversion.h>

///////////////////////////////////////////////////////////////////////////////
// Namespace URLUtil
///////////////////////////////////////////////////////////////////////////////

TQString URLUtil::filename(const TQString & name) {
  int slashPos = name.findRev("/");
  return slashPos<0 ? name : name.mid(slashPos+1);
}

///////////////////////////////////////////////////////////////////////////////

TQString URLUtil::directory(const TQString & name) {
  int slashPos = name.findRev("/");
  return slashPos<0 ? TQString("") : name.left(slashPos);
}

///////////////////////////////////////////////////////////////////////////////

TQString URLUtil::getRelativePath(const TQString& basepath, const TQString& destpath)
{
  TQString relpath = ".";
  if (!TQFile::exists(basepath) ||
      !TQFile::exists(destpath))
    return "";
  TQStringList basedirs = TQStringList::split(TQString( TQChar( TQDir::separator() ) ),basepath);
  TQStringList destdirs = TQStringList::split(TQString( TQChar( TQDir::separator() ) ),destpath);

  int maxCompare=0;
  if (basedirs.count()>=destdirs.count())
    maxCompare=destdirs.count();
  else
    maxCompare=basedirs.count();
  int lastCommonDir=-1;
  for (int i=0; i<maxCompare; i++)
  {
    if (basedirs[i] != destdirs[i])
      break;
    lastCommonDir=i;
  }
  for (uint i=0;i<basedirs.count()-(lastCommonDir+1); i++)
    relpath += TQString( TQChar( TQDir::separator() ) )+TQString("..");
  for (int i=0; i<lastCommonDir+1; i++)
    destdirs.pop_front();
  if (destdirs.count())
    relpath += TQString( TQChar( TQDir::separator() ) )+destdirs.join( TQChar( TQDir::separator() ) );
  return TQDir::cleanDirPath(relpath);
}

///////////////////////////////////////////////////////////////////////////////

TQString URLUtil::relativePath(const KURL & parent, const KURL & child, uint slashPolicy) {
  bool slashPrefix = slashPolicy & SLASH_PREFIX;
  bool slashSuffix = slashPolicy & SLASH_SUFFIX;
  if (parent.equals(child,true))
    return slashPrefix ? TQString("/") : TQString("");

  if (!parent.isParentOf(child)) return TQString();
  int a=slashPrefix ? -1 : 1;
  int b=slashSuffix ? 1 : -1;
  return child.path(b).mid(parent.path(a).length());
}

///////////////////////////////////////////////////////////////////////////////

TQString URLUtil::relativePath(const TQString & parent, const TQString & child, uint slashPolicy) {
  return relativePath(KURL(parent), KURL(child), slashPolicy);
}

///////////////////////////////////////////////////////////////////////////////

TQString URLUtil::upDir(const TQString & path, bool slashSuffix) {
  int slashPos = path.findRev("/");
  if (slashPos<1) return TQString();
  return path.mid(0,slashPos+ (slashSuffix ? 1 : 0) );
}

///////////////////////////////////////////////////////////////////////////////

KURL URLUtil::mergeURL(const KURL & source, const KURL & dest, const KURL & child) {

  // if already a child of source, then fine
  if (source.isParentOf(child) || source.equals(child,true)) return child;

  // if not a child of dest, return blank URL (error)
  if (!dest.isParentOf(child) && !dest.equals(child,true)) return KURL();

  // if child is same as dest, return source
  if (dest.equals(child,true)) return source;

  // calculate
  TQString childUrlStr = child.url(-1);
  TQString destStemStr = dest.url(1);
  TQString sourceStemStr = source.url(1);
  return KURL(sourceStemStr.append( childUrlStr.mid( destStemStr.length() ) ) );

}

///////////////////////////////////////////////////////////////////////////////

TQString URLUtil::getExtension(const TQString & path) {
  int dotPos = path.findRev('.');
  if (dotPos<0) return TQString("");
  return path.mid(dotPos+1);
}

///////////////////////////////////////////////////////////////////////////////

TQString URLUtil::extractPathNameRelative(const KURL &baseDirUrl, const KURL &url )
{
  TQString absBase = extractPathNameAbsolute( baseDirUrl ),
    absRef = extractPathNameAbsolute( url );
  int i = absRef.find( absBase, 0, true );

  if (i == -1)
    return TQString();

  if (absRef == absBase)
    return TQString( "." );
  else
    return absRef.replace( 0, absBase.length(), TQString() );
}

///////////////////////////////////////////////////////////////////////////////

TQString URLUtil::extractPathNameRelative(const TQString &basePath, const KURL &url )
{
  KURL baseDirUrl = KURL::fromPathOrURL( basePath );
  return extractPathNameRelative( baseDirUrl, url );
}

///////////////////////////////////////////////////////////////////////////////

TQString URLUtil::extractPathNameRelative(const TQString &basePath, const TQString &absFilePath )
{
  KURL baseDirUrl = KURL::fromPathOrURL( basePath ),
       fileUrl = KURL::fromPathOrURL( absFilePath );
  return extractPathNameRelative( baseDirUrl, fileUrl );
}

///////////////////////////////////////////////////////////////////////////////

TQString URLUtil::extractPathNameAbsolute( const KURL &url )
{
  if (isDirectory( url ))
    return url.path( +1 ); // with trailing "/" if none is present
  else
  {
    // Ok, this is an over-tight pre-condition on "url" since I hope nobody will never
    // stress this function with absurd cases ... but who knows?
  /*
    TQString path = url.path();
    TQFileInfo fi( path );  // Argh: TQFileInfo is back ;))
    return ( fi.exists()? path : TQString() );
  */
    return url.path();
  }
}

///////////////////////////////////////////////////////////////////////////////

bool URLUtil::isDirectory( const KURL &url )
{
  return isDirectory( url.path() );
}

///////////////////////////////////////////////////////////////////////////////

bool URLUtil::isDirectory( const TQString &absFilePath )
{
  return TQDir( absFilePath ).exists();
}

///////////////////////////////////////////////////////////////////////////////

void URLUtil::dump( const KURL::List &urls, const TQString &aMessage )
{
  if (!aMessage.isNull())
  {
    kdDebug(9000) << aMessage << endl;
  }
  kdDebug(9000) << " List has " << urls.count() << " elements." << endl;

  for (size_t i = 0; i<urls.count(); ++i)
  {
    KURL url = urls[ i ];
//    kdDebug(9000) << " * Element = "  << url.path() << endl;
  }
}

///////////////////////////////////////////////////////////////////////////////

TQStringList URLUtil::toRelativePaths( const TQString &baseDir, const KURL::List &urls)
{
  TQStringList paths;

  for (size_t i=0; i<urls.count(); ++i)
  {
    paths << extractPathNameRelative( baseDir, urls[i] );
  }

  return paths;
}

///////////////////////////////////////////////////////////////////////////////

TQString URLUtil::relativePathToFile( const TQString & dirUrl, const TQString & fileUrl )
{
  if (dirUrl.isEmpty() || (dirUrl == "/"))
    return fileUrl;

  TQStringList dir = TQStringList::split("/", dirUrl, false);
  TQStringList file = TQStringList::split("/", fileUrl, false);

  TQString resFileName = file.last();
  file.remove(file.last());

  uint i = 0;
  while ( (i < dir.count()) && (i < (file.count())) && (dir[i] == file[i]) )
    i++;

  TQString result_up;
  TQString result_down;
  TQString currDir;
  TQString currFile;
  do
  {
    i >= dir.count() ? currDir = "" : currDir = dir[i];
    i >= file.count() ? currFile = "" : currFile = file[i];
      //qWarning("i = %d, currDir = %s, currFile = %s", i, currDir.latin1(), currFile.latin1());
    if (currDir.isEmpty() && currFile.isEmpty())
      break;
    else if (currDir.isEmpty())
      result_down += file[i] + "/";
    else if (currFile.isEmpty())
      result_up += "../";
    else
    {
      result_down += file[i] + "/";
      result_up += "../";
    }
    i++;
  }
  while ( (!currDir.isEmpty()) || (!currFile.isEmpty()) );

  return result_up + result_down + resFileName;
}

///////////////////////////////////////////////////////////////////////////////

//TODO: remove for KDE4
TQString URLUtil::canonicalPath( const TQString & path )
{
    TQDir dir(path);
    return dir.canonicalPath();
}

///////////////////////////////////////////////////////////////////////////////

//written by "Dawit A." <adawit@kde.org>
//borrowed from his patch to KShell
TQString URLUtil::envExpand ( const TQString& str )
{
    uint len = str.length();

    if (len > 1 && str[0] == '$')
    {
      int pos = str.find ('/');

      if (pos < 0)
        pos = len;

      char* ret = getenv( TQConstString(str.tqunicode()+1, pos-1).string().local8Bit().data() );

      if (ret)
      {
        TQString expandedStr ( TQFile::decodeName( ret ) );
        if (pos < (int)len)
          expandedStr += str.mid(pos);
        return expandedStr;
      }
    }

    return str;
}