Instrument Neutral Distributed Interface INDI  1.9.5
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 namespace INDI
27 {
28 
29 int mkpath(std::string s, mode_t mode)
30 {
31  size_t pre = 0, pos;
32  std::string dir;
33  int mdret = 0;
34  struct stat st;
35 
36  if (s[s.size() - 1] != '/')
37  s += '/';
38 
39  while ((pos = s.find_first_of('/', pre)) != std::string::npos)
40  {
41  dir = s.substr(0, pos++);
42  pre = pos;
43  if (dir.size() == 0)
44  continue;
45 
46  if (stat(dir.c_str(), &st))
47  {
48  if (errno != ENOENT || ((mdret = mkdir(dir.c_str(), mode)) && errno != EEXIST))
49  {
50  return mdret;
51  }
52  }
53  else
54  {
55  if (!S_ISDIR(st.st_mode))
56  {
57  return -1;
58  }
59  }
60  }
61  return mdret;
62 }
63 
64 std::string format_time(const std::tm &tm, const char *format)
65 {
66  char cstr[32];
67 
68  int size = strftime(cstr, sizeof(cstr), format, &tm);
69 
70  return std::string(cstr, size);
71 }
72 
73 void replace_all(std::string &subject, const std::string& search, const std::string& replace)
74 {
75  size_t pos = 0;
76  while ((pos = subject.find(search, pos)) != std::string::npos)
77  {
78  subject.replace(pos, search.length(), replace);
79  pos += replace.length();
80  }
81 }
82 
83 }
indiutility.h
INDI::format_time
std::string format_time(const std::tm &tm, const char *format)
Converts the date and time to string - this function uses 'strftime'.
Definition: indiutility.cpp:64
INDI::mkpath
int mkpath(std::string s, mode_t mode)
Create a path directory - this function uses 'mkdir'.
Definition: indiutility.cpp:29
INDI::replace_all
void replace_all(std::string &subject, const std::string &search, const std::string &replace)
Replaces every occurrence of the string 'search' with the string 'replace'.
Definition: indiutility.cpp:73
INDI
Namespace to encapsulate INDI client, drivers, and mediator classes.
Definition: AlignmentSubsystemForClients.cpp:11
errno
int errno