cwk_path_get_first_segment.md (1404B)
1 --- 2 title: cwk_path_get_first_segment 3 description: Gets the first segment of a path. 4 --- 5 6 _(since v1.0.0)_ 7 Gets the first segment of a path. 8 9 ## Description 10 ```c 11 bool cwk_path_get_first_segment(const char *path, struct cwk_segment *segment); 12 ``` 13 14 ## Description 15 This function finds the first segment of a path. The position of the segment is set to the first character after the separator, and the length counts all characters until the next separator (excluding the separator). 16 17 ## Parameters 18 * **path**: The path which will be inspected. 19 * **segment**: The segment which will be extracted. 20 21 ## Return Value 22 Returns ``true`` if there is a segment or ``false`` if there is none. 23 24 ## Example 25 ```c 26 #include <cwalk.h> 27 #include <stdio.h> 28 #include <stddef.h> 29 #include <stdlib.h> 30 31 int main(int argc, char *argv[]) 32 { 33 struct cwk_segment segment; 34 35 if(!cwk_path_get_first_segment("/my/path.txt", &segment)) { 36 printf("Path doesn't have any segments."); 37 } 38 39 printf("Segment length is '%zu'.\n", segment.size); 40 printf("The segment is '%.*s'.", (int)segment.size, segment.begin); 41 42 return EXIT_SUCCESS; 43 } 44 ``` 45 46 Ouput: 47 ``` 48 Segment length is '2'. 49 The segment is 'my'. 50 ``` 51 52 ## Changelog 53 54 | Version | Description | 55 |------------|--------------------------------------------------------| 56 | **v1.0.0** | The function is introduced. |