473,472 Members | 2,191 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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.

Nov 24 '05 #1
2 6984

"Tatu Portin" <ax****@mbnet.fi> wrote in message
news:nG***************@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
Nov 24 '05 #2
Mike Wahler wrote:

"Tatu Portin" <ax****@mbnet.fi> wrote in message
news:nG***************@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.

Nov 24 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Joca | last post by:
Hi. I have a problem with a dirname. I can´t read a file if i write this code: <?php $user = $_REQUEST; $filename = dirname(_FILE_).'/../revadv/$user'; $lines = file($filename); ?>
2
by: Dmitri Zhukov | last post by:
Hello everyone, I am implementing software which has a medium sized number of text strings (max 100K) which are represented in a listbox. I want user to be able to search the string by typing...
3
by: Ignacio De Marco | last post by:
I'm not very familiar with C, so I would like to ask you how can use the algorithm Rijndael, suppousing that I want two simple functions (in C ANSI) implementing the CBC or ECB Modes (is the same...
1
by: Steve | last post by:
I have been trying to find documentation on the behavior Can anyone tell me why the first example works and the second doesn't and where I can read about it in the language reference? Steve ...
52
by: Paddy | last post by:
I was browsing the Voidspace blog item on "Flattening Lists", and followed up on the use of sum to do the flattening. A solution was: I would not have thought of using sum in this way. When...
2
by: Ole Nielsby | last post by:
First, bear with my xpost. This goes to comp.lang.c++ comp.lang.functional with follow-up to comp.lang.c++ - I want to discuss an aspect of using C++ to implement a functional language, and...
1
by: Zach | last post by:
Greetings, I am writing a routine that will print out the beginning and end strings for a game: String A1 -"Begin msg1" String B1 -"End msg1" String A2 -"Begin msg2" String B2 -"End msg2"
1
by: laredotornado | last post by:
Hi, On my php 4.4.4 environment, the call $dir = dirname(__FILE__); returns the full path of the directory. However, when I upload the above code to my hosting company's server, the call...
2
by: Bart Kastermans | last post by:
Summary: can't verify big O claim, how to properly time this? On Jun 15, 2:34 pm, "Terry Reedy" <tjre...@udel.eduwrote: Thanks for the idea. I would expect the separation to lead to somewhat...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.