libcdio  0.90
util.h
Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2004, 2005, 2006, 2008, 2010, 2012
00003     Rocky Bernstein <rocky@gnu.org>
00004     Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
00005 
00006     This program is free software: you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation, either version 3 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #ifndef CDIO_UTIL_H_
00021 #define CDIO_UTIL_H_
00022 
00029 #include <stdlib.h>
00030 #include <cdio/types.h>
00031 
00032 #if !defined CDIO_INLINE
00033 #if defined(__cplusplus) || defined(inline)
00034 #define CDIO_INLINE inline
00035 #elif defined(__GNUC__)
00036 #define CDIO_INLINE __inline__
00037 #elif defined(_MSC_VER)
00038 #define CDIO_INLINE __inline
00039 #else
00040 #define CDIO_INLINE
00041 #endif
00042 #endif /* CDIO_INLINE */
00043 
00044 #undef  MAX
00045 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
00046 
00047 #undef  MIN
00048 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
00049 
00050 #undef  IN
00051 #define IN(x, low, high) ((x) >= (low) && (x) <= (high))
00052 
00053 #undef  CLAMP
00054 #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
00055 
00056 static CDIO_INLINE uint32_t
00057 _cdio_len2blocks (uint32_t i_len, uint16_t i_blocksize)
00058 {
00059   uint32_t i_blocks;
00060 
00061   i_blocks = i_len / (uint32_t) i_blocksize;
00062   if (i_len % i_blocksize)
00063     i_blocks++;
00064 
00065   return i_blocks;
00066 }
00067 
00068 /* round up to next block boundary */
00069 static CDIO_INLINE unsigned 
00070 _cdio_ceil2block (unsigned offset, uint16_t i_blocksize)
00071 {
00072   return _cdio_len2blocks (offset, i_blocksize) * i_blocksize;
00073 }
00074 
00075 static CDIO_INLINE unsigned int
00076 _cdio_ofs_add (unsigned offset, unsigned length, uint16_t i_blocksize)
00077 {
00078   if (i_blocksize - (offset % i_blocksize) < length)
00079     offset = _cdio_ceil2block (offset, i_blocksize);
00080 
00081   offset += length;
00082 
00083   return offset;
00084 }
00085 
00086 static CDIO_INLINE const char *
00087 _cdio_bool_str (bool b)
00088 {
00089   return b ? "yes" : "no";
00090 }
00091 
00092 #ifdef __cplusplus
00093 extern "C" {
00094 #endif
00095 
00096 void *
00097 _cdio_memdup (const void *mem, size_t count);
00098 
00099 char *
00100 _cdio_strdup_upper (const char str[]);
00101 
00102 /* Duplicate path and make it platform compliant. Typically needed for
00103    MinGW/MSYS where a "/c/..." path must be translated to "c:/..." for
00104    use with fopen(), etc. Returned string must be freed by the caller. */
00105 char * 
00106 _cdio_strdup_fixpath (const char path[]);
00107 
00108 void
00109 _cdio_strfreev(char **strv);
00110 
00111 size_t
00112 _cdio_strlenv(char **str_array);
00113 
00114 char **
00115 _cdio_strsplit(const char str[], char delim);
00116 
00117 uint8_t cdio_to_bcd8(uint8_t n);
00118 uint8_t cdio_from_bcd8(uint8_t p);
00119 
00122 char *cdio_realpath (const char *psz_src, char *psz_dst);
00123 
00124 #ifdef WANT_FOLLOW_SYMLINK_COMPATIBILITY
00125 # define cdio_follow_symlink cdio_realpath
00126 #endif
00127   
00128 #ifdef __cplusplus
00129 }
00130 #endif
00131 
00132 #endif /* CDIO_UTIL_H_ */
00133 
00134 
00135 /* 
00136  * Local variables:
00137  *  c-file-style: "gnu"
00138  *  tab-width: 8
00139  *  indent-tabs-mode: nil
00140  * End:
00141  */