/[svn]/libgig/trunk/src/helper.h
ViewVC logotype

Annotation of /libgig/trunk/src/helper.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1330 - (hide annotations) (download) (as text)
Sun Sep 9 10:36:23 2007 UTC (16 years, 7 months ago) by persson
File MIME type: text/x-c++hdr
File size: 7183 byte(s)
* the configure script can now be used in Windows with MSYS

1 schoenebeck 803 /***************************************************************************
2     * *
3 schoenebeck 933 * libgig - C++ cross-platform Gigasampler format file access library *
4 schoenebeck 803 * *
5 schoenebeck 929 * Copyright (C) 2003-2006 by Christian Schoenebeck *
6 schoenebeck 803 * <cuse@users.sourceforge.net> *
7     * *
8     * This library is free software; you can redistribute it and/or modify *
9     * it under the terms of the GNU General Public License as published by *
10     * the Free Software Foundation; either version 2 of the License, or *
11     * (at your option) any later version. *
12     * *
13     * This library is distributed in the hope that it will be useful, *
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16     * GNU General Public License for more details. *
17     * *
18     * You should have received a copy of the GNU General Public License *
19     * along with this library; if not, write to the Free Software *
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23    
24     #ifndef __LIBGIG_HELPER_H__
25     #define __LIBGIG_HELPER_H__
26    
27 schoenebeck 809 #include <string.h>
28 schoenebeck 803 #include <string>
29     #include <sstream>
30    
31 persson 1330 #if defined(WIN32) && !HAVE_CONFIG_H
32     # include "../win32/libgig_private.h" // like config.h, automatically generated by Dev-C++
33     # define PACKAGE "libgig"
34     # define VERSION VER_STRING // VER_STRING defined in libgig_private.h
35     #endif // WIN32
36    
37 schoenebeck 929 #include "RIFF.h"
38    
39 schoenebeck 803 // *************** Helper Functions **************
40     // *
41    
42     template<class T> inline std::string ToString(T o) {
43     std::stringstream ss;
44     ss << o;
45     return ss.str();
46     }
47    
48 schoenebeck 809 inline long Min(long A, long B) {
49     return (A > B) ? B : A;
50     }
51    
52     inline long Abs(long val) {
53     return (val > 0) ? val : -val;
54     }
55    
56     /**
57 persson 1179 * Stores a 16 bit integer in memory using little-endian format.
58     *
59     * @param pData - memory pointer
60     * @param data - integer to be stored
61     */
62     inline void store16(uint8_t* pData, uint16_t data) {
63     pData[0] = data;
64     pData[1] = data >> 8;
65     }
66    
67     /**
68     * Stores a 32 bit integer in memory using little-endian format.
69     *
70     * @param pData - memory pointer
71     * @param data - integer to be stored
72     */
73     inline void store32(uint8_t* pData, uint32_t data) {
74     pData[0] = data;
75     pData[1] = data >> 8;
76     pData[2] = data >> 16;
77     pData[3] = data >> 24;
78     }
79    
80     /**
81 schoenebeck 809 * Swaps the order of the data words in the given memory area
82     * with a granularity given by \a WordSize.
83     *
84     * @param pData - pointer to the memory area to be swapped
85     * @param AreaSize - size of the memory area to be swapped (in bytes)
86     * @param WordSize - size of the data words (in bytes)
87     */
88     inline void SwapMemoryArea(void* pData, unsigned long AreaSize, uint WordSize) {
89     switch (WordSize) { // TODO: unefficient
90     case 1: {
91     uint8_t* pDst = (uint8_t*) pData;
92     uint8_t cache;
93     unsigned long lo = 0, hi = AreaSize - 1;
94     for (; lo < hi; hi--, lo++) {
95     cache = pDst[lo];
96     pDst[lo] = pDst[hi];
97     pDst[hi] = cache;
98     }
99     break;
100     }
101     case 2: {
102     uint16_t* pDst = (uint16_t*) pData;
103     uint16_t cache;
104     unsigned long lo = 0, hi = (AreaSize >> 1) - 1;
105     for (; lo < hi; hi--, lo++) {
106     cache = pDst[lo];
107     pDst[lo] = pDst[hi];
108     pDst[hi] = cache;
109     }
110     break;
111     }
112     case 4: {
113     uint32_t* pDst = (uint32_t*) pData;
114     uint32_t cache;
115     unsigned long lo = 0, hi = (AreaSize >> 2) - 1;
116     for (; lo < hi; hi--, lo++) {
117     cache = pDst[lo];
118     pDst[lo] = pDst[hi];
119     pDst[hi] = cache;
120     }
121     break;
122     }
123     default: {
124     uint8_t* pCache = new uint8_t[WordSize]; // TODO: unefficient
125     unsigned long lo = 0, hi = AreaSize - WordSize;
126     for (; lo < hi; hi -= WordSize, lo += WordSize) {
127     memcpy(pCache, (uint8_t*) pData + lo, WordSize);
128     memcpy((uint8_t*) pData + lo, (uint8_t*) pData + hi, WordSize);
129     memcpy((uint8_t*) pData + hi, pCache, WordSize);
130     }
131     delete[] pCache;
132     break;
133     }
134     }
135     }
136    
137 schoenebeck 929 /** @brief Load given info field (string).
138     *
139     * Load info field string from given info chunk (\a ck) and save value to \a s.
140     */
141     inline void LoadString(RIFF::Chunk* ck, std::string& s) {
142     if (ck) {
143     const char* str = (char*)ck->LoadChunkData();
144     int size = ck->GetSize();
145     int len;
146     for (len = 0 ; len < size ; len++)
147     if (str[len] == '\0') break;
148     s.assign(str, len);
149     ck->ReleaseChunkData();
150     }
151     }
152    
153     /** @brief Apply given INFO field to the respective chunk.
154     *
155     * Apply given info value string to given info chunk, which is a
156     * subchunk of INFO list chunk \a lstINFO. If the given chunk already
157     * exists, value \a s will be applied. Otherwise if it doesn't exist yet
158     * and either \a s or \a sDefault is not an empty string, such a chunk
159     * will be created and either \a s or \a sDefault will be applied
160     * (depending on which one is not an empty string, if both are not an
161     * empty string \a s will be preferred).
162     *
163     * @param ChunkID - 32 bit RIFF chunk ID of INFO subchunk (only used in case \a ck is NULL)
164     * @param ck - INFO (sub)chunk where string should be stored to
165     * @param lstINFO - parent (INFO) RIFF list chunk
166     * @param s - current value of info field
167     * @param sDefault - default value
168     * @param bUseFixedLengthStrings - should a specific string size be forced in the chunk?
169     * @param size - wanted size of the INFO chunk. This is ignored if bUseFixedLengthStrings is false.
170     */
171     inline void SaveString(uint32_t ChunkID, RIFF::Chunk* ck, RIFF::List* lstINFO, const std::string& s, const std::string& sDefault, bool bUseFixedLengthStrings, int size) {
172     if (ck) { // if chunk exists already, use 's' as value
173     if (!bUseFixedLengthStrings) size = s.size() + 1;
174     ck->Resize(size);
175     char* pData = (char*) ck->LoadChunkData();
176     strncpy(pData, s.c_str(), size);
177 persson 1180 } else if (s != "" || sDefault != "" || bUseFixedLengthStrings) { // create chunk
178 schoenebeck 929 const std::string& sToSave = (s != "") ? s : sDefault;
179     if (!bUseFixedLengthStrings) size = sToSave.size() + 1;
180     ck = lstINFO->AddSubChunk(ChunkID, size);
181     char* pData = (char*) ck->LoadChunkData();
182     strncpy(pData, sToSave.c_str(), size);
183     }
184     }
185    
186 schoenebeck 803 #endif // __LIBGIG_HELPER_H__

  ViewVC Help
Powered by ViewVC