GnuCash c935c2f+
Loading...
Searching...
No Matches
Files | Macros | Functions
Uri conversion

Files

file  gnc-uri-utils.h
 Utility functions for convert uri in separate components and back.
 

Macros

#define GNC_DATAFILE_EXT   ".gnucash"
 
#define GNC_LOGFILE_EXT   ".log"
 

Functions

gboolean gnc_uri_is_uri (const gchar *uri)
 Checks if the given uri is a valid uri.
 
void gnc_uri_get_components (const gchar *uri, gchar **scheme, gchar **hostname, gint32 *port, gchar **username, gchar **password, gchar **path)
 Converts a uri in separate components.
 
gchar * gnc_uri_get_scheme (const gchar *uri)
 Extracts the scheme from a uri.
 
gchar * gnc_uri_get_path (const gchar *uri)
 Extracts the path part from a uri.
 
gchar * gnc_uri_create_uri (const gchar *scheme, const gchar *hostname, gint32 port, const gchar *username, const gchar *password, const gchar *path)
 Composes a normalized uri starting from its separate components.
 
gchar * gnc_uri_normalize_uri (const gchar *uri, gboolean allow_password)
 Composes a normalized uri starting from any uri (filename, db spec,...).
 
gboolean gnc_uri_is_known_scheme (const gchar *scheme)
 Checks if there is a backend that explicitly stated to handle the given scheme.
 
gboolean gnc_uri_is_file_scheme (const gchar *scheme)
 Checks if the given scheme is used to refer to a file (as opposed to a network service like a database or web url)
 
gboolean gnc_uri_is_file_uri (const gchar *uri)
 Checks if the given uri defines a file (as opposed to a network service like a database or web url)
 
gboolean gnc_uri_targets_local_fs (const gchar *uri)
 Checks if the given uri is either a valid file uri or a local filesystem path.
 
gchar * gnc_uri_add_extension (const gchar *uri, const gchar *extension)
 Adds an extension to the uri if:
 

Detailed Description

Macro Definition Documentation

◆ GNC_DATAFILE_EXT

#define GNC_DATAFILE_EXT   ".gnucash"

Definition at line 61 of file gnc-uri-utils.h.

◆ GNC_LOGFILE_EXT

#define GNC_LOGFILE_EXT   ".log"

Definition at line 62 of file gnc-uri-utils.h.

Function Documentation

◆ gnc_uri_add_extension()

gchar * gnc_uri_add_extension ( const gchar *  uri,
const gchar *  extension 
)

Adds an extension to the uri if:

  • the uri is not empty and file based
  • doesn't already have the extension
Parameters
uriThe uri to process
extensionThe extension to add if missing. Note that the extension is added verbatim, so if a dot should be added, this should be part of the extension.
Returns
The uri, but guaranteed to end with extension if the uri is file based. Otherwise the uri is returned unmodified. Note that the returned value should be freed with g_free when no longer needed.

Definition at line 413 of file gnc-uri-utils.c.

414{
415 g_return_val_if_fail( uri != 0, NULL );
416
417 /* Only add extension if the user provided the extension and the uri is
418 * file based.
419 */
420 if ( !extension || !gnc_uri_is_file_uri( uri ) )
421 return g_strdup( uri );
422
423 /* Don't add extension if it's already there */
424 if ( g_str_has_suffix( uri, extension ) )
425 return g_strdup( uri );
426
427 /* Ok, all tests passed, let's add the extension */
428 return g_strconcat( uri, extension, NULL );
429}
gboolean gnc_uri_is_file_uri(const gchar *uri)
Checks if the given uri defines a file (as opposed to a network service like a database or web url)

◆ gnc_uri_create_uri()

gchar * gnc_uri_create_uri ( const gchar *  scheme,
const gchar *  hostname,
gint32  port,
const gchar *  username,
const gchar *  password,
const gchar *  path 
)

Composes a normalized uri starting from its separate components.

The resulting uri will take either of these forms:

Only the components that are provided will be inserted in the uri. However if no scheme has been provided, 'file' will be used as default scheme.

The function allocates memory for the uri. The calling function should free this memory with g_free the uri is no longer needed.

