Skip to content

Commit

Permalink
aws_string_c_str() (#534)
Browse files Browse the repository at this point in the history
  • Loading branch information
graebm authored Oct 28, 2019
1 parent f2ff1a8 commit e3e7ccd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
6 changes: 6 additions & 0 deletions include/aws/common/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ int aws_secure_strlen(const char *str, size_t max_read_len, size_t *str_len);
AWS_STATIC_IMPL
const uint8_t *aws_string_bytes(const struct aws_string *str);

/**
* Equivalent to `(const char *)str->bytes`.
*/
AWS_STATIC_IMPL
const char *aws_string_c_str(const struct aws_string *str);

/**
* Evaluates the set of properties that define the shape of all valid aws_string structures.
* It is also a cheap check, in the sense it run in constant time (i.e., no loops or recursion).
Expand Down
9 changes: 9 additions & 0 deletions include/aws/common/string.inl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ const uint8_t *aws_string_bytes(const struct aws_string *str) {
return str->bytes;
}

/**
* Equivalent to `(const char *)str->bytes`.
*/
AWS_STATIC_IMPL
const char *aws_string_c_str(const struct aws_string *str) {
AWS_PRECONDITION(aws_string_is_valid(str));
return (const char *)str->bytes;
}

/**
* Evaluates the set of properties that define the shape of all valid aws_string structures.
* It is also a cheap check, in the sense it run in constant time (i.e., no loops or recursion).
Expand Down
6 changes: 3 additions & 3 deletions source/posix/environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int aws_get_environment_value(
const struct aws_string *variable_name,
struct aws_string **value_out) {

const char *value = getenv((const char *)variable_name->bytes);
const char *value = getenv(aws_string_c_str(variable_name));
if (value == NULL) {
*value_out = NULL;
return AWS_OP_SUCCESS;
Expand All @@ -39,15 +39,15 @@ int aws_get_environment_value(

int aws_set_environment_value(const struct aws_string *variable_name, const struct aws_string *value) {

if (setenv((const char *)variable_name->bytes, (const char *)value->bytes, 1) != 0) {
if (setenv(aws_string_c_str(variable_name), aws_string_c_str(value), 1) != 0) {
return aws_raise_error(AWS_ERROR_ENVIRONMENT_SET);
}

return AWS_OP_SUCCESS;
}

int aws_unset_environment_value(const struct aws_string *variable_name) {
if (unsetenv((const char *)variable_name->bytes) != 0) {
if (unsetenv(aws_string_c_str(variable_name)) != 0) {
return aws_raise_error(AWS_ERROR_ENVIRONMENT_UNSET);
}

Expand Down
6 changes: 3 additions & 3 deletions source/windows/environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int aws_get_environment_value(
#pragma warning(push)
#pragma warning(disable : 4996)

const char *value = getenv((const char *)variable_name->bytes);
const char *value = getenv(aws_string_c_str(variable_name));

#pragma warning(pop)

Expand All @@ -44,7 +44,7 @@ int aws_get_environment_value(
}

int aws_set_environment_value(const struct aws_string *variable_name, const struct aws_string *value) {
if (_putenv_s((const char *)variable_name->bytes, (const char *)value->bytes) != 0) {
if (_putenv_s(aws_string_c_str(variable_name), aws_string_c_str(value)) != 0) {
return aws_raise_error(AWS_ERROR_ENVIRONMENT_SET);
}

Expand All @@ -54,7 +54,7 @@ int aws_set_environment_value(const struct aws_string *variable_name, const stru
AWS_STATIC_STRING_FROM_LITERAL(s_empty_string, "");

int aws_unset_environment_value(const struct aws_string *variable_name) {
if (_putenv_s((const char *)variable_name->bytes, (const char *)s_empty_string->bytes) != 0) {
if (_putenv_s(aws_string_c_str(variable_name), aws_string_c_str(s_empty_string)) != 0) {
return aws_raise_error(AWS_ERROR_ENVIRONMENT_UNSET);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/logging/log_formatter_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int do_default_log_formatter_test(
aws_log_formatter_clean_up(&formatter);

char buffer[TEST_FORMATTER_MAX_BUFFER_SIZE];
snprintf(buffer, TEST_FORMATTER_MAX_BUFFER_SIZE, "%s", (const char *)output->bytes);
snprintf(buffer, TEST_FORMATTER_MAX_BUFFER_SIZE, "%s", aws_string_c_str(output));

aws_string_destroy(output);

Expand Down

0 comments on commit e3e7ccd

Please sign in to comment.