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.
|
|
0
|
|
|
|
Reply
|
axel86 (74)
|
11/24/2005 7:40:03 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
|
|
0
|
|
|
|
Reply
|
mkwahler (3821)
|
11/24/2005 7:46:44 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.
|
|
0
|
|
|
|
Reply
|
axel86 (74)
|
11/24/2005 8:03:37 PM
|
|
|
2 Replies
21 Views
(page loaded in 0.13 seconds)
Similiar Articles: array of strings - comp.soft-sys.sasYou want to pass C style strings, that is nul terminated arrays of char. ... 9.3 Arrays of Strings - University of Hawaii System Previous: 9.2 Implementing Multi ... Iterating over a String - comp.lang.java.helpIt seems so obvious to have String (more generally, CharSequence) implement Iterable, but ... -- Eric Sosman esosman@ieee-dot-org.invalid Compare strings as integers - comp.unix.solarisinteger overflow in atoi - comp.lang.c I want to implement atoi function which converts string to an integer. So I did this ... So, yes, a correct version will need to ... Converting number to std::string ("itoa()" ) - comp.lang.c++ ...One implement such a conversion function eg. by looping (eg via %) through the digits ... insisted on doing it the C way, I suppose you could use sprintf and std::string::c ... (sh/bash) How to check for a string matching -*? - comp.unix.shell ...Currently, I use case/esac to test to see if a string starts with a dash: case $ ... No, not all modern shells implement [[ ... ]], and POSIX doesn't implement it. Help with JList and JComboBox - comp.lang.java.guiCalling swing gurus, I have a requirement to implement a sublist to a JList. ... getContentPane().add( topPanel ); // Create some items to add to the list String ... Enform query - Count, sort with partial string - comp.sys.tandem ...But I'm not able >to do that with partial string. Following query doesn't work. ... Write the report program in some other language: Cobol, Tal, C, maybe even TACL. How to implement batch print pdf file programmatically - comp.text ...Now I want to implement a function : given a batch of pdf files which may be located ... ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As ... implementation of atoi - comp.lang.cParses the C string str interpreting its content as an integral number ... implementation | PALLE Technologies blog It will show how to implement atoi function using c lang. ComboBox loaded with array list - comp.lang.java.guiI know I can load them using > defaultComboBoxModel but how do I get the combobox to see the strings? > implement custom ListCellRenderer -- Andrei Kouznetsov http ... Implementing dirname with C++ strings. - C / C++Implementing dirname with C++ strings.. C / C++ Forums on Bytes. PHP: dirname - ManualNotes. Note: dirname() operates naively on the input string, and is not aware of the actual filesystem, or path components such as "..". Note: 7/29/2012 8:58:06 AM
|