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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1179 - (hide annotations) (download) (as text)
Sat May 12 11:25:04 2007 UTC (16 years, 10 months ago) by persson
File MIME type: text/x-c++hdr
File size: 6916 byte(s)
* fixed write support for big-endian systems

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 schoenebeck 929 #include "RIFF.h"
32    
33 schoenebeck 803 // *************** Helper Functions **************
34     // *
35    
36     template<class T> inline std::string ToString(T o) {
37     std::stringstream ss;
38     ss << o;
39     return ss.str();
40     }
41    
42 schoenebeck 809 inline long Min(long A, long B) {
43     return (A > B) ? B : A;
44     }
45    
46     inline long Abs(long val) {
47     return (val > 0) ? val : -val;
48     }
49    
50     /**
51 persson 1179 * Stores a 16 bit integer in memory using little-endian format.
52     *
53     * @param pData - memory pointer
54     * @param data - integer to be stored
55     */
56     inline void store16(uint8_t* pData, uint16_t data) {
57     pData[0] = data;
58     pData[1] = data >> 8;
59     }
60    
61     /**
62     * Stores a 32 bit integer in memory using little-endian format.
63     *
64     * @param pData - memory pointer
65     * @param data - integer to be stored
66     */
67     inline void store32(uint8_t* pData, uint32_t data) {
68     pData[0] = data;
69     pData[1] = data >> 8;
70     pData[2] = data >> 16;
71     pData[3] = data >> 24;
72     }
73    
74     /**
75 schoenebeck 809 * Swaps the order of the data words in the given memory area
76     * with a granularity given by \a WordSize.
77     *
78     * @param pData - pointer to the memory area to be swapped
79     * @param AreaSize - size of the memory area to be swapped (in bytes)
80     * @param WordSize - size of the data words (in bytes)
81     */
82     inline void SwapMemoryArea(void* pData, unsigned long AreaSize, uint WordSize) {
83     switch (WordSize) { // TODO: unefficient
84     case 1: {
85     uint8_t* pDst = (uint8_t*) pData;
86     uint8_t cache;
87     unsigned long lo = 0, hi = AreaSize - 1;
88     for (; lo < hi; hi--, lo++) {
89     cache = pDst[lo];
90     pDst[lo] = pDst[hi];
91     pDst[hi] = cache;
92     }
93     break;
94     }
95     case 2: {
96     uint16_t* pDst = (uint16_t*) pData;
97     uint16_t cache;
98     unsigned long lo = 0, hi = (AreaSize >> 1) - 1;
99     for (; lo < hi; hi--, lo++) {
100     cache = pDst[lo];
101     pDst[lo] = pDst[hi];
102     pDst[hi] = cache;
103     }
104     break;
105     }
106     case 4: {
107     uint32_t* pDst = (uint32_t*) pData;
108     uint32_t cache;
109     unsigned long lo = 0, hi = (AreaSize >> 2) - 1;
110     for (; lo < hi; hi--, lo++) {
111     cache = pDst[lo];
112     pDst[lo] = pDst[hi];
113     pDst[hi] = cache;
114     }
115     break;
116     }
117     default: {
118     uint8_t* pCache = new uint8_t[WordSize]; // TODO: unefficient
119     unsigned long lo = 0, hi = AreaSize - WordSize;
120     for (; lo < hi; hi -= WordSize, lo += WordSize) {
121     memcpy(pCache, (uint8_t*) pData + lo, WordSize);
122     memcpy((uint8_t*) pData + lo, (uint8_t*) pData + hi, WordSize);
123     memcpy((uint8_t*) pData + hi, pCache, WordSize);
124     }
125     delete[] pCache;
126     break;
127     }
128     }
129     }
130    
131 schoenebeck 929 /** @brief Load given info field (string).
132     *
133     * Load info field string from given info chunk (\a ck) and save value to \a s.
134     */
135     inline void LoadString(RIFF::Chunk* ck, std::string& s) {
136     if (ck) {
137     const char* str = (char*)ck->LoadChunkData();
138     int size = ck->GetSize();
139     int len;
140     for (len = 0 ; len < size ; len++)
141     if (str[len] == '\0') break;
142     s.assign(str, len);
143     ck->ReleaseChunkData();
144     }
145     }
146    
147     /** @brief Apply given INFO field to the respective chunk.
148     *
149     * Apply given info value string to given info chunk, which is a
150     * subchunk of INFO list chunk \a lstINFO. If the given chunk already
151     * exists, value \a s will be applied. Otherwise if it doesn't exist yet
152     * and either \a s or \a sDefault is not an empty string, such a chunk
153     * will be created and either \a s or \a sDefault will be applied
154     * (depending on which one is not an empty string, if both are not an
155     * empty string \a s will be preferred).
156     *
157     * @param ChunkID - 32 bit RIFF chunk ID of INFO subchunk (only used in case \a ck is NULL)
158     * @param ck - INFO (sub)chunk where string should be stored to
159     * @param lstINFO - parent (INFO) RIFF list chunk
160     * @param s - current value of info field
161     * @param sDefault - default value
162     * @param bUseFixedLengthStrings - should a specific string size be forced in the chunk?
163     * @param size - wanted size of the INFO chunk. This is ignored if bUseFixedLengthStrings is false.
164     */
165     inline void SaveString(uint32_t ChunkID, RIFF::Chunk* ck, RIFF::List* lstINFO, const std::string& s, const std::string& sDefault, bool bUseFixedLengthStrings, int size) {
166     if (ck) { // if chunk exists already, use 's' as value
167     if (!bUseFixedLengthStrings) size = s.size() + 1;
168     ck->Resize(size);
169     char* pData = (char*) ck->LoadChunkData();
170     strncpy(pData, s.c_str(), size);
171     } else if (s != "" || sDefault != "") { // create chunk
172     const std::string& sToSave = (s != "") ? s : sDefault;
173     if (!bUseFixedLengthStrings) size = sToSave.size() + 1;
174     ck = lstINFO->AddSubChunk(ChunkID, size);
175     char* pData = (char*) ck->LoadChunkData();
176     strncpy(pData, sToSave.c_str(), size);
177     }
178     }
179    
180 schoenebeck 803 #endif // __LIBGIG_HELPER_H__

  ViewVC Help
Powered by ViewVC