Skip to content

Commit e7f3b63

Browse files
committed
Introduce cpj_path_join_and_normalize
1 parent 2d9e441 commit e7f3b63

File tree

6 files changed

+766
-478
lines changed

6 files changed

+766
-478
lines changed

.clang-format

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
BasedOnStyle: LLVM
3-
AlignAfterOpenBracket: DontAlign
3+
AlignAfterOpenBracket: BlockIndent
44
BinPackArguments: true
55
BinPackParameters: true
66
BreakBeforeBraces: Mozilla
@@ -16,3 +16,4 @@ AllowShortLoopsOnASingleLine: false
1616
PenaltyBreakAssignment: 10000
1717
PenaltyBreakBeforeFirstCallParameter: 100000
1818
IndentExternBlock: NoIndent
19+
ColumnLimit: 120

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ if(ENABLE_TESTS)
175175
create_test(DEFAULT normalize mixed)
176176
create_test(DEFAULT normalize overlap)
177177
create_test(DEFAULT normalize empty)
178+
create_test(DEFAULT normalize zero_length)
178179
create_test(DEFAULT normalize only_separators)
179180
create_test(DEFAULT normalize back_after_root)
180181
create_test(DEFAULT normalize forward_slashes)

include/cpj.h

Lines changed: 99 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <stdbool.h>
77
#include <stddef.h>
8+
#include <stdint.h>
89

910
#if defined(_WIN32) || defined(__CYGWIN__)
1011
#define CPJ_EXPORT __declspec(dllexport)
@@ -32,7 +33,17 @@ extern "C"
3233
{
3334
#endif
3435

35-
typedef size_t cpj_size_t;
36+
typedef uint8_t cpj_char_t;
37+
typedef uint32_t cpj_size_t;
38+
39+
/**
40+
* Description of a JerryScript string for arguments passing
41+
*/
42+
typedef struct
43+
{
44+
const cpj_char_t *ptr; /**< pointer to the zero-terminated ASCII/UTF-8/CESU-8 string */
45+
cpj_size_t size; /**< size of the string, excluding '\0' terminator */
46+
} cpj_string_t;
3647

3748
/**
3849
* A segment represents a single component of a path. For instance, on linux a
@@ -41,10 +52,10 @@ typedef size_t cpj_size_t;
4152
*/
4253
struct cpj_segment
4354
{
44-
const char *path;
45-
const char *segments;
46-
const char *begin;
47-
const char *end;
55+
const cpj_char_t *path;
56+
const cpj_char_t *segments;
57+
const cpj_char_t *begin;
58+
const cpj_char_t *end;
4859
cpj_size_t size;
4960
};
5061

@@ -67,11 +78,11 @@ enum cpj_segment_type
6778
* @brief Determines the style which is used for the path parsing and
6879
* generation.
6980
*/
70-
enum cpj_path_style
81+
typedef enum
7182
{
7283
CPJ_STYLE_WINDOWS,
73-
CPJ_STYLE_UNIX
74-
};
84+
CPJ_STYLE_UNIX,
85+
} cpj_path_style_t;
7586

7687
/**
7788
* @brief Generates an absolute path based on a base.
@@ -93,8 +104,10 @@ enum cpj_path_style
93104
* @param buffer_size The size of the result buffer.
94105
* @return Returns the total amount of characters of the new absolute path.
95106
*/
96-
CPJ_PUBLIC cpj_size_t cpj_path_get_absolute(enum cpj_path_style path_style,
97-
const char *base, const char *path, char *buffer, cpj_size_t buffer_size);
107+
CPJ_PUBLIC cpj_size_t cpj_path_get_absolute(
108+
cpj_path_style_t path_style, const cpj_char_t *base, const cpj_char_t *path, cpj_char_t *buffer,
109+
cpj_size_t buffer_size
110+
);
98111

