1

Is it possible to get URL fragment parameters in C under glib ?

I've got a url like file://localhost/home/me/notepad.txt#line=100,2

What's the best way to get the parameters specified at the end of the url ?

2
  • 1
    The relevant section of the GLib documentation suggests not; at least not in a handy do-it-for-you function. Commented Sep 3, 2019 at 0:37
  • Cheers, I guess they just implemented what they needed - seemed a bit weird there was nothing there. Commented Sep 3, 2019 at 11:57

4 Answers 4

2

There’s no single GLib function which will do what you want. If you can use libsoup (also part of the GNOME stack), then you can do it using SoupURI:

g_autoptr(SoupURI) uri = soup_uri_new (uri_string);
const gchar *fragment = soup_uri_get_fragment (uri);

That will set fragment to line=100,2. You’ll have to do further parsing of whatever your fragment format is, by hand. g_strsplit() would be useful for that.

Sign up to request clarification or add additional context in comments.

Comments

2

Since GLib 2.66 (released Sep 10, 2020) it is possible to parse uris by using GUri eg. g_uri_parse()

Most other answers are dated before 2020 that's why they say it's not possible.

Comments

0

You may also take a look on the function parse_sftp_uri from gnome-terminal terminal-nautilus.c file. It can be easily adapted for general URIs.

Comments

-1

Unsure if you mean to parse notepad.txt#line=100,2 or #line=100,2, nevertheless my answer should work in both cases.

You can use the strrchr() (man strrchr) function to get the last occurence of a character within a string.

Something like:

char *file;
file = strrchr(url, '/') + 1;

7 Comments

That won’t work if the fragment itself contains a /.
I believe there can't be a '/' in a URL or else it's not the parameter
RFC 3986 explicitly allows / (and ?) in fragments. The syntax for a fragment is fragment = *( pchar / "/" / "?" ).
That's a bit surprising to say the least, I was doing SEO and had to parse millions of URLs for analysis purposes and can't recall ever having this issue. Guess I was just lucky :) thanks for pointing that out!
Hi, it's the line=100,2 I'd like to extract.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.