|
1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 |
|
17 |
|
18 // INCLUDE FILES |
|
19 #include "EPos_CPosFileHandler.h" |
|
20 |
|
21 // ================= MEMBER FUNCTIONS ======================= |
|
22 |
|
23 // C++ default constructor can NOT contain any code, that |
|
24 // might leave. |
|
25 // |
|
26 CPosFileHandler::CPosFileHandler() |
|
27 { |
|
28 } |
|
29 |
|
30 // EPOC default constructor can leave. |
|
31 void CPosFileHandler::ConstructL(const TDesC& aFileName) |
|
32 { |
|
33 iFileName = aFileName.AllocL(); |
|
34 User::LeaveIfError(iFsSession.Connect()); |
|
35 User::LeaveIfError(iFile.Open(iFsSession, aFileName, |
|
36 EFileShareReadersOnly)); |
|
37 iStream.Attach(iFile); |
|
38 } |
|
39 |
|
40 // Two-phased constructor. |
|
41 CPosFileHandler* CPosFileHandler::NewL(const TDesC& aFileName) |
|
42 { |
|
43 CPosFileHandler* self = new (ELeave) CPosFileHandler; |
|
44 CleanupStack::PushL(self); |
|
45 self->ConstructL(aFileName); |
|
46 CleanupStack::Pop(self); |
|
47 return self; |
|
48 } |
|
49 |
|
50 // Destructor |
|
51 CPosFileHandler::~CPosFileHandler() |
|
52 { |
|
53 iStream.Close(); |
|
54 iFile.Close(); |
|
55 iFsSession.Close(); |
|
56 delete iFileName; |
|
57 } |
|
58 |
|
59 // ---------------------------------------------------------------------------- |
|
60 // CPosFileHandler::Read |
|
61 // |
|
62 // (other items were commented in a header). |
|
63 // ---------------------------------------------------------------------------- |
|
64 // |
|
65 TInt CPosFileHandler::Read(TDes8& aDes) |
|
66 { |
|
67 const TChar c(0x000A) ; |
|
68 TRAPD(err, (iStream.ReadL(aDes, c))); |
|
69 |
|
70 if (err == KErrEof) |
|
71 { |
|
72 TInt err2 = ReadFromStart(); |
|
73 if (err2 != KErrNone) |
|
74 { |
|
75 err = err2; |
|
76 } |
|
77 } |
|
78 return err; |
|
79 } |
|
80 |
|
81 // ---------------------------------------------------------------------------- |
|
82 // CPosFileHandler::ReadL |
|
83 // |
|
84 // (other items were commented in a header). |
|
85 // ---------------------------------------------------------------------------- |
|
86 // |
|
87 void CPosFileHandler::ReadL(CDesC8Array& aArray) |
|
88 { |
|
89 TInt err = KErrNone; |
|
90 TBuf8<KMaxSentenceLength> dataInfo; |
|
91 const TChar endoflinechar(';'); |
|
92 TStreamPos offset; |
|
93 while (err != KErrEof) |
|
94 { |
|
95 //Store the current offset in the stream |
|
96 offset = iStream.Source()->TellL(MStreamBuf::ERead); |
|
97 |
|
98 TRAP(err, (iStream.ReadL(dataInfo, endoflinechar))); |
|
99 if(err == KErrEof) |
|
100 { |
|
101 //End of File has been reached without finding an endoflinechar, so attempt to |
|
102 // extract the remaining data of the file |
|
103 TInt length = iStream.Source()->SizeL(); |
|
104 TInt readLength = length - (offset.Offset()); |
|
105 |
|
106 //Reset the stream back to the end of the previous successful data extraction |
|
107 iStream.Source()->SeekL(MStreamBuf::ERead, offset); |
|
108 //Read the remaining data from the stream |
|
109 TRAP(err, (iStream.ReadL(dataInfo, readLength))); |
|
110 if(err == KErrNone) |
|
111 { |
|
112 //Managed to read the remaining data. Ensure the error is EOF to exit the loop |
|
113 err = KErrEof; |
|
114 } |
|
115 } |
|
116 if (err != KErrNone && err != KErrEof) |
|
117 { |
|
118 User::Leave(err); |
|
119 } |
|
120 dataInfo.Trim(); |
|
121 if ((err == KErrNone || err == KErrEof) && dataInfo.Length() > 0) |
|
122 { |
|
123 aArray.AppendL(dataInfo); |
|
124 } |
|
125 } |
|
126 if (aArray.Count() != KNoOfSimulatedDataItems) |
|
127 { |
|
128 User::Leave(KErrCorrupt); |
|
129 } |
|
130 } |
|
131 |
|
132 // ---------------------------------------------------------------------------- |
|
133 // CPosFileHandler::ReadFromStart |
|
134 // |
|
135 // (other items were commented in a header). |
|
136 // ---------------------------------------------------------------------------- |
|
137 // |
|
138 TInt CPosFileHandler::ReadFromStart() |
|
139 { |
|
140 // Close stream and file and open them again. |
|
141 iStream.Close(); |
|
142 iFile.Close(); |
|
143 TInt err = iFile.Open(iFsSession, *iFileName, EFileShareReadersOnly); |
|
144 iStream.Attach(iFile); |
|
145 return err; |
|
146 } |
|
147 |
|
148 // End of File |