-
Notifications
You must be signed in to change notification settings - Fork 1
/
cute_path.h
562 lines (458 loc) · 16 KB
/
cute_path.h
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
------------------------------------------------------------------------------
Licensing information can be found at the end of the file.
------------------------------------------------------------------------------
cute_path.h - v1.01
To create implementation (the function definitions)
#define CUTE_PATH_IMPLEMENTATION
in *one* C/CPP file (translation unit) that includes this file
SUMMARY:
Collection of c-string manipulation functions for dealing with common file-path
operations. More or less a less-fully-featured replacement for Shlwapi.h path
functions on Windows.
Performs no dynamic memory management and has no external dependencies (other than
some crt funcs).
Revision history:
1.0 (11/01/2017) initial release
1.01 (11/10/2017) path_compact, path_pop bugfixes
*/
/*
Contributors:
sro5h 1.01 - path_compact, path_pop bugfixes
*/
#if !defined(CUTE_PATH_H)
#define CUTE_PATH_MAX_PATH 1024
#define CUTE_PATH_MAX_EXT 32
// Copies path to out, but not the extension. Places a nul terminator in out.
// Returns the length of the string in out, excluding the nul byte.
// Length of copied output can be up to CUTE_PATH_MAX_PATH. Can also copy the file
// extension into ext, up to CUTE_PATH_MAX_EXT.
#if !defined(__cplusplus)
int path_pop_ext(const char* path, char* out, char* ext);
#else
int path_pop_ext(const char* path, char* out = 0, char* ext = 0);
#endif
// Copies path to out, but excludes the final file or folder from the output.
// If the final file or folder contains a period, the file or folder will
// still be appropriately popped. If the path contains only one file or folder,
// the output will contain a period representing the current directory. All
// outputs are nul terminated.
// Returns the length of the string in out, excluding the nul byte.
// Length of copied output can be up to CUTE_PATH_MAX_PATH.
// Optionally stores the popped filename in pop. pop can be NULL.
// out can also be NULL.
#if !defined(__cplusplus)
int path_pop(const char* path, char* out, char* pop);
#else
int path_pop(const char* path, char* out = 0, char* pop = 0);
#endif
// Concatenates path_b onto the end of path_a. Will not write beyond max_buffer_length.
// Places a single '/' character between path_a and path_b. Does no other "intelligent"
// manipulation of path_a and path_b; it's a basic strcat kind of function.
void path_concat(const char* path_a, const char* path_b, char* out, int max_buffer_length);
// Copies the name of the folder the file sits in (but not the entire path) to out. Will
// not write beyond max_buffer_length. Length of copied output can be up to CUTE_PATH_MAX_PATH.
// path contains the full path to the file in question.
// Returns 0 for inputs of "", "." or ".." as the path, 1 otherwise (success).
int path_name_of_folder_im_in(const char* path, char* out);
// Shrinks the path to the desired length n, the out buffer will never be bigger than
// n + 1. Places three '.' between the last part of the path and the first part that
// will be shortened to fit. If the last part is too long to fit in a string of length n,
// the last part will be shortened to fit and three '.' will be added in front & back.
int path_compact(const char* path, char* out, int n);
// Some useful (but not yet implemented) functions
/*
int path_root(const char* path, char* out);
*/
#define CUTE_PATH_UNIT_TESTS 1
void path_do_unit_tests();
#define CUTE_PATH_H
#endif
#ifdef CUTE_PATH_IMPLEMENTATION
#ifndef CUTE_PATH_IMPLEMENTATION_ONCE
#define CUTE_PATH_IMPLEMENTATION_ONCE
#ifdef _WIN32
#if !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS
#endif
#endif
#include <string.h> // strncpy, strncat, strlen
#define CUTE_PATH_STRNCPY strncpy
#define CUTE_PATH_STRNCAT strncat
#define CUTE_PATH_STRLEN strlen
int path_is_slash(char c)
{
return (c == '/') | (c == '\\');
}
int path_pop_ext(const char* path, char* out, char* ext)
{
if (out != NULL)
*out = '\0';
if (ext != NULL)
*ext = '\0';
// skip leading dots
const char *p = path;
while (*p == '.')
++p;
const char *last_slash = path;
const char *last_period = NULL;
while (*p != '\0')
{
if (path_is_slash(*p))
last_slash = p;
else if (*p == '.')
last_period = p;
++p;
}
if (last_period != NULL && last_period > last_slash)
{
if (ext != NULL)
CUTE_PATH_STRNCPY(ext, last_period + 1, CUTE_PATH_MAX_EXT);
}
else
{
last_period = p;
}
int len = (int)(last_period - path);
if (len > CUTE_PATH_MAX_PATH - 1) len = CUTE_PATH_MAX_PATH - 1;
if (out != NULL)
{
CUTE_PATH_STRNCPY(out, path, len);
out[len] = '\0';
}
return len;
}
int path_pop(const char* path, char* out, char* pop)
{
const char* original = path;
int total_len = 0;
while (*path)
{
++total_len;
++path;
}
// ignore trailing slash from input path
if (path_is_slash(*(path - 1)))
{
--path;
total_len -= 1;
}
int pop_len = 0; // length of substring to be popped
while (!path_is_slash(*--path) && pop_len != total_len)
++pop_len;
int len = total_len - pop_len; // length to copy
// don't ignore trailing slash if it is the first character
if (len > 1)
{
len -= 1;
}
if (len > 0)
{
if (out)
{
CUTE_PATH_STRNCPY(out, original, len);
out[len] = 0;
}
if (pop)
{
CUTE_PATH_STRNCPY(pop, path + 1, pop_len);
pop[pop_len] = 0;
}
return len;
}
else
{
if (out)
{
out[0] = '.';
out[1] = 0;
}
if (pop) *pop = 0;
return 1;
}
}
static int path_strncpy(char* dst, const char* src, int n, int max)
{
int c;
do
{
if (n >= max - 1)
{
dst[max - 1] = 0;
break;
}
c = *src++;
dst[n] = c;
++n;
} while (c);
return n;
}
void path_concat(const char* path_a, const char* path_b, char* out, int max_buffer_length)
{
int n = path_strncpy(out, path_a, 0, max_buffer_length);
n = path_strncpy(out, "/", n - 1, max_buffer_length);
path_strncpy(out, path_b, n - 1, max_buffer_length);
}
int path_name_of_folder_im_in(const char* path, char* out)
{
// return failure for empty strings and "." or ".."
if (!*path || (*path == '.' && CUTE_PATH_STRLEN(path) < 3)) return 0;
int len = path_pop(path, out, NULL);
int has_slash = 0;
for (int i = 0; out[i]; ++i)
{
if (path_is_slash(out[i]))
{
has_slash = 1;
break;
}
}
if (has_slash)
{
int n = path_pop(out, NULL, NULL) + 1;
len -= n;
CUTE_PATH_STRNCPY(out, path + n, len);
}
else CUTE_PATH_STRNCPY(out, path, len);
out[len] = 0;
return 1;
}
int path_compact(const char* path, char* out, int n)
{
if (n <= 6) return 0;
const char* sep = "...";
const int seplen = (int)strlen(sep);
int pathlen = (int)strlen(path);
out[0] = 0;
if (pathlen <= n)
{
CUTE_PATH_STRNCPY(out, path, pathlen);
out[pathlen] = 0;
return pathlen;
}
// Find last path separator
// Ignores the last character as it could be a path separator
int i = pathlen - 1;
do
{
--i;
} while (!path_is_slash(path[i]) && i > 0);
const char* back = path + i;
int backlen = (int)strlen(back);
// No path separator was found or the first character was one
if (pathlen == backlen)
{
CUTE_PATH_STRNCPY(out, path, n - seplen);
out[n - seplen] = 0;
CUTE_PATH_STRNCAT(out, sep, seplen + 1);
return n;
}
// Last path part with separators in front equals n
if (backlen == n - seplen)
{
CUTE_PATH_STRNCPY(out, sep, seplen + 1);
CUTE_PATH_STRNCAT(out, back, backlen);
return n;
}
// Last path part with separators in front is too long
if (backlen > n - seplen)
{
CUTE_PATH_STRNCPY(out, sep, seplen + 1);
CUTE_PATH_STRNCAT(out, back, n - (2 * seplen));
CUTE_PATH_STRNCAT(out, sep, seplen);
return n;
}
int remaining = n - backlen - seplen;
CUTE_PATH_STRNCPY(out, path, remaining);
out[remaining] = 0;
CUTE_PATH_STRNCAT(out, sep, seplen);
CUTE_PATH_STRNCAT(out, back, backlen);
return n;
}
#if CUTE_PATH_UNIT_TESTS
#include <stdio.h>
#define CUTE_PATH_STRCMP strcmp
#define CUTE_PATH_EXPECT(X) do { if (!(X)) printf("Failed cute_path.h unit test at line %d of file %s.\n", __LINE__, __FILE__); } while (0)
void path_do_unit_tests()
{
char out[CUTE_PATH_MAX_PATH];
char pop[CUTE_PATH_MAX_PATH];
char ext[CUTE_PATH_MAX_PATH];
int n;
const char* path = "../root/file.ext";
path_pop_ext(path, out, ext);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "../root/file"));
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(ext, "ext"));
path_pop(path, out, pop);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "../root"));
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(pop, "file.ext"));
path = "../root/file";
path_pop_ext(path, out, ext);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "../root/file"));
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(ext, ""));
path_pop(path, out, pop);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "../root"));
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(pop, "file"));
path = "../root/";
path_pop_ext(path, out, ext);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "../root/"));
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(ext, ""));
path_pop(path, out, pop);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, ".."));
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(pop, "root"));
path = "../root";
path_pop_ext(path, out, ext);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "../root"));
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(ext, ""));
path_pop(path, out, pop);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, ".."));
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(pop, "root"));
path = "/file";
path_pop_ext(path, out, ext);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "/file"));
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(ext, ""));
path_pop(path, out, pop);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "/"));
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(pop, "file"));
path = "../";
path_pop_ext(path, out, ext);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "../"));
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(ext, ""));
path_pop(path, out, pop);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "."));
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(pop, ""));
path = "..";
path_pop_ext(path, out, ext);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, ".."));
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(ext, ""));
path_pop(path, out, pop);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "."));
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(pop, ""));
path = ".";
path_pop_ext(path, out, ext);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "."));
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(ext, ""));
path_pop(path, out, pop);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "."));
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(pop, ""));
path = "";
path_pop_ext(path, out, ext);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, ""));
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(ext, ""));
path_pop(path, out, pop);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "."));
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(pop, ""));
path = "../../file.ext";
path_pop_ext(path, out, ext);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "../../file"));
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(ext, "ext"));
path_pop(path, out, pop);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "../.."));
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(pop, "file.ext"));
path = "asdf/file.ext";
path_name_of_folder_im_in(path, out);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "asdf"));
path = "asdf/lkjh/file.ext";
path_name_of_folder_im_in(path, out);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "lkjh"));
path = "poiu/asdf/lkjh/file.ext";
path_name_of_folder_im_in(path, out);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "lkjh"));
path = "poiu/asdf/lkjhqwer/file.ext";
path_name_of_folder_im_in(path, out);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "lkjhqwer"));
path = "../file.ext";
path_name_of_folder_im_in(path, out);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, ".."));
path = "./file.ext";
path_name_of_folder_im_in(path, out);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "."));
path = "..";
CUTE_PATH_EXPECT(!path_name_of_folder_im_in(path, out));
path = ".";
CUTE_PATH_EXPECT(!path_name_of_folder_im_in(path, out));
path = "";
CUTE_PATH_EXPECT(!path_name_of_folder_im_in(path, out));
const char* path_a = "asdf";
const char* path_b = "qwerzxcv";
path_concat(path_a, path_b, out, CUTE_PATH_MAX_PATH);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "asdf/qwerzxcv"));
path_a = "path/owoasf.as.f.q.e.a";
path_b = "..";
path_concat(path_a, path_b, out, CUTE_PATH_MAX_PATH);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "path/owoasf.as.f.q.e.a/.."));
path_a = "a/b/c";
path_b = "d/e/f/g/h/i";
path_concat(path_a, path_b, out, CUTE_PATH_MAX_PATH);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "a/b/c/d/e/f/g/h/i"));
path = "/path/to/file.vim";
n = path_compact(path, out, 17);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "/path/to/file.vim"));
CUTE_PATH_EXPECT(n == CUTE_PATH_STRLEN(out));
path = "/path/to/file.vim";
n = path_compact(path, out, 16);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "/pat.../file.vim"));
CUTE_PATH_EXPECT(n == CUTE_PATH_STRLEN(out));
path = "/path/to/file.vim";
n = path_compact(path, out, 12);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, ".../file.vim"));
CUTE_PATH_EXPECT(n == CUTE_PATH_STRLEN(out));
path = "/path/to/file.vim";
n = path_compact(path, out, 11);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, ".../file..."));
CUTE_PATH_EXPECT(n == CUTE_PATH_STRLEN(out));
path = "longfile.vim";
n = path_compact(path, out, 12);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "longfile.vim"));
CUTE_PATH_EXPECT(n == CUTE_PATH_STRLEN(out));
path = "longfile.vim";
n = path_compact(path, out, 11);
CUTE_PATH_EXPECT(!CUTE_PATH_STRCMP(out, "longfile..."));
CUTE_PATH_EXPECT(n == CUTE_PATH_STRLEN(out));
}
#else
void path_do_unit_tests()
{
}
#endif // CUTE_PATH_UNIT_TESTS
#endif // CUTE_PATH_IMPLEMENTATION_ONCE
#endif // CUTE_PATH_IMPLEMENTATION
/*
------------------------------------------------------------------------------
This software is available under 2 licenses - you may choose the one you like.
------------------------------------------------------------------------------
ALTERNATIVE A - zlib license
Copyright (c) 2017 Randy Gaul http://www.randygaul.net
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/