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 » U++ Library support » U++ Core » Dehaviour of DirectoryExists
Dehaviour of DirectoryExists [message #44982] Thu, 06 August 2015 13:17 Go to next message
kov_serg is currently offline  kov_serg
Messages: 37
Registered: August 2008
Location: Russia
Member
#include <Core/Core.h>

using namespace Upp;

CONSOLE_APP_MAIN
{
	Cout()<<(DirectoryExists("C:\\WINDOWS")?"yes":"no")<<"\n";
	Cout()<<(DirectoryExists("C:\\WINDOWS\\")?"yes":"no")<<"\n";
}

What result should I expect if directory C:\WINDOWS exists?

output Win32 U++8760
yes
no

Confused
Re: Dehaviour of DirectoryExists [message #44995 is a reply to message #44982] Tue, 11 August 2015 15:31 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
This is consistent with both Posix and Win32 API... (after all, DirectoryExists does nothing else than to call API).

Mirek
Re: Dehaviour of DirectoryExists [message #44997 is a reply to message #44995] Wed, 12 August 2015 20:12 Go to previous messageGo to next message
kov_serg is currently offline  kov_serg
Messages: 37
Registered: August 2008
Location: Russia
Member
I expected something like this

Windows:
#include <windows.h>
#include <stdio.h>

int is_dir(const char *szPath) {
  DWORD dwAttrib = GetFileAttributesA(szPath);
  return (dwAttrib != INVALID_FILE_ATTRIBUTES && 
         (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
void test(const char* name) {
    printf("is_dir=%d : %s\n",is_dir(name),name);
}
int main(int argc,char** argv) {
	test("C:\\Windows");
	test("C:\\Windows\\");
	test("C:\\Windows\\.");
	test("C:\\Windows\\.\\");
	test("C:\\Windows\\none");
	test("\\\\?\\C:\\Windows");
	test("\\\\?\\C:\\Windows\\none");
	return 0;
}

Output:
is_dir=1 : C:\Windows
is_dir=1 : C:\Windows\
is_dir=1 : C:\Windows\.
is_dir=1 : C:\Windows\.\
is_dir=0 : C:\Windows\none
is_dir=1 : \\?\C:\Windows
is_dir=0 : \\?\C:\Windows\none


Linux:
include <sys/stat.h>
#include <stdio.h>

int is_dir(const char* name) {
    struct stat s;
    int err = stat(name, &s);
    if(-1 == err) return 0;
    return S_ISDIR(s.st_mode) ? 1:0;
}
void test(const char* name) {
    printf("is_dir=%d : %s\n",is_dir(name),name);
}
int main(int argc,char** argv) {
    test("/home");
    test("/home/");
    test("/home/.");
    test("/home/./");
    test("/home/none");
    return 0;
}

Output:
is_dir=1 : /home
is_dir=1 : /home/
is_dir=1 : /home/.
is_dir=1 : /home/./
is_dir=0 : /home/none


https://en.wikipedia.org/wiki/Principle_of_least_astonishmen t

[Updated on: Wed, 12 August 2015 20:14]

Report message to a moderator

Re: Dehaviour of DirectoryExists [message #45025 is a reply to message #44997] Thu, 20 August 2015 08:43 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
OK. We are using different API for *Exists functions, but I think I can change all to GetFileAttributes / stat.

Mirek
Previous Topic: Why Cout() and Cerr() work so different?
Next Topic: Suggestion for StringBuffer
Goto Forum:
  


Current Time: Thu Apr 18 02:56:02 CEST 2024

Total time taken to generate the page: 0.01542 seconds