libcdio  0.93
util.h
Go to the documentation of this file.
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 
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
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 /* round up to next block boundary */
69 static CDIO_INLINE unsigned
70 _cdio_ceil2block (unsigned offset, uint16_t i_blocksize)
71 {
72  return _cdio_len2blocks (offset, i_blocksize) * i_blocksize;
73 }
74 
75 static CDIO_INLINE unsigned int
76 _cdio_ofs_add (unsigned offset, unsigned length, uint16_t i_blocksize)
77 {
78  if (i_blocksize - (offset % i_blocksize) < length)
79  offset = _cdio_ceil2block (offset, i_blocksize);
80 
81  offset += length;
82 
83  return offset;
84 }
85 
86 static CDIO_INLINE const char *
87 _cdio_bool_str (bool b)
88 {
89  return b ? "yes" : "no";
90 }
91 
92 #ifdef __cplusplus
93 extern "C" {
94 #endif
95 
96 void *
97 _cdio_memdup (const void *mem, size_t count);
98 
99 char *
100 _cdio_strdup_upper (const char str[]);
101 
102 /* Duplicate path and make it platform compliant. Typically needed for
103  MinGW/MSYS where a "/c/..." path must be translated to "c:/..." for
104  use with fopen(), etc. Returned string must be freed by the caller
105  using cdio_free(). */
106 char *
107 _cdio_strdup_fixpath (const char path[]);
108 
109 void
110 _cdio_strfreev(char **strv);
111 
112 size_t
113 _cdio_strlenv(char **str_array);
114 
115 char **
116 _cdio_strsplit(const char str[], char delim);
117 
118 uint8_t cdio_to_bcd8(uint8_t n);
119 uint8_t cdio_from_bcd8(uint8_t p);
120 
123 char *cdio_realpath (const char *psz_src, char *psz_dst);
124 
125 #ifdef WANT_FOLLOW_SYMLINK_COMPATIBILITY
126 # define cdio_follow_symlink cdio_realpath
127 #endif
128 
129 #ifdef __cplusplus
130 }
131 #endif
132 
133 #endif /* CDIO_UTIL_H_ */
134 
135 
136 /*
137  * Local variables:
138  * c-file-style: "gnu"
139  * tab-width: 8
140  * indent-tabs-mode: nil
141  * End:
142  */
Common type definitions used pervasively in libcdio.
uint8_t cdio_from_bcd8(uint8_t p)
char ** _cdio_strsplit(const char str[], char delim)
void _cdio_strfreev(char **strv)
uint8_t cdio_to_bcd8(uint8_t n)
char * _cdio_strdup_fixpath(const char path[])
char * _cdio_strdup_upper(const char str[])
size_t _cdio_strlenv(char **str_array)
char * cdio_realpath(const char *psz_src, char *psz_dst)
#define CDIO_INLINE
Definition: util.h:40
void * _cdio_memdup(const void *mem, size_t count)