Home » Extra libraries, Code snippets, applications etc. » U++ users applications in progress and useful code snippets, including reference examples! » DirectoryUp doesn't seem to work.
|
Re: DirectoryUp doesn't seem to work. [message #18561 is a reply to message #18554] |
Wed, 08 October 2008 12:13 |
mr_ped
Messages: 825 Registered: November 2005 Location: Czech Republic - Praha
|
Experienced Contributor |
|
|
I did take a short look at that function, and I find it quite a mess... erm.
So far I think the PLATFORM_X11 vs PLATFORM_POSIX is little bug, but the FileSel is never(?) included without flagGUI, so the PLATFORM_X11 is defined anyway.
And I guess the function should move the input variable "dir" up, and return the name of the directory it was in before call.
So in your example the result should be:
file = "Isaac/" or "Isaac"
currentDir = "/home" or "/home/"
The problem is maybe with the last "/" at end of "/home/Isaac/" which will fool a bit the "if (basedir)" code path?
IMHO I would rewrite this function from scratch, would somebody tell me how exactly it should behave.
I think I will try anyway, I'm still not sure what "basedir" is supposed to do, but I will try to figure it out.
[Updated on: Wed, 08 October 2008 12:14] Report message to a moderator
|
|
|
|
|
Re: DirectoryUp doesn't seem to work. [message #18571 is a reply to message #18568] |
Thu, 09 October 2008 14:07 |
mrjt
Messages: 705 Registered: March 2007 Location: London
|
Contributor |
|
|
The function actually seems to work quite well once you understand what the goal is:
1- Ignore non-valid paths
2- Enforce POSIX minimum path of '/'
2- On Win32 jump from 'C:\' & '\\Server' style paths straight to '' path
Without these conditions the function would be:
name = GetFileTitle(dir);
dir = GetFileFolder(dir);
The only change I would make would be to remove trailing DIR_SEPs, but you can always add something like this:
if (dir.GetLength() > 1) {
const char *eos = dir.End()-1;
int cnt = 0;
while (*eos == DIR_SEP && eos > dir.Begin()) {
eos--;
cnt++;
}
if (cnt) {
#ifdef PLATFORM_WIN32
if (*eos != ':')
#endif
{
dir.Remove(dir.GetLength()-cnt, cnt);
}
}
}
[Updated on: Thu, 09 October 2008 14:10] Report message to a moderator
|
|
|
Re: DirectoryUp doesn't seem to work. [message #18573 is a reply to message #18554] |
Thu, 09 October 2008 16:11 |
mr_ped
Messages: 825 Registered: November 2005 Location: Czech Republic - Praha
|
Experienced Contributor |
|
|
I'm sorry, but I'm unable to work on this right now, but I want to get back to it next week, and rewrite it from scratch.
But Mjrt has valid point about how it works right now, but I think the trailing slash should be handled as well.
Quote: |
ASSERT(DirectoryUp("/home/Isaac/") == "/home");
ASSERT(DirectoryUp("/home/Isaac") == "/home");
|
No, the directory is changed as reference variable.
The return value of function is the name of directory which was left, i.e. in both cases the return value should be "Isaac", the only question is whether it should return Isaac/ and Isaac, or just Isaac.
Whichever you prefer, write here, I can't decide myself, but I think erasing the trailing slash always will in the end make it easier to use, even if you would have to add slash back in special cases.
Mjrt: I mostly don't like the code itself, there's way too much #ifdef and code duplicity for my taste. I have already some idea how to write it much more cleanly, but I don't have time right now. :/
|
|
|
|
Goto Forum:
Current Time: Fri Apr 26 16:14:43 CEST 2024
Total time taken to generate the page: 0.03159 seconds
|