99112
/**
100113
* @brief Generates a relative path based on a base.
@@ -116,9 +129,35 @@ CPJ_PUBLIC cpj_size_t cpj_path_get_absolute(enum cpj_path_style path_style,
116129
* @param buffer_size The size of the result buffer.
117130
* @return Returns the total amount of characters of the full path.
118131
*/
119-
CPJ_PUBLIC cpj_size_t cpj_path_get_relative(enum cpj_path_style path_style,
120-
const char *base_directory, const char *path, char *buffer,
121-
cpj_size_t buffer_size);
132+
CPJ_PUBLIC cpj_size_t cpj_path_get_relative(
133+
cpj_path_style_t path_style, const cpj_char_t *base_directory, const cpj_char_t *path, cpj_char_t *buffer,
134+
cpj_size_t buffer_size
135+
);
136+
137+
/**
138+
* @brief Joins two paths together.
139+
*
140+
* This function generates a new path by combining the two submitted paths. It
141+
* will remove double separators, and unlike cpj_path_get_absolute it permits
142+
* the use of two relative paths to combine. The result will be written to a
143+
* buffer, which might be truncated if the buffer is not large enough to hold
144+
* the full path. However, the truncated result will always be
145+
* null-terminated. The returned value is the amount of characters which the
146+
* resulting path would take if it was not truncated (excluding the
147+
* null-terminating character).
148+
*
149+
* @param path_style Style depending on the operating system. So this should
150+
* detect whether we should use windows or unix paths.
151+
* @param path_a The first path which comes first.
152+
* @param path_b The second path which comes after the first.
153+
* @param buffer The buffer where the result will be written to.
154+
* @param buffer_size The size of the result buffer.
155+
* @return Returns the total amount of characters of the full, combined path.
156+
*/
157+
CPJ_PUBLIC cpj_size_t cpj_path_join(
158+
cpj_path_style_t path_style, const cpj_char_t *path_a, const cpj_char_t *path_b, cpj_char_t *buffer,
159+
cpj_size_t buffer_size
160+
);
122161

123162
/**
124163
* @brief Joins two paths together.
@@ -140,8 +179,10 @@ CPJ_PUBLIC cpj_size_t cpj_path_get_relative(enum cpj_path_style path_style,
140179
* @param buffer_size The size of the result buffer.
141180
* @return Returns the total amount of characters of the full, combined path.
142181
*/
143-
CPJ_PUBLIC cpj_size_t cpj_path_join(enum cpj_path_style path_style,
144-
const char *path_a, const char *path_b, char *buffer, cpj_size_t buffer_size);
182+
CPJ_PUBLIC cpj_size_t cpj_path_join_module(
183+
cpj_path_style_t path_style, const cpj_char_t *path_a, const cpj_char_t *path_b, cpj_char_t *buffer,
184+
cpj_size_t buffer_size
185+
);
145186

146187
/**
147188
* @brief Joins multiple paths together.
@@ -163,8 +204,9 @@ CPJ_PUBLIC cpj_size_t cpj_path_join(enum cpj_path_style path_style,
163204
* @param buffer_size The size of the result buffer.
164205
* @return Returns the total amount of characters of the full, combined path.
165206
*/
166-
CPJ_PUBLIC cpj_size_t cpj_path_join_multiple(enum cpj_path_style path_style,
167-
const char **paths, char *buffer, cpj_size_t buffer_size);
207+
CPJ_PUBLIC cpj_size_t cpj_path_join_multiple(
208+
cpj_path_style_t path_style, const cpj_char_t **paths, cpj_char_t *buffer, cpj_size_t buffer_size
209+
);
168210

169211
/**
170212
* @brief Determines the root of a path.
@@ -178,8 +220,7 @@ CPJ_PUBLIC cpj_size_t cpj_path_join_multiple(enum cpj_path_style path_style,
178220
* @param path The path which will be inspected.
179221
* @param length The output of the root length.
180222
*/
181-
CPJ_PUBLIC void cpj_path_get_root(enum cpj_path_style path_style,
182-
const char *path, cpj_size_t *length);
223+
CPJ_PUBLIC void cpj_path_get_root(cpj_path_style_t path_style, const cpj_char_t *path, cpj_size_t *length);
183224

