Skip to content

Commit 1e34f65

Browse files
committed
[single] Introduce _is_valid_extension helper to simplify model validation
- Extracted the file extension checking logic into a new static helper function Signed-off-by: hyunil park <[email protected]>
1 parent b80c7f5 commit 1e34f65

File tree

1 file changed

+40
-33
lines changed

1 file changed

+40
-33
lines changed

c/src/ml-api-inference-single.c

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1997,6 +1997,30 @@ __ml_validate_model_file (const char *const *model,
19971997
return ML_ERROR_NONE;
19981998
}
19991999

2000+
/**
2001+
* @brief Internal helper to check if the file has one of the valid extensions.
2002+
* @return TRUE if valid, FALSE otherwise.
2003+
*/
2004+
static gboolean
2005+
_is_valid_extension (const char *filename, const char *const *valid_exts)
2006+
{
2007+
const char *dot;
2008+
2009+
if (!filename || !valid_exts)
2010+
return FALSE;
2011+
2012+
dot = strrchr (filename, '.');
2013+
if (!dot)
2014+
return FALSE;
2015+
2016+
for (; *valid_exts; valid_exts++) {
2017+
if (g_ascii_strcasecmp (dot, *valid_exts) == 0)
2018+
return TRUE;
2019+
}
2020+
2021+
return FALSE;
2022+
}
2023+
20002024
/**
20012025
* @brief Validates the nnfw model file.
20022026
* @since_tizen 5.5
@@ -2014,9 +2038,7 @@ _ml_validate_model_file (const char *const *model,
20142038
int status = ML_ERROR_NONE;
20152039
ml_nnfw_type_e detected = ML_NNFW_TYPE_ANY;
20162040
gboolean is_dir = FALSE;
2017-
gchar *pos, *fw_name;
2018-
gchar **file_ext = NULL;
2019-
guint i;
2041+
gchar *fw_name;
20202042

20212043
if (!nnfw)
20222044
_ml_error_report_return (ML_ERROR_INVALID_PARAMETER,
@@ -2060,19 +2082,6 @@ _ml_validate_model_file (const char *const *model,
20602082
goto done;
20612083
}
20622084

2063-
/* Handle mismatched case, check file extension. */
2064-
file_ext = g_malloc0 (sizeof (char *) * (num_models + 1));
2065-
for (i = 0; i < num_models; i++) {
2066-
if ((pos = strrchr (model[i], '.')) == NULL) {
2067-
_ml_error_report ("The given model [%d]=\"%s\" has invalid extension.", i,
2068-
model[i]);
2069-
status = ML_ERROR_INVALID_PARAMETER;
2070-
goto done;
2071-
}
2072-
2073-
file_ext[i] = g_ascii_strdown (pos, -1);
2074-
}
2075-
20762085
/** @todo Make sure num_models is correct for each nnfw type */
20772086
switch (*nnfw) {
20782087
case ML_NNFW_TYPE_NNFW:
@@ -2101,11 +2110,10 @@ _ml_validate_model_file (const char *const *model,
21012110
status = ML_ERROR_NOT_SUPPORTED;
21022111
break;
21032112
case ML_NNFW_TYPE_VD_AIFW:
2104-
if (!g_str_equal (file_ext[0], ".nb") &&
2105-
!g_str_equal (file_ext[0], ".ncp") &&
2106-
!g_str_equal (file_ext[0], ".tvn") &&
2107-
!g_str_equal (file_ext[0], ".bin")) {
2108-
status = ML_ERROR_INVALID_PARAMETER;
2113+
{
2114+
const char *exts[] = { ".nb", ".ncp", ".tvn", ".bin", NULL };
2115+
if (!_is_valid_extension (model[0], exts))
2116+
status = ML_ERROR_INVALID_PARAMETER;
21092117
}
21102118
break;
21112119
case ML_NNFW_TYPE_SNAP:
@@ -2116,20 +2124,20 @@ _ml_validate_model_file (const char *const *model,
21162124
/* SNAP requires multiple files, set supported if model file exists. */
21172125
break;
21182126
case ML_NNFW_TYPE_ARMNN:
2119-
if (!g_str_equal (file_ext[0], ".caffemodel") &&
2120-
!g_str_equal (file_ext[0], ".tflite") &&
2121-
!g_str_equal (file_ext[0], ".pb") &&
2122-
!g_str_equal (file_ext[0], ".prototxt")) {
2123-
_ml_error_report
2124-
("ARMNN accepts .caffemodel, .tflite, .pb, and .prototxt files only. Please support correct file extension. You have specified: \"%s\"",
2125-
file_ext[0]);
2126-
status = ML_ERROR_INVALID_PARAMETER;
2127+
{
2128+
const char *exts[] =
2129+
{ ".caffemodel", ".tflite", ".pb", ".prototxt", NULL };
2130+
if (!_is_valid_extension (model[0], exts)) {
2131+
_ml_error_report ("Invalid extension for ARMNN: %s", model[0]);
2132+
status = ML_ERROR_INVALID_PARAMETER;
2133+
}
21272134
}
21282135
break;
21292136
case ML_NNFW_TYPE_MXNET:
2130-
if (!g_str_equal (file_ext[0], ".params") &&
2131-
!g_str_equal (file_ext[0], ".json")) {
2132-
status = ML_ERROR_INVALID_PARAMETER;
2137+
{
2138+
const char *exts[] = { ".params", ".json", NULL };
2139+
if (!_is_valid_extension (model[0], exts))
2140+
status = ML_ERROR_INVALID_PARAMETER;
21332141
}
21342142
break;
21352143
default:
@@ -2153,6 +2161,5 @@ _ml_validate_model_file (const char *const *model,
21532161
model[0], num_models);
21542162
}
21552163

2156-
g_strfreev (file_ext);
21572164
return status;
21582165
}

0 commit comments

Comments
 (0)