summaryrefslogtreecommitdiffstats
path: root/lib/util/rurl.cpp
blob: 1ec23434c57748b187dc18a62f8c4c64d193d43e (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include <qstringlist.h>

#include "rurl.h"

namespace Relative{


//class Name

Name::Name( const QString & rurl, const Type type )
    :m_rurl(rurl), m_type(type)
{
    correct();
}

Name::Name( const char * rurl, const Type type )
    :m_rurl(rurl), m_type(type)
{
    correct();
}

void Name::correct()
{
    cleanRURL();
    if (m_rurl[0] == '/')
        m_rurl = m_rurl.mid(1);
    switch (m_type)
    {
        case File:
            if (m_rurl.endsWith("/"))
                m_rurl = m_rurl.mid(0, m_rurl.length()-1);
            break;
        case Directory:
            if (!m_rurl.endsWith("/"))
                m_rurl += "/";
            break;
        case Auto:
            if (m_rurl.endsWith("/"))
                m_type = Directory;
            else
                m_type = File;
            break;
    }
}

QString Name::correctName( const QString & rurl, const Type type )
{
    QString temp = rurl;
    temp = Name::cleanName(temp);
    if (temp[0] == '/')
        temp = temp.mid(1);
    
    switch (type)
    {
        case File:
            if (temp.endsWith("/"))
                temp = temp.mid(0, temp.length()-1);
            break;
        case Directory:
            if (!temp.endsWith("/"))
                temp += "/";
            break;
    }
    
    return temp;
}

void Name::setRURL( const QString & rurl, const Type type )
{
    m_rurl = rurl;
    m_type = type;
    correct();
}

QString Name::rurl( ) const
{
    return m_rurl;
}

void Name::addPath( const QString & addendum )
{
    QString temp = correctName(addendum, Directory);
    m_rurl = directory() + temp + fileName();
}

void Name::cleanRURL( )
{
    m_rurl = cleanName(m_rurl);
}

QString Name::cleanName( const QString & rurl )
{
    QString temp;
    bool wasSlash = false;
    for (unsigned int i = 0; i < rurl.length(); ++i)
    {
        if (wasSlash && (rurl[i] == '/'))
            continue;
        
        temp += rurl[i];
        if (rurl[i] == '/')
            wasSlash = true;
        else if (wasSlash)
            wasSlash = false;
    }
    
    return temp;
}

QString Name::extension( bool complete ) const
{
    if (m_type == File)
    {
        QString temp = fileName();
        if (complete)
            return temp.mid(temp.find('.')+1);
        else
            return temp.mid(temp.findRev('.')+1);
    }
    return QString::null;
}

QString Name::fileName( ) const
{
    if (m_type == File)
        return m_rurl.section('/', -1);
    return QString::null;
}

QString Name::directory( ) const
{
    if ( (m_type == File) && (m_rurl.findRev('/') == -1) )
        return QString::null;
    
    return m_rurl.mid(0, m_rurl.findRev('/')+1);
}

bool Name::isFile( ) const
{
    return m_type == File;
}

bool Name::isDirectory( ) const
{
    return m_type == Directory;
}

bool Name::operator ==( const Name & rname )
{
    return rname.rurl() == m_rurl;
}

bool Name::operator !=( const Name & rname )
{
    return rname.rurl() != m_rurl;
}

bool Name::isValid( ) const
{
    if (m_rurl.startsWith("/"))
        return false;
    if (m_rurl.contains("//"))
        return false;
    if ( (m_rurl.endsWith("/")) && (m_type == File) )
        return false;
    if ( (!m_rurl.endsWith("/")) && (m_type == Directory) )
        return false;
    if (m_type == Auto)
        return false;
    
    return true;
}

Name::Type Name::type( ) const
{
    return m_type;
}

void Name::setType( const Type type )
{
    m_type = type;
}

Name Name::relativeName( const QString &base, const QString &url )
{
    QString dirUrl = base;
    QString fileUrl = url;
        
    if (dirUrl.isEmpty() || (dirUrl == "/"))
        return Name(fileUrl);

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

    QString resFileName = file.last();
    if (url.endsWith("/"))
        resFileName += "/";
    file.remove(file.last());

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

    QString result_up;
    QString result_down;
    QString currDir;
    QString 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;            
}



//class URL

URL::URL( KURL base, KURL url, Type type )
    :Name(Name::relativeName(base.path(), url.path()).rurl(), type), m_base(base)
{
}

URL::URL( KURL base, QString url, bool isUrlRelative, Type type )
    :Name(isUrlRelative ? url : Name::relativeName(base.path(), url).rurl(), type), m_base(base)
{
}

void URL::setBase( const KURL & base )
{
    m_base = base;
}

void URL::setBase( const QString & base )
{
    KURL url;
    url.setPath(base);
    m_base = url;
}

KURL URL::base( ) const
{
    return m_base;
}

QString URL::basePath( ) const
{
    return m_base.path(1);
}

KURL URL::url( ) const
{
    KURL url = m_base;
    url.addPath(rurl());
    url.cleanPath();
    return url;
}

QString URL::urlPath( ) const
{
    KURL url = m_base;
    url.addPath(rurl());
    int mod = 0;
    if (type() == File)
        mod = -1;
    else if (type() == Directory)
        mod = 1;
    url.cleanPath();
    return url.path(mod);
}

QString URL::urlDirectory( ) const
{
    KURL url = m_base;
    url.addPath(rurl());
    url.cleanPath();
    return url.directory(false, false);
}

URL URL::relativeTo( KURL base )
{
    return URL(base, url(), type());
}

URL URL::relativeURL( KURL base, KURL url )
{
    return URL(base, url);
}

URL URL::relativeURL( KURL base, QString url, bool isUrlRelative )
{
    return URL(base, url, isUrlRelative);
}

bool Relative::URL::operator ==( const URL & url )
{
    return (m_base == url.base()) && (rurl() == url.rurl());
}

bool Relative::URL::operator !=( const URL & url )
{
    return (m_base != url.base()) || (rurl() != url.rurl());
}



// Directory class

Directory::Directory( KURL base, KURL url )
    :URL(base, url, Name::Directory)
{
}

Directory::Directory( KURL base, QString url, bool isRelativeUrl )
    :URL(base, url, isRelativeUrl, Name::Directory)
{
}

void Directory::setRURL( QString rurl )
{
    URL::setRURL(rurl, Name::Directory);
}

void Directory::setRURL( QString rurl, Type type )
{
    URL::setRURL(rurl, type);
}



//File class

File::File( KURL base, KURL url )
    :URL(base, url, Name::File)
{
}

File::File( KURL base, QString url, bool isRelativeUrl )
    :URL(base, url, isRelativeUrl, Name::File)
{
}

void File::setRURL( QString rurl, Type type )
{
    URL::setRURL(rurl, type);
}

void File::setRURL( QString rurl )
{
    URL::setRURL(rurl, Name::File);
}

}