summaryrefslogtreecommitdiff
path: root/glib/src/miscutils.ccg
blob: c0600a6c49cb5de81a3ff359895ea9ade74159c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* Copyright (C) 2002 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <cstddef>
#include <cstring>

#include <glibmm/miscutils.h>
#include <glibmm/utility.h>
#include <glib.h>


namespace Glib
{

Glib::ustring get_application_name()
{
  return convert_const_gchar_ptr_to_ustring (g_get_application_name());
}

void set_application_name(const Glib::ustring& application_name)
{
  g_set_application_name(application_name.c_str());
}

std::string get_prgname()
{
  return convert_const_gchar_ptr_to_stdstring(g_get_prgname());
}

void set_prgname(const std::string& prgname)
{
  g_set_prgname(prgname.c_str());
}

std::string getenv(const std::string& variable, bool& found)
{
  const char *const value = g_getenv(variable.c_str());
  found = (value != nullptr);
  return convert_const_gchar_ptr_to_stdstring(value);
}

std::string getenv(const std::string& variable)
{
  return convert_const_gchar_ptr_to_stdstring(g_getenv(variable.c_str()));
}

bool setenv(const std::string& variable, const std::string& value, bool overwrite)
{
  return g_setenv(variable.c_str(), value.c_str(), overwrite);
}

void unsetenv(const std::string& variable)
{
  g_unsetenv(variable.c_str());
}

Glib::ArrayHandle<std::string> listenv()
{
  char **value = g_listenv();
  char **end = value;
  while(*end){
    ++end;
  }
  return Glib::ArrayHandle<std::string>(value, end-value, Glib::OWNERSHIP_DEEP);
}

std::string get_user_name()
{
  return convert_const_gchar_ptr_to_stdstring(g_get_user_name());
}

std::string get_real_name()
{
  return convert_const_gchar_ptr_to_stdstring(g_get_real_name());
}

std::string get_home_dir()
{
  return convert_const_gchar_ptr_to_stdstring(g_get_home_dir());
}

std::string get_tmp_dir()
{
  return convert_const_gchar_ptr_to_stdstring(g_get_tmp_dir());
}

std::string get_current_dir()
{
  return convert_return_gchar_ptr_to_stdstring(g_get_current_dir());
}

#ifndef GLIBMM_DISABLE_DEPRECATED
std::string get_user_special_dir(GUserDirectory directory)
{
  return convert_const_gchar_ptr_to_stdstring(g_get_user_special_dir(directory));
}
#endif // GLIBMM_DISABLE_DEPRECATED

std::string get_user_special_dir(UserDirectory directory)
{
  return convert_const_gchar_ptr_to_stdstring(g_get_user_special_dir((GUserDirectory)directory));
}

std::string get_user_data_dir()
{
  return convert_const_gchar_ptr_to_stdstring(g_get_user_data_dir());
}

std::string get_user_config_dir()
{
  return convert_const_gchar_ptr_to_stdstring(g_get_user_config_dir());
}

std::vector<std::string> get_system_data_dirs()
{
  //TODO: Use a utility function:
  std::vector<std::string> result;
  const char* const * cresult = g_get_system_data_dirs();
  if(!cresult)
    return result;

  for(const gchar* const * iter = cresult; *iter != nullptr; ++iter)
  {
    result.emplace_back(
      convert_const_gchar_ptr_to_stdstring(*iter));
  }

  return result;
}

std::vector<std::string> get_system_config_dirs()
{
  //TODO: Use a utility function:
  std::vector<std::string> result;
  const char* const * cresult = g_get_system_config_dirs();
  if(!cresult)
    return result;

  for(const gchar* const * iter = cresult; *iter != nullptr; ++iter)
  {
    result.emplace_back(
      convert_const_gchar_ptr_to_stdstring(*iter));
  }

  return result;
}

std::string get_user_cache_dir()
{
  return convert_const_gchar_ptr_to_stdstring(g_get_user_cache_dir());
}

bool path_is_absolute(const std::string& filename)
{
  return (g_path_is_absolute(filename.c_str()) != 0);
}

std::string path_skip_root(const std::string& filename)
{
  // g_path_skip_root() returns a pointer _into_ the argument string,
  // or NULL if there was no root component.
  return convert_const_gchar_ptr_to_stdstring(g_path_skip_root(filename.c_str()));
}

std::string path_get_basename(const std::string& filename)
{
  return convert_return_gchar_ptr_to_stdstring(g_path_get_basename(filename.c_str()));
}

std::string path_get_dirname(const std::string& filename)
{
  return convert_return_gchar_ptr_to_stdstring(g_path_get_dirname(filename.c_str()));
}

std::string build_filename(const Glib::ArrayHandle<std::string>& elements)
{
  return convert_return_gchar_ptr_to_stdstring(g_build_filenamev(const_cast<char**>(elements.data())));
}

std::string build_filename(const std::string& elem1, const std::string& elem2)
{
  return convert_return_gchar_ptr_to_stdstring(g_build_filename(elem1.c_str(),
    elem2.c_str(), nullptr));
}

std::string build_filename(const std::string& elem1, const std::string& elem2,
                           const std::string& elem3)
{
  return convert_return_gchar_ptr_to_stdstring(g_build_filename(elem1.c_str(),
    elem2.c_str(), elem3.c_str(), nullptr));
}

std::string build_filename(const std::string& elem1, const std::string& elem2,
                           const std::string& elem3, const std::string& elem4)
{
  return convert_return_gchar_ptr_to_stdstring(g_build_filename(elem1.c_str(),
    elem2.c_str(), elem3.c_str(), elem4.c_str(), nullptr));
}

std::string build_filename(const std::string& elem1, const std::string& elem2,
                           const std::string& elem3, const std::string& elem4,
                           const std::string& elem5)
{
  return convert_return_gchar_ptr_to_stdstring(g_build_filename(elem1.c_str(),
    elem2.c_str(), elem3.c_str(), elem4.c_str(), elem5.c_str(),
    nullptr));
}

std::string build_filename(const std::string& elem1, const std::string& elem2,
                           const std::string& elem3, const std::string& elem4,
                           const std::string& elem5, const std::string& elem6)
{
  return convert_return_gchar_ptr_to_stdstring(g_build_filename(elem1.c_str(),
    elem2.c_str(), elem3.c_str(), elem4.c_str(), elem5.c_str(), elem6.c_str(),
    nullptr));
}

std::string build_filename(const std::string& elem1, const std::string& elem2,
                           const std::string& elem3, const std::string& elem4,
                           const std::string& elem5, const std::string& elem6,
                           const std::string& elem7)
{
  return convert_return_gchar_ptr_to_stdstring(g_build_filename(elem1.c_str(),
    elem2.c_str(), elem3.c_str(), elem4.c_str(), elem5.c_str(), elem6.c_str(),
    elem7.c_str(), nullptr));
}

std::string build_filename(const std::string& elem1, const std::string& elem2,
                           const std::string& elem3, const std::string& elem4,
                           const std::string& elem5, const std::string& elem6,
                           const std::string& elem7, const std::string& elem8)
{
  return convert_return_gchar_ptr_to_stdstring(g_build_filename(elem1.c_str(),
    elem2.c_str(), elem3.c_str(), elem4.c_str(), elem5.c_str(), elem6.c_str(),
    elem7.c_str(), elem8.c_str(), nullptr));
}

std::string build_filename(const std::string& elem1, const std::string& elem2,
                           const std::string& elem3, const std::string& elem4,
                           const std::string& elem5, const std::string& elem6,
                           const std::string& elem7, const std::string& elem8,
                           const std::string& elem9)
{
  return convert_return_gchar_ptr_to_stdstring(g_build_filename(elem1.c_str(),
    elem2.c_str(), elem3.c_str(), elem4.c_str(), elem5.c_str(), elem6.c_str(),
    elem7.c_str(), elem8.c_str(), elem9.c_str(), nullptr));
}

std::string build_path(const std::string& separator, const Glib::ArrayHandle<std::string>& elements)
{
  return convert_return_gchar_ptr_to_stdstring(g_build_pathv(separator.c_str(), const_cast<char**>(elements.data())));
}

std::string find_program_in_path(const std::string& program)
{
  return convert_return_gchar_ptr_to_stdstring(g_find_program_in_path(program.c_str()));
}

Glib::ustring format_size(guint64 size, FormatSizeFlags flags)
{
  return convert_return_gchar_ptr_to_ustring(g_format_size_full(size, (GFormatSizeFlags)flags));
}

} // namespace Glib