sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
*
|
sl@0
|
16 |
*/
|
sl@0
|
17 |
|
sl@0
|
18 |
|
sl@0
|
19 |
#include "filestream_private.h"
|
sl@0
|
20 |
#include "logger.h"
|
sl@0
|
21 |
#include "stringconv.h"
|
sl@0
|
22 |
#include "utils.h"
|
sl@0
|
23 |
|
sl@0
|
24 |
FileStreamBuf::FileStreamBuf(const std::string &aFileName, bool aWritable, bool aSpecialTextHandling)
|
sl@0
|
25 |
: iWritable(aWritable), iSpecialTextHandling(aSpecialTextHandling)
|
sl@0
|
26 |
{
|
sl@0
|
27 |
if(iWritable)
|
sl@0
|
28 |
{
|
sl@0
|
29 |
OpenUtf8FStreamForWrite(iFile, aFileName.c_str());
|
sl@0
|
30 |
}
|
sl@0
|
31 |
else
|
sl@0
|
32 |
{
|
sl@0
|
33 |
OpenUtf8FStreamForRead(iFile, aFileName.c_str());
|
sl@0
|
34 |
}
|
sl@0
|
35 |
if(iFile.fail())
|
sl@0
|
36 |
{
|
sl@0
|
37 |
dbg << Log::Indent() << "Failed to open '" << aFileName << "' for " << ((iWritable)?("output"):("input")) << "!" << Log::Endl();
|
sl@0
|
38 |
FatalError();
|
sl@0
|
39 |
}
|
sl@0
|
40 |
|
sl@0
|
41 |
}
|
sl@0
|
42 |
|
sl@0
|
43 |
static const char utf8Header[] =
|
sl@0
|
44 |
{
|
sl@0
|
45 |
0xef, 0xbb, 0xbf
|
sl@0
|
46 |
};
|
sl@0
|
47 |
|
sl@0
|
48 |
static const char utf16HeaderBig[] =
|
sl@0
|
49 |
{
|
sl@0
|
50 |
0xfe, 0xff
|
sl@0
|
51 |
};
|
sl@0
|
52 |
|
sl@0
|
53 |
static const char utf16HeaderLittle[] =
|
sl@0
|
54 |
{
|
sl@0
|
55 |
0xff, 0xfe
|
sl@0
|
56 |
};
|
sl@0
|
57 |
|
sl@0
|
58 |
void FileStreamBuf::StripUtf8HeaderIfPresent()
|
sl@0
|
59 |
{
|
sl@0
|
60 |
if(iWritable)
|
sl@0
|
61 |
{
|
sl@0
|
62 |
FatalError(); // Coding error
|
sl@0
|
63 |
}
|
sl@0
|
64 |
|
sl@0
|
65 |
std::streampos savedPos = iFile.tellg();
|
sl@0
|
66 |
char header[3];
|
sl@0
|
67 |
iFile.read(header, sizeof(header));
|
sl@0
|
68 |
|
sl@0
|
69 |
if(iFile.good() && memcmp(header, utf8Header, sizeof(utf8Header)) == 0)
|
sl@0
|
70 |
{
|
sl@0
|
71 |
// We read a UTF8 file header so simply return (and thereby skip it).
|
sl@0
|
72 |
return;
|
sl@0
|
73 |
}
|
sl@0
|
74 |
|
sl@0
|
75 |
if(iFile.good() && memcmp(header, utf16HeaderBig, sizeof(utf16HeaderBig)) == 0)
|
sl@0
|
76 |
{
|
sl@0
|
77 |
dbg << Log::Indent() << "Input file is Big Endian UTF16 - Only UTF-8 and ASCII are supported" << Log::Endl();
|
sl@0
|
78 |
FatalError();
|
sl@0
|
79 |
}
|
sl@0
|
80 |
if(iFile.good() && memcmp(header, utf16HeaderLittle, sizeof(utf16HeaderLittle)) == 0)
|
sl@0
|
81 |
{
|
sl@0
|
82 |
dbg << Log::Indent() << "Input file is Little Endian UTF16 - Only UTF-8 and ASCII are supported" << Log::Endl();
|
sl@0
|
83 |
FatalError();
|
sl@0
|
84 |
}
|
sl@0
|
85 |
|
sl@0
|
86 |
iFile.clear();
|
sl@0
|
87 |
iFile.seekg(savedPos);
|
sl@0
|
88 |
}
|
sl@0
|
89 |
|
sl@0
|
90 |
|
sl@0
|
91 |
TInt FileStreamBuf::DoReadL(TAny *aPtr,TInt aMaxLength)
|
sl@0
|
92 |
{
|
sl@0
|
93 |
BULLSEYE_OFF
|
sl@0
|
94 |
if(iFile.fail())
|
sl@0
|
95 |
{
|
sl@0
|
96 |
dbg << Log::Indent() << "Read error" << Log::Endl();
|
sl@0
|
97 |
FatalError(); // Read on broken stream
|
sl@0
|
98 |
}
|
sl@0
|
99 |
BULLSEYE_RESTORE
|
sl@0
|
100 |
|
sl@0
|
101 |
iFile.read((char *)aPtr, aMaxLength);
|
sl@0
|
102 |
|
sl@0
|
103 |
if(iFile.eof())
|
sl@0
|
104 |
{
|
sl@0
|
105 |
dbg << Log::Indent() << "Encountered EOF" << Log::Endl();
|
sl@0
|
106 |
TInt len = iFile.gcount();
|
sl@0
|
107 |
if(iSpecialTextHandling)
|
sl@0
|
108 |
{
|
sl@0
|
109 |
// Add a synthetic NL to the returned data to handle a
|
sl@0
|
110 |
// token immediately before the EOF
|
sl@0
|
111 |
((TUint8 *)aPtr)[len++] = '\n';
|
sl@0
|
112 |
}
|
sl@0
|
113 |
return len;
|
sl@0
|
114 |
}
|
sl@0
|
115 |
|
sl@0
|
116 |
BULLSEYE_OFF
|
sl@0
|
117 |
if(iFile.fail())
|
sl@0
|
118 |
{
|
sl@0
|
119 |
dbg << Log::Indent() << "Read error" << Log::Endl();
|
sl@0
|
120 |
FatalError(); // error other than EOF
|
sl@0
|
121 |
}
|
sl@0
|
122 |
BULLSEYE_RESTORE
|
sl@0
|
123 |
|
sl@0
|
124 |
return iFile.gcount();
|
sl@0
|
125 |
}
|
sl@0
|
126 |
|
sl@0
|
127 |
void FileStreamBuf::DoWriteL(const TUint8* aPtr,TInt aLength)
|
sl@0
|
128 |
{
|
sl@0
|
129 |
iFile.write((const char *)aPtr, aLength);
|
sl@0
|
130 |
BULLSEYE_OFF
|
sl@0
|
131 |
if(iFile.fail())
|
sl@0
|
132 |
{
|
sl@0
|
133 |
FatalError(); // error other than EOF
|
sl@0
|
134 |
}
|
sl@0
|
135 |
BULLSEYE_RESTORE
|
sl@0
|
136 |
//#warning "flush all data for debugging"
|
sl@0
|
137 |
// iFile.flush();
|
sl@0
|
138 |
}
|
sl@0
|
139 |
|
sl@0
|
140 |
TStreamPos FileStreamBuf::DoSeekL(TMark aMark,TStreamLocation aLocation,TInt anOffset)
|
sl@0
|
141 |
{
|
sl@0
|
142 |
std::ios_base::seekdir dir;
|
sl@0
|
143 |
switch(aLocation)
|
sl@0
|
144 |
{
|
sl@0
|
145 |
case EStreamBeginning:
|
sl@0
|
146 |
dir = std::ios_base::beg;
|
sl@0
|
147 |
break;
|
sl@0
|
148 |
case EStreamMark:
|
sl@0
|
149 |
dir = std::ios_base::cur;
|
sl@0
|
150 |
break;
|
sl@0
|
151 |
case EStreamEnd:
|
sl@0
|
152 |
dir = std::ios_base::end;
|
sl@0
|
153 |
break;
|
sl@0
|
154 |
BULLSEYE_OFF
|
sl@0
|
155 |
default:
|
sl@0
|
156 |
FatalError();
|
sl@0
|
157 |
BULLSEYE_RESTORE
|
sl@0
|
158 |
}
|
sl@0
|
159 |
|
sl@0
|
160 |
if(aMark == ERead)
|
sl@0
|
161 |
{
|
sl@0
|
162 |
iFile.seekg(anOffset, dir);
|
sl@0
|
163 |
BULLSEYE_OFF
|
sl@0
|
164 |
if(iFile.fail()) FatalError(); // error other than EOF
|
sl@0
|
165 |
BULLSEYE_RESTORE
|
sl@0
|
166 |
return iFile.tellg();
|
sl@0
|
167 |
}
|
sl@0
|
168 |
|
sl@0
|
169 |
if(aMark == EWrite)
|
sl@0
|
170 |
{
|
sl@0
|
171 |
iFile.seekp(anOffset, dir);
|
sl@0
|
172 |
BULLSEYE_OFF
|
sl@0
|
173 |
if(iFile.fail()) FatalError(); // error other than EOF
|
sl@0
|
174 |
BULLSEYE_RESTORE
|
sl@0
|
175 |
return iFile.tellp();
|
sl@0
|
176 |
}
|
sl@0
|
177 |
|
sl@0
|
178 |
FatalError();
|
sl@0
|
179 |
return -1;
|
sl@0
|
180 |
}
|
sl@0
|
181 |
|
sl@0
|
182 |
void FileStreamBuf::DoRelease()
|
sl@0
|
183 |
{
|
sl@0
|
184 |
iFile.close();
|
sl@0
|
185 |
}
|
sl@0
|
186 |
|
sl@0
|
187 |
FileWriteStream::FileWriteStream(const std::string &aFileName)
|
sl@0
|
188 |
: RWriteStream()
|
sl@0
|
189 |
{
|
sl@0
|
190 |
iSnk = new FileStreamBuf(aFileName, true);
|
sl@0
|
191 |
}
|
sl@0
|
192 |
|
sl@0
|
193 |
FileWriteStream::~FileWriteStream()
|
sl@0
|
194 |
{
|
sl@0
|
195 |
BULLSEYE_OFF
|
sl@0
|
196 |
if(iSnk)
|
sl@0
|
197 |
{
|
sl@0
|
198 |
dbg << Log::Indent() << "forgot to close FileWriteStream" << Log::Endl();
|
sl@0
|
199 |
FatalError();
|
sl@0
|
200 |
}
|
sl@0
|
201 |
BULLSEYE_RESTORE
|
sl@0
|
202 |
iSnk = 0;
|
sl@0
|
203 |
}
|
sl@0
|
204 |
|
sl@0
|
205 |
|
sl@0
|
206 |
|
sl@0
|
207 |
FileReadStream::FileReadStream(const std::string &aFileName, bool aSpecialTextHandling)
|
sl@0
|
208 |
: RReadStream()
|
sl@0
|
209 |
{
|
sl@0
|
210 |
iSrc = new FileStreamBuf(aFileName, false, aSpecialTextHandling);
|
sl@0
|
211 |
if(aSpecialTextHandling)
|
sl@0
|
212 |
{
|
sl@0
|
213 |
static_cast<FileStreamBuf *>(iSrc)->StripUtf8HeaderIfPresent();
|
sl@0
|
214 |
}
|
sl@0
|
215 |
}
|
sl@0
|
216 |
|
sl@0
|
217 |
|
sl@0
|
218 |
FileReadStream::~FileReadStream()
|
sl@0
|
219 |
{
|
sl@0
|
220 |
BULLSEYE_OFF
|
sl@0
|
221 |
if(iSrc)
|
sl@0
|
222 |
{
|
sl@0
|
223 |
dbg << Log::Indent() << "forgot to close FileReadStream" << Log::Endl();
|
sl@0
|
224 |
FatalError();
|
sl@0
|
225 |
}
|
sl@0
|
226 |
BULLSEYE_RESTORE
|
sl@0
|
227 |
iSrc = 0;
|
sl@0
|
228 |
}
|
sl@0
|
229 |
|
sl@0
|
230 |
// End of file
|