184225
/**
185226
* @brief Changes the root of a path.
@@ -200,8 +241,10 @@ CPJ_PUBLIC void cpj_path_get_root(enum cpj_path_style path_style,
200241
* written to.
201242
* @return Returns the total amount of characters of the new path.
202243
*/
203-
CPJ_PUBLIC cpj_size_t cpj_path_change_root(enum cpj_path_style path_style,
204-
const char *path, const char *new_root, char *buffer, cpj_size_t buffer_size);
244+
CPJ_PUBLIC cpj_size_t cpj_path_change_root(
245+
cpj_path_style_t path_style, const cpj_char_t *path, const cpj_char_t *new_root, cpj_char_t *buffer,
246+
cpj_size_t buffer_size
247+
);
205248

206249
/**
207250
* @brief Determine whether the path is absolute or not.
@@ -214,8 +257,7 @@ CPJ_PUBLIC cpj_size_t cpj_path_change_root(enum cpj_path_style path_style,
214257
* @param path The path which will be checked.
215258
* @return Returns true if the path is absolute or false otherwise.
216259
*/
217-
CPJ_PUBLIC bool cpj_path_is_absolute(enum cpj_path_style path_style,
218-
const char *path);
260+
CPJ_PUBLIC bool cpj_path_is_absolute(cpj_path_style_t path_style, const cpj_char_t *path);
219261

220262
/**
221263
* @brief Determine whether the path is relative or not.
@@ -228,8 +270,7 @@ CPJ_PUBLIC bool cpj_path_is_absolute(enum cpj_path_style path_style,
228270
* @param path The path which will be checked.
229271
* @return Returns true if the path is relative or false otherwise.
230272
*/
231-
CPJ_PUBLIC bool cpj_path_is_relative(enum cpj_path_style path_style,
232-
const char *path);
273+
CPJ_PUBLIC bool cpj_path_is_relative(cpj_path_style_t path_style, const cpj_char_t *path);
233274

234275
/**
235276
* @brief Gets the basename of a file path.
@@ -248,8 +289,9 @@ CPJ_PUBLIC bool cpj_path_is_relative(enum cpj_path_style path_style,
248289
* @param length The output of the length of the basename. This may be
249290
* null if not required.
250291
*/
251-
CPJ_PUBLIC void cpj_path_get_basename(enum cpj_path_style path_style,
252-
const char *path, const char **basename, cpj_size_t *length);
292+
CPJ_PUBLIC void cpj_path_get_basename(
293+
cpj_path_style_t path_style, const cpj_char_t *path, const cpj_char_t **basename, cpj_size_t *length
294+
);
253295

254296
/**
255297
* @brief Changes the basename of a file path.
@@ -271,8 +313,10 @@ CPJ_PUBLIC void cpj_path_get_basename(enum cpj_path_style path_style,
271313
* @return Returns the size which the complete new path would have if it was
272314
* not truncated.
273315
*/
274-
CPJ_PUBLIC cpj_size_t cpj_path_change_basename(enum cpj_path_style path_style,
275-
const char *path, const char *new_basename, char *buffer, cpj_size_t buffer_size);
316+
CPJ_PUBLIC cpj_size_t cpj_path_change_basename(
317+
cpj_path_style_t path_style, const cpj_char_t *path, const cpj_char_t *new_basename, cpj_char_t *buffer,
318+
cpj_size_t buffer_size
319+
);
276320

277321
/**
278322
* @brief Gets the dirname of a file path.
@@ -287,8 +331,7 @@ CPJ_PUBLIC cpj_size_t cpj_path_change_basename(enum cpj_path_style path_style,
287331
* @param path The path which will be inspected.
288332
* @param length The length of the dirname.
289333
*/
290-
CPJ_PUBLIC void cpj_path_get_dirname(enum cpj_path_style path_style,
291-
const char *path, cpj_size_t *length);
334+
CPJ_PUBLIC void cpj_path_get_dirname(cpj_path_style_t path_style, const cpj_char_t *path, cpj_size_t *length);
292335

293336
/**
294337
* @brief Gets the extension of a file path.
@@ -307,8 +350,9 @@ CPJ_PUBLIC void cpj_path_get_dirname(enum cpj_path_style path_style,
307350
* @param length The output of the length of the extension.
308351
* @return Returns true if an extension is found or false otherwise.
309352
*/
310-
CPJ_PUBLIC bool cpj_path_get_extension(enum cpj_path_style path_style,
311-
const char *path, const char **extension, cpj_size_t *length);
353+
CPJ_PUBLIC bool cpj_path_get_extension(
354+
cpj_path_style_t path_style, const cpj_char_t *path, const cpj_char_t **extension, cpj_size_t *length
355+
);
312356

