ArnLib  4.0.x
Active Registry Network
Arn.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2022 Michael Wiklund.
2 // All rights reserved.
3 // Contact: arnlib@wiklunden.se
4 //
5 // This file is part of the ArnLib - Active Registry Network.
6 // Parts of ArnLib depend on Qt and/or other libraries that have their own
7 // licenses. Usage of these other libraries is subject to their respective
8 // license agreements.
9 //
10 // GNU Lesser General Public License Usage
11 // This file may be used under the terms of the GNU Lesser General Public
12 // License version 2.1 as published by the Free Software Foundation and
13 // appearing in the file LICENSE_LGPL.txt included in the packaging of this
14 // file. In addition, as a special exception, you may use the rights described
15 // in the Nokia Qt LGPL Exception version 1.1, included in the file
16 // LGPL_EXCEPTION.txt in this package.
17 //
18 // GNU General Public License Usage
19 // Alternatively, this file may be used under the terms of the GNU General Public
20 // License version 3.0 as published by the Free Software Foundation and appearing
21 // in the file LICENSE_GPL.txt included in the packaging of this file.
22 //
23 // Other Usage
24 // Alternatively, this file may be used in accordance with the terms and conditions
25 // contained in a signed written agreement between you and Michael Wiklund.
26 //
27 // This program is distributed in the hope that it will be useful, but WITHOUT ANY
28 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
29 // PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
30 //
31 
32 #include "ArnInc/Arn.hpp"
33 #include "ArnInc/ArnLib.hpp"
34 #include "ArnInc/ArnCompat.hpp"
35 #include <QUuid>
36 #include <QStringList>
37 #if QT_VERSION >= 0x050a00
38  #include <QRandomGenerator>
39 #endif
40 
41 
42 
43 namespace Arn {
44 
45 const QString pathLocal = "/Local/"; // Must be absolute (full) path
46 const QString pathLocalSys = "Sys/";
47 const QString pathDiscover = "Sys/Discover/";
48 const QString pathDiscoverThis = "Sys/Discover/This/";
49 const QString pathDiscoverConnect = "Sys/Discover/Connect/";
50 const QString pathServer = "Sys/Server/";
51 const QString pathServerSessions = "Sys/Server/Sessions/";
52 
53 
54 QString convertName( const QString& name, NameF nameF)
55 {
56  bool isFolderMarked = name.endsWith('/');
57  QString baseName = name.left( name.size() - int( isFolderMarked)); // remove any foldermark
58 
59  QString retVal = convertBaseName( baseName, nameF);
60  if (isFolderMarked && !nameF.is(( nameF.NoFolderMark)))
61  retVal += '/'; // Restore previous foldermark
62 
63  return retVal;
64 }
65 
66 
67 #ifndef DOXYGEN_SKIP
68 QString convertBaseName( const QString& name, NameF nameF)
69 {
70  QString retVal("");
71 
72  if (name.isEmpty() && !nameF.is( nameF.EmptyOk))
73  retVal = '@';
74  else if ((name != "@") || !nameF.is( nameF.EmptyOk))
75  retVal = name;
76 
77  return retVal;
78 }
79 #endif
80 
81 
82 QString fullPath( const QString& path)
83 {
84  if (path.startsWith('/')) return path;
85 
86  return Arn::pathLocal + path;
87 }
88 
89 
90 QString itemName( const QString& path)
91 {
92  int from = path.endsWith('/') ? -2 : -1;
93  int pos = path.lastIndexOf('/', from);
94 
95  if (pos < 0) return path;
96  return Arn::convertName( path.mid( pos + 1));
97 }
98 
99 
100 QString childPath( const QString& parentPath, const QString& posterityPath)
101 {
102  QString parentPath_ = parentPath;
103  if (!parentPath_.endsWith('/')) parentPath_ += '/';
104  if (!posterityPath.startsWith( parentPath_)) return QString(); // Null, posterity not belonging tp parent
105 
106  int i = posterityPath.indexOf('/', parentPath_.size());
107  if (i >= 0) // The child part has folder(s)
108  return posterityPath.left(i + 1);
109  else
110  return posterityPath;
111 }
112 
113 
114 QString changeBasePath( const QString& oldBasePath, const QString& newBasePath, const QString& path)
115 {
116  QString oldBasePath_ = oldBasePath;
117  if (!oldBasePath_.endsWith('/')) oldBasePath_ += '/';
118  QString newBasePath_ = newBasePath;
119  if (!newBasePath_.endsWith('/')) newBasePath_ += '/';
120 
121  if (newBasePath_ == oldBasePath_) return path; // No change wanted
122  if (!path.startsWith( oldBasePath_)) return path; // Path not belonging tp old base path
123 
124  return newBasePath_ + path.mid( oldBasePath_.size());
125 }
126 
127 
128 QString makePath( const QString& parentPath, const QString& itemName)
129 {
130  QString parentPath_ = parentPath;
131  if (!parentPath_.endsWith('/')) parentPath_ += '/';
132 
133  return parentPath_ + Arn::convertName( itemName, Arn::NameF::EmptyOk);
134 }
135 
136 
137 QString addPath( const QString& parentPath, const QString& childRelPath, Arn::NameF nameF)
138 {
139  QString retPath = parentPath;
140  if (!retPath.endsWith('/'))
141  retPath += '/';
142  retPath += childRelPath;
143 
144  return convertPath( retPath, nameF);
145 }
146 
147 
148 QString convertPath( const QString& path, Arn::NameF nameF)
149 {
150  nameF.set( nameF.NoFolderMark, false); // Foldermark '/' must be ...
151  if (nameF.is( nameF.Relative))
152  nameF.set( nameF.EmptyOk, false); // Relative implicates no emty links
153 
154  QString retPath;
155  if (!nameF.is( nameF.Relative))
156  retPath += '/'; // Start of absolute path
157 
158  QString pathNorm = path.trimmed();
159  bool isFolder = pathNorm.isEmpty() || pathNorm.endsWith("/");
160  if (isFolder && !pathNorm.isEmpty())
161  pathNorm.resize( pathNorm.size() - 1); // Remove '/' at end (Also root become "")
162 
163  QStringList linkNames = pathNorm.split("/");
164  bool needSeparator = false;
165 
166  for (int i = 0; i < linkNames.size(); i++) {
167  QString linkName = linkNames.at(i);
168  if (linkName.isEmpty() && i == 0) // If link is root, go for next link
169  continue;
170  if (needSeparator)
171  retPath += '/'; // Add link separator
172 
173  retPath += Arn::convertName( linkName, nameF);
174  needSeparator = true;
175  }
176  if (isFolder && !pathNorm.isEmpty()) // Folder that is not root
177  retPath += '/'; // Add folder mark
178 
179  return retPath;
180 }
181 
182 
183 QString parentPath( const QString& path)
184 {
185  QString retPath = path;
186  if (retPath.endsWith("/"))
187  retPath.chop(1);
188  if (!retPath.contains("/")) return QString(); // Can't give a valid parent
189 
190  retPath.resize( retPath.lastIndexOf("/") + 1);
191  return retPath;
192 }
193 
194 
195 QString twinPath( const QString& path)
196 {
197  if (path.endsWith('/')) return path; // Can't return twin for a folder
198 
199  if (path.endsWith('!')) return path.left( path.size() - 1);
200  return path + '!';
201 }
202 
203 
204 QString providerPathIf( const QString& path, bool giveProviderPath)
205 {
206  return (giveProviderPath == isProviderPath( path)) ? path : twinPath( path);
207 }
208 
209 
210 bool isFolderPath( const QString& path)
211 {
212  return path.endsWith('/');
213 }
214 
215 
216 bool isProviderPath( const QString& path)
217 {
218  return path.endsWith('!');
219 }
220 
221 
222 QString uuidPath( const QString& path)
223 {
224  QUuid uuid = QUuid::createUuid();
225  bool isProvider = Arn::isProviderPath( path);
226 
227  QString retVal = providerPathIf( path, false); // Allways Requester path (no "!")
228  retVal += uuid.toString();
229  retVal = providerPathIf( retVal, isProvider); // Restore original "!"
230 
231  return retVal;
232 }
233 
234 
235 QString makeHostWithInfo( const QString& host, const QString& info)
236 {
237  return host + ((info.isEmpty() || info == host) ? QString()
238  : (" [" + info + "]"));
239 }
240 
241 
242 QString hostFromHostWithInfo( const QString& hostWithInfo)
243 {
244  QString retVal = hostWithInfo;
245  int pos = retVal.indexOf(" ");
246  if (pos >= 0)
247  retVal.resize( pos);
248 
249  return retVal;
250 }
251 
252 
253 bool isNullPtr( const void* ptr)
254 {
255  return ptr == arnNullptr;
256 }
257 
258 
259 uint rand()
260 {
261 #if QT_VERSION >= 0x050a00
262  if (sizeof(uint) >= sizeof(quint64))
263  return QRandomGenerator::global()->generate64();
264  else
265  return QRandomGenerator::global()->generate();
266 #else
267  return qrand();
268 #endif
269 
270 }
271 
272 } // Arn::
273 
QString childPath(const QString &parentPath, const QString &posterityPath)
Get substring for child from a path (posterityPath)
Definition: Arn.cpp:100
QString convertName(const QString &name, NameF nameF)
Convert a name to a specific format.
Definition: Arn.cpp:54
uint rand()
Definition: Arn.cpp:259
QString hostFromHostWithInfo(const QString &hostWithInfo)
Get the host from the HostWithInfo string.
Definition: Arn.cpp:242
const QString pathDiscover
Definition: Arn.cpp:47
Path: "/@/test" ==> "//test", Item: "@" ==> "".
Definition: Arn.hpp:189
const QString pathLocal
Definition: Arn.cpp:45
QString fullPath(const QString &path)
Convert a path to a full absolute path.
Definition: Arn.cpp:82
const QString pathServer
Definition: Arn.cpp:50
bool isNullPtr(const void *ptr)
Definition: Arn.cpp:253
const QString pathLocalSys
Definition: Arn.cpp:46
Only on path, no effect on discrete names. "/test/value" ==> "test/value".
Definition: Arn.hpp:191
QString addPath(const QString &parentPath, const QString &childRelPath, Arn::NameF nameF)
Make a path from a parent and an additional relative path.
Definition: Arn.cpp:137
const QString pathDiscoverConnect
Definition: Arn.cpp:49
QString makePath(const QString &parentPath, const QString &itemName)
Make a path from a parent and an item name.
Definition: Arn.cpp:128
QString changeBasePath(const QString &oldBasePath, const QString &newBasePath, const QString &path)
Change the base (start) of a path.
Definition: Arn.cpp:114
QString uuidPath(const QString &path)
Get a path to an Arn Object with a unique uuid name.
Definition: Arn.cpp:222
Definition: Arn.cpp:43
QString convertPath(const QString &path, Arn::NameF nameF)
Convert a path to a specific format.
Definition: Arn.cpp:148
QString twinPath(const QString &path)
Get the bidirectional twin to a given path
Definition: Arn.cpp:195
const QString pathServerSessions
Definition: Arn.cpp:51
QString providerPathIf(const QString &path, bool giveProviderPath)
Get provider path or requester path
Definition: Arn.cpp:204
const QString pathDiscoverThis
Definition: Arn.cpp:48
QString parentPath(const QString &path)
Get the parent to a given path
Definition: Arn.cpp:183
QString itemName(const QString &path)
The last part of a path
Definition: Arn.cpp:90
QString makeHostWithInfo(const QString &host, const QString &info)
Make a combined host and info string, i.e. HostWithInfo
Definition: Arn.cpp:235
bool isFolderPath(const QString &path)
Test if path is a folder path
Definition: Arn.cpp:210
Only on discrete names, no effect on path. "test/" ==> "test".
Definition: Arn.hpp:187
bool isProviderPath(const QString &path)
Test if path is a provider path
Definition: Arn.cpp:216