Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

utility.h

Go to the documentation of this file.
00001 #ifndef UTILITY_H
00002 #define UTILITY_H
00003 
00004 
00005 #ifdef _MSC_VER
00006 #pragma warning(disable : 4786)
00007 #endif
00008 
00009 
00010 #include <map>
00011 #include <string>
00012 #include <utility>
00013 #include "audiere.h"
00014 #include "types.h"
00015 
00016 
00017 #if defined(_MSC_VER) && _MSC_VER <= 1200
00018 
00019   // std::min and std::max are broken in VC++ 6, so define our own.
00020   // Unfortunately, this means we must include utility.h to use
00021   // std::min and std::max
00022   namespace std {
00023 
00024     #ifdef min
00025       #undef min
00026     #endif
00027 
00028     #ifdef max
00029       #undef max
00030     #endif
00031 
00032     template<typename T>
00033     inline T min(T a, T b) {
00034       return (a < b ? a : b);
00035     }
00036 
00037     template<typename T>
00038     inline T max(T a, T b) {
00039       return (a > b ? a : b);
00040     }
00041   }
00042 
00043 #else
00044 
00045   #include <algorithm>
00046 
00047 #endif
00048 
00049 
00050 namespace audiere {
00051 
00052   class ParameterList {
00053   public:
00054     ParameterList(const char* parameters);
00055     std::string getValue(std::string key, std::string defValue) const;
00056 
00057   private:
00058     std::map<std::string, std::string> m_values;
00059   };
00060 
00061   int strcmp_case(const char* a, const char* b);
00062 
00063 
00064   inline u16 read16_le(const u8* b) {
00065     return b[0] + (b[1] << 8);
00066   }
00067 
00068   inline u16 read16_be(const u8* b) {
00069     return (b[0] << 8) + b[1];
00070   }
00071 
00072   inline u32 read32_le(const u8* b) {
00073     return read16_le(b) + (read16_le(b + 2) << 16);
00074   }
00075 
00076   inline u32 read32_be(const u8* b) {
00077     return (read16_be(b) << 16) + read16_be(b + 2);
00078   }
00079 
00080 
00081   inline int GetFileLength(File* file) {
00082     int pos = file->tell();
00083     file->seek(0, File::END);
00084     int length = file->tell();
00085     file->seek(pos, File::BEGIN);
00086     return length;
00087   }
00088 
00089 
00090   class UnseekableSource : public RefImplementation<SampleSource> {
00091   public:
00092     bool ADR_CALL isSeekable()                  { return false; }
00093     int  ADR_CALL getLength()                   { return 0;     }
00094     void ADR_CALL setPosition(int /*position*/) {               }
00095     int  ADR_CALL getPosition()                 { return 0;     }
00096   };
00097 
00098 
00099   inline SampleSource* OpenBufferStream(
00100     void* samples, int sample_count,
00101     int channel_count, int sample_rate, SampleFormat sample_format)
00102   {
00103     return CreateSampleBuffer(
00104       samples, sample_count,
00105       channel_count, sample_rate, sample_format)->openStream();
00106   }
00107 
00108 
00109   class QueueBuffer {
00110   public:
00111     QueueBuffer() {
00112       m_capacity = 256;
00113       m_size = 0;
00114 
00115       m_buffer = (u8*)malloc(m_capacity);
00116     }
00117 
00118     ~QueueBuffer() {
00119       m_buffer = (u8*)realloc(m_buffer, 0);
00120     }
00121 
00122     int getSize() {
00123       return m_size;
00124     }
00125 
00126     void write(void* buffer, int size) {
00127       bool need_realloc = false;
00128       while (size + m_size > m_capacity) {
00129         m_capacity *= 2;
00130         need_realloc = true;
00131       }
00132 
00133       if (need_realloc) {
00134         m_buffer = (u8*)realloc(m_buffer, m_capacity);
00135       }
00136 
00137       memcpy(m_buffer + m_size, buffer, size);
00138       m_size += size;
00139     }
00140 
00141     int read(void* buffer, int size) {
00142       int to_read = std::min(size, m_size);
00143       memcpy(buffer, m_buffer, to_read);
00144       memmove(m_buffer, m_buffer + to_read, m_size - to_read);
00145       m_size -= to_read;
00146       return to_read;
00147     }
00148 
00149     void clear() {
00150       m_size = 0;
00151     }
00152 
00153   private:
00154     u8* m_buffer;
00155     int m_capacity;
00156     int m_size;
00157 
00158     // private and unimplemented to prevent their use
00159     QueueBuffer(const QueueBuffer&);
00160     QueueBuffer& operator=(const QueueBuffer&);
00161   };
00162 
00163 
00164   class SizedBuffer {
00165   public:
00166     SizedBuffer() {
00167       m_capacity = 256;
00168       m_buffer = malloc(m_capacity);
00169     }
00170 
00171     ~SizedBuffer() {
00172       m_buffer = realloc(m_buffer, 0);
00173     }
00174 
00175     void ensureSize(int size) {
00176       bool need_realloc = false;
00177       while (m_capacity < size) {
00178         m_capacity *= 2;
00179         need_realloc = true;
00180       }
00181       if (need_realloc) {
00182         m_buffer = realloc(m_buffer, m_capacity);
00183       }
00184     }
00185 
00186     void* get() {
00187       return m_buffer;
00188     }
00189 
00190   private:
00191     void* m_buffer;
00192     int m_capacity;
00193 
00194     // private and unimplemented to prevent their use
00195     SizedBuffer(const SizedBuffer&);
00196     SizedBuffer& operator=(const SizedBuffer&);
00197   };
00198 
00199 }
00200 
00201 
00202 #endif

Generated on Sat Oct 12 01:43:04 2002 for audiere by doxygen1.2.17