4 * Copyright (C) 2008 Krzysztof Foltman
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 #ifndef __CALF_UTILS_H
22 #define __CALF_UTILS_H
32 /// Pthreads based mutex class
38 ptmutex(int type
= PTHREAD_MUTEX_RECURSIVE
)
40 pthread_mutexattr_t attr
;
41 pthread_mutexattr_init(&attr
);
42 pthread_mutexattr_settype(&attr
, type
);
43 pthread_mutex_init(&pm
, &attr
);
44 pthread_mutexattr_destroy(&attr
);
49 return pthread_mutex_lock(&pm
) == 0;
54 return pthread_mutex_trylock(&pm
) == 0;
59 pthread_mutex_unlock(&pm
);
64 pthread_mutex_destroy(&pm
);
68 /// Exception-safe mutex lock
75 ptlock(ptmutex
&_m
) : mutex(_m
), locked(true)
95 /// Exception-safe temporary assignment
101 scope_assign(T
&_data
, T new_value
)
102 : data(_data
), old_value(_data
)
112 struct text_exception
: public std::exception
115 std::string container
;
117 text_exception(const std::string
&t
) : container(t
) { text
= container
.c_str(); }
118 virtual const char *what() const throw () { return text
; }
119 virtual ~text_exception() throw () {}
122 struct file_exception
: public std::exception
125 std::string message
, filename
, container
;
127 file_exception(const std::string
&f
) : message(strerror(errno
)), filename(f
), container(filename
+ ":" + message
) { text
= container
.c_str(); }
128 file_exception(const std::string
&f
, const std::string
&t
) : message(t
), filename(f
), container(filename
+ ":" + message
) { text
= container
.c_str(); }
129 virtual const char *what() const throw () { return text
; }
130 virtual ~file_exception() throw () {}
133 /// String-to-string mapping
134 typedef std::map
<std::string
, std::string
> dictionary
;
136 /// Serialize a dictonary to a string
137 extern std::string
encode_map(const dictionary
&data
);
138 /// Deserialize a dictonary from a string
139 extern void decode_map(dictionary
&data
, const std::string
&src
);
142 extern std::string
i2s(int value
);
145 extern std::string
f2s(double value
);
147 /// Escape a string to be used in XML file
148 std::string
xml_escape(const std::string
&src
);
150 /// Load file from disk into a std::string blob, or throw file_exception
151 std::string
load_file(const std::string
&src
);
153 /// Indent a string by another string (prefix each line)
154 std::string
indent(const std::string
&src
, const std::string
&indent
);