Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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.
DirectoryUp doesn't seem to work. [message #18554] Wed, 08 October 2008 03:15 Go to next message
blueapples is currently offline  blueapples
Messages: 10
Registered: October 2008
Promising Member
I have a currentDir String that contains the directory last loaded into a FileList. I am adding a ".." item to the top of the FileList manually so that the user can navigate upwards by double clicking this, therefore I looked for a function to get the directory of the current directory and found DirectoryUp. Problem is, on Linux anyway, it doesn't seem to work.

String currentDir, file;
currentDir = "/home/Isaac/";
file = DirectoryUp(currentDir, true);
// At this point file seems to be a long string full of blanks.


I traced into this function and found it a little confounding, lots of intertwined execution paths for different platforms. If I do not specify the "basedir" value as true, the code actually gets into PLATFORM_X11 code near the bottom of the function - which I do not believe is correct. Linux should be a POSIX platform...

Anyway.. any help? Smile
Re: DirectoryUp doesn't seem to work. [message #18561 is a reply to message #18554] Wed, 08 October 2008 12:13 Go to previous messageGo to next message
mr_ped is currently offline  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 #18562 is a reply to message #18554] Wed, 08 October 2008 12:20 Go to previous messageGo to next message
mr_ped is currently offline  mr_ped
Messages: 825
Registered: November 2005
Location: Czech Republic - Praha
Experienced Contributor
I think I figured it out, the "basedir" set to true means you already add some directory ahead of "dir" variable anyway, so it can be safely assumed the DirectoryUp function will never have to solve the "C:" or "\\SERVER" or "/" cases and returning empty "dir" is ok in such case.

Let's see what more I can do with this stuff. Smile
Re: DirectoryUp doesn't seem to work. [message #18568 is a reply to message #18554] Thu, 09 October 2008 02:09 Go to previous messageGo to next message
blueapples is currently offline  blueapples
Messages: 10
Registered: October 2008
Promising Member
Any luck?

Personally I would expect semantics like this:

1. Remove any trailing /.
2. Extract the part of the path up to but not including the last /.

So, from my example, both these would be the same:

ASSERT(DirectoryUp("/home/Isaac/") == "/home");
ASSERT(DirectoryUp("/home/Isaac") == "/home");
Re: DirectoryUp doesn't seem to work. [message #18571 is a reply to message #18568] Thu, 09 October 2008 14:07 Go to previous messageGo to next message
mrjt is currently offline  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 Go to previous messageGo to next message
mr_ped is currently offline  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. :/
Re: DirectoryUp doesn't seem to work. [message #18723 is a reply to message #18573] Sat, 18 October 2008 19:34 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
mr_ped wrote on Thu, 09 October 2008 10:11


But Mjrt has valid point about how it works right now, but I think the trailing slash should be handled as well.



String DirectoryUp(String& dir, bool basedir)
{
	while(*dir.Last() == '\\' || *dir.Last() == '/')
		dir.Trim(dir.GetCount() - 1);
	String s = dir;
	String name;


Like this?

Mirek
Previous Topic: Drawing raw data to an Image / Draw object?
Next Topic: More small gems
Goto Forum:
  


Current Time: Thu Mar 28 13:28:28 CET 2024

Total time taken to generate the page: 0.00974 seconds