0

is there is any function in c++ ,similar to dirname in php...it is used to normalize the url

eg
<?php
$url = "../tets/index.html";
$currentURL = "http://example.com/somedir/anotherdir";
echo dirname($currentURL).substr($url, 2);
?>
raagavan
  • 951
  • 3
  • 12
  • 16
  • 2
    Dup of http://stackoverflow.com/questions/2616011/easy-way-to-parse-a-url-in-c-cross-platform - use `boost::network::http::uri::path()` to extract that part of a URL. – FrankH. Feb 22 '11 at 11:59

3 Answers3

3

Standard C++ has no notion of directories, so you will either have to use a platform-specific function, or a portable library such as Boost.Filesystem.

I wouldn't use such a function on a URL though; try to find a proper URL parsing library.

Community
  • 1
  • 1
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
3

No, but implementing it yourself is trivial.

std::string DirName(std::string source)
{
    source.erase(std::find(source.rbegin(), source.rend(), '/').base(), source.end());
    return source;
}

Even better would be to implement it as a method template:

template<typename string_t>
string_t DirName(string_t source)
{
    source.erase(std::find(source.rbegin(), source.rend(), '/').base(), source.end());
    return source;
}

EDIT: And for some reason if you want what @larsmans is talking about in the comment below:

template<typename string_t>
string_t DirName(string_t source)
{
    if (source.size() <= 1) //Make sure it's possible to check the last character.
    {
        return source;
    }
    if (*(source.rbegin() + 1) == '/') //Remove trailing slash if it exists.
    {
        source.pop_back();
    }
    source.erase(std::find(source.rbegin(), source.rend(), '/').base(), source.end());
    return source;
}
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • ... except that the OP might want to turn `foo/bar/` into `foo` instead of `foo/bar`, and there are several more corner cases in path/URL handling. – Fred Foo Feb 22 '11 at 15:24
  • @larsmans: This code does what [`dirname`](http://en.wikipedia.org/wiki/Dirname) does. If your description is what the OP wants then (s)he needs to specify that. Obviously if (s)he needs to do more then it'd be a good idea to use a library, but this task in and of itself is trivial. – Billy ONeal Feb 22 '11 at 15:27
  • Except that "If there are no slashes in path, a dot ('.') is returned, indicating the current directory"... Fair enough, +1. – Fred Foo Feb 22 '11 at 15:38
  • @larsmans: Also, this doesn't remove the trailing slash. That differs from `dirname` but matches the OP's example above :/ – Billy ONeal Feb 22 '11 at 15:46
0

There is no directory access in the C++ language. You need to implement this with platform dependent functions like FindFirst/FindNext (Microsoft Windows) or with a library like boost (Boost.Filesystem).

harper
  • 13,345
  • 8
  • 56
  • 105
  • There certainly is file access in C++. – Fred Foo Feb 22 '11 at 12:03
  • there is more likely a `Path...()` functions in `shlwapi.lib` that perform the same stuff without the need to iterate through the path using `FindFirst()`/`FindNext()`... – Adrien Plisson Feb 22 '11 at 12:13
  • @larsman: You can access files with C-run-time functions and the C++ standard library, but not in the C++ language. What function or classes do you propose for directory access? – harper Feb 22 '11 at 14:57
  • ok, it's not in the *language*, it's in the *library*. I don't think that distinction is relevant to the OP's needs. – Fred Foo Feb 22 '11 at 15:22