Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pathmix.cpp « mix « src « far2l - github.com/elfmz/far2l.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 16970796827d1aeefd0964615f0b2da198f60b79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
pathmix.cpp

Misc functions for processing of path names
*/
/*
Copyright (c) 1996 Eugene Roshal
Copyright (c) 2000 Far Group
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. The name of the authors may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "headers.hpp"


#include "pathmix.hpp"
#include "strmix.hpp"
#include "panel.hpp"
#include <time.h>
#include <sys/time.h>

NTPath::NTPath(LPCWSTR Src)
{
	if (Src&&*Src)
	{
		Str=Src;
		if (!HasPathPrefix(Src) && *Src!='/')
		{
			ConvertNameToFull(Str,Str);
		}
	}
}

bool IsAbsolutePath(const wchar_t *Path)
{
	return Path && (HasPathPrefix(Path) || IsLocalPath(Path) || IsNetworkPath(Path));
}

bool IsNetworkPath(const wchar_t *Path)
{
	return false;
}

bool IsNetworkServerPath(const wchar_t *Path)
{
	return false;
}

bool IsLocalPath(const wchar_t *Path)
{
	return (Path && Path[0] == L'/'); // && Path[1] != L'/'
}

bool IsLocalRootPath(const wchar_t *Path)
{
	return (Path && Path[0] == L'/' && !Path[1]);
}

bool HasPathPrefix(const wchar_t *Path)
{
	return false;
}

bool IsLocalPrefixPath(const wchar_t *Path)
{
	return HasPathPrefix(Path);
}

bool IsLocalPrefixRootPath(const wchar_t *Path)
{
	return IsLocalPrefixPath(Path) && !Path[7];
}

bool IsLocalVolumePath(const wchar_t *Path)
{
	return HasPathPrefix(Path);//todo  && !_wcsnicmp(&Path[4],L"Volume{",7) && Path[47] == L'}';
}

bool IsLocalVolumeRootPath(const wchar_t *Path)
{
	return IsLocalVolumePath(Path) && (!Path[48] || (IsSlash(Path[48]) && !Path[49]));
}

bool PathCanHoldRegularFile(const wchar_t *Path)
{
	if (!IsNetworkPath(Path))
		return true;

	/* \\ */
	unsigned offset = 0;

	const wchar_t *p = FirstSlash(Path + offset);

	/* server || server\ */
	if (!p || !*(p+1))
		return false;

	return true;
}

bool IsPluginPrefixPath(const wchar_t *Path) //Max:
{
	if (Path[0] == GOOD_SLASH)
		return false;

	const wchar_t* pC = wcschr(Path, L':');

	if (!pC)
		return false;

	const wchar_t* pS = FirstSlash(Path);

	if (pS && pS < pC)
		return false;

	return true;
}

bool TestParentFolderName(const wchar_t *Name)
{
	return Name[0] == L'.' && Name[1] == L'.' && (!Name[2] || (IsSlash(Name[2]) && !Name[3]));
}

bool TestCurrentFolderName(const wchar_t *Name)
{
	return Name[0] == L'.' && (!Name[1] || (IsSlash(Name[1]) && !Name[2]));
}

bool TestCurrentDirectory(const wchar_t *TestDir)
{
	FARString strCurDir;

	if (apiGetCurrentDirectory(strCurDir) && !StrCmp(strCurDir,TestDir))
		return true;

	return false;
}

const wchar_t* WINAPI PointToName(const wchar_t *lpwszPath)
{
	return PointToName(lpwszPath,nullptr);
}

const wchar_t* PointToName(FARString& strPath)
{
	const wchar_t *lpwszPath=strPath.CPtr();
	const wchar_t *lpwszEndPtr=lpwszPath+strPath.GetLength();
	return PointToName(lpwszPath,lpwszEndPtr);
}