Parameters
schemeThe scheme for this uri. If NULL,, 'file' will be used in the uri.
hostnameThe host name of the server to connect to. This will be ignored for the 'file' type schemes ('file', 'xml', 'sqlite').
portAn optional port to set o, the uri, or 0 if no port is to be set. This will be ignored for the 'file' type schemes ('file', 'xml', 'sqlite').
usernameOptional user name to set in the uri or NULL otherwise. This will be ignored for the 'file' type schemes ('file', 'xml', 'sqlite').
passwordOptional password to set in the uri or NULL otherwise. This will be ignored for the 'file' type schemes ('file', 'xml', 'sqlite').
pathThe path to set in the uri.
Returns
The normalized uri.

Definition at line 297 of file gnc-uri-utils.c.

303{
304 gchar *userpass = NULL, *portstr = NULL, *uri = NULL;
305
306 g_return_val_if_fail( path != 0, NULL );
307
308 if (!scheme || gnc_uri_is_file_scheme (scheme))
309 {
310 /* Compose a file based uri, which means ignore everything but
311 * the scheme and the path
312 * We return an absolute pathname if the scheme is known or
313 * no scheme was given. For an unknown scheme, we return the
314 * path info as is.
315 */
316 gchar *abs_path;
317 gchar *uri_scheme;
318 if (scheme && (!gnc_uri_is_known_scheme (scheme)) )
319 abs_path = g_strdup ( path );
320 else
321 abs_path = gnc_resolve_file_path ( path );
322
323 if (!scheme)
324 uri_scheme = g_strdup ("file");
325 else
326 uri_scheme = g_strdup (scheme);
327
328 /* Arrive here with...
329 *
330 * /my/path/to/file with space.txt
331 * becomes file:///my/path/to/file with space.txt
332 *
333 * c:\my\path\to\file with space.txt
334 * becomes file:///c:\my\path\to\file with space.txt
335 *
336 * \\myserver\share\path\to\file with space.txt
337 * becomes file://\\myserver\share\path\to\file with space.txt
338 *
339 * probably they should all be forward slashes and spaces escaped
340 * also with UNC it could be file://myserver/share/path/to/file with space.txt
341 */
342
343 if (g_str_has_prefix (abs_path, "/") || g_str_has_prefix (abs_path, "\\"))
344 uri = g_strdup_printf ( "%s://%s", uri_scheme, abs_path );
345 else // for windows add an extra "/"
346 uri = g_strdup_printf ( "%s:///%s", uri_scheme, abs_path );
347
348 g_free (uri_scheme);
349 g_free (abs_path);
350
351 return uri;
352 }
353
354 /* Not a file based uri, we need to setup all components that are not NULL
355 * For this scenario, hostname is mandatory.
356 */
357 g_return_val_if_fail( hostname != 0, NULL );
358
359 if ( username != NULL && *username )
360 {
361 if ( password != NULL && *password )
362 userpass = g_strdup_printf ( "%s:%s@", username, password );
363 else
364 userpass = g_strdup_printf ( "%s@", username );
365 }
366 else
367 userpass = g_strdup ( "" );
368
369 if ( port != 0 )
370 portstr = g_strdup_printf ( ":%d", port );
371 else
372 portstr = g_strdup ( "" );
373
374 // XXX Do I have to add the slash always or are there situations
375 // it is in the path already ?
376 uri = g_strconcat ( scheme, "://", userpass, hostname, portstr, "/", path, NULL );
377
378 g_free ( userpass );
379 g_free ( portstr );
380
381 return uri;
382
383}
gboolean gnc_uri_is_file_scheme(const gchar *scheme)
Checks if the given scheme is used to refer to a file (as opposed to a network service like a databas...
gboolean gnc_uri_is_known_scheme(const gchar *scheme)
Checks if there is a backend that explicitly stated to handle the given scheme.

◆ gnc_uri_get_components()

void gnc_uri_get_components ( const gchar *  uri,
gchar **  scheme,
gchar **  hostname,
gint32 *  port,
gchar **  username,
gchar **  password,
gchar **  path 
)

Converts a uri in separate components.

The function allocates memory for each of the components that it finds in the uri. The calling function should free this memory with g_free when the items are no longer needed.

Parameters
uriThe uri to convert
schemeThe scheme for this uri. If the uri doesn't have an explicit scheme, NULL will be returned.
hostnameThe host name of the server to connect to. In case of the local file system path, NULL will be returned
portAn optional port to connect to or 0 if the default port is to be used. For local filesystem path this is always 0 as well.
usernameOptional user name found in this uri or NULL if none is found.
passwordOptional password found in this uri or NULL if none is found.
pathThe path found in this uri.

Definition at line 143 of file gnc-uri-utils.c.

150{
151 gchar **splituri;
152 gchar *url = NULL, *tmpusername = NULL, *tmphostname = NULL;
153 gchar *delimiter = NULL;
154
155 *scheme = NULL;
156 *hostname = NULL;
157 *port = 0;
158 *username = NULL;
159 *password = NULL;
160 *path = NULL;
161
162 g_return_if_fail( uri != NULL && strlen (uri) > 0);
163
164 splituri = g_strsplit ( uri, "://", 2 );
165 if ( splituri[1] == NULL )
166 {
167 /* No scheme means simple file path.
168 Set path to copy of the input. */
169 *path = g_strdup ( uri );
170 g_strfreev ( splituri );
171 return;
172 }
173
174 /* At least a scheme was found, set it here */
175 *scheme = g_strdup ( splituri[0] );
176
177 if ( gnc_uri_is_file_scheme ( *scheme ) )
178 {
179 /* a true file uri on windows can start file:///N:/
180 so we come here with /N:/, it could also be /N:\
181 */
182 if (g_str_has_prefix (splituri[1], "/") &&
183 ((g_strstr_len (splituri[1], -1, ":/") != NULL) || (g_strstr_len (splituri[1], -1, ":\\") != NULL)))
184 {
185 gchar *ptr = splituri[1];
186 *path = gnc_resolve_file_path ( ptr + 1 );
187 }
188 else
189 *path = gnc_resolve_file_path ( splituri[1] );
190 g_strfreev ( splituri );
191 return;
192 }
193
194 /* Protocol indicates full network style uri, let's see if it
195 * has a username and/or password
196 */
197 url = g_strdup (splituri[1]);
198 g_strfreev ( splituri );
199
200 /* Check for "@" sign, but start from the end - the password may contain
201 * this sign as well
202 */
203 delimiter = g_strrstr ( url, "@" );
204 if ( delimiter != NULL )
205 {
206 /* There is at least a username in the url */
207 delimiter[0] = '\0';
208 tmpusername = url;
209 tmphostname = delimiter + 1;
210
211 /* Check if there's a password too by looking for a :
212 * Start from the beginning this time to avoid possible :
213 * in the password */
214 delimiter = g_strstr_len ( tmpusername, -1, ":" );
215 if ( delimiter != NULL )
216 {
217 /* There is password in the url */
218 delimiter[0] = '\0';
219 *password = g_strdup ( (const gchar*)(delimiter + 1) );
220 }
221 *username = g_strdup ( (const gchar*)tmpusername );
222 }
223 else
224 {
225 /* No username and password were given */
226 tmphostname = url;
227 }
228
229 /* Find the path part */
230 delimiter = g_strstr_len ( tmphostname, -1, "/" );
231 if ( delimiter != NULL )
232 {
233 delimiter[0] = '\0';
234 if ( gnc_uri_is_file_scheme ( *scheme ) ) /* always return absolute file paths */
235 *path = gnc_resolve_file_path ( (const gchar*)(delimiter + 1) );
236 else /* path is no file path, so copy it as is */
237 *path = g_strdup ( (const gchar*)(delimiter + 1) );
238 }
239
240 /* Check for a port specifier */
241 delimiter = g_strstr_len ( tmphostname, -1, ":" );
242 if ( delimiter != NULL )
243 {
244 delimiter[0] = '\0';
245 *port = g_ascii_strtoll ( delimiter + 1, NULL, 0 );
246 }
247
248 *hostname = g_strdup ( (const gchar*)tmphostname );
249
250 g_free ( url );
251
252 return;
253
254}

◆ gnc_uri_get_path()

gchar * gnc_uri_get_path ( const gchar *  uri)

Extracts the path part from a uri.

The function allocates memory for the path. The calling function should free this memory with g_free if it no longer needs the string.

Parameters
uriThe uri to extract the path part from
Returns
The path for this uri, or NULL if no path could be extracted.

Definition at line 276 of file gnc-uri-utils.c.

277{
278 gchar *scheme = NULL;
279 gchar *hostname = NULL;
280 gint32 port = 0;
281 gchar *username = NULL;
282 gchar *password = NULL;
283 gchar *path = NULL;
284
285 gnc_uri_get_components ( uri, &scheme, &hostname, &port,
286 &username, &password, &path );
287
288 g_free (scheme);
289 g_free (hostname);
290 g_free (username);
291 g_free (password);
292
293 return path;
294}
void gnc_uri_get_components(const gchar *uri, gchar **scheme, gchar **hostname, gint32 *port, gchar **username, gchar **password, gchar **path)
Converts a uri in separate components.

◆ gnc_uri_get_scheme()

gchar * gnc_uri_get_scheme ( const gchar *  uri)

Extracts the scheme from a uri.

The function allocates memory for the scheme. The calling function should free this memory with g_free if it no longer needs the string.

Parameters
uriThe uri to extract the scheme from
Returns
The scheme for this uri. If the uri didn't have an explicit scheme, NULL will be returned.

Definition at line 256 of file gnc-uri-utils.c.

257{
258 gchar *scheme = NULL;
259 gchar *hostname = NULL;
260 gint32 port = 0;
261 gchar *username = NULL;
262 gchar *password = NULL;
263 gchar *path = NULL;
264
265 gnc_uri_get_components ( uri, &scheme, &hostname, &port,
266 &username, &password, &path );
267
268 g_free (hostname);
269 g_free (username);
270 g_free (password);
271 g_free (path);
272
273 return scheme;
274}

◆ gnc_uri_is_file_scheme()

gboolean gnc_uri_is_file_scheme ( const gchar *  scheme)

Checks if the given scheme is used to refer to a file (as opposed to a network service like a database or web url)

Parameters
schemeThe scheme to check
Returns
TRUE if the scheme is used with files, FALSE if the scheme is normally used with network services (database, web url,...). It will also return FALSE if scheme is NULL.

Definition at line 90 of file gnc-uri-utils.c.

91{
92 return (scheme &&
93 (!g_ascii_strcasecmp (scheme, "file") ||
94 !g_ascii_strcasecmp (scheme, "xml") ||
95 !g_ascii_strcasecmp (scheme, "sqlite3")));
96}

◆ gnc_uri_is_file_uri()

gboolean gnc_uri_is_file_uri ( const gchar *  uri)

Checks if the given uri defines a file (as opposed to a network service like a database or web url)

Parameters
uriThe uri to check
Returns
TRUE if the uri is a files, FALSE of the scheme is normally used with network services (database, web url,...)

Definition at line 101 of file gnc-uri-utils.c.

102{
103 gchar *scheme = gnc_uri_get_scheme ( uri );
104 gboolean result = gnc_uri_is_file_scheme ( scheme );
105
106 g_free ( scheme );
107
108 return result;
109}
gchar * gnc_uri_get_scheme(const gchar *uri)
Extracts the scheme from a uri.

◆ gnc_uri_is_known_scheme()

gboolean gnc_uri_is_known_scheme ( const gchar *  scheme)

Checks if there is a backend that explicitly stated to handle the given scheme.

Parameters
schemeThe scheme to check
Returns
TRUE if at least one backend explicitly handles this scheme, otherwise FALSE

Definition at line 61 of file gnc-uri-utils.c.

62{
63 gboolean is_known_scheme = FALSE;
64 GList *node;
65 GList *known_scheme_list = qof_backend_get_registered_access_method_list();
66
67 for ( node = known_scheme_list; node != NULL; node = node->next )
68 {
69 gchar *known_scheme = node->data;
70 if ( !g_ascii_strcasecmp (scheme, known_scheme) )
71 {
72 is_known_scheme = TRUE;
73 break;
74 }
75 }
76
77 g_list_free (known_scheme_list);
78 return is_known_scheme;
79}
GList * qof_backend_get_registered_access_method_list(void)
Return a list of strings for the registered access methods.

◆ gnc_uri_is_uri()

gboolean gnc_uri_is_uri ( const gchar *  uri)

Checks if the given uri is a valid uri.

A valid uri is defined by having at least a scheme and a path. If the uri is not referring to a file on the local file system a hostname should be set as well.

Parameters
uriThe uri to check
Returns
TRUE if the input is a valid uri, FALSE otherwise

Definition at line 32 of file gnc-uri-utils.c.

33{
34
35 gchar *scheme = NULL, *hostname = NULL;
36 gchar *username = NULL, *password = NULL;
37 gchar *path = NULL;
38 gint port = 0;
39 gboolean is_uri = FALSE;
40
41 gnc_uri_get_components ( uri, &scheme, &hostname, &port,
42 &username, &password, &path );
43
44 /* For gnucash to consider a uri valid the following must be true:
45 * - scheme and path must not be NULL
46 * - for anything but local filesystem uris, hostname must be valid as well */
47 is_uri = (scheme && path && (gnc_uri_is_file_scheme(scheme) || hostname));
48
49 g_free (scheme);
50 g_free (hostname);
51 g_free (username);
52 g_free (password);
53 g_free (path);
54
55 return is_uri;
56}

◆ gnc_uri_normalize_uri()

gchar * gnc_uri_normalize_uri ( const gchar *  uri,
gboolean  allow_password 
)

Composes a normalized uri starting from any uri (filename, db spec,...).

The resulting uri will take either of these forms:

Only the components that are provided will be inserted in the uri. The allow_password parameter controls if the password should be added to the returned uri when available. If no scheme has been provided, 'file' will be used as default scheme.

The function allocates memory for the uri. The calling function should free this memory with g_free the uri is no longer needed.

Parameters
uriThe uri that should be converted into a normalized uri
allow_passwordIf set to TRUE, the normalized uri and the input uri has a password, this passworld will also be set in the normalized uri. Otherwise no password will be set in the normalized uri.
Returns
The normalized uri.

Definition at line 385 of file gnc-uri-utils.c.

386{
387 gchar *scheme = NULL;
388 gchar *hostname = NULL;
389 gint32 port = 0;
390 gchar *username = NULL;
391 gchar *password = NULL;
392 gchar *path = NULL;
393 gchar *newuri = NULL;
394
395 gnc_uri_get_components ( uri, &scheme, &hostname, &port,
396 &username, &password, &path );
397 if (allow_password)
398 newuri = gnc_uri_create_uri ( scheme, hostname, port,
399 username, password, path);
400 else
401 newuri = gnc_uri_create_uri ( scheme, hostname, port,
402 username, /* no password */ NULL, path);
403
404 g_free (scheme);
405 g_free (hostname);
406 g_free (username);
407 g_free (password);
408 g_free (path);
409
410 return newuri;
411}
gchar * gnc_uri_create_uri(const gchar *scheme, const gchar *hostname, gint32 port, const gchar *username, const gchar *password, const gchar *path)
Composes a normalized uri starting from its separate components.

◆ gnc_uri_targets_local_fs()

gboolean gnc_uri_targets_local_fs ( const gchar *  uri)

Checks if the given uri is either a valid file uri or a local filesystem path.

A valid file uri is defined by having a file targeting scheme ('file', 'xml' or 'sqlite3' are accepted) and a non-NULL path.

Parameters
uriThe uri to check
Returns
TRUE if the input is a valid file uri or a local filesystem path. FALSE otherwise

Definition at line 113 of file gnc-uri-utils.c.

114{
115
116 gchar *scheme = NULL, *hostname = NULL;
117 gchar *username = NULL, *password = NULL;
118 gchar *path = NULL;
119 gint port = 0;
120 gboolean is_local_fs = FALSE;
121
122 gnc_uri_get_components ( uri, &scheme, &hostname, &port,
123 &username, &password, &path );
124
125 /* For gnucash to consider a uri to target the local fs:
126 * path must not be NULL
127 * AND
128 * scheme should be NULL
129 * OR
130 * scheme must be file type scheme (file, xml, sqlite) */
131 is_local_fs = (path && (!scheme || gnc_uri_is_file_scheme(scheme)));
132
133 g_free (scheme);
134 g_free (hostname);
135 g_free (username);
136 g_free (password);
137 g_free (path);
138
139 return is_local_fs;
140}