313357
/**
314358
* @brief Determines whether the file path has an extension.
@@ -321,8 +365,7 @@ CPJ_PUBLIC bool cpj_path_get_extension(enum cpj_path_style path_style,
321365
* @param path The path which will be inspected.
322366
* @return Returns true if the path has an extension or false otherwise.
323367
*/
324-
CPJ_PUBLIC bool cpj_path_has_extension(enum cpj_path_style path_style,
325-
const char *path);
368+
CPJ_PUBLIC bool cpj_path_has_extension(cpj_path_style_t path_style, const cpj_char_t *path);
326369

327370
/**
328371
* @brief Changes the extension of a file path.
@@ -347,9 +390,10 @@ CPJ_PUBLIC bool cpj_path_has_extension(enum cpj_path_style path_style,
347390
* @return Returns the total size which the output would have if it was not
348391
* truncated.
349392
*/
350-
CPJ_PUBLIC cpj_size_t cpj_path_change_extension(enum cpj_path_style path_style,
351-
const char *path, const char *new_extension, char *buffer,
352-
cpj_size_t buffer_size);
393+
CPJ_PUBLIC cpj_size_t cpj_path_change_extension(
394+
cpj_path_style_t path_style, const cpj_char_t *path, const cpj_char_t *new_extension, cpj_char_t *buffer,
395+
cpj_size_t buffer_size
396+
);
353397

354398
/**
355399
* @brief Creates a normalized version of the path.
@@ -376,8 +420,8 @@ CPJ_PUBLIC cpj_size_t cpj_path_change_extension(enum cpj_path_style path_style,
376420
* @return The size which the complete normalized path has if it was not
377421
* truncated.
378422
*/
379-
CPJ_PUBLIC cpj_size_t cpj_path_normalize(enum cpj_path_style path_style,
380-
const char *path, char *buffer, cpj_size_t buffer_size);
423+
CPJ_PUBLIC cpj_size_t
424+
cpj_path_normalize(cpj_path_style_t path_style, const cpj_char_t *path, cpj_char_t *buffer, cpj_size_t buffer_size);
381425

382426
/**
383427
* @brief Finds common portions in two paths.
@@ -392,8 +436,8 @@ CPJ_PUBLIC cpj_size_t cpj_path_normalize(enum cpj_path_style path_style,
392436
* @param path_other The other path which will compared with the base path.
393437
* @return Returns the number of characters which are common in the base path.
394438
*/
395-
CPJ_PUBLIC cpj_size_t cpj_path_get_intersection(enum cpj_path_style path_style,
396-
const char *path_base, const char *path_other);
439+
CPJ_PUBLIC cpj_size_t
440+
cpj_path_get_intersection(cpj_path_style_t path_style, const cpj_char_t *path_base, const cpj_char_t *path_other);
397441

398442
/**
399443
* @brief Gets the first segment of a path.
@@ -408,8 +452,8 @@ CPJ_PUBLIC cpj_size_t cpj_path_get_intersection(enum cpj_path_style path_style,
408452
* @param segment The segment which will be extracted.
409453
* @return Returns true if there is a segment or false if there is none.
410454
*/
411-
CPJ_PUBLIC bool cpj_path_get_first_segment(enum cpj_path_style path_style,
412-
const char *path, struct cpj_segment *segment);
455+
CPJ_PUBLIC bool
456+
cpj_path_get_first_segment(cpj_path_style_t path_style, const cpj_char_t *path, struct cpj_segment *segment);
413457

414458
/**
415459
* @brief Gets the last segment of the path.
@@ -426,8 +470,8 @@ CPJ_PUBLIC bool cpj_path_get_first_segment(enum cpj_path_style path_style,
426470
* @param segment The segment which will be extracted.
427471
* @return Returns true if there is a segment or false if there is none.
428472
*/
429-
CPJ_PUBLIC bool cpj_path_get_last_segment(enum cpj_path_style path_style,
430-
const char *path, struct cpj_segment *segment);
473+
CPJ_PUBLIC bool
474+
cpj_path_get_last_segment(cpj_path_style_t path_style, const cpj_char_t *path, struct cpj_segment *segment);
431475