const wchar_t* PointToName(const wchar_t *lpwszPath,const wchar_t *lpwszEndPtr)
{
	if (!lpwszPath)
		return nullptr;

	if (lpwszEndPtr)
	{
		for (const wchar_t *lpwszScanPtr = lpwszEndPtr; lpwszScanPtr != lpwszPath;)
		{
			--lpwszScanPtr;
			if (IsSlash(*lpwszScanPtr))
				return lpwszScanPtr + 1;
		}
	}
	else
	{
		const wchar_t *lpwszLastSlashPtr = nullptr;
		for (const wchar_t *lpwszScanPtr = lpwszPath; *lpwszScanPtr; ++lpwszScanPtr)
		{
			if (IsSlash(*lpwszScanPtr))
				lpwszLastSlashPtr = lpwszScanPtr;
		}
		if (lpwszLastSlashPtr)
			return lpwszLastSlashPtr + 1;
	}

	return lpwszPath;
}

//   Аналог PointToName, только для строк типа
//   "name\" (оканчивается на слеш) возвращает указатель на name, а не на пустую
//   строку
const wchar_t* WINAPI PointToFolderNameIfFolder(const wchar_t *Path)
{
	if (!Path)
		return nullptr;

	const wchar_t *NamePtr=Path, *prevNamePtr=Path;

	while (*Path)
	{
		if (IsSlash(*Path))
		{
			prevNamePtr=NamePtr;
			NamePtr=Path+1;
		}

		++Path;
	}

	return ((*NamePtr)?NamePtr:prevNamePtr);
}

const wchar_t* PointToExt(const wchar_t *lpwszPath)
{
	if (!lpwszPath)
		return nullptr;

	const wchar_t *lpwszEndPtr = lpwszPath;

	while (*lpwszEndPtr) lpwszEndPtr++;

	return PointToExt(lpwszPath,lpwszEndPtr);
}

const wchar_t* PointToExt(FARString& strPath)
{
	return PointToExt(strPath.CPtr(), strPath.CEnd());
}

const wchar_t* PointToExt(const wchar_t *lpwszPath,const wchar_t *lpwszEndPtr)
{
	if (!lpwszPath || !lpwszEndPtr)
		return nullptr;

	const wchar_t *lpwszExtPtr = lpwszEndPtr;

	while (lpwszExtPtr != lpwszPath)
	{
		if (*lpwszExtPtr==L'.')
		{
			return lpwszExtPtr;
		}

		if (IsSlash(*lpwszExtPtr))
			return lpwszEndPtr;

		lpwszExtPtr--;
	}

	return lpwszEndPtr;
}

void AddEndSlash(FARString &strPath)
{
	if (strPath.IsEmpty() || strPath[strPath.GetLength() - 1] != GOOD_SLASH)
		strPath+= GOOD_SLASH;
}

void AddEndSlash(std::wstring &strPath)
{
	if (strPath.empty() || strPath.back() != GOOD_SLASH)
		strPath+= GOOD_SLASH;
}

BOOL WINAPI AddEndSlash(wchar_t *Path)
{
	const size_t len = wcslen(Path);
	if (len && Path[len - 1] == GOOD_SLASH)
		return FALSE;

	Path[len] = GOOD_SLASH;
	Path[len + 1] = 0;
	return TRUE;
}

bool DeleteEndSlash(wchar_t *Path, bool AllEndSlash)
{
	bool Ret = false;
	size_t len = StrLength(Path);

	while (len && IsSlash(Path[--len]))
	{
		Ret = true;
		Path[len] = L'\0';

		if (!AllEndSlash)
			break;
	}

	return Ret;
}

BOOL DeleteEndSlash(std::wstring &strPath, bool AllEndSlash)
{
	BOOL out = FALSE;
	while (!strPath.empty() && IsSlash(strPath.back())) {
		out = TRUE;
		strPath.pop_back();
		if (!AllEndSlash)
			break;
	}
	return out;
}

BOOL DeleteEndSlash(FARString &strPath, bool AllEndSlash)
{
	size_t LenToSlash = strPath.GetLength();

	while (LenToSlash != 0 && IsSlash(strPath.At(LenToSlash - 1)))
	{
		--LenToSlash;
		if (!AllEndSlash) break;
	}

	if (LenToSlash == strPath.GetLength())
		return FALSE;

	strPath.Truncate(LenToSlash);
	return TRUE;
}

