1 /***************************************************************************
2 copyright : (C) 2007 by Lukas Lalinsky
3 email : lukas@oxygene.sk
4 ***************************************************************************/
5
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. *
10 * *
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. *
15 * *
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., 51 Franklin Street, Fifth Floor, Boston, MA *
19 * 02110-1301 USA *
20 * *
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 ***************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #ifdef _WIN32
31 #include <windows.h>
32 #else
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <sys/fcntl.h>
36 #include <sys/stat.h>
37 #endif
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <string>
42 #include <fstream>
43
44 using namespace std;
45
testFilePath(const string & filename)46 inline string testFilePath(const string &filename)
47 {
48 return string(TESTS_DIR "data/") + filename;
49 }
50
51 #define TEST_FILE_PATH_C(f) testFilePath(f).c_str()
52
copyFile(const string & filename,const string & ext)53 inline string copyFile(const string &filename, const string &ext)
54 {
55 char testFileName[1024];
56
57 #ifdef _WIN32
58 char tempDir[MAX_PATH + 1];
59 GetTempPathA(sizeof(tempDir), tempDir);
60 wsprintfA(testFileName, "%s\\taglib-test%s", tempDir, ext.c_str());
61 #else
62 snprintf(testFileName, sizeof(testFileName), "/%s/taglib-test%s", P_tmpdir, ext.c_str());
63 #endif
64
65 string sourceFileName = testFilePath(filename) + ext;
66 ifstream source(sourceFileName.c_str(), std::ios::binary);
67 ofstream destination(testFileName, std::ios::binary);
68 destination << source.rdbuf();
69 return string(testFileName);
70 }
71
deleteFile(const string & filename)72 inline void deleteFile(const string &filename)
73 {
74 remove(filename.c_str());
75 }
76
fileEqual(const string & filename1,const string & filename2)77 inline bool fileEqual(const string &filename1, const string &filename2)
78 {
79 char buf1[BUFSIZ];
80 char buf2[BUFSIZ];
81
82 ifstream stream1(filename1.c_str(), ios_base::in | ios_base::binary);
83 ifstream stream2(filename2.c_str(), ios_base::in | ios_base::binary);
84
85 if(!stream1 && !stream2) return true;
86 if(!stream1 || !stream2) return false;
87
88 for(;;)
89 {
90 stream1.read(buf1, BUFSIZ);
91 stream2.read(buf2, BUFSIZ);
92
93 streamsize n1 = stream1.gcount();
94 streamsize n2 = stream2.gcount();
95
96 if(n1 != n2) return false;
97
98 if(n1 == 0) break;
99
100 if(memcmp(buf1, buf2, static_cast<size_t>(n1)) != 0) return false;
101 }
102
103 return stream1.good() == stream2.good();
104 }
105
106 #ifdef TAGLIB_STRING_H
107
108 namespace TagLib {
109
110 inline String longText(size_t length, bool random = false)
111 {
112 const wchar_t chars[] = L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_";
113
114 std::wstring text(length, L'X');
115
116 if(random) {
117 for(size_t i = 0; i < length; ++i)
118 text[i] = chars[rand() % 53];
119 }
120
121 return String(text);
122 }
123 }
124
125 #endif
126
127 class ScopedFileCopy
128 {
129 public:
130 ScopedFileCopy(const string &filename, const string &ext, bool deleteFile=true) :
m_deleteFile(deleteFile)131 m_deleteFile(deleteFile),
132 m_filename(copyFile(filename, ext))
133 {
134 }
135
~ScopedFileCopy()136 ~ScopedFileCopy()
137 {
138 if(m_deleteFile)
139 deleteFile(m_filename);
140 }
141
fileName()142 string fileName() const
143 {
144 return m_filename;
145 }
146
147 private:
148 const bool m_deleteFile;
149 const string m_filename;
150 };
151