Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
PathName.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
9
10#include "PathName.h"
11
12using namespace Stroika::Foundation;
14using namespace Stroika::Foundation::IO;
16
18
19/*
20 ********************************************************************************
21 ************** FileSystem::AssureDirectoryPathSlashTerminated ******************
22 ********************************************************************************
23 */
25{
26 if (dirPath.empty ()) {
27 AssertNotReached (); // not sure if this is an error or not. Not sure how code used.
28 // put assert in there to find out... Probably should THROW!
29 // -- LGP 2009-05-12
30 return String{filesystem::path::preferred_separator};
31 }
32 else {
33 Character lastChar = dirPath[dirPath.size () - 1];
34 if (lastChar == filesystem::path::preferred_separator) {
35 return dirPath;
36 }
37 StringBuilder result = dirPath;
38 result += filesystem::path::preferred_separator;
39 return result;
40 }
41}
42
43/*
44 ********************************************************************************
45 *********************** FileSystem::SafeFilenameChars **************************
46 ********************************************************************************
47 */
49{
50 // analyze as wide-char string so we don't mis-identify characters (by looking at lead bytes etc)
51 wstring tmp = s.As<wstring> ();
52Again:
53 for (auto i = tmp.begin (); i != tmp.end (); ++i) {
54 switch (*i) {
55 case '\\':
56 *i = '_';
57 break;
58 case '/':
59 *i = '_';
60 break;
61 case ':':
62 *i = ' ';
63 break;
64 case '.':
65 tmp.erase (i);
66 goto Again;
67 }
68 }
69 return tmp;
70}
71
72/*
73 ********************************************************************************
74 ********************* FileSystem::AssureLongFileName ***************************
75 ********************************************************************************
76 */
78{
79#if qStroika_Foundation_Common_Platform_Windows
80 DWORD r = ::GetLongPathNameW (fileName.As<wstring> ().c_str (), nullptr, 0);
81 if (r != 0) {
82 StackBuffer<wchar_t> buf{Memory::eUninitialized, r};
83 r = ::GetLongPathNameW (fileName.As<wstring> ().c_str (), buf.data (), r);
84 if (r != 0) {
85 return static_cast<const wchar_t*> (buf);
86 }
87 }
88#endif
89 return fileName;
90}
#define AssertNotReached()
Definition Assertions.h:355
Similar to String, but intended to more efficiently construct a String. Mutable type (String is large...
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
nonvirtual size_t size() const noexcept
Definition String.inl:534
Logically halfway between std::array and std::vector; Smart 'direct memory array' - which when needed...
String SafeFilenameChars(const String &s)
Definition PathName.cpp:48
String AssureLongFileName(const String &fileName)
Definition PathName.cpp:77
String AssureDirectoryPathSlashTerminated(const String &dirPath)
Definition PathName.cpp:24