bool CutToSlash(FARString &strStr, bool bInclude)
{
	size_t pos;

	if (FindLastSlash(pos,strStr))
	{
		if (bInclude)
			strStr.Truncate(pos);
		else
			strStr.Truncate(pos+1);

		return true;
	}

	return false;
}

bool CutToSlash(std::wstring &strStr, bool bInclude)
{
	size_t pos = strStr.rfind(GOOD_SLASH);
	if (pos == std::string::npos)
		return false;

	strStr.resize(bInclude ? pos + 1 : pos);
	return true;
}

FARString &CutToFolderNameIfFolder(FARString &strPath)
{
	wchar_t *lpwszPath = strPath.GetBuffer();
	wchar_t *lpwszNamePtr=lpwszPath, *lpwszprevNamePtr=lpwszPath;

	while (*lpwszPath)
	{
		if (IsSlash(*lpwszPath))
		{
			lpwszprevNamePtr=lpwszNamePtr;
			lpwszNamePtr=lpwszPath+1;
		}

		++lpwszPath;
	}

	if (*lpwszNamePtr)
		*lpwszNamePtr=0;
	else
		*lpwszprevNamePtr=0;

	strPath.ReleaseBuffer();
	return strPath;
}

const wchar_t *FirstSlash(const wchar_t *String)
{
	do
	{
		if (IsSlash(*String))
			return String;
	}
	while (*String++);

	return nullptr;
}

const wchar_t *LastSlash(const wchar_t *String)
{
	const wchar_t *Start = String;

	while (*String++)
		;

	while (--String!=Start && !IsSlash(*String))
		;

	return IsSlash(*String)?String:nullptr;
}

bool FindSlash(size_t &Pos, const FARString &Str, size_t StartPos)
{
	for (size_t p = StartPos; p < Str.GetLength(); p++)
	{
		if (IsSlash(Str[p]))
		{
			Pos = p;
			return true;
		}
	}

	return false;
}

bool FindLastSlash(size_t &Pos, const FARString &Str)
{
	for (size_t p = Str.GetLength(); p > 0; p--)
	{
		if (IsSlash(Str[p - 1]))
		{
			Pos = p - 1;
			return true;
		}
	}

	return false;
}

FARString ExtractFileName(const FARString &Path)
{
	size_t p;

	if (FindLastSlash(p, Path))
		p++;
	else
		p = 0;

	return FARString(Path.CPtr() + p, Path.GetLength() - p);
}

FARString ExtractFilePath(const FARString &Path)
{
	size_t p;

	if (!FindLastSlash(p, Path))
		p = 0;

	return FARString(Path.CPtr(), p);
}

bool IsRootPath(const FARString &Path)
{
	return Path == L"/";
}

static std::string LookupExecutableInEnvPath(const char *file)
{
	std::string out;

	FARString str_path;
	apiGetEnvironmentVariable("PATH", str_path);
	const std::string &mb_path = str_path.GetMB();

	for (const char *s = mb_path.c_str(); *s; ){
		const char *p = strchr(s, ':');

		if (p != NULL) {
			out.assign(s, p - s);
		} else {
			out.assign(s);
		}

		if (out.empty() || out[out.size() - 1] != '/') {
			out+= '/';
		}
		out+= file;
		struct stat st{};
		if (stat(out.c_str(), &st) == 0) {
			break;
		}
		out.clear();
		if (p == NULL) break;
		s = p + 1;
	}

	return out;
}

FARString LookupExecutable(const char *file)
{
	FARString out;
	if (file[0] == GOOD_SLASH) {
		out = file;

	} else if (file[0] == '.' && file[1] == GOOD_SLASH) {
		apiGetCurrentDirectory(out);
		out+= file + 1;

	} else {
		out = LookupExecutableInEnvPath(file);
		if (out.IsEmpty()) {
			apiGetCurrentDirectory(out);
			out+= GOOD_SLASH;
			out+= file;
		}
	}
	return out;
}

bool PathHasParentPrefix(const FARString &Path)
{
	return (Path.Begins(GOOD_SLASH) || Path.Begins(L"./") || Path == L".");
}

void EnsurePathHasParentPrefix(FARString &Path)
{
	if (!PathHasParentPrefix(Path)) {
		Path.Insert(0, L"./");
	}
}