Skip to content

Commit 282887d

Browse files
committed
Review of Thomas PR.
1 parent b4d6730 commit 282887d

File tree

4 files changed

+148
-69
lines changed

4 files changed

+148
-69
lines changed

source/environment/include/environment/environment_variable_path.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,19 @@ extern "C" {
5454

5555
/**
5656
* @brief
57-
* If the value of `name` exists as an environment variable, return a live string of its value, otherwise return a live value of `default_path` or "/".
57+
* If the value of @name exists as an environment variable, return a live string of its value, otherwise return a live value of @default_path or "/".
58+
* @name should not be NULL.
59+
* If @default_path is not NULL, @default_path_size must be set to <= the length (including null-terminator) of the @default_path string.
60+
* If @env_size is not NULL, the length (including null-terminator) of the returned string will be set to it.
5861
*
59-
* `name` should not be `NULL`.
60-
*
61-
* If `default_path` is not `NULL`, `default_path_size` must be set to <= the length (including 0-terminator) of the `default_path` string.
62-
*
63-
* If `env_size` is not `NULL`, the length (including 0-terminator) of the returned string will be set to it
6462
* @param[in] name
6563
* The environment variable name to look up.
6664
*
6765
* @param[in] default_path
6866
* If the environment variable value is not found, the value to return instead.
6967
*
7068
* @param[in] default_path_size
71-
* The length (including 0-terminator) of `default_path` in chars.
69+
* The length (including null-terminator) of @default_path in chars.
7270
*
7371
* @param[out] env_size
7472
* Pointer to a size_t to write the length of the returned string to (optional).

source/environment/source/environment_variable_path.c

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,53 @@
4545

4646
char *environment_variable_path_create(const char *const name, const char *const default_path, const size_t default_path_size, size_t *const env_size)
4747
{
48-
const char *value = getenv(name);
49-
size_t size;
50-
if (value)
51-
size = strlen(value) + 1;
48+
const char *env_variable = getenv(name);
49+
char *path;
50+
size_t size, alloc_size;
51+
52+
if (env_variable)
53+
{
54+
size = strlen(env_variable) + 1;
55+
}
5256
else if (default_path)
5357
{
54-
value = default_path;
58+
env_variable = default_path;
5559
size = default_path_size;
5660
}
5761
else
5862
{
59-
value = ENVIRONMENT_VARIABLE_PATH_SEPARATOR_STR;
63+
env_variable = ENVIRONMENT_VARIABLE_PATH_SEPARATOR_STR;
6064
size = sizeof(ENVIRONMENT_VARIABLE_PATH_SEPARATOR_STR);
6165
}
62-
size_t alloc_size = size;
66+
67+
alloc_size = size;
68+
6369
if (size > 1)
64-
alloc_size += !ENVIRONMENT_VARIABLE_PATH_SEPARATOR(value[size - 2]);
65-
char *const path = malloc(sizeof(char) * alloc_size);
66-
memcpy(path, value, sizeof(char) * size);
70+
{
71+
alloc_size += !ENVIRONMENT_VARIABLE_PATH_SEPARATOR(env_variable[size - 2]);
72+
}
73+
74+
path = malloc(sizeof(char) * alloc_size);
75+
76+
if (path == NULL)
77+
{
78+
return NULL;
79+
}
80+
81+
memcpy(path, env_variable, sizeof(char) * size);
82+
6783
if (size > 1)
84+
{
6885
path[alloc_size - 2] = ENVIRONMENT_VARIABLE_PATH_SEPARATOR_C;
86+
}
87+
6988
path[alloc_size - 1] = '\0';
89+
7090
if (env_size)
91+
{
7192
*env_size = alloc_size;
93+
}
94+
7295
return path;
7396
}
7497

source/portability/include/portability/portability_path.h

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,22 @@ extern "C" {
112112
/**
113113
* @brief
114114
* Get the file name portion out of a path and strip away the file extension if any.
115+
* If @path is NULL this will return an empty string.
116+
* If @name is NULL or @name_size is 0 this will return the size it requires in order to write @name.
117+
* If @path or @name are not NULL, then @path_size or @name_size, respectively, must be set to <= the length (including null-terminator) of the memory regions pointed to by @path and @name.
115118
*
116-
* If `path` is NULL this will return an empty string.
117-
*
118-
* If `name` is NULL or `name_size is 0 this will return the size it requires in order to write `name`.
119-
*
120-
* If `path` or `name` are not NULL, then `path_size` or `name_size`, respectively, must be set to <= the length (including 0-terminator) of the memory regions pointed to by `path` and `name`.
121119
* @param[in] path
122120
* The full path to extract the name from.
121+
*
123122
* @param[in] path_size
124-
* The length (including 0-terminator) of `path` in chars.
123+
* The length (including null-terminator) of @path in chars.
124+
*
125125
* @param[out] name
126-
* The memory location to write the extracted name to. If `NULL` the size required will be returned instead of the size written.
126+
* The memory location to write the extracted name to. If NULL the size required will be returned instead of the size written.
127+
*
127128
* @param[in] name_size
128-
* The size of the memory location pointed to by `name`.
129+
* The size of the memory location pointed to by @name.
130+
*
129131
* @return
130132
* The size of the name.
131133
*/
@@ -134,26 +136,27 @@ PORTABILITY_API size_t portability_path_get_name(const char *const path, const s
134136
/**
135137
* @brief
136138
* Get the file name portion out of a path and strip away any amount of file extensions.
137-
*
138139
* When called with `"/foo/bar.baz.qux"`:
139140
*
140141
* - `portability_path_get_name` will produce the string `"bar.baz"`
141-
*
142142
* - `portability_path_get_name_canonical` will produce the string `"bar"`
143143
*
144-
* If `path` is NULL this will return an empty string.
145-
*
146-
* If `name` is NULL or `name_size is 0 this will return the size it requires in order to write `name`.
144+
* If @path is NULL this will return an empty string.
145+
* If @name is NULL or @name_size is 0 this will return the size it requires in order to write @name.
146+
* If @path or @name are not NULL, then @path_size or @name_size, respectively, must be set to <= the length (including null-terminator) of the memory regions pointed to by @path and @name.
147147
*
148-
* If `path` or `name` are not NULL, then `path_size` or `name_size`, respectively, must be set to <= the length (including 0-terminator) of the memory regions pointed to by `path` and `name`.
149148
* @param[in] path
150149
* The full path to extract the name from.
150+
*
151151
* @param[in] path_size
152-
* The length (including 0-terminator) of `path` in chars.
152+
* The length (including null-terminator) of @path in chars.
153+
*
153154
* @param[out] name
154-
* The memory location to write the extracted name to. If `NULL` the size required will be returned instead of the size written.
155+
* The memory location to write the extracted name to. If NULL the size required will be returned instead of the size written.
156+
*
155157
* @param[in] name_size
156-
* The size of the memory location pointed to by `name`.
158+
* The size of the memory location pointed to by @name.
159+
*
157160
* @return
158161
* The size of the name.
159162
*/

source/portability/source/portability_path.c

Lines changed: 90 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,80 +25,135 @@
2525
/* Define separator checking for any platform */
2626
#define PORTABILITY_PATH_SEPARATOR_ALL(chr) (chr == '\\' || chr == '/')
2727

28-
static size_t basename_offset(const char *const path, const size_t path_size)
28+
static size_t portability_path_basename_offset(const char *const path, const size_t path_size)
2929
{
3030
size_t offset = path_size;
31-
for (; offset != 0; offset--)
32-
if (PORTABILITY_PATH_SEPARATOR(path[offset - 1]))
33-
break;
31+
32+
while (offset > 0 && !PORTABILITY_PATH_SEPARATOR(path[offset - 1]))
33+
{
34+
--offset;
35+
}
36+
3437
return offset;
3538
}
3639

3740
size_t portability_path_get_name(const char *const path, const size_t path_size, char *const name, const size_t name_size)
3841
{
42+
size_t name_start, rightmost_dot, length, size;
43+
3944
if (path == NULL)
4045
{
4146
if (name == NULL || name_size == 0)
47+
{
4248
return 0;
49+
}
50+
4351
name[0] = '\0';
52+
4453
return 1;
4554
}
46-
// find rightmost path separator
47-
const size_t name_start = basename_offset(path, path_size);
48-
// Find rightmost dot
49-
size_t rightmost_dot = path_size;
50-
for (; rightmost_dot != name_start; rightmost_dot--)
51-
if (path[rightmost_dot - 1] == '.')
52-
break;
53-
// No dots found, or name starts with dot and is non-empty, use whole name
55+
56+
/* Find rightmost path separator */
57+
name_start = portability_path_basename_offset(path, path_size);
58+
59+
/* Find rightmost dot */
60+
rightmost_dot = path_size;
61+
62+
while (rightmost_dot != name_start && path[rightmost_dot - 1] != '.')
63+
{
64+
--rightmost_dot;
65+
}
66+
67+
/* No dots found, or name starts with dot and is non-empty, use whole name */
5468
if (rightmost_dot == name_start || (rightmost_dot == name_start + 1 && rightmost_dot != path_size - 1))
69+
{
5570
rightmost_dot = path_size - 1;
56-
// remove all consecutive dots at the end
71+
}
72+
73+
/* Remove all consecutive dots at the end */
5774
while (rightmost_dot != name_start && path[rightmost_dot - 1] == '.')
58-
rightmost_dot--;
59-
const size_t length = rightmost_dot - name_start;
60-
const size_t size = length + 1;
61-
// Return required size
75+
{
76+
--rightmost_dot;
77+
}
78+
79+
length = rightmost_dot - name_start;
80+
size = length + 1;
81+
82+
/* Return required size */
6283
if (name == NULL || size > name_size)
84+
{
6385
return size;
64-
if (length)
86+
}
87+
88+
if (length > 0)
89+
{
6590
memcpy(name, path + name_start, length);
91+
}
92+
6693
name[length] = '\0';
94+
6795
return size;
6896
}
6997

7098
size_t portability_path_get_name_canonical(const char *const path, const size_t path_size, char *const name, const size_t name_size)
7199
{
100+
size_t name_start, leftmost_dot, length, size;
101+
72102
if (path == NULL)
73103
{
74104
if (name == NULL || name_size == 0)
105+
{
75106
return 0;
107+
}
108+
76109
name[0] = '\0';
110+
77111
return 1;
78112
}
79-
// find rightmost path separator
80-
const size_t name_start = basename_offset(path, path_size);
81-
// find leftmost dot
82-
size_t leftmost_dot = name_start;
83-
for (; leftmost_dot < path_size; leftmost_dot++)
84-
if (path[leftmost_dot] == '.')
85-
break;
86-
// No dots found, use whole name
113+
114+
/* Find rightmost path separator */
115+
name_start = portability_path_basename_offset(path, path_size);
116+
117+
/* Find leftmost dot */
118+
leftmost_dot = name_start;
119+
120+
while (leftmost_dot < path_size && path[leftmost_dot] != '.')
121+
{
122+
++leftmost_dot;
123+
}
124+
125+
/* No dots found, use whole name */
87126
if (leftmost_dot == path_size)
88-
leftmost_dot--;
89-
// name starts with dot, use the following dot instead
127+
{
128+
--leftmost_dot;
129+
}
130+
131+
/* Name starts with dot, use the following dot instead */
90132
if (leftmost_dot == name_start)
91-
for (leftmost_dot = name_start + 1; leftmost_dot < path_size; leftmost_dot++)
92-
if (path[leftmost_dot] == '.')
93-
break;
94-
const size_t length = leftmost_dot - name_start;
95-
const size_t size = length + 1;
96-
// Return required size
133+
{
134+
do
135+
{
136+
++leftmost_dot;
137+
138+
} while (leftmost_dot < path_size && path[leftmost_dot] != '.');
139+
}
140+
141+
length = leftmost_dot - name_start;
142+
size = length + 1;
143+
144+
/* Return required size */
97145
if (name == NULL || size > name_size)
146+
{
98147
return size;
99-
if (length)
148+
}
149+
150+
if (length > 0)
151+
{
100152
memcpy(name, path + name_start, length);
153+
}
154+
101155
name[length] = '\0';
156+
102157
return size;
103158
}
104159

0 commit comments

Comments
 (0)