1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2024-06-24 05:53:16 +00:00

Replace the use of dirent.h by C++ standard libraries

This commit is contained in:
Carles Fernandez 2020-11-28 12:14:21 +01:00
parent 015d111c2f
commit 77d58e1630
No known key found for this signature in database
GPG Key ID: 4C583C52B0C3877D
2 changed files with 123 additions and 13 deletions

View File

@ -116,6 +116,18 @@ if(USE_BOOST_BIND_PLACEHOLDERS)
)
endif()
if(ENABLE_FPGA)
if(FILESYSTEM_FOUND)
target_compile_definitions(core_libs PRIVATE -DHAS_STD_FILESYSTEM=1)
if(find_experimental)
target_compile_definitions(core_libs PRIVATE -DHAS_STD_FILESYSTEM_EXPERIMENTAL=1)
endif()
target_link_libraries(core_libs PRIVATE std::filesystem)
else()
target_link_libraries(core_libs PRIVATE Boost::filesystem Boost::system)
endif()
endif()
if(ENABLE_CLANG_TIDY)
if(CLANG_TIDY_EXE)
set_target_properties(core_libs

View File

@ -19,10 +19,27 @@
*/
#include "uio_fpga.h"
#include <dirent.h> // scandir()
#include <fstream> // ifstream
#include <iostream> // cout
#include <sstream> // std::stringstream
#include <algorithm> // sort
#include <cstdint> // int8_t
#include <fstream> // ifstream
#include <iostream> // cout
#include <locale> // isdigit
#include <sstream> // std::stringstream
#include <vector>
#if HAS_STD_FILESYSTEM
#if HAS_STD_FILESYSTEM_EXPERIMENTAL
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif
#else
#include <boost/filesystem.hpp>
#include <boost/filesystem/path.hpp>
namespace fs = boost::filesystem;
#endif
int32_t get_uio_num(std::string uio_name)
{
@ -40,6 +57,7 @@ int32_t get_uio_num(std::string uio_name)
return atoi(uio_num.c_str());
}
void get_uio_name(uint32_t uio_num, std::string &uio_name)
{
std::stringstream filename;
@ -64,21 +82,101 @@ void get_uio_name(uint32_t uio_num, std::string &uio_name)
}
}
int my_strverscmp(const char *s1, const char *s2)
{
// from https://code.woboq.org/userspace/glibc/string/strverscmp.c.html
const uint8_t S_N = 0x0;
const uint8_t S_I = 0x3;
const uint8_t S_F = 0x6;
const uint8_t S_Z = 0x9;
const int8_t CMP = 2;
const int8_t LEN = 3;
const unsigned char *p1 = (const unsigned char *)s1;
const unsigned char *p2 = (const unsigned char *)s2;
/* Symbol(s) 0 [1-9] others
Transition (10) 0 (01) d (00) x */
static const uint8_t next_state[] =
{
/* state x d 0 */
/* S_N */ S_N, S_I, S_Z,
/* S_I */ S_N, S_I, S_I,
/* S_F */ S_N, S_F, S_F,
/* S_Z */ S_N, S_F, S_Z};
static const int8_t result_type[] =
{
/* state x/x x/d x/0 d/x d/d d/0 0/x 0/d 0/0 */
/* S_N */ CMP, CMP, CMP, CMP, LEN, CMP, CMP, CMP, CMP,
/* S_I */ CMP, -1, -1, +1, LEN, LEN, +1, LEN, LEN,
/* S_F */ CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
/* S_Z */ CMP, +1, +1, -1, CMP, CMP, -1, CMP, CMP};
if (p1 == p2)
{
return 0;
}
unsigned char c1 = *p1++;
unsigned char c2 = *p2++;
/* Hint: '0' is a digit too. */
int state = S_N + ((c1 == '0') + (isdigit(c1) != 0));
int diff;
while ((diff = c1 - c2) == 0)
{
if (c1 == '\0')
{
return diff;
}
state = next_state[state];
c1 = *p1++;
c2 = *p2++;
state += (c1 == '0') + (isdigit(c1) != 0);
}
state = result_type[state * 3 + (((c2 == '0') + (isdigit(c2) != 0)))];
switch (state)
{
case CMP:
return diff;
case LEN:
while (isdigit(*p1++))
{
if (!isdigit(*p2++))
{
return 1;
}
}
return isdigit(*p2) ? -1 : diff;
default:
return state;
}
}
bool sort_directories(fs::directory_entry a, fs::directory_entry b)
{
int cmp = my_strverscmp(a.path().string().c_str(), b.path().string().c_str());
return (cmp < 0);
}
int32_t find_uio_num(const std::string &device_name, uint32_t device_num)
{
struct dirent **files;
int32_t uio_num;
// get the number of directory entries in the uio driver description system directories
uint32_t num_dir_entries = scandir(uio_dir.c_str(), &files, 0, versionsort);
uint32_t device_count = 0, uio_count = 0;
uint32_t uio_count = 0;
// search for the requested device driver
while (device_count < num_dir_entries)
fs::path path(uio_dir);
std::vector<fs::directory_entry> dirs;
for (auto &p : fs::directory_iterator(path))
{
dirs.push_back(p);
}
std::sort(dirs.begin(), dirs.end(), sort_directories);
for (auto &p : dirs)
{
// get the uio number corresponding to directory entry n
uio_num = get_uio_num(files[device_count]->d_name);
uio_num = get_uio_num(p.path().string());
if (uio_num >= 0) // valid uio number
{
std::string nametemp;
@ -95,11 +193,11 @@ int32_t find_uio_num(const std::string &device_name, uint32_t device_num)
}
}
}
device_count++;
}
return -1; // uio number not found
}
int32_t find_uio_dev_file_name(std::string &device_file_name, const std::string &device_name, uint32_t device_num)
{
int32_t uio_num = find_uio_num(device_name, device_num);