432476
/**
433477
* @brief Advances to the next segment.
@@ -441,8 +485,7 @@ CPJ_PUBLIC bool cpj_path_get_last_segment(enum cpj_path_style path_style,
441485
* @param segment The current segment which will be advanced to the next one.
442486
* @return Returns true if another segment was found or false otherwise.
443487
*/
444-
CPJ_PUBLIC bool cpj_path_get_next_segment(enum cpj_path_style path_style,
445-
struct cpj_segment *segment);
488+
CPJ_PUBLIC bool cpj_path_get_next_segment(cpj_path_style_t path_style, struct cpj_segment *segment);
446489

447490
/**
448491
* @brief Moves to the previous segment.
@@ -457,8 +500,7 @@ CPJ_PUBLIC bool cpj_path_get_next_segment(enum cpj_path_style path_style,
457500
* @return Returns true if there is a segment before this one or false
458501
* otherwise.
459502
*/
460-
CPJ_PUBLIC bool cpj_path_get_previous_segment(enum cpj_path_style path_style,
461-
struct cpj_segment *segment);
503+
CPJ_PUBLIC bool cpj_path_get_previous_segment(cpj_path_style_t path_style, struct cpj_segment *segment);
462504

463505
/**
464506
* @brief Gets the type of the submitted path segment.
@@ -471,8 +513,7 @@ CPJ_PUBLIC bool cpj_path_get_previous_segment(enum cpj_path_style path_style,
471513
* @param segment The segment which will be inspected.
472514
* @return Returns the type of the segment.
473515
*/
474-
CPJ_PUBLIC enum cpj_segment_type cpj_path_get_segment_type(
475-
const struct cpj_segment *segment);
516+
CPJ_PUBLIC enum cpj_segment_type cpj_path_get_segment_type(const struct cpj_segment *segment);
476517

477518
/**
478519
* @brief Changes the content of a segment.
@@ -493,9 +534,10 @@ CPJ_PUBLIC enum cpj_segment_type cpj_path_get_segment_type(
493534
* @return Returns the total size which would have been written if the output
494535
* was not truncated.
495536
*/
496-
CPJ_PUBLIC cpj_size_t cpj_path_change_segment(enum cpj_path_style path_style,
497-
struct cpj_segment *segment, const char *value, char *buffer,
498-
cpj_size_t buffer_size);
537+
CPJ_PUBLIC cpj_size_t cpj_path_change_segment(
538+
cpj_path_style_t path_style, struct cpj_segment *segment, const cpj_char_t *value, cpj_char_t *buffer,
539+
cpj_size_t buffer_size
540+
);
499541

500542
/**
501543
* @brief Checks whether the submitted pointer points to a separator.
@@ -510,8 +552,7 @@ CPJ_PUBLIC cpj_size_t cpj_path_change_segment(enum cpj_path_style path_style,
510552
* @param str A pointer to a string.
511553
* @return Returns true if it is a separator, or false otherwise.
512554
*/
513-
CPJ_PUBLIC bool cpj_path_is_separator(enum cpj_path_style path_style,
514-
const char *str);
555+
CPJ_PUBLIC bool cpj_path_is_separator(cpj_path_style_t path_style, const cpj_char_t str);
515556

516557
/**
517558
* @brief Guesses the path style.
@@ -523,7 +564,7 @@ CPJ_PUBLIC bool cpj_path_is_separator(enum cpj_path_style path_style,
523564
* @param path The path which will be inspected.
524565
* @return Returns the style which is most likely used for the path.
525566
*/
526-
CPJ_PUBLIC enum cpj_path_style cpj_path_guess_style(const char *path);
567+
CPJ_PUBLIC cpj_path_style_t cpj_path_guess_style(const cpj_char_t *path);
527568

528569
#ifdef __cplusplus
529570
} // extern "C"

0 commit comments

Comments
 (0)