473,569 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ 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_currentdi r(const char* const program_locatio n)
{
register int i;
const char* const loc = program_locatio n;

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_currentdi r (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 6991

"Tatu Portin" <ax****@mbnet.f i> wrote in message
news:nG******** *******@read3.i net.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_currentdi r(const char* const program_locatio n)
{
register int i;
const char* const loc = program_locatio n;

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_currentdi r (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.f i> wrote in message
news:nG******** *******@read3.i net.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_currentdi r(const char* const program_locatio n)
{
register int i;
const char* const loc = program_locatio n;

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_currentdi r (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
5621
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
2695
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 the first letters of the record and showing coinciding record if any (similar to quickserach functionality of Norton/Total commander). So i guess I...
3
2401
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 for me, because I have both implementations in Delphi, but i need the same implementations in C for an AS400) with an interface similiar to this (in...
1
3585
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 print os.path.join(os.path.dirname(os.tmpnam()),*("a","b","c")) #works OUTPUT:/var/tmp/a/b/c and
52
3920
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 I did help(sum) the docstring was very number-centric: It went further, and precluded its use on strings:
2
3385
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 I'd like the attention of fp as well as C++ gurus if available. The language I'm implementing - PILS - is dynamically
1
363
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
2377
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 returns ".". What is the setting to force dirname(__FILE__) to alwasy return a full path instead of a "."?
2
2239
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 more code, but all the "checking the root" code would be separated out in the tree class. The node class would be very smooth. I'll try this when...
0
7697
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5219
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.