forked from RandyGaul/cute_headers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinypath.h
516 lines (411 loc) · 12.5 KB
/
tinypath.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
/*
tinypath.h - v1.01
To create implementation (the function definitions)
#define TINYPATH_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-featuerd 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) tpCompact, tpPop bugfixes
*/
/*
Contributors:
sro5h 1.01 - tpCompact, tpPop bugfixes
*/
#if !defined( TINYPATH_H )
#define TP_MAX_PATH 1024
#define TP_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 TP_MAX_PATH. Can also copy the file
// extension into ext, up to TP_MAX_EXT.
#if !defined( __cplusplus )
int tpPopExt( const char* path, char* out, char* ext );
#else
int tpPopExt( 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 TP_MAX_PATH.
// Optionally stores the popped filename in pop. pop can be NULL.
// out can also be NULL.
#if !defined( __cplusplus )
int tpPop( const char* path, char* out, char* pop );
#else
int tpPop( 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 tpConcat( 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 TP_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 tpNameOfFolderImIn( 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 tpCompact( const char* path, char* out, int n );
// Some useful (but not yet implemented) functions
/*
int tpRoot( const char* path, char* out );
*/
#define TP_UNIT_TESTS 1
void tpDoUnitTests();
#define TINYPATH_H
#endif
#if defined( TINYPATH_IMPLEMENTATION )
#endif TINYPATH_IMPLEMENTATION
#ifdef _WIN32
#if !defined( _CRT_SECURE_NO_WARNINGS )
#define _CRT_SECURE_NO_WARNINGS
#endif
#endif
#include <string.h> // strncpy, strncat, strlen
#define TP_STRNCPY strncpy
#define TP_STRNCAT strncat
#define TP_STRLEN strlen
int tpIsSlash( char c )
{
return ( c == '/' ) | ( c == '\\' );
}
int tpPopExt( const char* path, char* out, char* ext )
{
int initial_skipped_periods = 0;
while ( *path == '.' )
{
++path;
++initial_skipped_periods;
}
const char* period = path;
char c;
while ( ( c = *period++ ) ) if ( c == '.' ) break;
int has_period = c == '.';
int len = (int)( period - path ) - 1 + initial_skipped_periods;
if ( len > TP_MAX_PATH - 1 ) len = TP_MAX_PATH - 1;
if ( out )
{
TP_STRNCPY( out, path - initial_skipped_periods, len );
out[ len ] = 0;
}
if ( ext )
{
if ( has_period )
{
TP_STRNCPY( ext, path - initial_skipped_periods + len + 1, TP_MAX_EXT );
}
else
{
ext[ 0 ] = 0;
}
}
return len;
}
int tpPop( 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 ( tpIsSlash( *(path - 1) ) )
{
--path;
total_len -= 1;
}
int pop_len = 0; // length of substring to be popped
while ( !tpIsSlash( *--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 )
{
TP_STRNCPY( out, original, len );
out[ len ] = 0;
}
if ( pop )
{
TP_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 tpStrnCpy( char* dst, const char* src, int n, int max )
{
int c;
const char* original = src;
do
{
if ( n >= max - 1 )
{
dst[ max - 1 ] = 0;
break;
}
c = *src++;
dst[ n ] = c;
++n;
} while ( c );
return n;
}
void tpConcat( const char* path_a, const char* path_b, char* out, int max_buffer_length )
{
int n = tpStrnCpy( out, path_a, 0, max_buffer_length );
n = tpStrnCpy( out, "/", n - 1, max_buffer_length );
tpStrnCpy( out, path_b, n - 1, max_buffer_length );
}
int tpNameOfFolderImIn( const char* path, char* out )
{
// return failure for empty strings and "." or ".."
if ( !*path || *path == '.' && TP_STRLEN( path ) < 3 ) return 0;
int len = tpPop( path, out, NULL );
int has_slash = 0;
for ( int i = 0; out[ i ]; ++i )
{
if ( tpIsSlash( out[ i ] ) )
{
has_slash = 1;
break;
}
}
if ( has_slash )
{
int n = tpPop( out, out, NULL ) + 1;
len -= n;
TP_STRNCPY( out, path + n, len );
}
else TP_STRNCPY( out, path, len );
out[ len ] = 0;
return 1;
}
int tpCompact( const char* path, char* out, int n )
{
if (n <= 6) return 0;
const char* sep = "...";
const int seplen = strlen( sep );
int pathlen = strlen( path );
out[ 0 ] = 0;
if ( pathlen <= n )
{
TP_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 ( !tpIsSlash( path[ i ] ) && i > 0 );
const char* back = path + i;
int backlen = strlen( back );
// No path separator was found or the first character was one
if ( pathlen == backlen )
{
TP_STRNCPY( out, path, n - seplen );
out[ n - seplen ] = 0;
TP_STRNCAT( out, sep, seplen + 1 );
return n;
}
// Last path part with separators in front equals n
if ( backlen == n - seplen )
{
TP_STRNCPY( out, sep, seplen + 1 );
TP_STRNCAT( out, back, backlen );
return n;
}
// Last path part with separators in front is too long
if ( backlen > n - seplen )
{
TP_STRNCPY( out, sep, seplen + 1 );
TP_STRNCAT( out, back, n - ( 2 * seplen ) );
TP_STRNCAT( out, sep, seplen );
return n;
}
int remaining = n - backlen - seplen;
TP_STRNCPY( out, path, remaining );
out[ remaining ] = 0;
TP_STRNCAT( out, sep, seplen );
TP_STRNCAT( out, back, backlen );
return n;
}
#if TP_UNIT_TESTS
#include <stdio.h>
#define TP_STRCMP strcmp
#define TP_EXPECT(X) do { if ( !(X) ) printf( "Failed tinyfiles.h unit test at line %d of file %s.\n", __LINE__, __FILE__ ); } while ( 0 )
void tpDoUnitTests()
{
char out[ TP_MAX_PATH ];
char pop[ TP_MAX_PATH ];
char ext[ TP_MAX_PATH ];
int n;
const char* path = "../root/file.ext";
tpPopExt( path, out, ext );
TP_EXPECT( !TP_STRCMP( out, "../root/file" ) );
TP_EXPECT( !TP_STRCMP( ext, "ext" ) );
tpPop( path, out, pop );
TP_EXPECT( !TP_STRCMP( out, "../root" ) );
TP_EXPECT( !TP_STRCMP( pop, "file.ext" ) );
path = "../root/file";
tpPopExt( path, out, ext );
TP_EXPECT( !TP_STRCMP( out, "../root/file" ) );
TP_EXPECT( !TP_STRCMP( ext, "" ) );
tpPop( path, out, pop );
TP_EXPECT( !TP_STRCMP( out, "../root" ) );
TP_EXPECT( !TP_STRCMP( pop, "file" ) );
path = "../root/";
tpPopExt( path, out, ext );
TP_EXPECT( !TP_STRCMP( out, "../root/" ) );
TP_EXPECT( !TP_STRCMP( ext, "" ) );
tpPop( path, out, pop );
TP_EXPECT( !TP_STRCMP( out, ".." ) );
TP_EXPECT( !TP_STRCMP( pop, "root" ) );
path = "../root";
tpPopExt( path, out, ext );
TP_EXPECT( !TP_STRCMP( out, "../root" ) );
TP_EXPECT( !TP_STRCMP( ext, "" ) );
tpPop( path, out, pop );
TP_EXPECT( !TP_STRCMP( out, ".." ) );
TP_EXPECT( !TP_STRCMP( pop, "root" ) );
path = "/file";
tpPopExt( path, out, ext );
TP_EXPECT( !TP_STRCMP( out, "/file" ) );
TP_EXPECT( !TP_STRCMP( ext, "" ) );
tpPop( path, out, pop );
TP_EXPECT( !TP_STRCMP( out, "/" ) );
TP_EXPECT( !TP_STRCMP( pop, "file" ) );
path = "../";
tpPopExt( path, out, ext );
TP_EXPECT( !TP_STRCMP( out, "../" ) );
TP_EXPECT( !TP_STRCMP( ext, "" ) );
tpPop( path, out, pop );
TP_EXPECT( !TP_STRCMP( out, "." ) );
TP_EXPECT( !TP_STRCMP( pop, "" ) );
path = "..";
tpPopExt( path, out, ext );
TP_EXPECT( !TP_STRCMP( out, ".." ) );
TP_EXPECT( !TP_STRCMP( ext, "" ) );
tpPop( path, out, pop );
TP_EXPECT( !TP_STRCMP( out, "." ) );
TP_EXPECT( !TP_STRCMP( pop, "" ) );
path = ".";
tpPopExt( path, out, ext );
TP_EXPECT( !TP_STRCMP( out, "." ) );
TP_EXPECT( !TP_STRCMP( ext, "" ) );
tpPop( path, out, pop );
TP_EXPECT( !TP_STRCMP( out, "." ) );
TP_EXPECT( !TP_STRCMP( pop, "" ) );
path = "";
tpPopExt( path, out, ext );
TP_EXPECT( !TP_STRCMP( out, "" ) );
TP_EXPECT( !TP_STRCMP( ext, "" ) );
tpPop( path, out, pop );
TP_EXPECT( !TP_STRCMP( out, "." ) );
TP_EXPECT( !TP_STRCMP( pop, "" ) );
path = "asdf/file.ext";
tpNameOfFolderImIn( path, out );
TP_EXPECT( !TP_STRCMP( out, "asdf" ) );
path = "asdf/lkjh/file.ext";
tpNameOfFolderImIn( path, out );
TP_EXPECT( !TP_STRCMP( out, "lkjh" ) );
path = "poiu/asdf/lkjh/file.ext";
tpNameOfFolderImIn( path, out );
TP_EXPECT( !TP_STRCMP( out, "lkjh" ) );
path = "poiu/asdf/lkjhqwer/file.ext";
tpNameOfFolderImIn( path, out );
TP_EXPECT( !TP_STRCMP( out, "lkjhqwer" ) );
path = "../file.ext";
tpNameOfFolderImIn( path, out );
TP_EXPECT( !TP_STRCMP( out, ".." ) );
path = "./file.ext";
tpNameOfFolderImIn( path, out );
TP_EXPECT( !TP_STRCMP( out, "." ) );
path = "..";
TP_EXPECT( !tpNameOfFolderImIn( path, out ) );
path = ".";
TP_EXPECT( !tpNameOfFolderImIn( path, out ) );
path = "";
TP_EXPECT( !tpNameOfFolderImIn( path, out ) );
const char* path_a = "asdf";
const char* path_b = "qwerzxcv";
tpConcat( path_a, path_b, out, TP_MAX_PATH );
TP_EXPECT( !TP_STRCMP( out, "asdf/qwerzxcv" ) );
path_a = "path/owoasf.as.f.q.e.a";
path_b = "..";
tpConcat( path_a, path_b, out, TP_MAX_PATH );
TP_EXPECT( !TP_STRCMP( out, "path/owoasf.as.f.q.e.a/.." ) );
path_a = "a/b/c";
path_b = "d/e/f/g/h/i";
tpConcat( path_a, path_b, out, TP_MAX_PATH );
TP_EXPECT( !TP_STRCMP( out, "a/b/c/d/e/f/g/h/i" ) );
path = "/path/to/file.vim";
n = tpCompact( path, out, 17 );
TP_EXPECT( !TP_STRCMP( out, "/path/to/file.vim" ) );
TP_EXPECT( n == TP_STRLEN( out ) );
path = "/path/to/file.vim";
n = tpCompact( path, out, 16 );
TP_EXPECT( !TP_STRCMP( out, "/pat.../file.vim" ) );
TP_EXPECT( n == TP_STRLEN( out ) );
path = "/path/to/file.vim";
n = tpCompact( path, out, 12 );
TP_EXPECT( !TP_STRCMP( out, ".../file.vim" ) );
TP_EXPECT( n == TP_STRLEN( out ) );
path = "/path/to/file.vim";
n = tpCompact( path, out, 11 );
TP_EXPECT( !TP_STRCMP( out, ".../file..." ) );
TP_EXPECT( n == TP_STRLEN( out ) );
path = "longfile.vim";
n = tpCompact( path, out, 12 );
TP_EXPECT( !TP_STRCMP( out, "longfile.vim" ) );
TP_EXPECT( n == TP_STRLEN( out ) );
path = "longfile.vim";
n = tpCompact( path, out, 11 );
TP_EXPECT( !TP_STRCMP( out, "longfile..." ) );
TP_EXPECT( n == TP_STRLEN( out ) );
}
#else
void tpDoUnitTests()
{
}
#endif // TP_UNIT_TESTS
#endif // TINYPATH_IMPLEMENTATION
/*
This is free and unencumbered software released into the public domain.
Our intent is that anyone is free to copy and use this software,
for any purpose, in any form, and by any means.
The authors dedicate any and all copyright interest in the software
to the public domain, at their own expense for the betterment of mankind.
The software is provided "as is", without any kind of warranty, including
any implied warranty. If it breaks, you get to keep both pieces.
*/