30#include "io_strings.h"
35#if !defined HAVE_MKSTEMP
39#if defined HAVE_UNISTD_H
43#if defined HAVE_SYS_STAT_H
47#if defined WIN32 && (!defined(WINCE))
49static int truncate(
const char *path,
size_t length)
54 fh = ::CreateFile(path,
55 GENERIC_WRITE | GENERIC_READ,
59 FILE_ATTRIBUTE_NORMAL,
62 if(INVALID_HANDLE_VALUE != fh)
64 SetFilePointer(fh, length,
NULL, FILE_BEGIN);
74# if defined CreateFile
82static int truncate(
const char *path,
size_t length)
85 wchar_t wcTempPath[256];
86 mbstowcs(wcTempPath,path,255);
88 fh = ::CreateFile(wcTempPath,
89 GENERIC_WRITE | GENERIC_READ,
93 FILE_ATTRIBUTE_NORMAL,
96 if (INVALID_HANDLE_VALUE != fh)
98 SetFilePointer(fh, length,
NULL, FILE_BEGIN);
107#elif defined(macintosh)
109static int truncate(
const char *path,
size_t length)
128 return this->
Link(fileInfo, tt);
133 _tags_to_parse.set(tag_types);
135 if (
NULL == fileInfo)
140 _file_name = fileInfo;
151 _tags_to_parse.set(tag_types);
174 file.seekp(0, ios::end);
196 file.seekp(0, ios::end);
209 ID3D_NOTICE(
"RenderV2ToFile: starting" );
212 ID3D_WARNING(
"RenderV2ToFile: error in file" );
217 io::StringWriter writer(tagString);
219 ID3D_NOTICE(
"RenderV2ToFile: rendered v2" );
221 const char* tagData = tagString.data();
222 size_t tagSize = tagString.size();
228 file.seekp(0, ios::beg);
229 file.write(tagData, tagSize);
234 String sTmpSuffix =
".XXXXXX";
235 if (filename.size() + sTmpSuffix.size() > ID3_PATH_LENGTH)
241 char sTempFile[ID3_PATH_LENGTH];
242 strcpy(sTempFile, filename.c_str());
243 strcat(sTempFile, sTmpSuffix.c_str());
245#if !defined(HAVE_MKSTEMP)
248 createFile(sTempFile, tmpOut);
250 tmpOut.write(tagData, tagSize);
252 char *tmpBuffer[BUFSIZ];
255 file.read((
char *)tmpBuffer, BUFSIZ);
256 size_t nBytes = file.gcount();
257 tmpOut.write((
char *)tmpBuffer, nBytes);
266 int fd = mkstemp(sTempFile);
273 ofstream tmpOut(sTempFile);
283 tmpOut.write(tagData, tagSize);
285 uchar tmpBuffer[BUFSIZ];
288 file.read((
char *)tmpBuffer, BUFSIZ);
289 size_t nBytes = file.gcount();
290 tmpOut.write((
char *)tmpBuffer, nBytes);
302#if defined(HAVE_SYS_STAT_H)
303 struct stat fileStat;
304 if(stat(filename.c_str(), &fileStat) == 0)
307 remove(filename.c_str());
308 rename(sTempFile, filename.c_str());
309#if defined(HAVE_SYS_STAT_H)
319# if defined(HAVE_UNISTD_H)
320 if (chown(filename.c_str(), fileStat.st_uid, fileStat.st_gid) != 0)
322 ID3D_WARNING(
"RenderV2ToFile: could not restore owner/group of " <<
326 chmod(filename.c_str(), fileStat.st_mode);
332 openWritableFile(filename, file);
345 ID3_Err err = openWritableFile(filename, file);
346 _file_size = getFileSize(file);
350 err = createFile(filename, file);
360 if (_prepended_bytes)
375 _appended_bytes += tag_bytes;
381 _file_tags.add(tags);
382 _file_size = getFileSize(file);
400 _file_size = getFileSize(file);
408 uchar aucBuffer[BUFSIZ];
411 size_t nBytesToCopy = data_size;
424 size_t nBytesRemaining = nBytesToCopy,
428#if (defined(__GNUC__) && __GNUC__ == 2)
429 size_t nBytesToRead = (size_t)dami::min((
unsigned int)(nBytesRemaining - nBytesCopied), (
unsigned int)BUFSIZ);
431 size_t nBytesToRead = min((
unsigned int)(nBytesRemaining - nBytesCopied), (
unsigned int)BUFSIZ);
433 file.read((
char *)aucBuffer, nBytesToRead);
434 size_t nBytesRead = file.gcount();
436 if (nBytesRead != nBytesToRead)
445 file.seekp(-offset, ios::cur);
446 file.write((
char *)aucBuffer, nBytesRead);
448 nBytesCopied += nBytesRead;
451 if (nBytesCopied == nBytesToCopy || nBytesToRead < BUFSIZ)
459 size_t nNewFileSize = data_size;
486 if (ulTags && (truncate(_file_name.c_str(), nNewFileSize) == -1))
494 _appended_bytes = (ulTags &
ID3TT_APPENDED) ? 0 : _appended_bytes;
495 _file_size = data_size + _prepended_bytes + _appended_bytes;
497 _changed = _file_tags.remove(ulTags) || _changed;
flags_t Strip(flags_t=(flags_t) ID3TT_ALL)
size_t GetFileSize() const
size_t GetAppendedBytes() const
bool HasTagType(ID3_TagType tt) const
dami::String GetFileName() const
void ParseReader(ID3_Reader &reader)
size_t GetPrependedBytes() const
flags_t Update(flags_t=(flags_t) ID3TT_ALL)
size_t Link(const char *fileInfo, flags_t=(flags_t) ID3TT_ALL)
ID3_Err
Predefined id3lib error types.
@ ID3E_ReadOnly
Attempting to write to a read-only file.
@ ID3E_NoError
No error reported.
@ ID3E_NoFile
No file to parse.
@ ID3TT_APPENDED
Represents all tag types that can be appended to a file.
@ ID3TT_ID3V2
Represents an id3v2 tag.
@ ID3TT_PREPENDED
Represents all tag types that can be prepended to a file.
@ ID3TT_ID3V1
Represents an id3v1 or id3v1.1 tag.
@ ID3TT_NONE
Represents an empty or non-existant tag.
void render(ID3_Writer &, const ID3_TagImpl &)
void render(ID3_Writer &writer, const ID3_TagImpl &tag)
size_t RenderV1ToFile(ID3_TagImpl &tag, fstream &file)
size_t RenderV2ToFile(const ID3_TagImpl &tag, fstream &file)
size_t ID3_GetDataSize(const ID3_TagImpl &tag)
size_t ID3_GetDataSize(const ID3_TagImpl &)