1 /***************************************************************************
2 copyright : (C) 2002 - 2008 by Scott Wheeler
3 email : wheeler@kde.org
4 ***************************************************************************/
6 /***************************************************************************
7 * This library is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU Lesser General Public License version *
9 * 2.1 as published by the Free Software Foundation. *
11 * This library is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the Free Software *
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
21 * Alternatively, this file is available under the Mozilla Public *
22 * License Version 1.1. You may obtain a copy of the License at *
23 * http://www.mozilla.org/MPL/ *
24 ***************************************************************************/
29 #include "taglib_export.h"
31 #include "tbytevector.h"
37 class AudioProperties;
40 class TAGLIB_EXPORT FileName
43 FileName(const wchar_t *name) : m_wname(name) {}
44 FileName(const char *name) : m_name(name) {}
45 operator const wchar_t *() const { return m_wname.c_str(); }
46 operator const char *() const { return m_name.c_str(); }
52 typedef const char *FileName;
55 //! A file class with some useful methods for tag manipulation
58 * This class is a basic file class with some methods that are particularly
59 * useful for tag editors. It has methods to take advantage of
60 * ByteVector and a binary search method for finding patterns in a file.
63 class TAGLIB_EXPORT File
67 * Position in the file used for seeking.
70 //! Seek from the beginning of the file.
72 //! Seek from the current position in the file.
74 //! Seek from the end of the file.
79 * Destroys this File instance.
84 * Returns the file name in the local file system encoding.
86 FileName name() const;
89 * Returns a pointer to this file's tag. This should be reimplemented in
90 * the concrete subclasses.
92 virtual Tag *tag() const = 0;
95 * Returns a pointer to this file's audio properties. This should be
96 * reimplemented in the concrete subclasses. If no audio properties were
97 * read then this will return a null pointer.
99 virtual AudioProperties *audioProperties() const = 0;
102 * Save the file and its associated tags. This should be reimplemented in
103 * the concrete subclasses. Returns true if the save succeeds.
105 * \warning On UNIX multiple processes are able to write to the same file at
106 * the same time. This can result in serious file corruption. If you are
107 * developing a program that makes use of TagLib from multiple processes you
108 * must insure that you are only doing writes to a particular file from one
111 virtual bool save() = 0;
114 * Reads a block of size \a length at the current get pointer.
116 ByteVector readBlock(ulong length);
119 * Attempts to write the block \a data at the current get pointer. If the
120 * file is currently only opened read only -- i.e. readOnly() returns true --
121 * this attempts to reopen the file in read/write mode.
123 * \note This should be used instead of using the streaming output operator
124 * for a ByteVector. And even this function is significantly slower than
125 * doing output with a char[].
127 void writeBlock(const ByteVector &data);
130 * Returns the offset in the file that \a pattern occurs at or -1 if it can
131 * not be found. If \a before is set, the search will only continue until the
132 * pattern \a before is found. This is useful for tagging purposes to search
133 * for a tag before the synch frame.
135 * Searching starts at \a fromOffset, which defaults to the beginning of the
138 * \note This has the practial limitation that \a pattern can not be longer
139 * than the buffer size used by readBlock(). Currently this is 1024 bytes.
141 long find(const ByteVector &pattern,
143 const ByteVector &before = ByteVector::null);
146 * Returns the offset in the file that \a pattern occurs at or -1 if it can
147 * not be found. If \a before is set, the search will only continue until the
148 * pattern \a before is found. This is useful for tagging purposes to search
149 * for a tag before the synch frame.
151 * Searching starts at \a fromOffset and proceeds from the that point to the
152 * beginning of the file and defaults to the end of the file.
154 * \note This has the practial limitation that \a pattern can not be longer
155 * than the buffer size used by readBlock(). Currently this is 1024 bytes.
157 long rfind(const ByteVector &pattern,
159 const ByteVector &before = ByteVector::null);
162 * Insert \a data at position \a start in the file overwriting \a replace
163 * bytes of the original content.
165 * \note This method is slow since it requires rewriting all of the file
166 * after the insertion point.
168 void insert(const ByteVector &data, ulong start = 0, ulong replace = 0);
171 * Removes a block of the file starting a \a start and continuing for
174 * \note This method is slow since it involves rewriting all of the file
175 * after the removed portion.
177 void removeBlock(ulong start = 0, ulong length = 0);
180 * Returns true if the file is read only (or if the file can not be opened).
182 bool readOnly() const;
185 * Since the file can currently only be opened as an argument to the
186 * constructor (sort-of by design), this returns if that open succeeded.
191 * Returns true if the file is open and readble.
193 bool isValid() const;
196 * Move the I/O pointer to \a offset in the file from position \a p. This
197 * defaults to seeking from the beginning of the file.
201 void seek(long offset, Position p = Beginning);
204 * Reset the end-of-file and error flags on the file.
209 * Returns the current offset within the file.
214 * Returns the length of the file.
219 * Returns true if \a file can be opened for reading. If the file does not
220 * exist, this will return false.
224 static bool isReadable(const char *file);
227 * Returns true if \a file can be opened for writing.
231 static bool isWritable(const char *name);
235 * Construct a File object and opens the \a file. \a file should be a
236 * be a C-string in the local file system encoding.
238 * \note Constructor is protected since this class should only be
239 * instantiated through subclasses.
244 * Marks the file as valid or invalid.
248 void setValid(bool valid);
251 * Truncates the file to a \a length.
253 void truncate(long length);
256 * Returns the buffer size that is used for internal buffering.
258 static uint bufferSize();
262 File &operator=(const File &);