MCPcopy Create free account
hub / github.com/ObEngine/ObEngine / tinydir_open_sorted

Function tinydir_open_sorted

extlibs/tinydir/include/tinydir/tinydir.h:352–413  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

350}
351
352_TINYDIR_FUNC
353int tinydir_open_sorted(tinydir_dir *dir, const _tinydir_char_t *path)
354{
355 /* Count the number of files first, to pre-allocate the files array */
356 size_t n_files = 0;
357 if (tinydir_open(dir, path) == -1)
358 {
359 return -1;
360 }
361 while (dir->has_next)
362 {
363 n_files++;
364 if (tinydir_next(dir) == -1)
365 {
366 goto bail;
367 }
368 }
369 tinydir_close(dir);
370
371 if (n_files == 0 || tinydir_open(dir, path) == -1)
372 {
373 return -1;
374 }
375
376 dir->n_files = 0;
377 dir->_files = (tinydir_file *)_TINYDIR_MALLOC(sizeof *dir->_files * n_files);
378 if (dir->_files == NULL)
379 {
380 goto bail;
381 }
382 while (dir->has_next)
383 {
384 tinydir_file *p_file;
385 dir->n_files++;
386
387 p_file = &dir->_files[dir->n_files - 1];
388 if (tinydir_readfile(dir, p_file) == -1)
389 {
390 goto bail;
391 }
392
393 if (tinydir_next(dir) == -1)
394 {
395 goto bail;
396 }
397
398 /* Just in case the number of files has changed between the first and
399 second reads, terminate without writing into unallocated memory */
400 if (dir->n_files == n_files)
401 {
402 break;
403 }
404 }
405
406 qsort(dir->_files, dir->n_files, sizeof(tinydir_file), _tinydir_file_cmp);
407
408 return 0;
409

Callers 1

tinydir.hFile · 0.85

Calls 2

tinydir_nextFunction · 0.85
tinydir_closeFunction · 0.85

Tested by

no test coverage detected