12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- #ifdef __cplusplus
16
- extern "C"
17
- {
18
- #endif
19
15
#include "rcutils/filesystem.h"
20
16
21
17
#include <errno.h>
@@ -54,7 +50,7 @@ extern "C"
54
50
# define RCUTILS_PATH_DELIMITER "/"
55
51
#endif // _WIN32
56
52
57
- typedef struct rcutils_dir_iter_state_t
53
+ typedef struct rcutils_dir_iter_state_s
58
54
{
59
55
#ifdef _WIN32
60
56
HANDLE handle ;
@@ -417,49 +413,48 @@ rcutils_dir_iter_start(const char * directory_path, const rcutils_allocator_t al
417
413
RCUTILS_CHECK_ALLOCATOR_WITH_MSG (
418
414
& allocator , "allocator is invalid" , return NULL );
419
415
420
- rcutils_dir_iter_t * iter = ( rcutils_dir_iter_t * ) allocator .zero_allocate (
416
+ rcutils_dir_iter_t * iter = allocator .zero_allocate (
421
417
1 , sizeof (rcutils_dir_iter_t ), allocator .state );
422
418
if (NULL == iter ) {
423
419
return NULL ;
424
420
}
425
421
iter -> allocator = allocator ;
426
422
427
- rcutils_dir_iter_state_t * state = ( rcutils_dir_iter_state_t * ) allocator .zero_allocate (
423
+ iter -> state = allocator .zero_allocate (
428
424
1 , sizeof (rcutils_dir_iter_state_t ), allocator .state );
429
- if (NULL == state ) {
425
+ if (NULL == iter -> state ) {
430
426
RCUTILS_SET_ERROR_MSG (
431
427
"Failed to allocate memory.\n" );
432
428
goto rcutils_dir_iter_start_fail ;
433
429
}
434
- iter -> state = (void * )state ;
435
430
436
431
#ifdef _WIN32
437
432
char * search_path = rcutils_join_path (directory_path , "*" , allocator );
438
433
if (NULL == search_path ) {
439
434
goto rcutils_dir_iter_start_fail ;
440
435
}
441
- state -> handle = FindFirstFile (search_path , & state -> data );
436
+ iter -> state -> handle = FindFirstFile (search_path , & iter -> state -> data );
442
437
allocator .deallocate (search_path , allocator .state );
443
- if (INVALID_HANDLE_VALUE == state -> handle ) {
438
+ if (INVALID_HANDLE_VALUE == iter -> state -> handle ) {
444
439
DWORD error = GetLastError ();
445
440
if (ERROR_FILE_NOT_FOUND != error || !rcutils_is_directory (directory_path )) {
446
441
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING (
447
442
"Can't open directory %s. Error code: %d\n" , directory_path , error );
448
443
goto rcutils_dir_iter_start_fail ;
449
444
}
450
445
} else {
451
- iter -> entry_name = state -> data .cFileName ;
446
+ iter -> entry_name = iter -> state -> data .cFileName ;
452
447
}
453
448
#else
454
- state -> dir = opendir (directory_path );
455
- if (NULL == state -> dir ) {
449
+ iter -> state -> dir = opendir (directory_path );
450
+ if (NULL == iter -> state -> dir ) {
456
451
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING (
457
452
"Can't open directory %s. Error code: %d\n" , directory_path , errno );
458
453
goto rcutils_dir_iter_start_fail ;
459
454
}
460
455
461
456
errno = 0 ;
462
- struct dirent * entry = readdir (state -> dir );
457
+ struct dirent * entry = readdir (iter -> state -> dir );
463
458
if (NULL != entry ) {
464
459
iter -> entry_name = entry -> d_name ;
465
460
} else if (0 != errno ) {
@@ -480,17 +475,15 @@ bool
480
475
rcutils_dir_iter_next (rcutils_dir_iter_t * iter )
481
476
{
482
477
RCUTILS_CHECK_ARGUMENT_FOR_NULL (iter , false);
483
- rcutils_dir_iter_state_t * state = (rcutils_dir_iter_state_t * )iter -> state ;
484
- RCUTILS_CHECK_FOR_NULL_WITH_MSG (state , "iter is invalid" , false);
478
+ RCUTILS_CHECK_FOR_NULL_WITH_MSG (iter -> state , "iter is invalid" , false);
485
479
486
480
#ifdef _WIN32
487
- if (FindNextFile (state -> handle , & state -> data )) {
488
- iter -> entry_name = state -> data .cFileName ;
481
+ if (FindNextFile (iter -> state -> handle , & iter -> state -> data )) {
482
+ iter -> entry_name = iter -> state -> data .cFileName ;
489
483
return true;
490
484
}
491
- FindClose (state -> handle );
492
485
#else
493
- struct dirent * entry = readdir (state -> dir );
486
+ struct dirent * entry = readdir (iter -> state -> dir );
494
487
if (NULL != entry ) {
495
488
iter -> entry_name = entry -> d_name ;
496
489
return true;
@@ -511,17 +504,17 @@ rcutils_dir_iter_end(rcutils_dir_iter_t * iter)
511
504
rcutils_allocator_t allocator = iter -> allocator ;
512
505
RCUTILS_CHECK_ALLOCATOR_WITH_MSG (
513
506
& allocator , "allocator is invalid" , return );
514
- rcutils_dir_iter_state_t * state = ( rcutils_dir_iter_state_t * ) iter -> state ;
515
- if (NULL != state ) {
507
+
508
+ if (NULL != iter -> state ) {
516
509
#ifdef _WIN32
517
- FindClose (state -> handle );
510
+ FindClose (iter -> state -> handle );
518
511
#else
519
- if (NULL != state -> dir ) {
520
- closedir (state -> dir );
512
+ if (NULL != iter -> state -> dir ) {
513
+ closedir (iter -> state -> dir );
521
514
}
522
515
#endif
523
516
524
- allocator .deallocate (state , allocator .state );
517
+ allocator .deallocate (iter -> state , allocator .state );
525
518
}
526
519
527
520
allocator .deallocate (iter , allocator .state );
@@ -540,7 +533,3 @@ rcutils_get_file_size(const char * file_path)
540
533
int rc = stat (file_path , & stat_buffer );
541
534
return rc == 0 ? (size_t )(stat_buffer .st_size ) : 0 ;
542
535
}
543
-
544
- #ifdef __cplusplus
545
- }
546
- #endif
0 commit comments