gmio_core: add new internal functions
gmio_fileutils_find_basefilename() gmio_const_string_concat()
This commit is contained in:
parent
4f319710be
commit
6897ca12d5
48
src/gmio_core/internal/const_string.c
Normal file
48
src/gmio_core/internal/const_string.c
Normal file
@ -0,0 +1,48 @@
|
||||
/****************************************************************************
|
||||
** Copyright (c) 2016, Fougue Ltd. <http://www.fougue.pro>
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
**
|
||||
** 2. Redistributions in binary form must reproduce the above
|
||||
** copyright notice, this list of conditions and the following
|
||||
** disclaimer in the documentation and/or other materials provided
|
||||
** with the distribution.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "const_string.h"
|
||||
#include "min_max.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
size_t gmio_const_string_concat(
|
||||
char* dst, size_t dst_capacity,
|
||||
const struct gmio_const_string* lhs,
|
||||
const struct gmio_const_string* rhs)
|
||||
{
|
||||
const size_t capacity_sub_rhs = dst_capacity - rhs->len - 1;
|
||||
const size_t lhs_copy_len = GMIO_MIN(capacity_sub_rhs, lhs->len);
|
||||
strncpy(dst, lhs->ptr, lhs_copy_len);
|
||||
const size_t dst_remaining_cap = dst_capacity - lhs_copy_len;
|
||||
const size_t rhs_copy_len = GMIO_MIN(dst_remaining_cap, rhs->len);
|
||||
strncpy(dst + lhs_copy_len, rhs->ptr, rhs_copy_len);
|
||||
dst[lhs_copy_len + rhs_copy_len] = '\0';
|
||||
return lhs_copy_len + rhs_copy_len;
|
||||
}
|
@ -59,6 +59,13 @@ GMIO_INLINE struct gmio_const_string gmio_const_string(const char* ptr, size_t l
|
||||
/*! Returns \c true if \p str has no characters, otherwise returns \c false */
|
||||
GMIO_INLINE bool gmio_const_string_is_empty(const struct gmio_const_string* str);
|
||||
|
||||
/*! Concatenates \p lhs + \p rhs into destination string \p dst and returns
|
||||
* total string length */
|
||||
size_t gmio_const_string_concat(
|
||||
char* dst, size_t dst_capacity,
|
||||
const struct gmio_const_string* lhs,
|
||||
const struct gmio_const_string* rhs);
|
||||
|
||||
/*
|
||||
* -- Implementation
|
||||
*/
|
||||
|
60
src/gmio_core/internal/file_utils.c
Normal file
60
src/gmio_core/internal/file_utils.c
Normal file
@ -0,0 +1,60 @@
|
||||
/****************************************************************************
|
||||
** Copyright (c) 2016, Fougue Ltd. <http://www.fougue.pro>
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
**
|
||||
** 2. Redistributions in binary form must reproduce the above
|
||||
** copyright notice, this list of conditions and the following
|
||||
** disclaimer in the documentation and/or other materials provided
|
||||
** with the distribution.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "file_utils.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
struct gmio_const_string gmio_fileutils_find_basefilename(const char* filepath)
|
||||
{
|
||||
struct gmio_const_string basefilename = {0};
|
||||
const size_t filepath_len = filepath != NULL ? strlen(filepath) : 0;
|
||||
if (filepath_len == 0)
|
||||
return basefilename;
|
||||
|
||||
const char* const pos_filepath_begin = filepath;
|
||||
const char* const pos_filepath_end = filepath + filepath_len;
|
||||
const char* pos_last_dot = pos_filepath_end;
|
||||
const char* it = filepath_len != 0 ? pos_filepath_end - 1 : NULL;
|
||||
while (it != pos_filepath_begin
|
||||
&& *it != '/'
|
||||
&& *it != '\\')
|
||||
{
|
||||
if (*it == '.' && pos_last_dot == pos_filepath_end)
|
||||
pos_last_dot = it;
|
||||
--it;
|
||||
}
|
||||
it = it != pos_filepath_begin ? it + 1 : it;
|
||||
basefilename.ptr = it;
|
||||
basefilename.len =
|
||||
filepath_len
|
||||
- (it - pos_filepath_begin) /* complete filename */
|
||||
- (pos_filepath_end - pos_last_dot); /* filename suffix */
|
||||
return basefilename;
|
||||
}
|
40
src/gmio_core/internal/file_utils.h
Normal file
40
src/gmio_core/internal/file_utils.h
Normal file
@ -0,0 +1,40 @@
|
||||
/****************************************************************************
|
||||
** Copyright (c) 2016, Fougue Ltd. <http://www.fougue.pro>
|
||||
** All rights reserved.
|
||||
**
|
||||
** Redistribution and use in source and binary forms, with or without
|
||||
** modification, are permitted provided that the following conditions
|
||||
** are met:
|
||||
**
|
||||
** 1. Redistributions of source code must retain the above copyright
|
||||
** notice, this list of conditions and the following disclaimer.
|
||||
**
|
||||
** 2. Redistributions in binary form must reproduce the above
|
||||
** copyright notice, this list of conditions and the following
|
||||
** disclaimer in the documentation and/or other materials provided
|
||||
** with the distribution.
|
||||
**
|
||||
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef GMIO_INTERNAL_FILE_UTILS_H
|
||||
#define GMIO_INTERNAL_FILE_UTILS_H
|
||||
|
||||
#include "const_string.h"
|
||||
|
||||
/*! Returns the base name of the file without the path.
|
||||
* The base name consists of all characters in the file up to (but not including)
|
||||
* the last '.' character. */
|
||||
struct gmio_const_string gmio_fileutils_find_basefilename(const char* filepath);
|
||||
|
||||
#endif /* GMIO_INTERNAL_FILE_UTILS_H */
|
@ -48,6 +48,7 @@ const char* all_tests()
|
||||
|
||||
UTEST_RUN(test_internal__byte_swap);
|
||||
UTEST_RUN(test_internal__byte_codec);
|
||||
UTEST_RUN(test_internal__const_string);
|
||||
UTEST_RUN(test_internal__fast_atof);
|
||||
UTEST_RUN(test_internal__locale_utils);
|
||||
UTEST_RUN(test_internal__error_check);
|
||||
@ -58,6 +59,7 @@ const char* all_tests()
|
||||
UTEST_RUN(test_internal__benchmark_gmio_fast_atof);
|
||||
UTEST_RUN(test_internal__zip_utils);
|
||||
UTEST_RUN(test_internal__zlib_enumvalues);
|
||||
UTEST_RUN(test_internal__file_utils);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
@ -37,6 +37,7 @@
|
||||
#include "../src/gmio_core/internal/convert.h"
|
||||
#include "../src/gmio_core/internal/error_check.h"
|
||||
#include "../src/gmio_core/internal/fast_atof.h"
|
||||
#include "../src/gmio_core/internal/file_utils.h"
|
||||
#include "../src/gmio_core/internal/helper_stream.h"
|
||||
#include "../src/gmio_core/internal/locale_utils.h"
|
||||
#include "../src/gmio_core/internal/numeric_utils.h"
|
||||
@ -106,6 +107,35 @@ static const char* test_internal__byte_codec()
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char* test_internal__const_string()
|
||||
{
|
||||
char buff[512] = {0};
|
||||
struct gmio_const_string lhs = { "file", 4 };
|
||||
struct gmio_const_string rhs = { ".txt", 4 };
|
||||
UTEST_COMPARE_UINT(
|
||||
lhs.len + rhs.len,
|
||||
gmio_const_string_concat(buff, sizeof(buff), &lhs, &rhs));
|
||||
UTEST_COMPARE_CSTR("file.txt", buff);
|
||||
|
||||
lhs = gmio_const_string("a", 1);
|
||||
rhs = gmio_const_string("b", 1);
|
||||
UTEST_COMPARE_UINT(
|
||||
lhs.len + rhs.len,
|
||||
gmio_const_string_concat(buff, sizeof(buff), &lhs, &rhs));
|
||||
UTEST_COMPARE_CSTR("ab", buff);
|
||||
|
||||
char small_buff[8] = {0};
|
||||
lhs = gmio_const_string("1234567890", 10);
|
||||
rhs = gmio_const_string("abc", 3);
|
||||
UTEST_COMPARE_UINT(
|
||||
sizeof(small_buff)-1,
|
||||
gmio_const_string_concat(
|
||||
small_buff, sizeof(small_buff), &lhs, &rhs));
|
||||
UTEST_COMPARE_CSTR("1234abc", small_buff);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void __tc__fprintf_atof_err(
|
||||
const char* func_fast_atof_str,
|
||||
const char* val_str,
|
||||
@ -830,4 +860,40 @@ static const char* test_internal__zlib_enumvalues()
|
||||
UTEST_COMPARE_INT(Z_HUFFMAN_ONLY, GMIO_ZLIB_COMPRESSION_STRATEGY_HUFFMAN_ONLY);
|
||||
UTEST_COMPARE_INT(Z_RLE, GMIO_ZLIB_COMPRESSION_STRATEGY_RLE);
|
||||
UTEST_COMPARE_INT(Z_FIXED, GMIO_ZLIB_COMPRESSION_STRATEGY_FIXED);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char* test_internal__file_utils()
|
||||
{
|
||||
struct gmio_const_string cstr = {0};
|
||||
|
||||
cstr = gmio_fileutils_find_basefilename("");
|
||||
UTEST_ASSERT(gmio_const_string_is_empty(&cstr));
|
||||
|
||||
cstr = gmio_fileutils_find_basefilename("file");
|
||||
UTEST_ASSERT(strncmp("file", cstr.ptr, cstr.len) == 0);
|
||||
|
||||
cstr = gmio_fileutils_find_basefilename("file.txt");
|
||||
UTEST_ASSERT(strncmp("file", cstr.ptr, cstr.len) == 0);
|
||||
|
||||
cstr = gmio_fileutils_find_basefilename("file.txt.zip");
|
||||
UTEST_ASSERT(strncmp("file.txt", cstr.ptr, cstr.len) == 0);
|
||||
|
||||
cstr = gmio_fileutils_find_basefilename("/home/me/file.txt");
|
||||
UTEST_ASSERT(strncmp("file", cstr.ptr, cstr.len) == 0);
|
||||
|
||||
cstr = gmio_fileutils_find_basefilename("/home/me/file.");
|
||||
UTEST_ASSERT(strncmp("file", cstr.ptr, cstr.len) == 0);
|
||||
|
||||
cstr = gmio_fileutils_find_basefilename("/home/me/file");
|
||||
UTEST_ASSERT(strncmp("file", cstr.ptr, cstr.len) == 0);
|
||||
|
||||
cstr = gmio_fileutils_find_basefilename("/home/me/file for you.txt");
|
||||
UTEST_ASSERT(strncmp("file for you", cstr.ptr, cstr.len) == 0);
|
||||
|
||||
cstr = gmio_fileutils_find_basefilename("C:\\Program Files\\gmio\\file.txt");
|
||||
UTEST_ASSERT(strncmp("file", cstr.ptr, cstr.len) == 0);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user