Skip to content

Commit 0aa6cca

Browse files
committed
refactor(common): move ocre_is_valid_id
We will need it outside of OcreCore Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
1 parent b78ceb9 commit 0aa6cca

7 files changed

Lines changed: 60 additions & 43 deletions

File tree

src/common/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,18 @@ add_custom_command(
4444
COMMAND cmake -E echo
4545
)
4646

47-
add_library(OcreCommon INTERFACE)
47+
add_library(OcreCommon)
4848

4949
# if we are not in a git repository, just fail automatically if we are missing commit_id.h
5050
target_sources(OcreCommon
5151
PRIVATE
52+
common.c
5253
include/ocre/build_info.h
5354
include/ocre/commit_id.h
5455
)
5556

5657
target_include_directories(OcreCommon
57-
INTERFACE
58+
PUBLIC
5859
include
5960
${CMAKE_CURRENT_BINARY_DIR}/include
6061
)

src/common/common.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <stddef.h>
2+
#include <ctype.h>
3+
#include <string.h>
4+
5+
int ocre_is_valid_id(const char *id)
6+
{
7+
/* Cannot be NULL */
8+
9+
if (!id) {
10+
return 0;
11+
}
12+
13+
/* Cannot be empty or start with a dot '.' */
14+
15+
if (id[0] == '\0' || id[0] == '.') {
16+
return 0;
17+
}
18+
19+
/* Can only contain alphanumeric characters, dots, underscores, and hyphens */
20+
21+
for (size_t i = 0; i < strlen(id); i++) {
22+
if ((isalnum((int)id[i]) && islower((int)id[i])) || id[i] == '.' || id[i] == '_' || id[i] == '-') {
23+
continue;
24+
}
25+
26+
return 0;
27+
}
28+
29+
return 1;
30+
}

src/common/include/ocre/common.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @copyright Copyright (c) contributors to Project Ocre,
3+
* which has been established as Project Ocre a Series of LF Projects, LLC
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#ifndef OCRE_COMMON_H
9+
#define OCRE_COMMON_H
10+
11+
/**
12+
* @brief Check if a container or image ID is valid
13+
* @memberof ocre_context
14+
*
15+
* Checks if a container or image ID is valid. A valid container or image ID must not be NULL, empty, or start with a
16+
* dot '.'. It can only contain alphanumeric characters, dots, underscores, and hyphens.
17+
*
18+
* @param id A pointer to the container or image ID to check
19+
*
20+
* @return Zero if invalid, 1 if valid
21+
*/
22+
int ocre_is_valid_id(const char *id);
23+
24+
#endif /* OCRE_COMMON_H */

src/ocre/include/ocre/library.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,4 @@ int ocre_destroy_context(struct ocre_context *context);
8787
*/
8888
void ocre_deinitialize(void);
8989

90-
/**
91-
* @brief Check if a container or image ID is valid
92-
* @memberof ocre_context
93-
*
94-
* Checks if a container or image ID is valid. A valid container or image ID must not be NULL, empty, or start with a
95-
* dot '.'. It can only contain alphanumeric characters, dots, underscores, and hyphens.
96-
*
97-
* @param id A pointer to the container or image ID to check
98-
*
99-
* @return Zero if invalid, 1 if valid
100-
*/
101-
int ocre_is_valid_id(const char *id);
102-
10390
#endif /* OCRE_LIBRARY_H */

src/ocre/include/ocre/ocre.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
* See ocre_runtime_vtable class for documentation.
5252
*/
5353

54+
#include <ocre/common.h>
5455
#include <ocre/library.h>
5556
#include <ocre/context.h>
5657
#include <ocre/container.h>

src/ocre/ocre.c

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <errno.h>
1010
#include <stdlib.h>
1111
#include <strings.h>
12-
#include <ctype.h>
1312
#include <dirent.h>
1413
#include <pthread.h>
1514
#include <sys/stat.h>
@@ -452,30 +451,3 @@ void ocre_deinitialize(void)
452451
free(r_elt);
453452
}
454453
}
455-
456-
int ocre_is_valid_id(const char *id)
457-
{
458-
/* Cannot be NULL */
459-
460-
if (!id) {
461-
return 0;
462-
}
463-
464-
/* Cannot be empty or start with a dot '.' */
465-
466-
if (id[0] == '\0' || id[0] == '.') {
467-
return 0;
468-
}
469-
470-
/* Can only contain alphanumeric characters, dots, underscores, and hyphens */
471-
472-
for (size_t i = 0; i < strlen(id); i++) {
473-
if ((isalnum(id[i]) && islower(id[i])) || id[i] == '.' || id[i] == '_' || id[i] == '-') {
474-
continue;
475-
}
476-
477-
return 0;
478-
}
479-
480-
return 1;
481-
}

zephyr/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ endif()
3232
if(CONFIG_OCRE_STORAGE_PARTITION)
3333
include(storage_partition.cmake)
3434
endif()
35+
36+
target_link_libraries(OcreCommon PUBLIC zephyr_interface)

0 commit comments

Comments
 (0)