• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • KDevelop Utility Library
 

KDevelop Utility Library

  • lib
  • util
rurl.cpp
1#include <tqstringlist.h>
2
3#include "rurl.h"
4
5namespace Relative{
6
7
8//class Name
9
10Name::Name( const TQString & rurl, const Type type )
11 :m_rurl(rurl), m_type(type)
12{
13 correct();
14}
15
16Name::Name( const char * rurl, const Type type )
17 :m_rurl(rurl), m_type(type)
18{
19 correct();
20}
21
22void 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
46TQString Name::correctName( const TQString & rurl, const Type type )
47{
48 TQString 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
68void Name::setRURL( const TQString & rurl, const Type type )
69{
70 m_rurl = rurl;
71 m_type = type;
72 correct();
73}
74
75TQString Name::rurl( ) const
76{
77 return m_rurl;
78}
79
80void Name::addPath( const TQString & addendum )
81{
82 TQString temp = correctName(addendum, Directory);
83 m_rurl = directory() + temp + fileName();
84}
85
86void Name::cleanRURL( )
87{
88 m_rurl = cleanName(m_rurl);
89}
90
91TQString Name::cleanName( const TQString & rurl )
92{
93 TQString 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
110TQString Name::extension( bool complete ) const
111{
112 if (m_type == File)
113 {
114 TQString temp = fileName();
115 if (complete)
116 return temp.mid(temp.find('.')+1);
117 else
118 return temp.mid(temp.findRev('.')+1);
119 }
120 return TQString();
121}
122
123TQString Name::fileName( ) const
124{
125 if (m_type == File)
126 return m_rurl.section('/', -1);
127 return TQString();
128}
129
130TQString Name::directory( ) const
131{
132 if ( (m_type == File) && (m_rurl.findRev('/') == -1) )
133 return TQString();
134
135 return m_rurl.mid(0, m_rurl.findRev('/')+1);
136}
137
138bool Name::isFile( ) const
139{
140 return m_type == File;
141}
142
143bool Name::isDirectory( ) const
144{
145 return m_type == Directory;
146}
147
148bool Name::operator ==( const Name & rname )
149{
150 return rname.rurl() == m_rurl;
151}
152
153bool Name::operator !=( const Name & rname )
154{
155 return rname.rurl() != m_rurl;
156}
157
158bool 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
174Name::Type Name::type( ) const
175{
176 return m_type;
177}
178
179void Name::setType( const Type type )
180{
181 m_type = type;
182}
183
184Name Name::relativeName( const TQString &base, const TQString &url )
185{
186 TQString dirUrl = base;
187 TQString fileUrl = url;
188
189 if (dirUrl.isEmpty() || (dirUrl == "/"))
190 return Name(fileUrl);
191
192 TQStringList dir = TQStringList::split("/", dirUrl, false);
193 TQStringList file = TQStringList::split("/", fileUrl, false);
194
195 TQString 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 TQString result_up;
205 TQString result_down;
206 TQString currDir;
207 TQString currFile;
208 do
209 {
210 i >= dir.count() ? currDir = "" : currDir = dir[i];
211 i >= file.count() ? currFile = "" : currFile = file[i];
212// tqWarning("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
235URL::URL( KURL base, KURL url, Type type )
236 :Name(Name::relativeName(base.path(), url.path()).rurl(), type), m_base(base)
237{
238}
239
240URL::URL( KURL base, TQString url, bool isUrlRelative, Type type )
241 :Name(isUrlRelative ? url : Name::relativeName(base.path(), url).rurl(), type), m_base(base)
242{
243}
244
245void URL::setBase( const KURL & base )
246{
247 m_base = base;
248}
249
250void URL::setBase( const TQString & base )
251{
252 KURL url;
253 url.setPath(base);
254 m_base = url;
255}
256
257KURL URL::base( ) const
258{
259 return m_base;
260}
261
262TQString URL::basePath( ) const
263{
264 return m_base.path(1);
265}
266
267KURL URL::url( ) const
268{
269 KURL url = m_base;
270 url.addPath(rurl());
271 url.cleanPath();
272 return url;
273}
274
275TQString 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
288TQString URL::urlDirectory( ) const
289{
290 KURL url = m_base;
291 url.addPath(rurl());
292 url.cleanPath();
293 return url.directory(false, false);
294}
295
296URL URL::relativeTo( KURL base )
297{
298 return URL(base, url(), type());
299}
300
301URL URL::relativeURL( KURL base, KURL url )
302{
303 return URL(base, url);
304}
305
306URL URL::relativeURL( KURL base, TQString url, bool isUrlRelative )
307{
308 return URL(base, url, isUrlRelative);
309}
310
311bool Relative::URL::operator ==( const URL & url )
312{
313 return (m_base == url.base()) && (rurl() == url.rurl());
314}
315
316bool Relative::URL::operator !=( const URL & url )
317{
318 return (m_base != url.base()) || (rurl() != url.rurl());
319}
320
321
322
323// Directory class
324
325Directory::Directory( KURL base, KURL url )
326 :URL(base, url, Name::Directory)
327{
328}
329
330Directory::Directory( KURL base, TQString url, bool isRelativeUrl )
331 :URL(base, url, isRelativeUrl, Name::Directory)
332{
333}
334
335void Directory::setRURL( TQString rurl )
336{
337 URL::setRURL(rurl, Name::Directory);
338}
339
340void Directory::setRURL( TQString rurl, Type type )
341{
342 URL::setRURL(rurl, type);
343}
344
345
346
347//File class
348
349File::File( KURL base, KURL url )
350 :URL(base, url, Name::File)
351{
352}
353
354File::File( KURL base, TQString url, bool isRelativeUrl )
355 :URL(base, url, isRelativeUrl, Name::File)
356{
357}
358
359void File::setRURL( TQString rurl, Type type )
360{
361 URL::setRURL(rurl, type);
362}
363
364void File::setRURL( TQString rurl )
365{
366 URL::setRURL(rurl, Name::File);
367}
368
369}
Relative::Directory
Relative directory name.
Definition rurl.h:149
Relative::Directory::setRURL
void setRURL(TQString rurl)
Works as URL::setRURL(TQString), only implies Name::Directory mode.
Definition rurl.cpp:335
Relative::File
Relative file name.
Definition rurl.h:165
Relative::File::setRURL
void setRURL(TQString rurl)
Works as URL::setRURL(TQString), only implies Name::File mode.
Definition rurl.cpp:364
Relative::Name
Relative name of a file or directory.
Definition rurl.h:16
Relative::Name::addPath
void addPath(const TQString &addendum)
Adds addendum to the directory path.
Definition rurl.cpp:80
Relative::Name::correctName
static TQString correctName(const TQString &rurl, const Type type=Auto)
Corrects rurl according to the given type and returns corrected url.
Definition rurl.cpp:46
Relative::Name::relativeName
static Name relativeName(const TQString &base, const TQString &url)
Creates and returns relative name between base and url.
Definition rurl.cpp:184
Relative::Name::fileName
TQString fileName() const
Returns the name of the file without the path or TQString() for directories.
Definition rurl.cpp:123
Relative::Name::cleanName
static TQString cleanName(const TQString &rurl)
Cleans rurl by removing extra slashes.
Definition rurl.cpp:91
Relative::Name::Name
Name(const TQString &rurl, const Type type=Auto)
Constructor takes the relative name of a directory or file.
Definition rurl.cpp:10
Relative::Name::extension
TQString extension(bool complete=true) const
Returns the extension of a file or TQString() for directories.
Definition rurl.cpp:110
Relative::Name::isDirectory
bool isDirectory() const
Returns true if the type of RName is directory.
Definition rurl.cpp:143
Relative::Name::isValid
bool isValid() const
Checks if RName is valid.
Definition rurl.cpp:158
Relative::Name::setRURL
void setRURL(const TQString &rurl, const Type type)
Sets the relative name.
Definition rurl.cpp:68
Relative::Name::rurl
TQString rurl() const
Gets the relative name in form dir/dir/ -> directory or dir/dir/file -> file.
Definition rurl.cpp:75
Relative::Name::setType
void setType(const Type type)
Sets a type of the relative name - file or directory.
Definition rurl.cpp:179
Relative::Name::correct
void correct()
Corrects m_rurl and m_type according to the relative name storing policy, i.e.
Definition rurl.cpp:22
Relative::Name::type
Type type() const
Returns a type of the relative name - file or directory.
Definition rurl.cpp:174
Relative::Name::directory
TQString directory() const
Returns the name of the directory or TQString() if there are no dirs in path.
Definition rurl.cpp:130
Relative::Name::isFile
bool isFile() const
Returns true if the type of RName is file.
Definition rurl.cpp:138
Relative::Name::cleanRURL
void cleanRURL()
Removes "//" from the name.
Definition rurl.cpp:86
Relative::URL
Relative name of file or directory to some base location.
Definition rurl.h:91
Relative::URL::urlPath
TQString urlPath() const
Returns a path of a complete url to the location.
Definition rurl.cpp:275
Relative::URL::relativeTo
URL relativeTo(KURL base)
Returns a new URL that is relative to given base.
Definition rurl.cpp:296
Relative::URL::basePath
TQString basePath() const
Returns a path of a base KURL (using KURL::path()).
Definition rurl.cpp:262
Relative::URL::relativeURL
static URL relativeURL(KURL base, KURL url)
Returns a new relative URL constructed from base and given url.
Definition rurl.cpp:301
Relative::URL::setBase
void setBase(const KURL &base)
Sets a new base for a RURL.
Definition rurl.cpp:245
Relative::URL::urlDirectory
TQString urlDirectory() const
Returns a directory of a complete url to the location.
Definition rurl.cpp:288
Relative::URL::base
KURL base() const
Returns RURL base.
Definition rurl.cpp:257
Relative::URL::URL
URL(KURL base, KURL url, Type type=Auto)
Evaluates the relative path between url and base and creates RURL object.
Definition rurl.cpp:235
Relative::URL::url
KURL url() const
Returns a complete url to the RURL location.
Definition rurl.cpp:267
Relative
Classes and functions to work with relative URLs.
Definition rurl.cpp:5
rurl.h
Classes and functions to work with relative 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.