How to determine the shortest path?

How to determine the URL, URI and Path in C or Objective-C?

  • I need a way in C or Objective-C to separate the URI and the Path, given a full URL. Example: Given the URL mms://a11.l412923342423.c658546.g.lm.akamaistream.net/D/13/414392/v0001/reflector:36751 Getting the URL is easy, but how can I determine where the URI ends and where the Path starts, in C or Objective-C? I know that the URI is a11.l412923342423.c658546.g.lm.akamaistream.net and that the Path is D/13/414392/v0001/reflector:36751, but how to identify that programmatically? I can't figure that out and any sample code will GREATLY help me. Thank you.

  • Answer:

    Looks like // denotes the start of the URI and a subsequent / marks the start of the path: char *uri_start; // Start of URI int uri_length; // Length of URI char *path_start; // Start of Path (until end of string) uri_start = strstr(url, "//"); if (uri_start == NULL) { uri_start = url; } else { uri_start += 2; // skip "//" } path_start = strstr(uri_start, "/"); if (path_start == NULL) { path_start = ""; // Path empty uri_length = strlen(uri_start); } else { path_start += 1; // skip "/" uri_length = path_start - uri_start - 1; } Edit: Copying the uri: char uri[300]; // or char *uri = malloc(uri_length + 1); memcpy(uri, uri_start, uri_length); // Copy the uri uri[uri_length] = '\0'; // nul-terminate the uri string or (if it is ok, to alter the original string): uri_start[uri_length] = '\0'; // nul-terminates the uri but alters the url

Winston at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

One could look for the third SLASH in the URL, and even test if the first and second are successive.

NitWit

There are lots of properties on NSURL objects that give the various components of the URL. Have you tried using those?

Jim

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.