2
* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License as
6
* published by the Free Software Foundation; version 2 of the
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
36
#include "wbpublic_public_interface.h"
39
#define UNUSED __attribute__((unused))
66
#define DATETIME_FMT "%Y-%m-%d %H:%M"
67
std::string WBPUBLICBACKEND_PUBLIC_FUNC fmttime(time_t t= 0, const char *fmt= "%b %d, %Y");
69
std::string WBPUBLICBACKEND_PUBLIC_FUNC replace_string(const std::string &s,
70
const std::string &from,
71
const std::string &to);
72
void WBPUBLICBACKEND_PUBLIC_FUNC replace_string_inplace(std::string &text,
73
const std::string &from,
74
const std::string &to);
75
std::string WBPUBLICBACKEND_PUBLIC_FUNC rtrim(const std::string &value);
78
// replaces a variable from a string in format %variable%
79
// a filter can be passed to the variable as in %variable|filter%
80
// supported filters are upper, lower and capitalize
81
std::string WBPUBLICBACKEND_PUBLIC_FUNC replace_variable(const std::string &format, const std::string &variable, const std::string &value);
83
std::string WBPUBLICBACKEND_PUBLIC_FUNC append_extension_if_needed(const std::string &path,
84
const std::string &ext);
86
inline bool has_prefix(const std::string &str, const std::string &pref)
88
if (strncmp(str.c_str(), pref.c_str(), pref.length())==0)
94
inline bool has_suffix(const std::string &str, const std::string &suf)
96
if (suf.length() < str.length() && strncmp(str.c_str()+str.length()-suf.length(), suf.c_str(), suf.length())==0)
102
std::string WBPUBLICBACKEND_PUBLIC_FUNC make_path(const std::string &prefix, const std::string &file);
104
inline std::string pathlist_append(const std::string &l, const std::string &s)
108
return l+G_SEARCHPATH_SEPARATOR+s;
111
inline std::string pathlist_prepend(const std::string &l, const std::string &s)
115
return s+G_SEARCHPATH_SEPARATOR+l;
119
size_t find_list_ref_item_position(grt::ListRef<T> &item_data, std::string& name, MatchType match = MatchAny, grt::Ref<T>* reference = NULL, FindType find_mode = FindPrefix)
122
if ((match == MatchBefore || match == MatchAfter) && !reference)
123
throw std::invalid_argument("A reference must be specified for MatchBefore and MatchAfter");
125
bool search_enabled = match != MatchAfter;
128
size_t index = grt::BaseListRef::npos;
130
for ( grt::TypedListConstIterator<T> end = item_data.end(),
131
inst = item_data.begin(); inst != end && !exit; ++inst)
133
// If skip is defined will omit the entries until the 'skip' element is found
136
// For MatchBefore the search ends when the reference item is found
137
if (match == MatchBefore && (*reference) == (*inst))
141
std::string item_name = (*inst)->name();
143
int compare_result = (find_mode == FindPrefix) ? item_name.compare(0, name.length(), name) : item_name.compare(name);
145
// index will contain always the position of the last matched entry
146
if (compare_result == 0)
148
index = item_data.get_index(*inst);
150
// MatchBefore needs to search until the reference is found
151
// MatchLast needs to search until the whole list has been searched to get the last match
152
// MatchAfter and MatchAny are done as soon as a match is found
153
if (match != MatchBefore && match != MatchLast)
159
// For MatchAfter the search starts once the reference item has been found
160
else if ((*reference) == (*inst))
161
search_enabled = true;
168
void move_list_ref_item(MoveType move_type, grt::ListRef<T> items, const grt::ValueRef &object)
170
grt::Type object_type = object.type();
172
std::string group_name;
173
std::string item_name;
174
std::string search_name = "";
175
size_t group_indicator_position = std::string::npos;
177
MatchType match = (move_type == MoveUp) ? MatchBefore : MatchAfter;
181
// Gets the relevant index for the selected object
182
size_t item_index = grt::BaseListRef::npos;
183
if (object_type == grt::ObjectType)
185
item = grt::Ref<T>::cast_from(object);
187
item_index = items.get_index( item );
188
item_name = item->name();
189
group_indicator_position = item_name.find("/");
191
// When a grouped item is selected, the movement will be done across same
193
if (group_indicator_position != std::string::npos)
194
search_name = item_name.substr(0, group_indicator_position + 1);
198
group_name = object.repr();
201
// Searchses the index of the initial element of the group position
202
item_index = find_list_ref_item_position<T>(items, group_name);
203
item = items[item_index];
204
item_name = group_name;
208
// This is executed whenever the target position depends on the main list
209
// The only case where this is excluded is when the selected item belongs to a group
210
if (group_indicator_position == std::string::npos)
212
std::vector<std::string> items_list;
214
// Gets the main list items ( groups and non grouped items )
215
for ( grt::TypedListConstIterator<T> end = items.end(),
216
inst = items.begin(); inst != end; ++inst)
218
std::string item_name = (*inst)->name();
219
size_t position = item_name.find("/");
221
if (position != std::string::npos)
223
std::string group_name = item_name.substr(0, position + 1);
224
if (std::find(items_list.begin(), items_list.end(), group_name) == items_list.end())
225
items_list.push_back(group_name);
228
items_list.push_back(item_name);
231
// Searches the item inside the list of non grouped items/groups, will find it only if it's a non grouped item
232
size_t item_list_position = std::find(items_list.begin(), items_list.end(), std::string(item_name)) - items_list.begin();
234
size_t offset = ( move_type == MoveUp ) ? -1 : 1;
236
item_name = items_list.at(item_list_position + offset);
238
// If the next item is a group
239
group_indicator_position = item_name.find("/");
241
if(group_indicator_position != std::string::npos)
243
search_name = item_name.substr(0, group_indicator_position + 1);
244
if (move_type == MoveUp)
248
search_name = item_name;
251
// Searchses the index of the target position
252
size_t target_index = grt::BaseListRef::npos;
253
target_index = find_list_ref_item_position<T>(items, search_name, match, &item);
255
if (move_type == MoveDown)
256
items.reorder(target_index, item_index);
258
items.reorder(item_index, target_index);
262
//! \brief RAII style GMutex locker/unlocker
263
class WBPUBLICBACKEND_PUBLIC_FUNC GMutexLock
268
GMutexLock(GMutex *mtx) : mutex(mtx)
273
GMutexLock(const GMutexLock &lock)
275
GMutexLock *lock_= const_cast<GMutexLock*>(&lock);
292
g_mutex_unlock(mutex);
296
GMutexLock & operator=(GMutexLock& lock)
298
mutex= lock.release();
303
class WBPUBLICBACKEND_PUBLIC_FUNC GMutexTryLock : public GMutexLock
306
GMutexTryLock(GMutex *mtx) : GMutexLock(0)
308
if (!g_mutex_trylock(mtx))
316
return mutex != NULL;
320
//! \brief RAII style GStaticMutex locker/unlocker
321
class WBPUBLICBACKEND_PUBLIC_FUNC GStaticMutexLock // Note: must compile with strict-aliasing disabled (-fno-strict-aliasing in gcc)
325
GStaticMutexLock(GStaticMutex& mtx);
329
class WBPUBLICBACKEND_PUBLIC_FUNC GStaticRecMutexLock // Note: must compile with strict-aliasing disabled (-fno-strict-aliasing in gcc)
331
GStaticRecMutex &mutex;
333
GStaticRecMutexLock(GStaticRecMutex& mtx);
334
~GStaticRecMutexLock();
337
class WBPUBLICBACKEND_PUBLIC_FUNC TimerActionThread
340
typedef boost::function<void ()> Action;
341
static TimerActionThread * create(const Action &action, gulong milliseconds);
342
~TimerActionThread();
343
void stop(bool clear_exit_signal);
344
boost::signals2::signal<void ()> on_exit;
346
GMutex *_action_mutex;
348
gulong _microseconds;
350
TimerActionThread(const Action &action, gulong milliseconds);
351
static gpointer start(gpointer data);
355
class WBPUBLICBACKEND_PUBLIC_FUNC ScopeExitTrigger
358
typedef boost::function<void ()> Slot;
359
ScopeExitTrigger() {}
360
ScopeExitTrigger(const Slot &cb) : slot(cb) {}
361
~ScopeExitTrigger() { slot(); }
362
ScopeExitTrigger & operator=(const Slot &cb) { slot= cb; return *this; }
368
#endif /* _BE_COMMON_H_ */