1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18 /*********************************************************************************/
19 /*
20 This MP3Utils Class contains sime useful methods for operating FILE
21 */
22 /*********************************************************************************/
23
24 #include "mp3utils.h"
25 #include "oscl_base.h"
26
27 #ifndef OSCL_SNPRINTF_H_INCLUDED
28 #include "oscl_snprintf.h"
29 #endif
30
31 int32
getCurrentFilePosition(PVFile * fp)32 MP3Utils::getCurrentFilePosition(PVFile *fp)
33 {
34 return (fp->Tell());
35 }
36
OpenFile(OSCL_wHeapString<OsclMemAllocator> filename,uint32 mode,MP3_FF_FILE * fp)37 int32 MP3Utils::OpenFile(OSCL_wHeapString<OsclMemAllocator> filename,
38 uint32 mode,
39 MP3_FF_FILE *fp)
40 {
41 OSCL_UNUSED_ARG(mode);
42 if (fp != NULL)
43 {
44 return (fp->_pvfile.Open(filename.get_cstr(),
45 Oscl_File::MODE_READ | Oscl_File::MODE_BINARY,
46 (fp->_fileServSession)));
47 }
48 return -1;
49 }
50
CloseFile(PVFile * fp)51 int32 MP3Utils::CloseFile(PVFile *fp)
52 {
53 if (fp != NULL)
54 {
55 return (fp->Close());
56 }
57 return -1;
58 }
59
Flush(PVFile * fp)60 int32 MP3Utils::Flush(PVFile *fp)
61 {
62 if (fp != NULL)
63 {
64 return (fp->Flush());
65 }
66 return -1;
67 }
68
getCurrentFileSize(PVFile * fp,uint32 & aCurrentSize)69 bool MP3Utils::getCurrentFileSize(PVFile *fp, uint32& aCurrentSize)
70 {
71 if (fp != NULL)
72 {
73 aCurrentSize = 0;
74 uint32 aRemBytes = 0;
75 if (fp->GetRemainingBytes(aRemBytes))
76 {
77 uint32 currPos = (uint32)(fp->Tell());
78 aCurrentSize = currPos + aRemBytes;
79 return true;
80 }
81 }
82 return false;
83 }
84
SeektoOffset(PVFile * aFP,int32 aOffset,Oscl_File::seek_type aSeekType)85 MP3ErrorType MP3Utils::SeektoOffset(PVFile *aFP, int32 aOffset, Oscl_File::seek_type aSeekType)
86 {
87 uint32 currFileSize = 0;
88 uint32 currPos = 0;
89 int32 seekOffset = 0;
90 currPos = MP3Utils::getCurrentFilePosition(aFP);
91 MP3Utils::getCurrentFileSize(aFP, currFileSize);
92
93 // translate the seek offset to seek from current position
94 switch (aSeekType)
95 {
96 case Oscl_File::SEEKSET:
97 seekOffset = aOffset - currPos;
98 break;
99 case Oscl_File::SEEKEND:
100 seekOffset = currFileSize - currPos;
101 break;
102 case Oscl_File::SEEKCUR:
103 seekOffset = aOffset;
104 default:
105 break;
106 }
107
108 if (aOffset <= 0 || currFileSize >= (uint32) aOffset)
109 {
110 if (aFP->Seek(seekOffset, Oscl_File::SEEKCUR) != 0)
111 {
112 return MP3_FILE_READ_ERR;
113 }
114 return MP3_SUCCESS;
115 }
116 else
117 {
118 return MP3_INSUFFICIENT_DATA;
119 }
120 return MP3_ERROR_UNKNOWN;
121 }
122