Instrument Neutral Distributed Interface INDI  2.0.2
indiutility.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2020 by Pawel Soja <kernel32.pl@gmail.com>
3  Copyright (C) 2015 by Jasem Mutlaq <mutlaqja@ikarustech.com>
4  Copyright (C) 2014 by geehalel <geehalel@gmail.com>
5 
6  Stream Recorder
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 */
23 #include "indiutility.h"
24 #include <cerrno>
25 
26 #ifdef _MSC_VER
27 
28 #include <direct.h>
29 
30 #ifndef S_ISDIR
31 #define S_ISDIR(m) (((m) & _S_IFDIR) == _S_IFDIR)
32 #endif
33 
34 #define mkdir _mkdir
35 
36 #endif
37 
38 namespace INDI
39 {
40 
41 int mkdir(const char *path, mode_t mode)
42 {
43 #ifdef _WIN32
44  INDI_UNUSED(mode);
45  return ::mkdir(path);
46 #else
47  return ::mkdir(path, mode);
48 #endif
49 }
50 
51 int mkpath(std::string s, mode_t mode)
52 {
53  size_t pre = 0, pos;
54  std::string dir;
55  int mdret = 0;
56  struct stat st;
57 
58  if (s[s.size() - 1] != '/')
59  s += '/';
60 
61  while ((pos = s.find_first_of('/', pre)) != std::string::npos)
62  {
63  dir = s.substr(0, pos++);
64  pre = pos;
65  if (dir.size() == 0)
66  continue;
67 
68  if (stat(dir.c_str(), &st))
69  {
70  if (errno != ENOENT || ((mdret = mkdir(dir.c_str(), mode)) && errno != EEXIST))
71  {
72  return mdret;
73  }
74  }
75  else
76  {
77  if (!S_ISDIR(st.st_mode))
78  {
79  return -1;
80  }
81  }
82  }
83  return mdret;
84 }
85 
86 std::string format_time(const std::tm &tm, const char *format)
87 {
88  char cstr[32];
89 
90  size_t size = strftime(cstr, sizeof(cstr), format, &tm);
91 
92  return std::string(cstr, size);
93 }
94 
95 void replace_all(std::string &subject, const std::string &search, const std::string &replace)
96 {
97  size_t pos = 0;
98  while ((pos = subject.find(search, pos)) != std::string::npos)
99  {
100  subject.replace(pos, search.length(), replace);
101  pos += replace.length();
102  }
103 }
104 
105 }
int errno
#define INDI_UNUSED(x)
Definition: indidevapi.h:131
Namespace to encapsulate INDI client, drivers, and mediator classes.
int mkdir(const char *path, mode_t mode)
Definition: indiutility.cpp:41
std::string format_time(const std::tm &tm, const char *format)
Definition: indiutility.cpp:86
void replace_all(std::string &subject, const std::string &search, const std::string &replace)
Definition: indiutility.cpp:95
int mkpath(std::string s, mode_t mode)
Definition: indiutility.cpp:51