Implementing dirname with C++ strings.



 DEVELOP > c-Plus-Plus > Implementing dirname with C++ strings.

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Tatu Portin"
Date: 24 Nov 2005 01:40:03 PM
Object: Implementing dirname with C++ strings.
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[i] != DIR_SEPARATOR); --i)
;
if (loc[i] == DIR_SEPARATOR)
{
char* curdir = (char*) malloc((i + 1) * sizeof(char));
if (curdir != NULL)
{
strncpy (curdir, loc, (size_t) i);
curdir[i] = '\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.
.

User: "Mike Wahler"

Title: Re: Implementing dirname with C++ strings. 24 Nov 2005 01:46:44 PM
"Tatu Portin" <axel86@mbnet.fi> wrote in message
news:nGohf.338$nf2.102@read3.inet.fi...

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[i] != DIR_SEPARATOR); --i)
;

if (loc[i] == DIR_SEPARATOR)
{
char* curdir = (char*) malloc((i + 1) * sizeof(char));
if (curdir != NULL)
{
strncpy (curdir, loc, (size_t) i);
curdir[i] = '\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
.
User: "Tatu Portin"

Title: Re: Implementing dirname with C++ strings. 24 Nov 2005 02:03:37 PM
Mike Wahler wrote:


"Tatu Portin" <axel86@mbnet.fi> wrote in message
news:nGohf.338$nf2.102@read3.inet.fi...

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[i] != DIR_SEPARATOR); --i)
;

if (loc[i] == DIR_SEPARATOR)
{
char* curdir = (char*) malloc((i + 1) * sizeof(char));
if (curdir != NULL)
{
strncpy (curdir, loc, (size_t) i);
curdir[i] = '\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.
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER