1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
// $Id: utils.cpp,v 1.26 2002/07/02 22:04:36 t1mpy Exp $
// id3lib: a C++ library for creating and manipulating id3v1/v2 tags
// Copyright 1999, 2000 Scott Thomas Haug
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Library General Public License as published by
// the Free Software Foundation; either version 2 of the License, or (at your
// option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
// License for more details.
//
// You should have received a copy of the GNU Library General Public License
// along with this library; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// The id3lib authors encourage improvements and optimisations to be sent to
// the id3lib coordinator. Please see the README file for details on where to
// send such submissions. See the AUTHORS file for a list of people who have
// contributed to id3lib. See the ChangeLog file for a list of changes to
// id3lib. These files are distributed with id3lib at
// http://download.sourceforge.net/id3lib/
#include <ctype.h>
#if (defined(__GNUC__) && __GNUC__ == 2)
#define NOCREATE ios::nocreate
#else
#if defined(macintosh) //not sure if this is still needed
#define toascii(X) (X) //not sure if this is still needed
#endif //not sure if this is still needed
#define NOCREATE ((std::ios_base::openmode)0)
#endif
#include "id3/utils.h" // has <config.h> "id3/id3lib_streams.h" "id3/globals.h" "id3/id3lib_strings.h"
#if defined HAVE_ICONV_H
// check if we have all unicodes
#if (defined(ID3_ICONV_FORMAT_UTF16BE) && defined(ID3_ICONV_FORMAT_UTF16) && defined(ID3_ICONV_FORMAT_UTF8) && defined(ID3_ICONV_FORMAT_ASCII))
# include <iconv.h>
# include <errno.h>
#else
# undef HAVE_ICONV_H
#endif
#endif
// converts an ASCII string into a Unicode one
dami::String mbstoucs(dami::String data)
{
size_t size = data.size();
dami::String unicode(size * 2, '\0');
for (size_t i = 0; i < size; ++i)
{
unicode[i*2+1] = toascii(data[i]);
}
return unicode;
}
// converts a Unicode string into ASCII
dami::String ucstombs(dami::String data)
{
size_t size = data.size() / 2;
dami::String ascii(size, '\0');
for (size_t i = 0; i < size; ++i)
{
ascii[i] = toascii(data[i*2+1]);
}
return ascii;
}
dami::String oldconvert(dami::String data, ID3_TextEnc sourceEnc, ID3_TextEnc targetEnc)
{
dami::String target;
#define ID3_IS_ASCII(enc) ((enc) == ID3TE_ASCII || (enc) == ID3TE_ISO8859_1 || (enc) == ID3TE_UTF8)
#define ID3_IS_UNICODE(enc) ((enc) == ID3TE_UNICODE || (enc) == ID3TE_UTF16 || (enc) == ID3TE_UTF16BE)
if (ID3_IS_ASCII(sourceEnc) && ID3_IS_UNICODE(targetEnc))
{
target = mbstoucs(data);
}
else if (ID3_IS_UNICODE(sourceEnc) && ID3_IS_ASCII(targetEnc))
{
target = ucstombs(data);
}
return target;
}
using namespace dami;
size_t dami::renderNumber(uchar *buffer, uint32 val, size_t size)
{
uint32 num = val;
for (size_t i = 0; i < size; i++)
{
buffer[size - i - 1] = (uchar)(num & MASK8);
num >>= 8;
}
return size;
}
String dami::renderNumber(uint32 val, size_t size)
{
String str(size, '\0');
uint32 num = val;
for (size_t i = 0; i < size; i++)
{
str[size - i - 1] = (uchar)(num & MASK8);
num >>= 8;
}
return str;
}
#if defined(HAVE_ICONV_H)
namespace
{
String convert_i(iconv_t cd, String source)
{
String target;
size_t source_size = source.size();
#if defined(ID3LIB_ICONV_OLDSTYLE)
const char* source_str = source.data();
#else
char *source_str = new char[source.size()+1];
source.copy(source_str, String::npos);
source_str[source.length()] = 0;
#endif
#define ID3LIB_BUFSIZ 1024
char buf[ID3LIB_BUFSIZ];
char* target_str = buf;
size_t target_size = ID3LIB_BUFSIZ;
do
{
errno = 0;
size_t nconv = iconv(cd,
&source_str, &source_size,
&target_str, &target_size);
if (nconv == (size_t) -1 && errno != EINVAL && errno != E2BIG)
{
// errno is probably EILSEQ here, which means either an invalid byte sequence or a valid but unconvertible byte sequence
return target;
}
target.append(buf, ID3LIB_BUFSIZ - target_size);
target_str = buf;
target_size = ID3LIB_BUFSIZ;
}
while (source_size > 0);
return target;
}
const char* getFormat(ID3_TextEnc enc)
{
const char* format = NULL;
switch (enc)
{
case ID3TE_ASCII:
format = ID3_ICONV_FORMAT_ASCII;
break;
case ID3TE_UTF16:
format = ID3_ICONV_FORMAT_UTF16;
break;
case ID3TE_UTF16BE:
format = ID3_ICONV_FORMAT_UTF16BE;
break;
case ID3TE_UTF8:
format = ID3_ICONV_FORMAT_UTF8;
break;
default:
break;
}
return format;
}
}
#endif
String dami::convert(String data, ID3_TextEnc sourceEnc, ID3_TextEnc targetEnc)
{
String target;
if ((sourceEnc != targetEnc) && (data.size() > 0 ))
{
#if !defined HAVE_ICONV_H
target = oldconvert(data, sourceEnc, targetEnc);
#else
const char* targetFormat = getFormat(targetEnc);
const char* sourceFormat = getFormat(sourceEnc);
iconv_t cd = iconv_open (targetFormat, sourceFormat);
if (cd != (iconv_t) -1)
{
target = convert_i(cd, data);
if (target.size() == 0)
{
//try it without iconv
target = oldconvert(data, sourceEnc, targetEnc);
}
}
else
{
target = oldconvert(data, sourceEnc, targetEnc);
}
iconv_close (cd);
#endif
}
return target;
}
size_t dami::ucslen(const unicode_t *unicode)
{
if (NULL != unicode)
{
for (size_t size = 0; true; size++)
{
if (NULL_UNICODE == unicode[size])
{
return size;
}
}
}
return 0;
}
namespace
{
bool exists(String name)
{
ifstream file(name.c_str(), NOCREATE);
return file.is_open() != 0;
}
};
ID3_Err dami::createFile(String name, fstream& file)
{
if (file.is_open())
{
file.close();
}
file.open(name.c_str(), ios::in | ios::out | ios::binary | ios::trunc);
if (!file)
{
return ID3E_ReadOnly;
}
return ID3E_NoError;
}
size_t dami::getFileSize(fstream& file)
{
size_t size = 0;
if (file.is_open())
{
streamoff curpos = file.tellg();
file.seekg(0, ios::end);
size = file.tellg();
file.seekg(curpos);
}
return size;
}
size_t dami::getFileSize(ifstream& file)
{
size_t size = 0;
if (file.is_open())
{
streamoff curpos = file.tellg();
file.seekg(0, ios::end);
size = file.tellg();
file.seekg(curpos);
}
return size;
}
size_t dami::getFileSize(ofstream& file)
{
size_t size = 0;
if (file.is_open())
{
streamoff curpos = file.tellp();
file.seekp(0, ios::end);
size = file.tellp();
file.seekp(curpos);
}
return size;
}
ID3_Err dami::openWritableFile(String name, fstream& file)
{
if (!exists(name))
{
return ID3E_NoFile;
}
if (file.is_open())
{
file.close();
}
file.open(name.c_str(), ios::in | ios::out | ios::binary | NOCREATE);
if (!file)
{
return ID3E_ReadOnly;
}
return ID3E_NoError;
}
ID3_Err dami::openWritableFile(String name, ofstream& file)
{
if (!exists(name))
{
return ID3E_NoFile;
}
if (file.is_open())
{
file.close();
}
file.open(name.c_str(), ios::in | ios::out | ios::binary | NOCREATE);
if (!file)
{
return ID3E_ReadOnly;
}
return ID3E_NoError;
}
ID3_Err dami::openReadableFile(String name, fstream& file)
{
if (file.is_open())
{
file.close();
}
file.open(name.c_str(), ios::in | ios::binary | NOCREATE);
if (!file)
{
return ID3E_NoFile;
}
return ID3E_NoError;
}
ID3_Err dami::openReadableFile(String name, ifstream& file)
{
if (file.is_open())
{
file.close();
}
file.open(name.c_str(), ios::in | ios::binary | NOCREATE);
if (!file)
{
return ID3E_NoFile;
}
return ID3E_NoError;
}
String dami::toString(uint32 val)
{
if (val == 0)
{
return "0";
}
String text;
while (val > 0)
{
String tmp;
char ch = (val % 10) + '0';
tmp += ch;
text = tmp + text;
val /= 10;
}
return text;
}
WString dami::toWString(const unicode_t buf[], size_t len)
{
WString str;
str.reserve(len);
for (size_t i = 0; i < len; ++i)
{
str += static_cast<WString::value_type>(buf[i]);
}
return str;
}
|