os/security/securityanddataprivacytools/securitytools/certapp/utils/filestream.cpp
First public contribution.
2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include "filestream_private.h"
21 #include "stringconv.h"
24 FileStreamBuf::FileStreamBuf(const std::string &aFileName, bool aWritable, bool aSpecialTextHandling)
25 : iWritable(aWritable), iSpecialTextHandling(aSpecialTextHandling)
29 OpenUtf8FStreamForWrite(iFile, aFileName.c_str());
33 OpenUtf8FStreamForRead(iFile, aFileName.c_str());
37 dbg << Log::Indent() << "Failed to open '" << aFileName << "' for " << ((iWritable)?("output"):("input")) << "!" << Log::Endl();
43 static const char utf8Header[] =
48 static const char utf16HeaderBig[] =
53 static const char utf16HeaderLittle[] =
58 void FileStreamBuf::StripUtf8HeaderIfPresent()
62 FatalError(); // Coding error
65 std::streampos savedPos = iFile.tellg();
67 iFile.read(header, sizeof(header));
69 if(iFile.good() && memcmp(header, utf8Header, sizeof(utf8Header)) == 0)
71 // We read a UTF8 file header so simply return (and thereby skip it).
75 if(iFile.good() && memcmp(header, utf16HeaderBig, sizeof(utf16HeaderBig)) == 0)
77 dbg << Log::Indent() << "Input file is Big Endian UTF16 - Only UTF-8 and ASCII are supported" << Log::Endl();
80 if(iFile.good() && memcmp(header, utf16HeaderLittle, sizeof(utf16HeaderLittle)) == 0)
82 dbg << Log::Indent() << "Input file is Little Endian UTF16 - Only UTF-8 and ASCII are supported" << Log::Endl();
87 iFile.seekg(savedPos);
91 TInt FileStreamBuf::DoReadL(TAny *aPtr,TInt aMaxLength)
96 dbg << Log::Indent() << "Read error" << Log::Endl();
97 FatalError(); // Read on broken stream
101 iFile.read((char *)aPtr, aMaxLength);
105 dbg << Log::Indent() << "Encountered EOF" << Log::Endl();
106 TInt len = iFile.gcount();
107 if(iSpecialTextHandling)
109 // Add a synthetic NL to the returned data to handle a
110 // token immediately before the EOF
111 ((TUint8 *)aPtr)[len++] = '\n';
119 dbg << Log::Indent() << "Read error" << Log::Endl();
120 FatalError(); // error other than EOF
124 return iFile.gcount();
127 void FileStreamBuf::DoWriteL(const TUint8* aPtr,TInt aLength)
129 iFile.write((const char *)aPtr, aLength);
133 FatalError(); // error other than EOF
136 //#warning "flush all data for debugging"
140 TStreamPos FileStreamBuf::DoSeekL(TMark aMark,TStreamLocation aLocation,TInt anOffset)
142 std::ios_base::seekdir dir;
145 case EStreamBeginning:
146 dir = std::ios_base::beg;
149 dir = std::ios_base::cur;
152 dir = std::ios_base::end;
162 iFile.seekg(anOffset, dir);
164 if(iFile.fail()) FatalError(); // error other than EOF
166 return iFile.tellg();
171 iFile.seekp(anOffset, dir);
173 if(iFile.fail()) FatalError(); // error other than EOF
175 return iFile.tellp();
182 void FileStreamBuf::DoRelease()
187 FileWriteStream::FileWriteStream(const std::string &aFileName)
190 iSnk = new FileStreamBuf(aFileName, true);
193 FileWriteStream::~FileWriteStream()
198 dbg << Log::Indent() << "forgot to close FileWriteStream" << Log::Endl();
207 FileReadStream::FileReadStream(const std::string &aFileName, bool aSpecialTextHandling)
210 iSrc = new FileStreamBuf(aFileName, false, aSpecialTextHandling);
211 if(aSpecialTextHandling)
213 static_cast<FileStreamBuf *>(iSrc)->StripUtf8HeaderIfPresent();
218 FileReadStream::~FileReadStream()
223 dbg << Log::Indent() << "forgot to close FileReadStream" << Log::Endl();