1 /* MPEG/WAVE Sound library
2
3 (C) 1997 by Woo-jae Jung */
4
5 // Binput.cc
6 // Inputstream from file
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #include <sys/stat.h>
13 #include <unistd.h>
14
15 #include "mpegsound.h"
16
17 /************************/
18 /* Input bitstrem class */
19 /************************/
20
Soundinputstreamfromfile()21 Soundinputstreamfromfile::Soundinputstreamfromfile()
22 {
23 fp = NULL;
24 __canseek = true;
25 }
26
~Soundinputstreamfromfile()27 Soundinputstreamfromfile::~Soundinputstreamfromfile()
28 {
29 if(fp)
30 fclose(fp);
31 }
32
open(const char * filename)33 bool Soundinputstreamfromfile::open(const char *filename)
34 {
35 struct stat buf;
36
37 if(filename==NULL)
38 {
39 fp=stdin;
40 size=0;
41 return true;
42 }
43 else if((fp=fopen(filename,"r"))==NULL)
44 {
45 seterrorcode(SOUND_ERROR_FILEOPENFAIL);
46 return false;
47 }
48
49 stat(filename,&buf);
50 size=buf.st_size;
51 if (size < 10 * 1024)
52 {
53 seterrorcode(SOUND_ERROR_FILEOPENFAIL);
54 return false;
55 }
56
57 return true;
58 }
59
getbytedirect(void)60 int Soundinputstreamfromfile::getbytedirect(void)
61 {
62 int c;
63
64 if((c=getc(fp))<0)
65 seterrorcode(SOUND_ERROR_FILEREADFAIL);
66
67 return c;
68 }
69
_readbuffer(char * buffer,int size)70 bool Soundinputstreamfromfile::_readbuffer(char *buffer,int size)
71 {
72 if(fread(buffer,size,1,fp)!=1)
73 {
74 seterrorcode(SOUND_ERROR_FILEREADFAIL);
75 return false;
76 }
77 return true;
78 }
79
eof(void)80 bool Soundinputstreamfromfile::eof(void)
81 {
82 return feof(fp);
83 };
84
getblock(char * buffer,int size)85 int Soundinputstreamfromfile::getblock(char *buffer,int size)
86 {
87 return fread(buffer,1,size,fp);
88 }
89
getsize(void)90 int Soundinputstreamfromfile::getsize(void)
91 {
92 return size;
93 }
94
setposition(int pos)95 void Soundinputstreamfromfile::setposition(int pos)
96 {
97 if(fp==stdin)return;
98 fseek(fp,(long)pos,SEEK_SET);
99 }
100
getposition(void)101 int Soundinputstreamfromfile::getposition(void)
102 {
103 if(fp==stdin)return 0;
104 return (int)ftell(fp);
105 }
106
107
close(void)108 void Soundinputstreamfromfile::close(void)
109 {
110 if (fp)
111 {
112 fclose(fp);
113 fp = NULL;
114 }
115 }
116