David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include <stdbool.h> |
| 20 | #include <stddef.h> |
| 21 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 22 | #include "hf/memiter.h" |
| 23 | |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 24 | /** |
| 25 | * Maximum length of a string including the NULL terminator. |
| 26 | * This is an arbitrary number and can be adjusted to fit use cases. |
| 27 | */ |
| 28 | #define STRING_MAX_SIZE 32 |
| 29 | |
| 30 | enum string_return_code { |
| 31 | STRING_SUCCESS, |
| 32 | STRING_ERROR_INVALID_INPUT, |
| 33 | STRING_ERROR_TOO_LONG, |
| 34 | }; |
| 35 | |
| 36 | /** |
| 37 | * Statically-allocated string data structure with input validation to ensure |
| 38 | * strings are properly NULL-terminated. |
| 39 | * |
| 40 | * This is intentionally kept as simple as possible and should not be extended |
| 41 | * to perform complex string operations without a good use case. |
| 42 | */ |
| 43 | struct string { |
| 44 | char data[STRING_MAX_SIZE]; |
| 45 | }; |
| 46 | |
| 47 | /** |
| 48 | * Macro to initialize `struct string` from a string constant. |
| 49 | * Triggers a compilation error if the string does not fit into the buffer. |
| 50 | */ |
| 51 | #define STRING_INIT(str) ((struct string){.data = str}) |
| 52 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 53 | enum string_return_code string_init(struct string *str, |
| 54 | const struct memiter *data); |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 55 | void string_init_empty(struct string *str); |
| 56 | bool string_is_empty(const struct string *str); |
| 57 | const char *string_data(const struct string *str); |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame^] | 58 | bool string_eq(const struct string *str, const struct memiter *data); |