Instrument Neutral Distributed Interface INDI 2.2.5
indiutility.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 by Pawel Soja <[email protected]>
3 Copyright (C) 2015 by Jasem Mutlaq <[email protected]>
4 Copyright (C) 2014 by geehalel <[email protected]>
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
38namespace INDI
39{
40
41int 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
51int mkpath(std::string s, mode_t mode)
52{
53 if (s.empty())
54 return -1;
55
56 size_t pre = 0, pos;
57 std::string dir;
58 int mdret = 0;
59 struct stat st;
60
61 if (s[s.size() - 1] != '/')
62 s += '/';
63
64 while ((pos = s.find_first_of('/', pre)) != std::string::npos)
65 {
66 dir = s.substr(0, pos++);
67 pre = pos;
68 if (dir.size() == 0)
69 continue;
70
71 if (stat(dir.c_str(), &st))
72 {
73 if (errno != ENOENT || ((mdret = mkdir(dir.c_str(), mode)) && errno != EEXIST))
74 {
75 return mdret;
76 }
77 }
78 else
79 {
80 if (!S_ISDIR(st.st_mode))
81 {
82 return -1;
83 }
84 }
85 }
86 return mdret;
87}
88
89std::string format_time(const std::tm &tm, const char *format)
90{
91 char cstr[32];
92
93 size_t size = strftime(cstr, sizeof(cstr), format, &tm);
94
95 return std::string(cstr, size);
96}
97
98void replace_all(std::string &subject, const std::string &search, const std::string &replace)
99{
100 size_t pos = 0;
101 while ((pos = subject.find(search, pos)) != std::string::npos)
102 {
103 subject.replace(pos, search.length(), replace);
104 pos += replace.length();
105 }
106}
107
108}
int errno
#define INDI_UNUSED(x)
Definition indidevapi.h:132
Namespace to encapsulate INDI client, drivers, and mediator classes.
int mkdir(const char *path, mode_t mode)
std::string format_time(const std::tm &tm, const char *format)
void replace_all(std::string &subject, const std::string &search, const std::string &replace)
int mkpath(std::string s, mode_t mode)