Implementing dirname with C++ strings.

T

Tatu Portin

I need to implement simple function to get current directory from
program directory.

e.g. /home/tatu/src/main -> /home/tatu/src
where main is the currently running program.
i.e. argv[0] == /home/tatu/src/main.

First, I did it with C-style:

// start-of-file

static const char*
parse_currentdir(const char* const program_location)
{
register int i;
const char* const loc = program_location;

size_t size = strlen(loc);

for (i = (size - 1); (i >= 0) && (loc != DIR_SEPARATOR); --i)
;

if (loc == DIR_SEPARATOR)
{
char* curdir = (char*) malloc((i + 1) * sizeof(char));
if (curdir != NULL)
{
strncpy (curdir, loc, (size_t) i);
curdir = '\0';
return curdir;
}
else
{
// Memory allocation error.
}
}
else
{
// There was no DIR_SEPARATOR in LOC.
}

const char* curdir = ".";
return curdir;
}

int
main (int argc, const char* const argv[])
{
const char* curdir = parse_currentdir (argv[0]);
return 0;
}

// end-of-file

But then I don't learn anything about C++. In particular, I found
out[1] that I could use find_last_of() member routine of C++ string
class. But find_last_of() returns type of size_type, and I cannot
figure out how I should use the size_type to create smaller string
containing only current path.

Can I convert size_type to an iterator?


[1] found out:
http://www.cppreference.com/cppstring/end.html
If you know better reference pages, please include them in your
answer.
 
M

Mike Wahler

Tatu Portin said:
I need to implement simple function to get current directory from
program directory.

e.g. /home/tatu/src/main -> /home/tatu/src
where main is the currently running program.
i.e. argv[0] == /home/tatu/src/main.

First, I did it with C-style:

// start-of-file

static const char*
parse_currentdir(const char* const program_location)
{
register int i;
const char* const loc = program_location;

size_t size = strlen(loc);

for (i = (size - 1); (i >= 0) && (loc != DIR_SEPARATOR); --i)
;

if (loc == DIR_SEPARATOR)
{
char* curdir = (char*) malloc((i + 1) * sizeof(char));
if (curdir != NULL)
{
strncpy (curdir, loc, (size_t) i);
curdir = '\0';
return curdir;
}
else
{
// Memory allocation error.
}
}
else
{
// There was no DIR_SEPARATOR in LOC.
}

const char* curdir = ".";
return curdir;
}

int
main (int argc, const char* const argv[])
{
const char* curdir = parse_currentdir (argv[0]);
return 0;
}

// end-of-file

But then I don't learn anything about C++. In particular, I found
out[1] that I could use find_last_of() member routine of C++ string
class. But find_last_of() returns type of size_type, and I cannot
figure out how I should use the size_type to create smaller string
containing only current path.

Can I convert size_type to an iterator?


For sequences which support random-access iterators
(of which string is one):

iterator + size_type == iterator
iterator - size_type == iterator
iterator - iterator == size type
iterators begin() and/or end() could be useful in computations

All this is subject to boundary rules of course.


-Mike
 
T

Tatu Portin

Mike said:
Tatu Portin said:
I need to implement simple function to get current directory from
program directory.

e.g. /home/tatu/src/main -> /home/tatu/src
where main is the currently running program.
i.e. argv[0] == /home/tatu/src/main.

First, I did it with C-style:

// start-of-file

static const char*
parse_currentdir(const char* const program_location)
{
register int i;
const char* const loc = program_location;

size_t size = strlen(loc);

for (i = (size - 1); (i >= 0) && (loc != DIR_SEPARATOR); --i)
;

if (loc == DIR_SEPARATOR)
{
char* curdir = (char*) malloc((i + 1) * sizeof(char));
if (curdir != NULL)
{
strncpy (curdir, loc, (size_t) i);
curdir = '\0';
return curdir;
}
else
{
// Memory allocation error.
}
}
else
{
// There was no DIR_SEPARATOR in LOC.
}

const char* curdir = ".";
return curdir;
}

int
main (int argc, const char* const argv[])
{
const char* curdir = parse_currentdir (argv[0]);
return 0;
}

// end-of-file

But then I don't learn anything about C++. In particular, I found
out[1] that I could use find_last_of() member routine of C++ string
class. But find_last_of() returns type of size_type, and I cannot
figure out how I should use the size_type to create smaller string
containing only current path.

Can I convert size_type to an iterator?


For sequences which support random-access iterators
(of which string is one):

iterator + size_type == iterator
iterator - size_type == iterator
iterator - iterator == size type
iterators begin() and/or end() could be useful in computations

All this is subject to boundary rules of course.


Thanks. Got it working the C++ way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top