LCOV - code coverage report
Current view: top level - libs/kiss/src - path.cpp (source / functions) Hit Total Coverage
Test: coverage.info.cleaned Lines: 13 53 24.5 %
Date: 2024-04-29 14:43:01 Functions: 3 8 37.5 %

          Line data    Source code
       1             : #include "kiss/path.h"
       2             : 
       3             : #ifdef WIN32
       4             : #  include "windows.h"
       5             : #  ifndef S_ISREG
       6             : #    define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
       7             : #  endif
       8             : #  ifndef S_ISDIR
       9             : #    define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
      10             : #  endif
      11             : #  include "sys/stat.h"
      12             : #else
      13             : #include <sys/types.h>
      14             : #include <fcntl.h>
      15             : #include <unistd.h>
      16             : #include <dirent.h>
      17             : #include "sys/stat.h"
      18             : #endif
      19             : 
      20             : #include <stdexcept>
      21             : #include <cstdio>
      22             : 
      23           0 : bool create_directory(const std::string &path, size_t user_permission,
      24             :                 size_t group_permission, size_t other_permission) {
      25             : #ifdef WIN32
      26             :         BOOL result = ::CreateDirectoryA(path.c_str(), 0);
      27             :         return result == TRUE;
      28             : #else
      29             :         mode_t m = 0;
      30           0 :         m |= (user_permission << 6);
      31           0 :         m |= (group_permission << 3);
      32           0 :         m |= (other_permission << 0);
      33           0 :         int result = mkdir(path.c_str(), m);
      34           0 :         return (result == 0);
      35             : #endif
      36             : }
      37             : 
      38          26 : bool is_directory(const std::string &path) {
      39             : #ifdef WIN32
      40             :         struct _stat buf;
      41             :         int result = _stat(path.c_str(), &buf);
      42             :         if (result == 0 && S_ISDIR(buf.st_mode))
      43             :                 return true;
      44             :         else
      45             :                 return false;
      46             : #else
      47             :         struct stat buf;
      48          26 :         int result = stat(path.c_str(), &buf);
      49          26 :         if (result == 0 && S_ISDIR(buf.st_mode))
      50             :         return true;
      51             :         else
      52          26 :         return false;
      53             : #endif
      54             : }
      55             : 
      56           0 : bool list_directory(const std::string &directory,
      57             :                 std::vector<std::string> &elements) {
      58             : #ifdef WIN32
      59             :         WIN32_FIND_DATA findData;
      60             :         HANDLE hFind = FindFirstFileA((directory + "\\*.*").c_str(), &findData);
      61             :         if (hFind == INVALID_HANDLE_VALUE)
      62             :                 throw std::runtime_error("Failed to get directory list");
      63             :         for (;;) {
      64             :                 if (findData.cFileName[0] != '.') {
      65             :                         elements.push_back(findData.cFileName);
      66             :                 }
      67             :                 if (FindNextFileA(hFind, &findData) == 0)
      68             :                         break;
      69             :         }
      70             :         FindClose(hFind);
      71             : #else
      72           0 :         DIR* dir = opendir(directory.c_str());
      73           0 :         if (dir == NULL)
      74             :         return false;
      75             :         dirent* entry;
      76           0 :         while ((entry = readdir(dir)) != NULL) {
      77           0 :                 if (entry->d_name[0] != '.') {
      78           0 :                         elements.push_back(entry->d_name);
      79             :                 }
      80             :         }
      81           0 :         closedir(dir);
      82             : #endif
      83           0 :         return true;
      84             : }
      85             : 
      86           0 : bool create_directory_recursive(const std::string &dir, size_t user_permission,
      87             :                 size_t group_permission, size_t other_permission) {
      88             :         // split path
      89           0 :         const char seperators[] = "\\/";
      90             :         std::vector<std::string> elements;
      91             :         std::string::size_type a, b;
      92             :         a = dir.find_first_not_of(seperators), b = dir.find_first_of(seperators, a);
      93           0 :         while (a != std::string::npos) {
      94           0 :                 elements.push_back(dir.substr(a, b - a));
      95             :                 a = dir.find_first_not_of(seperators, b), b = dir.find_first_of(
      96             :                                 seperators, a);
      97             :         }
      98             : 
      99             :         // create all non existing parts
     100             :         std::string path;
     101           0 :         for (size_t i = 0; i < elements.size(); i++) {
     102             :                 path += elements[i];
     103             :                 path += path_seperator;
     104           0 :                 if (is_directory(path) == false)
     105           0 :                         create_directory(path);
     106             :         }
     107             : 
     108           0 :         return is_directory(dir);
     109           0 : }
     110             : 
     111         547 : std::string concat_path(const std::string &a, const std::string &b) {
     112         547 :         char last = *a.rbegin();
     113         547 :         if (last == '\\' || last == '/')
     114           0 :                 return a + b;
     115             :         else
     116        1094 :                 return a + path_seperator + b;
     117             : 
     118             : }
     119             : 
     120           0 : std::string concat_path(const std::string &a, const std::string &b,
     121             :                 const std::string &c) {
     122             :         std::string p = a;
     123           0 :         if (*p.rbegin() != '\\' && *p.rbegin() != '/')
     124             :                 p += path_seperator;
     125             :         p += b;
     126           0 :         if (*p.rbegin() != '\\' && *p.rbegin() != '/')
     127             :                 p += path_seperator;
     128             :         p += c;
     129           0 :         return p;
     130             : }
     131             : 
     132           0 : void append_file(const std::string &target, const std::string &source,
     133             :                 bool binary) {
     134           0 :         FILE *t = fopen(target.c_str(), binary ? "wb+" : "w+");
     135           0 :         if (t == NULL)
     136           0 :                 return;
     137             : 
     138           0 :         FILE *s = fopen(source.c_str(), binary ? "rb" : "r");
     139           0 :         if (s == NULL) {
     140           0 :                 fclose(t);
     141           0 :                 return;
     142             :         }
     143             : 
     144             :         size_t size = 0;
     145             :         char buffer[1 << 20];
     146           0 :         while ((size = fread(buffer, 1, sizeof(buffer), s)) > 0) {
     147           0 :                 fwrite(buffer, 1, size, t);
     148             :         }
     149             : 
     150           0 :         fclose(t);
     151           0 :         fclose(s);
     152             : }
     153             : 
     154          13 : std::string executable_path() {
     155             :         char buf[1024];
     156             : 
     157             : //#if linux
     158          13 :         size_t len = readlink("/proc/self/exe", buf, sizeof(buf)-1);
     159             : //#else
     160             : //      size_t len = ::GetModuleFileName(NULL, buf, sizeof(buf)-1 );
     161             : //#endif
     162         192 :         for (size_t i = 1; i < len; i++) {
     163         189 :                 if (buf[len - 1] == path_seperator)
     164             :                         break;
     165             :                 else
     166             :                         len --;
     167             :         }
     168          13 :         return std::string(buf, len);
     169             : }

Generated by: LCOV version 1.14