00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #if defined HAVE_CONFIG_H
00028 #include <config.h>
00029 #endif
00030
00031 #if defined HAVE_ICONV_H
00032 # include <iconv.h>
00033 # include <errno.h>
00034 #endif
00035
00036 #include <ctype.h>
00037 #include <iostream.h>
00038
00039 #ifdef macintosh
00040 #define NOCREATE ((std::ios_base::openmode)0)
00041 #define toascii(X) (X)
00042 #else
00043 #define NOCREATE ios::nocreate
00044 #endif
00045
00046 #include "utils.h"
00047
00048 using namespace dami;
00049
00050 size_t dami::renderNumber(uchar *buffer, uint32 val, size_t size)
00051 {
00052 uint32 num = val;
00053 for (size_t i = 0; i < size; i++)
00054 {
00055 buffer[size - i - 1] = (uchar)(num & MASK8);
00056 num >>= 8;
00057 }
00058 return size;
00059 }
00060
00061 String dami::renderNumber(uint32 val, size_t size)
00062 {
00063 String str(size, '\0');
00064 uint32 num = val;
00065 for (size_t i = 0; i < size; i++)
00066 {
00067 str[size - i - 1] = (uchar)(num & MASK8);
00068 num >>= 8;
00069 }
00070 return str;
00071 }
00072
00073
00074 namespace
00075 {
00076 #if !defined(HAVE_ICONV_H)
00077
00078 String mbstoucs(String data)
00079 {
00080 size_t size = data.size();
00081 String unicode(size * 2, '\0');
00082 for (index_t i = 0; i < size; i++)
00083 {
00084 unicode[i*2+1] = toascii(data[i]);
00085 }
00086 return unicode;
00087 }
00088
00089
00090
00091 String ucstombs(String data)
00092 {
00093 size_t size = data.size() / 2;
00094 String ascii(size, '\0');
00095 for (index_t i = 0; i < size; i++)
00096 {
00097 ascii[i] = toascii(data[i*2+1]);
00098 }
00099 return ascii;
00100 }
00101 #else
00102
00103 String convert_i(iconv_t cd, String source)
00104 {
00105 String target;
00106 size_t source_size = source.size();
00107
00108 char * source_str = new char[source.length()+1];
00109 source.copy(source_str, string::npos);
00110 source_str[source.length()] = 0;
00111
00112 #define BUFSIZ 1024
00113 char buf[BUFSIZ];
00114 char* target_str = buf;
00115 size_t target_size = BUFSIZ;
00116
00117 do
00118 {
00119 size_t nconv = iconv(cd,
00120 &source_str, &source_size,
00121 &target_str, &target_size);
00122 if (nconv == (size_t) -1 && errno != EINVAL && errno != E2BIG)
00123 {
00124 return target;
00125 }
00126 target.append(buf, BUFSIZ - target_size);
00127 target_str = buf;
00128 target_size = BUFSIZ;
00129 }
00130 while (source_size > 0);
00131 return target;
00132 }
00133
00134 const char* getFormat(ID3_TextEnc enc)
00135 {
00136 const char* format = NULL;
00137 switch (enc)
00138 {
00139 case ID3TE_ASCII:
00140 format = ID3_ICONV_FORMAT_ASCII;
00141 break;
00142
00143 case ID3TE_UTF16:
00144 format = ID3_ICONV_FORMAT_UTF16;
00145 break;
00146
00147 case ID3TE_UTF16BE:
00148 format = ID3_ICONV_FORMAT_UTF16BE;
00149 break;
00150
00151 case ID3TE_UTF8:
00152 format = ID3_ICONV_FORMAT_UTF8;
00153 break;
00154
00155 default:
00156 break;
00157 }
00158 return format;
00159 }
00160 #endif
00161 }
00162
00163 String dami::convert(String data, ID3_TextEnc sourceEnc, ID3_TextEnc targetEnc)
00164 {
00165 String target;
00166 #if !defined HAVE_ICONV_H
00167 #define ID3_IS_ASCII(enc) \
00168 ((enc) == ID3TE_ASCII || \
00169 (enc) == ID3TE_ISO8859_1 || \
00170 (enc) == ID3TE_UTF8)
00171 #define ID3_IS_UNICODE(enc) \
00172 ((enc) == ID3TE_UNICODE || \
00173 (enc) == ID3TE_UTF16 || \
00174 (enc) == ID3TE_UTF16BE)
00175 if (ID3_IS_ASCII(sourceEnc) && ID3_IS_UNICODE(targetEnc))
00176 {
00177 target = mbstoucs(data);
00178 }
00179 else if (ID3_IS_UNICODE(sourceEnc) && ID3_IS_ASCII(targetEnc))
00180 {
00181 target = ucstombs(data);
00182 }
00183 #else
00184 if (sourceEnc != targetEnc)
00185 {
00186 const char* targetFormat = getFormat(targetEnc);
00187 const char* sourceFormat = getFormat(sourceEnc);
00188
00189 iconv_t cd = iconv_open (targetFormat, sourceFormat);
00190 if (cd != (iconv_t) -1)
00191 {
00192 target = convert_i(cd, data);
00193 }
00194 iconv_close (cd);
00195 }
00196 #endif
00197 return target;
00198 }
00199
00200 size_t dami::ucslen(const unicode_t *unicode)
00201 {
00202 if (NULL != unicode)
00203 {
00204 for (size_t size = 0; true; size++)
00205 {
00206 if (NULL_UNICODE == unicode[size])
00207 {
00208 return size;
00209 }
00210 }
00211 }
00212 return 0;
00213 }
00214
00215 namespace
00216 {
00217 bool exists(String name)
00218 {
00219 ifstream file(name.c_str(), NOCREATE);
00220 return file.is_open() != 0;
00221 }
00222 };
00223
00224 ID3_Err dami::createFile(String name, fstream& file)
00225 {
00226 if (file.is_open())
00227 {
00228 file.close();
00229 }
00230
00231 file.open(name.c_str(), ios::out | ios::binary | ios::trunc);
00232 if (!file)
00233 {
00234 return ID3E_ReadOnly;
00235 }
00236
00237 return ID3E_NoError;
00238 }
00239
00240 size_t dami::getFileSize(fstream& file)
00241 {
00242 size_t size = 0;
00243 if (file.is_open())
00244 {
00245 streamoff curpos = file.tellg();
00246 file.seekg(0, ios::end);
00247 size = file.tellg();
00248 file.seekg(curpos);
00249 }
00250 return size;
00251 }
00252
00253 size_t dami::getFileSize(ifstream& file)
00254 {
00255 size_t size = 0;
00256 if (file.is_open())
00257 {
00258 streamoff curpos = file.tellg();
00259 file.seekg(0, ios::end);
00260 size = file.tellg();
00261 file.seekg(curpos);
00262 }
00263 return size;
00264 }
00265
00266 size_t dami::getFileSize(ofstream& file)
00267 {
00268 size_t size = 0;
00269 if (file.is_open())
00270 {
00271 streamoff curpos = file.tellp();
00272 file.seekp(0, ios::end);
00273 size = file.tellp();
00274 file.seekp(curpos);
00275 }
00276 return size;
00277 }
00278
00279 ID3_Err dami::openWritableFile(String name, fstream& file)
00280 {
00281 if (!exists(name))
00282 {
00283 return ID3E_NoFile;
00284 }
00285
00286 if (file.is_open())
00287 {
00288 file.close();
00289 }
00290 file.open(name.c_str(), ios::in | ios::out | ios::binary | NOCREATE);
00291 if (!file)
00292 {
00293 return ID3E_ReadOnly;
00294 }
00295
00296 return ID3E_NoError;
00297 }
00298
00299 ID3_Err dami::openWritableFile(String name, ofstream& file)
00300 {
00301 if (!exists(name))
00302 {
00303 return ID3E_NoFile;
00304 }
00305
00306 if (file.is_open())
00307 {
00308 file.close();
00309 }
00310 file.open(name.c_str(), ios::in | ios::out | ios::binary | NOCREATE);
00311 if (!file)
00312 {
00313 return ID3E_ReadOnly;
00314 }
00315
00316 return ID3E_NoError;
00317 }
00318
00319 ID3_Err dami::openReadableFile(String name, fstream& file)
00320 {
00321 if (file.is_open())
00322 {
00323 file.close();
00324 }
00325 file.open(name.c_str(), ios::in | ios::binary | NOCREATE);
00326 if (!file)
00327 {
00328 return ID3E_NoFile;
00329 }
00330
00331 return ID3E_NoError;
00332 }
00333
00334 ID3_Err dami::openReadableFile(String name, ifstream& file)
00335 {
00336 if (file.is_open())
00337 {
00338 file.close();
00339 }
00340 file.open(name.c_str(), ios::in | ios::binary | NOCREATE);
00341 if (!file)
00342 {
00343 return ID3E_NoFile;
00344 }
00345
00346 return ID3E_NoError;
00347 }
00348
00349 String dami::toString(uint32 val)
00350 {
00351 if (val == 0)
00352 {
00353 return "0";
00354 }
00355 String text;
00356 while (val > 0)
00357 {
00358 String tmp;
00359 char ch = (val % 10) + '0';
00360 tmp += ch;
00361 text = tmp + text;
00362 val /= 10;
00363 }
00364 return text;
00365 }
00366
00367 WString dami::toWString(const unicode_t buf[], size_t len)
00368 {
00369 WString str;
00370 str.reserve(len);
00371
00372 for (size_t i = 0; i < len; ++i)
00373 {
00374 str += static_cast<WString::value_type>(buf[i]);
00375 }
00376 return str;
00377 }