~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to lib/util/rurl.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2006-05-23 18:39:42 UTC
  • Revision ID: james.westby@ubuntu.com-20060523183942-hucifbvh68k2bwz7
Tags: upstream-3.3.2
Import upstream version 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <qstringlist.h>
 
2
 
 
3
#include "rurl.h"
 
4
 
 
5
namespace Relative{
 
6
 
 
7
 
 
8
//class Name
 
9
 
 
10
Name::Name( const QString & rurl, const Type type )
 
11
    :m_rurl(rurl), m_type(type)
 
12
{
 
13
    correct();
 
14
}
 
15
 
 
16
Name::Name( const char * rurl, const Type type )
 
17
    :m_rurl(rurl), m_type(type)
 
18
{
 
19
    correct();
 
20
}
 
21
 
 
22
void Name::correct()
 
23
{
 
24
    cleanRURL();
 
25
    if (m_rurl[0] == '/')
 
26
        m_rurl = m_rurl.mid(1);
 
27
    switch (m_type)
 
28
    {
 
29
        case File:
 
30
            if (m_rurl.endsWith("/"))
 
31
                m_rurl = m_rurl.mid(0, m_rurl.length()-1);
 
32
            break;
 
33
        case Directory:
 
34
            if (!m_rurl.endsWith("/"))
 
35
                m_rurl += "/";
 
36
            break;
 
37
        case Auto:
 
38
            if (m_rurl.endsWith("/"))
 
39
                m_type = Directory;
 
40
            else
 
41
                m_type = File;
 
42
            break;
 
43
    }
 
44
}
 
45
 
 
46
QString Name::correctName( const QString & rurl, const Type type )
 
47
{
 
48
    QString temp = rurl;
 
49
    temp = Name::cleanName(temp);
 
50
    if (temp[0] == '/')
 
51
        temp = temp.mid(1);
 
52
    
 
53
    switch (type)
 
54
    {
 
55
        case File:
 
56
            if (temp.endsWith("/"))
 
57
                temp = temp.mid(0, temp.length()-1);
 
58
            break;
 
59
        case Directory:
 
60
            if (!temp.endsWith("/"))
 
61
                temp += "/";
 
62
            break;
 
63
    }
 
64
    
 
65
    return temp;
 
66
}
 
67
 
 
68
void Name::setRURL( const QString & rurl, const Type type )
 
69
{
 
70
    m_rurl = rurl;
 
71
    m_type = type;
 
72
    correct();
 
73
}
 
74
 
 
75
QString Name::rurl( ) const
 
76
{
 
77
    return m_rurl;
 
78
}
 
79
 
 
80
void Name::addPath( const QString & addendum )
 
81
{
 
82
    QString temp = correctName(addendum, Directory);
 
83
    m_rurl = directory() + temp + fileName();
 
84
}
 
85
 
 
86
void Name::cleanRURL( )
 
87
{
 
88
    m_rurl = cleanName(m_rurl);
 
89
}
 
90
 
 
91
QString Name::cleanName( const QString & rurl )
 
92
{
 
93
    QString temp;
 
94
    bool wasSlash = false;
 
95
    for (unsigned int i = 0; i < rurl.length(); ++i)
 
96
    {
 
97
        if (wasSlash && (rurl[i] == '/'))
 
98
            continue;
 
99
        
 
100
        temp += rurl[i];
 
101
        if (rurl[i] == '/')
 
102
            wasSlash = true;
 
103
        else if (wasSlash)
 
104
            wasSlash = false;
 
105
    }
 
106
    
 
107
    return temp;
 
108
}
 
109
 
 
110
QString Name::extension( bool complete ) const
 
111
{
 
112
    if (m_type == File)
 
113
    {
 
114
        QString temp = fileName();
 
115
        if (complete)
 
116
            return temp.mid(temp.find('.')+1);
 
117
        else
 
118
            return temp.mid(temp.findRev('.')+1);
 
119
    }
 
120
    return QString::null;
 
121
}
 
122
 
 
123
QString Name::fileName( ) const
 
124
{
 
125
    if (m_type == File)
 
126
        return m_rurl.section('/', -1);
 
127
    return QString::null;
 
128
}
 
129
 
 
130
QString Name::directory( ) const
 
131
{
 
132
    if ( (m_type == File) && (m_rurl.findRev('/') == -1) )
 
133
        return QString::null;
 
134
    
 
135
    return m_rurl.mid(0, m_rurl.findRev('/')+1);
 
136
}
 
137
 
 
138
bool Name::isFile( ) const
 
139
{
 
140
    return m_type == File;
 
141
}
 
142
 
 
143
bool Name::isDirectory( ) const
 
144
{
 
145
    return m_type == Directory;
 
146
}
 
147
 
 
148
bool Name::operator ==( const Name & rname )
 
149
{
 
150
    return rname.rurl() == m_rurl;
 
151
}
 
152
 
 
153
bool Name::operator !=( const Name & rname )
 
154
{
 
155
    return rname.rurl() != m_rurl;
 
156
}
 
157
 
 
158
bool Name::isValid( ) const
 
159
{
 
160
    if (m_rurl.startsWith("/"))
 
161
        return false;
 
162
    if (m_rurl.contains("//"))
 
163
        return false;
 
164
    if ( (m_rurl.endsWith("/")) && (m_type == File) )
 
165
        return false;
 
166
    if ( (!m_rurl.endsWith("/")) && (m_type == Directory) )
 
167
        return false;
 
168
    if (m_type == Auto)
 
169
        return false;
 
170
    
 
171
    return true;
 
172
}
 
173
 
 
174
Name::Type Name::type( ) const
 
175
{
 
176
    return m_type;
 
177
}
 
178
 
 
179
void Name::setType( const Type type )
 
180
{
 
181
    m_type = type;
 
182
}
 
183
 
 
184
Name Name::relativeName( const QString &base, const QString &url )
 
185
{
 
186
    QString dirUrl = base;
 
187
    QString fileUrl = url;
 
188
        
 
189
    if (dirUrl.isEmpty() || (dirUrl == "/"))
 
190
        return Name(fileUrl);
 
191
 
 
192
    QStringList dir = QStringList::split("/", dirUrl, false);
 
193
    QStringList file = QStringList::split("/", fileUrl, false);
 
194
 
 
195
    QString resFileName = file.last();
 
196
    if (url.endsWith("/"))
 
197
        resFileName += "/";
 
198
    file.remove(file.last());
 
199
 
 
200
    uint i = 0;
 
201
    while ( (i < dir.count()) && (i < (file.count())) && (dir[i] == file[i]) )
 
202
        i++;
 
203
 
 
204
    QString result_up;
 
205
    QString result_down;
 
206
    QString currDir;
 
207
    QString currFile;
 
208
    do
 
209
    {
 
210
        i >= dir.count() ? currDir = "" : currDir = dir[i];
 
211
        i >= file.count() ? currFile = "" : currFile = file[i];
 
212
//        qWarning("i = %d, currDir = %s, currFile = %s", i, currDir.latin1(), currFile.latin1());
 
213
        if (currDir.isEmpty() && currFile.isEmpty())
 
214
            break;
 
215
        else if (currDir.isEmpty())
 
216
            result_down += file[i] + "/";
 
217
        else if (currFile.isEmpty())
 
218
            result_up += "../";
 
219
        else
 
220
        {
 
221
            result_down += file[i] + "/";
 
222
            result_up += "../";
 
223
        }
 
224
        i++;
 
225
    }
 
226
    while ( (!currDir.isEmpty()) || (!currFile.isEmpty()) );
 
227
 
 
228
    return result_up + result_down + resFileName;            
 
229
}
 
230
 
 
231
 
 
232
 
 
233
//class URL
 
234
 
 
235
URL::URL( KURL base, KURL url, Type type )
 
236
    :Name(Name::relativeName(base.path(), url.path()).rurl(), type), m_base(base)
 
237
{
 
238
}
 
239
 
 
240
URL::URL( KURL base, QString url, bool isUrlRelative, Type type )
 
241
    :Name(isUrlRelative ? url : Name::relativeName(base.path(), url).rurl(), type), m_base(base)
 
242
{
 
243
}
 
244
 
 
245
void URL::setBase( const KURL & base )
 
246
{
 
247
    m_base = base;
 
248
}
 
249
 
 
250
void URL::setBase( const QString & base )
 
251
{
 
252
    KURL url;
 
253
    url.setPath(base);
 
254
    m_base = url;
 
255
}
 
256
 
 
257
KURL URL::base( ) const
 
258
{
 
259
    return m_base;
 
260
}
 
261
 
 
262
QString URL::basePath( ) const
 
263
{
 
264
    return m_base.path(1);
 
265
}
 
266
 
 
267
KURL URL::url( ) const
 
268
{
 
269
    KURL url = m_base;
 
270
    url.addPath(rurl());
 
271
    url.cleanPath();
 
272
    return url;
 
273
}
 
274
 
 
275
QString URL::urlPath( ) const
 
276
{
 
277
    KURL url = m_base;
 
278
    url.addPath(rurl());
 
279
    int mod = 0;
 
280
    if (type() == File)
 
281
        mod = -1;
 
282
    else if (type() == Directory)
 
283
        mod = 1;
 
284
    url.cleanPath();
 
285
    return url.path(mod);
 
286
}
 
287
 
 
288
QString URL::urlDirectory( ) const
 
289
{
 
290
    KURL url = m_base;
 
291
    url.addPath(rurl());
 
292
    url.cleanPath();
 
293
    return url.directory(false, false);
 
294
}
 
295
 
 
296
URL URL::relativeTo( KURL base )
 
297
{
 
298
    return URL(base, url(), type());
 
299
}
 
300
 
 
301
URL URL::relativeURL( KURL base, KURL url )
 
302
{
 
303
    return URL(base, url);
 
304
}
 
305
 
 
306
URL URL::relativeURL( KURL base, QString url, bool isUrlRelative )
 
307
{
 
308
    return URL(base, url, isUrlRelative);
 
309
}
 
310
 
 
311
bool Relative::URL::operator ==( const URL & url )
 
312
{
 
313
    return (m_base == url.base()) && (rurl() == url.rurl());
 
314
}
 
315
 
 
316
bool Relative::URL::operator !=( const URL & url )
 
317
{
 
318
    return (m_base != url.base()) || (rurl() != url.rurl());
 
319
}
 
320
 
 
321
 
 
322
 
 
323
// Directory class
 
324
 
 
325
Directory::Directory( KURL base, KURL url )
 
326
    :URL(base, url, Name::Directory)
 
327
{
 
328
}
 
329
 
 
330
Directory::Directory( KURL base, QString url, bool isRelativeUrl )
 
331
    :URL(base, url, isRelativeUrl, Name::Directory)
 
332
{
 
333
}
 
334
 
 
335
void Directory::setRURL( QString rurl )
 
336
{
 
337
    URL::setRURL(rurl, Name::Directory);
 
338
}
 
339
 
 
340
void Directory::setRURL( QString rurl, Type type )
 
341
{
 
342
    URL::setRURL(rurl, type);
 
343
}
 
344
 
 
345
 
 
346
 
 
347
//File class
 
348
 
 
349
File::File( KURL base, KURL url )
 
350
    :URL(base, url, Name::File)
 
351
{
 
352
}
 
353
 
 
354
File::File( KURL base, QString url, bool isRelativeUrl )
 
355
    :URL(base, url, isRelativeUrl, Name::File)
 
356
{
 
357
}
 
358
 
 
359
void File::setRURL( QString rurl, Type type )
 
360
{
 
361
    URL::setRURL(rurl, type);
 
362
}
 
363
 
 
364
void File::setRURL( QString rurl )
 
365
{
 
366
    URL::setRURL(rurl, Name::File);
 
367
}
 
368
 
 
369
}