cAudio  2.3.0
3d Audio Engine
cFileSource.cpp
1 // Copyright (c) 2008-2011 Raynaldo (Wildicv) Rivera, Joshua (Dark_Kilauea) Jones, Murat (wolfmanfx) Sari
2 // This file is part of the "cAudio Engine"
3 // For conditions of distribution and use, see copyright notice in cAudio.h
4 
5 #include "cFileSource.h"
6 #include "cUtils.h"
7 
8 #if CAUDIO_COMPILE_WITH_FILE_SOURCE == 1
9 
10 #include <cstring>
11 
12 namespace cAudio
13 {
14 
15 cFileSource::cFileSource(const char* filename) : pFile(NULL), Valid(false), Filesize(0)
16 {
17  cAudioString safeFilename = fromUTF8(filename);
18  if(safeFilename.length() != 0)
19  {
20  pFile = cfopen(safeFilename, "rb");
21  if(pFile)
22  Valid = true;
23  }
24 
25  if(Valid)
26  {
27  fseek(pFile, 0, SEEK_END);
28  Filesize = ftell(pFile);
29  fseek(pFile, 0, SEEK_SET);
30  }
31 }
32 
33 cFileSource::~cFileSource()
34 {
35  if(pFile)
36  fclose(pFile);
37 }
38 
39 bool cFileSource::isValid()
40 {
41  return Valid;
42 }
43 
44 int cFileSource::getCurrentPos()
45 {
46  return ftell(pFile);
47 }
48 
49 int cFileSource::getSize()
50 {
51  return Filesize;
52 }
53 
54 int cFileSource::read(void* output, int size)
55 {
56  return fread(output, sizeof(char), size, pFile);
57 }
58 
59 bool cFileSource::seek(int amount, bool relative)
60 {
61  if(relative == true)
62  {
63  int oldamount = ftell(pFile);
64  fseek(pFile, amount, SEEK_CUR);
65 
66  //check against the absolute position
67  if(oldamount+amount != ftell(pFile))
68  return false;
69  }
70  else
71  {
72  fseek(pFile, amount, SEEK_SET);
73  if(amount != ftell(pFile))
74  return false;
75  }
76 
77  return true;
78 }
79 
80 };
81 
82 #endif
Main namespace for the entire cAudio library.
Definition: cAudioCapture.h:15