Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
julianoes committed Sep 4, 2023
1 parent 817bf8f commit a385e04
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 1,342 deletions.
11 changes: 0 additions & 11 deletions src/mavsdk/core/filesystem_include.h

This file was deleted.

169 changes: 70 additions & 99 deletions src/mavsdk/core/fs.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
#if defined(WINDOWS)
#include "tronkko_dirent.h"
#include "stackoverflow_unistd.h"
#include <direct.h>
#define mkdir(D, M) _mkdir(D)
#else
#include <dirent.h>
#include <unistd.h>
#endif

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stack>
#include <stdio.h>

#include "fs.h"
#include "filesystem_include.h"
#include "log.h"
#include "unused.h"

Expand All @@ -27,88 +10,76 @@

namespace mavsdk {

bool fs_exists(const fs::path& path)
{
return fs::exists(path);
}

uint32_t fs_file_size(const std::string& filename)
{
struct stat stat_buf;
int rc = stat(filename.c_str(), &stat_buf);
return (rc == 0) ? stat_buf.st_size : 0;
}

static void relative_dir_base_split(const std::string& path, std::string& dir, std::string& base)
{
std::string::size_type slash_pos = path.rfind(path_separator);
if (slash_pos != std::string::npos) {
slash_pos++;
dir = path.substr(0, slash_pos);
base = path.substr(slash_pos);
} else {
dir.clear();
base = path;
}
}

std::string fs_filename(const std::string& path)
{
std::string dir;
std::string base;
relative_dir_base_split(path, dir, base);
return base;
}

std::string fs_canonical(const std::string& path)
{
std::deque<std::string> st;
std::string res;

int len = path.length();

int index = 0;
while (index < len) {
int next_index = path.find(path_separator, index);
if (next_index == -1) {
next_index = path.size();
}

std::string dir = path.substr(index, next_index - index);

if (dir.compare("..") == 0 && !st.empty()) {
st.pop_back();
} else if (dir.length() != 0 && dir.compare(".") != 0) {
st.push_back(dir);
}

index = next_index + path_separator.size();
}

if (path.rfind(path_separator, path_separator.size() - 1) != 0) {
char buffer[PATH_MAX];
if (getcwd(buffer, sizeof(buffer)) != nullptr) {
res.append(buffer);
if (!st.empty()) {
res.append(path_separator);
}
}
} else {
res.append(path_separator);
}

while (!st.empty()) {
std::string temp = st.front();
if (st.size() != 1) {
res.append(temp + path_separator);
} else {
res.append(temp);
}
st.pop_front();
}

return res;
}
//static void relative_dir_base_split(const std::string& path, std::string& dir, std::string& base)
//{
// std::string::size_type slash_pos = path.rfind(path_separator);
// if (slash_pos != std::string::npos) {
// slash_pos++;
// dir = path.substr(0, slash_pos);
// base = path.substr(slash_pos);
// } else {
// dir.clear();
// base = path;
// }
//}

//std::string fs_filename(const std::string& path)
//{
// std::string dir;
// std::string base;
// relative_dir_base_split(path, dir, base);
// return base;
//}

//std::string fs_canonical(const std::string& path)
//{
// std::deque<std::string> st;
// std::string res;
//
// int len = path.length();
//
// int index = 0;
// while (index < len) {
// int next_index = path.find(path_separator, index);
// if (next_index == -1) {
// next_index = path.size();
// }
//
// std::string dir = path.substr(index, next_index - index);
//
// if (dir.compare("..") == 0 && !st.empty()) {
// st.pop_back();
// } else if (dir.length() != 0 && dir.compare(".") != 0) {
// st.push_back(dir);
// }
//
// index = next_index + path_separator.size();
// }
//
// if (path.rfind(path_separator, path_separator.size() - 1) != 0) {
// char buffer[PATH_MAX];
// if (getcwd(buffer, sizeof(buffer)) != nullptr) {
// res.append(buffer);
// if (!st.empty()) {
// res.append(path_separator);
// }
// }
// } else {
// res.append(path_separator);
// }
//
// while (!st.empty()) {
// std::string temp = st.front();
// if (st.size() != 1) {
// res.append(temp + path_separator);
// } else {
// res.append(temp);
// }
// st.pop_front();
// }
//
// return res;
//}

bool fs_create_directory(const std::string& path)
{
Expand Down
8 changes: 0 additions & 8 deletions src/mavsdk/core/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,8 @@ namespace fs = std::filesystem;

namespace mavsdk {

#if defined(WINDOWS)
const std::string path_separator = "\\";
#else
const std::string path_separator = "/";
#endif

bool fs_exists(const fs::path& filename);

uint32_t fs_file_size(const std::string& filename);

std::string fs_filename(const std::string& path);

std::string fs_canonical(const std::string& path);
Expand Down
Loading

0 comments on commit a385e04

Please sign in to comment.