1 /*
2 Copyright (C) 2004, 2005, 2006, 2008, 2010, 2012, 2014
3 Rocky Bernstein <rocky@gnu.org>
4 Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
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
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef CDIO_UTIL_H_
21 #define CDIO_UTIL_H_
22
23 /*!
24 \file util.h
25 \brief Miscellaneous utility functions.
26
27 Warning: this will probably get removed/replaced by using glib.h
28 */
29 #include <stdlib.h>
30 #include <cdio/types.h>
31
32 #if !defined CDIO_INLINE
33 #if defined(__cplusplus) || defined(inline)
34 #define CDIO_INLINE inline
35 #elif defined(__GNUC__)
36 #define CDIO_INLINE __inline__
37 #elif defined(_MSC_VER)
38 #define CDIO_INLINE __inline
39 #else
40 #define CDIO_INLINE
41 #endif
42 #endif /* CDIO_INLINE */
43
44 #undef MAX
45 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
46
47 #undef MIN
48 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
49
50 #undef IN
51 #define IN(x, low, high) ((x) >= (low) && (x) <= (high))
52
53 #undef CLAMP
54 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
55
56 static CDIO_INLINE uint32_t
_cdio_len2blocks(uint32_t i_len,uint16_t i_blocksize)57 _cdio_len2blocks (uint32_t i_len, uint16_t i_blocksize)
58 {
59 uint32_t i_blocks;
60
61 i_blocks = i_len / (uint32_t) i_blocksize;
62 if (i_len % i_blocksize)
63 i_blocks++;
64
65 return i_blocks;
66 }
67
68
69 /*! free() and NULL out p_obj it is not already null. */
70 #define CDIO_FREE_IF_NOT_NULL(p_obj) \
71 if (NULL != p_obj) { free(p_obj); p_obj=NULL; };
72
73
74 /* round up to next block boundary */
75 static CDIO_INLINE unsigned
_cdio_ceil2block(unsigned offset,uint16_t i_blocksize)76 _cdio_ceil2block (unsigned offset, uint16_t i_blocksize)
77 {
78 return _cdio_len2blocks (offset, i_blocksize) * i_blocksize;
79 }
80
81 static CDIO_INLINE unsigned int
_cdio_ofs_add(unsigned offset,unsigned length,uint16_t i_blocksize)82 _cdio_ofs_add (unsigned offset, unsigned length, uint16_t i_blocksize)
83 {
84 if (i_blocksize - (offset % i_blocksize) < length)
85 offset = _cdio_ceil2block (offset, i_blocksize);
86
87 offset += length;
88
89 return offset;
90 }
91
92 static CDIO_INLINE const char *
_cdio_bool_str(bool b)93 _cdio_bool_str (bool b)
94 {
95 return b ? "yes" : "no";
96 }
97
98 #ifdef __cplusplus
99 extern "C" {
100 #endif
101
102 void *
103 _cdio_memdup (const void *mem, size_t count);
104
105 char *
106 _cdio_strdup_upper (const char str[]);
107
108 /*! Duplicate path and make it platform compliant. Typically needed for
109 MinGW/MSYS where a "/c/..." path must be translated to "c:/..." for
110 use with fopen(), etc. Returned string must be freed by the caller
111 using cdio_free(). */
112 char *
113 _cdio_strdup_fixpath (const char path[]);
114
115 void
116 _cdio_strfreev(char **strv);
117
118 size_t
119 _cdio_strlenv(char **str_array);
120
121 char **
122 _cdio_strsplit(const char str[], char delim);
123
124 uint8_t cdio_to_bcd8(uint8_t n);
125 uint8_t cdio_from_bcd8(uint8_t p);
126
127 /*! cdio_realpath() same as POSIX.1-2001 realpath if that's
128 around. If not we do poor-man's simulation of that behavior. */
129 char *cdio_realpath (const char *psz_src, char *psz_dst);
130
131 #ifdef WANT_FOLLOW_SYMLINK_COMPATIBILITY
132 # define cdio_follow_symlink cdio_realpath
133 #endif
134
135 #ifdef __cplusplus
136 }
137 #endif
138
139 #endif /* CDIO_UTIL_H_ */
140
141
142 /*
143 * Local variables:
144 * c-file-style: "gnu"
145 * tab-width: 8
146 * indent-tabs-mode: nil
147 * End:
148 */
149