Skip to content

Commit 363c0cc

Browse files
committed
Resync
1 parent 9213fac commit 363c0cc

3 files changed

Lines changed: 74 additions & 112 deletions

File tree

file/file_path.c

Lines changed: 50 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,25 @@ size_t fill_pathname(char *s, const char *in_path,
353353
**/
354354
char *find_last_slash(const char *str)
355355
{
356-
const char *slash = strrchr(str, '/');
357-
const char *backslash = strrchr(str, '\\');
358-
char *last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
359-
return last_slash;
356+
const char *p;
357+
const char *last_slash = NULL;
358+
const char *last_backslash = NULL;
359+
360+
/* Traverse the string once */
361+
for (p = str; *p != '\0'; ++p)
362+
{
363+
if (*p == '/')
364+
last_slash = p; /* Update last forward slash */
365+
else if (*p == '\\')
366+
last_backslash = p; /* Update last backslash */
367+
}
368+
369+
/* Determine which one is last */
370+
if (!last_slash) /* Backslash */
371+
return (char*)last_backslash;
372+
if (!last_backslash) /* Forward slash */
373+
return (char*)last_slash;
374+
return (last_backslash > last_slash) ? (char*)last_backslash : (char*)last_slash;
360375
}
361376

362377
/**
@@ -369,14 +384,15 @@ char *find_last_slash(const char *str)
369384
**/
370385
size_t fill_pathname_slash(char *s, size_t len)
371386
{
372-
const char *slash = strrchr(s, '/');
373-
const char *backslash = strrchr(s, '\\');
374-
char *last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
387+
char *last_slash = find_last_slash(s);
388+
len = strlen(s);
375389
if (!last_slash)
376-
return strlcat(s, PATH_DEFAULT_SLASH(), len);
377-
len = strlen(s);
390+
{
391+
s[ len] = PATH_DEFAULT_SLASH_C();
392+
s[++len] = '\0';
393+
}
378394
/* Try to preserve slash type. */
379-
if (last_slash != (s + len - 1))
395+
else if (last_slash != (s + len - 1))
380396
{
381397
s[ len] = last_slash[0];
382398
s[++len] = '\0';
@@ -462,16 +478,12 @@ size_t fill_pathname_parent_dir_name(char *s, const char *in_dir, size_t len)
462478
{
463479
size_t _len = 0;
464480
char *tmp = strdup(in_dir);
465-
const char *slash = strrchr(tmp, '/');
466-
const char *backslash = strrchr(tmp, '\\');
467-
char *last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
481+
char *last_slash = find_last_slash(tmp);
468482

469483
if (last_slash && last_slash[1] == 0)
470484
{
471485
*last_slash = '\0';
472-
slash = strrchr(tmp, '/');
473-
backslash = strrchr(tmp, '\\');
474-
last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
486+
last_slash = find_last_slash(tmp);
475487
}
476488

477489
/* Cut the last part of the string (the filename) after the slash,
@@ -482,9 +494,7 @@ size_t fill_pathname_parent_dir_name(char *s, const char *in_dir, size_t len)
482494
/* Point in_dir to the address of the last slash.
483495
* If in_dir is NULL, it means there was no slash in tmp,
484496
* so use tmp as-is. */
485-
slash = strrchr(tmp, '/');
486-
backslash = strrchr(tmp, '\\');
487-
in_dir = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
497+
in_dir = find_last_slash(tmp);
488498
if (!in_dir)
489499
in_dir = tmp;
490500

@@ -592,23 +602,19 @@ size_t fill_str_dated_filename(char *s,
592602
**/
593603
size_t path_basedir(char *s)
594604
{
595-
const char *slash;
596-
const char *backslash;
597605
char *last_slash = NULL;
598606
if (!s || s[0] == '\0' || s[1] == '\0')
599607
return (s && s[0] != '\0') ? 1 : 0;
600-
slash = strrchr(s, '/');
601-
backslash = strrchr(s, '\\');
602-
last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
603-
if (last_slash)
608+
last_slash = find_last_slash(s);
609+
if (!last_slash)
604610
{
605-
last_slash[1] = '\0';
606-
return last_slash + 1 - s;
611+
s[0] = '.';
612+
s[1] = PATH_DEFAULT_SLASH_C();
613+
s[2] = '\0';
614+
return 2;
607615
}
608-
s[0] = '.';
609-
s[1] = PATH_DEFAULT_SLASH_C();
610-
s[2] = '\0';
611-
return 2;
616+
last_slash[1] = '\0';
617+
return last_slash + 1 - s;
612618
}
613619

614620
/**
@@ -630,15 +636,9 @@ size_t path_parent_dir(char *s, size_t len)
630636
if (len && PATH_CHAR_IS_SLASH(s[len - 1]))
631637
{
632638
char *last_slash;
633-
const char *slash;
634-
const char *backslash;
635639
bool was_absolute = path_is_absolute(s);
636-
637640
s[len - 1] = '\0';
638-
639-
slash = strrchr(s, '/');
640-
backslash = strrchr(s, '\\');
641-
last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
641+
last_slash = find_last_slash(s);
642642

643643
if (was_absolute && !last_slash)
644644
{
@@ -667,9 +667,7 @@ const char *path_basename(const char *path)
667667
/* We cut either at the first compression-related hash,
668668
* or we cut at the last slash */
669669
const char *ptr = NULL;
670-
const char *slash = strrchr(path, '/');
671-
const char *backslash = strrchr(path, '\\');
672-
char *last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
670+
char *last_slash = find_last_slash(path);
673671
return ((ptr = path_get_archive_delim(path)) || (ptr = last_slash))
674672
? (ptr + 1) : path;
675673
}
@@ -687,9 +685,7 @@ const char *path_basename(const char *path)
687685
const char *path_basename_nocompression(const char *path)
688686
{
689687
/* We cut at the last slash */
690-
const char *slash = strrchr(path, '/');
691-
const char *backslash = strrchr(path, '\\');
692-
char *last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
688+
char *last_slash = find_last_slash(path);
693689
return (last_slash) ? (last_slash + 1) : path;
694690
}
695691

@@ -999,22 +995,18 @@ size_t fill_pathname_join_special(char *s,
999995

1000996
if (*s)
1001997
{
1002-
const char *slash = strrchr(s, '/');
1003-
const char *backslash = strrchr(s, '\\');
1004-
char *last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
1005-
if (last_slash)
998+
char *last_slash = find_last_slash(s);
999+
if (!last_slash)
10061000
{
1007-
/* Try to preserve slash type. */
1008-
if (last_slash != (s + _len - 1))
1009-
{
1010-
s[ _len] = last_slash[0];
1011-
s[++_len] = '\0';
1012-
}
1001+
s[ _len] = PATH_DEFAULT_SLASH_C();
1002+
s[++_len] = '\0';
1003+
10131004
}
1014-
else
1005+
/* Try to preserve slash type. */
1006+
else if (last_slash != (s + _len - 1))
10151007
{
1016-
s[ _len] = PATH_DEFAULT_SLASH_C();
1017-
s[++_len] = '\0';
1008+
s[ _len] = last_slash[0];
1009+
s[++_len] = '\0';
10181010
}
10191011
}
10201012

@@ -1327,8 +1319,6 @@ size_t fill_pathname_abbreviated_or_relative(char *s,
13271319
**/
13281320
void path_basedir_wrapper(char *s)
13291321
{
1330-
const char *slash;
1331-
const char *backslash;
13321322
char *last_slash = NULL;
13331323
if (!s || s[0] == '\0' || s[1] == '\0')
13341324
return;
@@ -1337,9 +1327,7 @@ void path_basedir_wrapper(char *s)
13371327
if ((last_slash = (char*)path_get_archive_delim(s)))
13381328
*last_slash = '\0';
13391329
#endif
1340-
slash = strrchr(s, '/');
1341-
backslash = strrchr(s, '\\');
1342-
last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
1330+
last_slash = find_last_slash(s);
13431331
if (last_slash)
13441332
last_slash[1] = '\0';
13451333
else

include/file/file_path.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,6 @@ void path_basedir_wrapper(char *s);
628628
* if not already there.
629629
630630
* Hidden non-leaf function cost:
631-
* - can call strlcat once if it returns false
632631
* - calls strlen
633632
**/
634633
size_t fill_pathname_slash(char *s, size_t len);

lists/file_list.c

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,17 @@ static bool file_list_deinitialize_internal(file_list_t *list)
5757

5858
bool file_list_reserve(file_list_t *list, size_t nitems)
5959
{
60-
const size_t item_size = sizeof(struct item_file);
6160
struct item_file *new_data;
61+
const size_t item_size = sizeof(struct item_file);
6262

63-
if (nitems < list->capacity || nitems > (size_t)-1/item_size)
63+
if (nitems < list->capacity || nitems > (size_t)-1 / item_size)
6464
return false;
6565

66+
/* Allocate the new memory block */
6667
if (!(new_data = (struct item_file*)realloc(list->list, nitems * item_size)))
6768
return false;
6869

69-
memset(&new_data[list->capacity], 0, item_size * (nitems - list->capacity));
70+
memset(new_data + list->capacity, 0, (nitems - list->capacity) * item_size);
7071

7172
list->list = new_data;
7273
list->capacity = nitems;
@@ -77,57 +78,31 @@ bool file_list_reserve(file_list_t *list, size_t nitems)
7778
bool file_list_insert(file_list_t *list,
7879
const char *path, const char *label,
7980
unsigned type, size_t directory_ptr,
80-
size_t entry_idx,
81-
size_t idx)
81+
size_t entry_idx, size_t idx)
8282
{
83-
int i;
84-
83+
struct item_file *new_item;
8584
/* Expand file list if needed */
86-
if (list->size >= list->capacity)
87-
if (!file_list_reserve(list, list->capacity * 2 + 1))
88-
return false;
89-
90-
for (i = (unsigned)list->size; i > (int)idx; i--)
91-
{
92-
struct item_file *copy = (struct item_file*)
93-
malloc(sizeof(struct item_file));
94-
95-
if (copy)
96-
{
97-
copy->path = NULL;
98-
copy->label = NULL;
99-
copy->alt = NULL;
100-
copy->type = 0;
101-
copy->directory_ptr = 0;
102-
copy->entry_idx = 0;
103-
copy->userdata = NULL;
104-
copy->actiondata = NULL;
105-
106-
memcpy(copy, &list->list[i-1], sizeof(struct item_file));
107-
108-
memcpy(&list->list[i-1], &list->list[i], sizeof(struct item_file));
109-
memcpy(&list->list[i], copy, sizeof(struct item_file));
110-
111-
free(copy);
112-
}
113-
}
114-
115-
list->list[idx].path = NULL;
116-
list->list[idx].label = NULL;
117-
list->list[idx].alt = NULL;
118-
list->list[idx].type = type;
119-
list->list[idx].directory_ptr = directory_ptr;
120-
list->list[idx].entry_idx = entry_idx;
121-
list->list[idx].userdata = NULL;
122-
list->list[idx].actiondata = NULL;
85+
if ( list->size >= list->capacity
86+
&& !file_list_reserve(list, list->capacity * 2 + 1))
87+
return false;
12388

124-
if (label)
125-
list->list[idx].label = strdup(label);
126-
if (path)
127-
list->list[idx].path = strdup(path);
89+
/* Shift elements to make room for the new item */
90+
memmove(&list->list[idx + 1],
91+
&list->list[idx],
92+
(list->size - idx) * sizeof(struct item_file));
93+
94+
/* Initialize the new item */
95+
new_item = &list->list[idx];
96+
new_item->path = path ? strdup(path) : NULL;
97+
new_item->label = label ? strdup(label) : NULL;
98+
new_item->alt = NULL;
99+
new_item->type = type;
100+
new_item->directory_ptr = directory_ptr;
101+
new_item->entry_idx = entry_idx;
102+
new_item->userdata = NULL;
103+
new_item->actiondata = NULL;
128104

129105
list->size++;
130-
131106
return true;
132107
}
133108

0 commit comments

Comments
 (0)