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 » Developing U++ » UppHub » DirectoryCreateMulti as an alternative for RealizeDirectory
DirectoryCreateMulti as an alternative for RealizeDirectory [message #19484] Sun, 07 December 2008 20:15 Go to next message
Sender Ghost is currently offline  Sender Ghost
Messages: 301
Registered: November 2008
Senior Member
In the "Core\Path.h" file we have useful function RealizeDirectory that creates not existent directories from the given path. I create alternative function with name DirectoryCreateMulti. Complexity: recursion versus memory management with Vector.

#ifdef PLATFORM_POSIX
bool DirectoryCreateMulti(const String& path, int mode = 0755)
#else
bool DirectoryCreateMulti(const String& path)
#endif
{
	Vector<String> dirs;
	
	for (int i=path.GetCount() - 1; ; --i)
	{
		i = path.ReverseFind(DIR_SEP, i);
#ifdef PLATFORM_POSIX
		if (i>0)
#else
		if (i>0 && path[i - 1] != ':')
#endif
		{
			const String tmp(path.Left(i));
			if (!DirectoryExists(tmp))
			{
				dirs.Add(tmp);
			}
			else break;
		}
		else break;
	}
	
	for (int i=dirs.GetCount() - 1; i>=0; --i)
	{
#ifdef PLATFORM_POSIX
		if (!DirectoryCreate(dirs[i], mode))
#else
		if (!DirectoryCreate(dirs[i]))
#endif
		{
			return false;
		}
	}
#ifdef PLATFORM_POSIX
	return DirectoryCreate(path, mode);
#else
	return DirectoryCreate(path);
#endif
}

[Updated on: Sun, 07 December 2008 21:34]

Report message to a moderator

Re: DirectoryCreateMulti as an alternative for RealizeDirectory [message #19552 is a reply to message #19484] Mon, 15 December 2008 09:32 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Thanks, I am afraid you are right that linear recursion is usually not a best thing to do....

Mirek
Re: DirectoryCreateMulti as an alternative for RealizeDirectory [message #19553 is a reply to message #19484] Mon, 15 December 2008 09:42 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
OK, I have ended with this code

#ifdef POSIX
void RealizeDirectory(String dir, int mode)
#else
void RealizeDirectory(String dir)
#endif
{
	Vector<String> p;
	while(dir.GetLength() > DIR_MIN) {
		p.Add(dir);
		dir = GetFileFolder(dir);
	}
	for(int i = p.GetCount() - 1; i >= 0; i--)
		if(!DirectoryExists(p[i]))
#ifdef POSIX
			DirectoryCreate(p[i], mode);
#else
			DirectoryCreate(p[i]);
#endif
}


This is definitely a step in the right direction Smile

Mirek
Re: DirectoryCreateMulti as an alternative for RealizeDirectory [message #19557 is a reply to message #19553] Mon, 15 December 2008 15:59 Go to previous messageGo to next message
Sender Ghost is currently offline  Sender Ghost
Messages: 301
Registered: November 2008
Senior Member
luzr wrote on Mon, 15 December 2008 13:42

OK, I have ended with this code

#ifdef POSIX
void RealizeDirectory(String dir, int mode)
#else
void RealizeDirectory(String dir)
#endif
{
	Vector<String> p;
	while(dir.GetLength() > DIR_MIN) {
		p.Add(dir);
		dir = GetFileFolder(dir);
	}
	for(int i = p.GetCount() - 1; i >= 0; i--)
		if(!DirectoryExists(p[i]))
#ifdef POSIX
			DirectoryCreate(p[i], mode);
#else
			DirectoryCreate(p[i]);
#endif
}


This is definitely a step in the right direction Smile

Mirek

Without NormalizePath function your code still not working with "a\b\c" directory. As stated in the comments:
#ifdef PLATFORM_WIN32
#define DIR_MIN 3 //!! wrong! what about \a\b\c ?
#endif

This is my intention.
It's not wrong with recursion, if they not too long for the stack overflow Wink. I think it's not a problem (MAX_PATH == 260 in the stdlib.h).
So, you prefer:
dir.GetLength() > DIR_MIN

I use another more adequate method (in my opinion of course):
i>0 && path[i - 1] != ':'

To note:
- DirectoryCreate returns variable about success of directory creation. DirectoryCreateMulti do this also.
- DirectoryCreateMulti doesn't keep all directories for verification of existent directory. They use backward search with ReverseFind function.

[Updated on: Mon, 15 December 2008 16:53]

Report message to a moderator

Re: DirectoryCreateMulti as an alternative for RealizeDirectory [message #19558 is a reply to message #19557] Mon, 15 December 2008 17:08 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Sender Ghost wrote on Mon, 15 December 2008 09:59

luzr wrote on Mon, 15 December 2008 13:42

OK, I have ended with this code

#ifdef POSIX
void RealizeDirectory(String dir, int mode)
#else
void RealizeDirectory(String dir)
#endif
{
	Vector<String> p;
	while(dir.GetLength() > DIR_MIN) {
		p.Add(dir);
		dir = GetFileFolder(dir);
	}
	for(int i = p.GetCount() - 1; i >= 0; i--)
		if(!DirectoryExists(p[i]))
#ifdef POSIX
			DirectoryCreate(p[i], mode);
#else
			DirectoryCreate(p[i]);
#endif
}


This is definitely a step in the right direction Smile

Mirek

Without NormalizePath function your code still not working with "a\b\c" directory. As stated in the comments:
#ifdef PLATFORM_WIN32
#define DIR_MIN 3 //!! wrong! what about \a\b\c ?
#endif

This is my intention.
It's not wrong with recursion, if they not too long for the stack overflow Wink. I think it's not a problem (MAX_PATH == 260 in the stdlib.h).
So, you prefer:
dir.GetLength() > DIR_MIN

I use another more adequate method (in my opinion of course):
i>0 && path[i - 1] != ':'

To note:
- DirectoryCreate returns variable about success of directory creation. DirectoryCreateMulti do this also.
- DirectoryCreateMulti doesn't keep all directories for verification of existent directory. They use backward search with ReverseFind function.


Good points, thanks.

#ifdef POSIX
bool RealizeDirectory(const String& d, int mode)
#else
bool RealizeDirectory(const String& d)
#endif
{
	String dir = NormalizePath(d);
	Vector<String> p;
	while(dir.GetLength() > DIR_MIN) {
		p.Add(dir);
		dir = GetFileFolder(dir);
	}
	for(int i = p.GetCount() - 1; i >= 0; i--)
		if(!DirectoryExists(p[i]))
#ifdef POSIX
			if(!DirectoryCreate(p[i], mode))
#else
			if(!DirectoryCreate(p[i]))
#endif
				return false;
	return true;
}


Mirek

Re: DirectoryCreateMulti as an alternative for RealizeDirectory [message #19559 is a reply to message #19558] Mon, 15 December 2008 17:47 Go to previous messageGo to next message
Sender Ghost is currently offline  Sender Ghost
Messages: 301
Registered: November 2008
Senior Member
luzr wrote on Mon, 15 December 2008 21:08


Good points, thanks.

#ifdef POSIX
bool RealizeDirectory(const String& d, int mode)
#else
bool RealizeDirectory(const String& d)
#endif
{
	String dir = NormalizePath(d);
	Vector<String> p;
	while(dir.GetLength() > DIR_MIN) {
		p.Add(dir);
		dir = GetFileFolder(dir);
	}
	for(int i = p.GetCount() - 1; i >= 0; i--)
		if(!DirectoryExists(p[i]))
#ifdef POSIX
			if(!DirectoryCreate(p[i], mode))
#else
			if(!DirectoryCreate(p[i]))
#endif
				return false;
	return true;
}


Mirek




Thank you, Mirek.

[Updated on: Mon, 15 December 2008 21:58]

Report message to a moderator

Re: DirectoryCreateMulti as an alternative for RealizeDirectory [message #19576 is a reply to message #19559] Thu, 18 December 2008 11:19 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Now even fixed to work in POSIX Smile (Just a typo, must be "PLATFORM_POSIX", not "POSIX").

Mirek
Previous Topic: Added Signals package
Next Topic: Cairo
Goto Forum:
  


Current Time: Thu Mar 28 23:15:08 CET 2024

Total time taken to generate the page: 0.03095 seconds