1 /* MPEG 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::~Soundinputstreamfromfile()
21 {
22   if(fp)fclose(fp);
23 }
24 
25 bool Soundinputstreamfromfile::open(char *filename)
26 {
27   struct stat buf;
28 
29   if(filename==NULL)
30   {
31     fp=stdin;
32     size=0;
33     return true;
34   }
35   else if((fp=fopen(filename,"r"))==NULL)
36   {
37     seterrorcode(SOUND_ERROR_FILEOPENFAIL);
38     return false;
39   }
40 
41   stat(filename,&buf);
42   size=buf.st_size;
43 
44   return true;
45 }
46 
47 int Soundinputstreamfromfile::getbytedirect(void)
48 {
49   int c;
50 
51   if((c=getc(fp))<0)
52     seterrorcode(SOUND_ERROR_FILEREADFAIL);
53 
54   return c;
55 }
56 
57 bool Soundinputstreamfromfile::_readbuffer(char *buffer,int size)
58 {
59   if(fread(buffer,size,1,fp)!=1)
60   {
61     seterrorcode(SOUND_ERROR_FILEREADFAIL);
62     return false;
63   }
64   return true;
65 }
66 
67 bool Soundinputstreamfromfile::eof(void)
68 {
69   return feof(fp);
70 };
71 
72 int Soundinputstreamfromfile::getblock(char *buffer,int size)
73 {
74   return fread(buffer,1,size,fp);
75 }
76 
77 int Soundinputstreamfromfile::getsize(void)
78 {
79   return size;
80 }
81 
82 void Soundinputstreamfromfile::setposition(int pos)
83 {
84   if(fp==stdin)return;
85   fseek(fp,pos,SEEK_SET);
86 }
87 
88 int  Soundinputstreamfromfile::getposition(void)
89 {
90   if(fp==stdin)return 0;
91   return ftell(fp);
92 }
93 
94