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
|
/*
* Copyright (c) 2017 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file parc_PathName.h
* @ingroup inputoutput
* @brief Path Name Manipulation
*
*/
#ifndef libparc_parc_PathName_h
#define libparc_parc_PathName_h
struct parc_pathname;
typedef struct parc_pathname PARCPathName;
#include <parc/algol/parc_BufferComposer.h>
/**
* Create an empty, relative, `PARCPathName`.
*
* @return A pointer to a `PARCPathName` instance.
*
* @see {@link parcPathName_MakeAbsolute}
*
* Example:
* @code
* {
* PARCPathName *result = parcPathName_Create();
* parcPathName_Destroy(&result);
* }
* <#example#>
* @endcode
*/
PARCPathName *parcPathName_Create(void);
/**
* Parse a null-terminated C string as a `PARCPathName` limited to specific length.
* Components are separated by a single '/' character.
*
* @param [in] limit The limit to the length
* @param [in] path The string to parse
*
* @return A pointer to the new `PARCPathName`
*
* Example:
* @code
* <#example#>
* @endcode
*/
PARCPathName *parcPathName_ParseToLimit(size_t limit, const char *path);
/**
* Parse a null-terminated C string as a `PARCPathName`
* Components are separated by a single '/' character.
*
* @param [in] path The string to be parsed
* @return A pointer to the new `PARCPathName`
*
* Example:
* @code
* <#example#>
* @endcode
*/
PARCPathName *parcPathName_Parse(const char *path);
/**
* Acquire a new reference to an instance of `PARCPathName`.
*
* The reference count to the instance is incremented.
*
* @param [in] pathName The instance of `PARCPathName` to which to refer.
*
* @return The same value as the input parameter @p pathName
*
* Example:
* @code
* {
* <#example#>
* }
* @endcode
*/
PARCPathName *parcPathName_Acquire(const PARCPathName *pathName);
/**
* Release a previously acquired reference to the specified instance,
* decrementing the reference count for the instance.
*
* The pointer to the instance is set to NULL as a side-effect of this function.
*
* If the invocation causes the last reference to the instance to be released,
* the instance is deallocated and the instance's implementation will perform
* additional cleanup and release other privately held references.
*
* @param [in,out] pathNamePtr A pointer to a pointer to the instance to release.
*
*
* Example:
* @code
* {
* PARCPathName *pathName = parcPathName_Parse("/tmp/foo");
*
* parcPathName_Release(&pathName);
* }
* @endcode
*/
void parcPathName_Release(PARCPathName **pathNamePtr);
/**
* Determine if two `PARCPathName` instances are equal.
*
* The following equivalence relations on non-null `PARCPathName` instances are maintained:
*
* * It is reflexive: for any non-null reference value x, `parcPathName_Equals(x, x)` must return true.
*
* * It is symmetric: for any non-null reference values x and y, `parcPathName_Equals(x, y)` must return true if and only if
* `parcPathName_Equals(y x)` returns true.
*
* * It is transitive: for any non-null reference values x, y, and z, if
* `parcPathName_Equals(x, y)` returns true and
* `parcPathName_Equals(y, z)` returns true,
* then `parcPathName_Equals(x, z)` must return true.
*
* * It is consistent: for any non-null reference values x and y, multiple invocations of `parcPathName_Equals(x, y)`
* consistently return true or consistently return false.
*
* * For any non-null reference value x, `parcPathName_Equals(x, NULL)` must return false.
*
* @param [in] x A pointer to a `PARCPathName` instance.
* @param [in] y A pointer to a `PARCPathName` instance.
*
* @return true `PARCPathName` x and y are equal.
* @return false `PARCPathName` x and y are not equal.
*
* Example:
* @code
* {
* PARCPathName *a = parcPathName_Create();
* PARCPathName *b = parcPathName_Create();
*
* if (parcPathName_Equals(a, b)) {
* // true
* } else {
* // false
* }
* }
* @endcode
*
*/
bool parcPathName_Equals(const PARCPathName *x, const PARCPathName *y);
/**
* Copy a pathName into a new instance of `PARCPathName`
*
* @param [in] pathName An instance of `PARCPathName` to be copied
*
* @return A new instance of `PARCPathName` that is a copy of @p pathName
*
* Example:
* @code
* {
* <#example#>
* }
* @endcode
*
*/
PARCPathName *parcPathName_Copy(const PARCPathName *pathName);
/**
* Return true if the instance of `PARCPathName` is absolute
*
* An absolute path name is a fully specified path starting at the root as the left-most segment.
* A relative path name is an incomplete path starting at an unspecified root.
*
* For example, an absolute UNIX file name path begins with a `/` character.
* `/foo/bar`, `/tmp/test` are both absolute path names.
*
* @param [in] pathName The instance of `PARCPathName` to test for absoluteness
* @return True is the path name is absolute, false otherwise.
*
* Example:
* @code
* <#example#>
* @endcode
*/
bool parcPathName_IsAbsolute(const PARCPathName *pathName);
/**
* Make a `PARCPathName` absolute or relative.
*
* An absolute path name is a fully specified path starting at the root as the left-most segment.
* A relative path name is an incomplete path starting at an unspecified root.
*
* For example, an absolute UNIX file name path begins with a `/` character.
* `/foo/bar`, `/tmp/test` are both absolute path names.
*
*
* @param [in,out] pathName A pointer to a `PARCPathName` instance to be modified
* @param [in] absolute a flad as to whether the @p pathName should be set to absolute or relative; true indicates absolute
*
* @return true if the `PARCPathName` was previously an absolute path name.
* @return false if the `PARCPathName` was previously a relative path name.
*
* Example:
* @code
* {
* parcPathName_MakeAbsolute(pathName true)
* }
* @endcode
*/
bool parcPathName_MakeAbsolute(PARCPathName *pathName, bool absolute);
/**
* Append a name segment to a `PARCPathName`
*
* The C string, `name` is copied.
*
* @param [in,out] pathName The instance of `PARCPathName` to modify
* @param [in] name A pointer to a null-terminated string. The contents are appended to the @p pathName.
*
* @return The input @p pathName
*
* Example:
* @code
* <#example#>
* @endcode
*
*/
PARCPathName *parcPathName_Append(PARCPathName *pathName, const char *name);
/**
* Prepend a name segment to a `PARCPathName`
*
* The C string, `name` is copied.
*
* @param [in,out] pathName The instance of `PARCPathName` to modify
* @param [in] name A pointer to a null-terminated string. The contents are prepended to the @p pathName.
*
* @return The input @p pathName
*
* Example:
* @code
* <#example#>
* @endcode
*
*/
PARCPathName *parcPathName_Prepend(PARCPathName *pathName, const char *name);
/**
* Create a new `PARCPathName` instance that consists of the head of an existing `PARCPathName`.
*
* If the original `PARCPathName` is an absolute path, the new `PARCPathName` will also be absolute.
* Otherwise, it will be a relative path.
*
* The new `PARCPathName` contains a copy of the requisite components of the orignal `PARCPathName`.
*
* @param [in] pathName The original `PARCPathName`
* @param [in] size The maximum number of path segments to include in the new `PARCPathName`.
*
* @return a Pointer to the new `PARCPathName`
*
* Example:
* @code
* <#example#>
* @endcode
*/
PARCPathName *parcPathName_Head(const PARCPathName *pathName, size_t size);
/**
* Create a new `PARCPathName` instance that consists of the tail of an existing `PARCPathName`.
*
* The new `PARCPathName` is a relative path and contains a copy of the requisite components of the orignal `PARCPathName`.
*
* @param [in] pathName The original `PARCPathName`
* @param [in] size The maximum number of path segments to include in the new `PARCPathName`.
*
* @return a Pointer to the new `PARCPathName`
*
* Example:
* @code
* <#example#>
* @endcode
*/
PARCPathName *parcPathName_Tail(const PARCPathName *pathName, size_t size);
/**
* Get a pointer to the null-terminated C string for the specified path name segment.
*
* @param [in] pathName A pointer to a `PARCPathName` instance.
* @param [in] index The index of the segment
*
* @return a pointer to the null-terminate C string for the specified path name segment
*
* Example:
* @code
* <#example#>
* @endcode
*
*/
char *parcPathName_GetAtIndex(const PARCPathName *pathName, size_t index);
/**
* Get the number of path segments in a `PARCPathName`.
*
* @param [in] pathName A pointer to a `PARCPathName` instance.
*
* @return The number of path segments in the `PARCPathName`
*
* Example:
* @code
* <#example#>
* @endcode
*
*/
size_t parcPathName_Size(const PARCPathName *pathName);
/**
* Append a representation of the specified instance to the given {@link PARCBufferComposer}.
*
* @param [in] path A pointer to the `PARCPathName` whose representation should be added to the @p string.
* @param [in] string A pointer to the `PARCBufferComposer` to which to append the representation of @p path
*
*
* @return NULL Cannot allocate memory.
* @return non-NULL The given `PARCBufferComposer`.
*
* Example:
* @code
* {
* PARCBufferComposer *result = parcBufferComposer_Create();
*
* parcPathName_BuildString(instance, result);
*
* PARCBuffer *string = parcBufferComposer_FinalizeBuffer(result);
* printf("Hello: %s\n", parcBuffer_ToString(string));
* parcBuffer_Release(&string);
*
* parcBufferComposer_Release(&result);
* }
* @endcode
*/
PARCBufferComposer *parcPathName_BuildString(const PARCPathName *path, PARCBufferComposer *string);
/**
* Produce a C string representation of the given `PARCPathName`.
*
* Produce an allocated, null-terminated string representation of the given `PARCPathName`.
* The result must be freed by the caller via the `parcMemory_Deallocate()` function.
*
* @param [in] pathName A pointer to the `PARCPathName` to convert to a `String`
*
* @return The string representation of @p pathName
*
* Example:
* @code
* <#example#>
* @endcode
*
*/
char *parcPathName_ToString(const PARCPathName *pathName);
/**
* Print a human readable representation of the given `PARCPathName`.
*
* @param [in] pathName A pointer to the instance of `PARCPathName` to display.
* @param [in] indentation The level of indentation to use to pretty-print the output.
*
* Example:
* @code
* {
* PARCPathName *instance = parcPathName_Create();
*
* parcPathName_Display(instance, 0);
*
* parcPathName_Release(&instance);
* }
* @endcode
*
*/
void parcPathName_Display(const PARCPathName *pathName, int indentation);
#endif // libparc_parc_PathName_h
|