-1

I am trying to iterate through an input argument in C which I'm storing in a pointer to char like this: const char *page = argv[1];

Now the argument is going to be a http address so it might look something like this:

http://host:port/folder1/folder2/file.jpg

And what I want is: to parse this string and separate it to substrings

char host = "host"; 
char port = "port"; 
char path = "/folder1/folder2/file.jpg";
char file = "file.jpg"; 

So I thought a simple for loop counting the number of slashes for example might do the trick.

int slash_cnt = 0;
int i = 0;

for (i = 0, page[i] != '\0', i++)
{ 
    if (strcmp(&page[i], "/")) 
    { 
        slash_cnt++; 
    }
}

But this isn't working.

If anybody could help me out here or even suggest a better way to parse the http address specified in the input and separate it accordingly, I'd be very grateful!

7
  • 1
    Possible duplicate of Best ways of parsing a URL using C? Commented Mar 12, 2017 at 12:56
  • Well I just tried writing a custom parser and as you can see it's not working so, that's why I'm asking the question : ) Commented Mar 12, 2017 at 12:57
  • strcmp() compares strings, not individual characters or parts of strings. RTFM. Commented Mar 12, 2017 at 12:57
  • well if (strcmp(&page[i], "/")) can be simplified to if (page[i] == '/')). And can you please make it a little clearer what you want this parser to do? If you wanted to clean things up you could have an array of char* and access them through an enum. Anyway, the best thing to do, is to call strcpy on the pointers current pos - the last pos where you found a slash ( or the begging of the string ). Commented Mar 12, 2017 at 12:57
  • 1
    @George - strcmp(&page[i], "/") is not equivalent to page[i] == '/'. Commented Mar 12, 2017 at 12:59

1 Answer 1

0

strstr could be used to search for the strings //, / and : that separate the sections of interest. strrchr could be used to find the trailing / that precedes the filename.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char *page = "http://host:port/folder1/folder2/file.jpg";
    char *slashes = "//";
    char *slash = "/";
    char *colon = ":";
    char *start = NULL;
    char *stop = NULL;
    char *host = NULL;
    char *port = NULL;
    char *path = NULL;
    char *file = NULL;
    size_t span = 0;

    if ( ( start = strstr ( page, slashes)) == NULL) {
        fputs ( "could not find \"//\"\n", stderr);
        return 1;
    }
    start += strlen ( slashes);//move pointer so the slashes are excluded
    if ( ( stop = strstr ( start, colon)) == NULL) {
        fputs ( "could not find \":\"\n", stderr);
        return 1;
    }
    span = stop - start;
    if ( ( host = malloc ( span)) == NULL) {
        fputs ( "malloc failed\n", stderr);
        return 1;
    }
    memmove ( host, start, span);
    host[span] = '\0';

    start = stop + strlen ( colon);
    if ( ( stop = strstr ( start, slash)) == NULL) {
        fputs ( "could not find leading \"/\"\n", stderr);
        return 1;
    }
    span = stop - start;
    if ( ( port = malloc ( span)) == NULL) {
        fputs ( "malloc failed\n", stderr);
        return 1;
    }
    memmove ( port, start, span);
    port[span] = '\0';

    span = strlen ( stop) + 1;
    if ( ( path = malloc ( span)) == NULL) {
        fputs ( "malloc failed\n", stderr);
        return 1;
    }
    memmove ( path, stop, span);
    path[span] = '\0';

    if ( ( start = strrchr ( page, slash[0])) == NULL) {
        fputs ( "could not find trailing \"/\"\n", stderr);
        return 1;
    }
    start += strlen ( slash);
    span = strlen ( start) + 1;
    if ( ( file = malloc ( span)) == NULL) {
        fputs ( "malloc failed\n", stderr);
        return 1;
    }
    memmove ( file, start, span);
    file[span] = '\0';

    puts ( host);
    puts ( port);
    puts ( path);
    puts ( file);

    free ( host);
    free ( port);
    free ( path);
    free ( file);

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

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.