~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to mpeglib/lib/input/fileInputStream.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  reads input data
 
3
  Copyright (C) 1999  Martin Vogt
 
4
 
 
5
  This program is free software; you can redistribute it and/or modify
 
6
  it under the terms of the GNU Library General Public License as published by
 
7
  the Free Software Foundation.
 
8
 
 
9
  For more information look at the file COPYRIGHT in this package
 
10
 
 
11
 */
 
12
 
 
13
 
 
14
 
 
15
#include "fileInputStream.h"
 
16
 
 
17
 
 
18
FileInputStream::FileInputStream() {
 
19
  file=NULL;
 
20
  lopen=false;
 
21
  fileLen=0;
 
22
}
 
23
 
 
24
 
 
25
FileInputStream::~FileInputStream() {
 
26
  close();
 
27
}
 
28
 
 
29
 
 
30
int FileInputStream::open(const char* dest) {
 
31
 
 
32
  close();
 
33
  if (dest == NULL) {
 
34
    return false;
 
35
  }
 
36
  setUrl(dest);
 
37
  if (strlen(dest) == 1) {
 
38
    if (strncmp(dest,"-",1)==0) {
 
39
      file=::fdopen(0,"rb");
 
40
    }
 
41
  }
 
42
  // load out of current dir if no full path is given
 
43
  if (file == NULL) {
 
44
    file=fopen(dest,"rb");
 
45
  }
 
46
  fileLen=0;
 
47
  if (file == NULL) {
 
48
    cout << dest<<endl;
 
49
    perror("cannot open file");
 
50
  } else {
 
51
    lopen=true;
 
52
    struct stat fileStat;
 
53
    stat(dest,&fileStat);
 
54
    fileLen=(long)fileStat.st_size;
 
55
  }
 
56
  int back=(file!=NULL);
 
57
  return back;
 
58
}
 
59
 
 
60
 
 
61
void FileInputStream::close() {
 
62
  if (isOpen()) {
 
63
    ::fclose(file);
 
64
    file=NULL;
 
65
    lopen=false;
 
66
  }
 
67
}
 
68
 
 
69
 
 
70
int FileInputStream::isOpen() {
 
71
  return lopen;
 
72
}
 
73
 
 
74
 
 
75
int FileInputStream::eof() {
 
76
  if (isOpen()==false){
 
77
    return true;
 
78
  }
 
79
  int back=true;
 
80
  if (file != NULL) {
 
81
    back=feof(file);
 
82
  }
 
83
 
 
84
  return back;
 
85
}
 
86
 
 
87
 
 
88
int FileInputStream::read(char* ptr,int size) {
 
89
  int bytesRead=-1;
 
90
  if (isOpen()) {
 
91
    if (size <= 0) {
 
92
      cout << "size is <= 0!"<<endl;
 
93
      return 0;
 
94
    }
 
95
    if (file != NULL) {
 
96
      bytesRead=fread(ptr,1,size,file);
 
97
    }
 
98
  } else {
 
99
    cerr << "read on not open file want:"<<size<<endl;
 
100
    return 0;
 
101
  }
 
102
  if (bytesRead == 0){
 
103
    //perror("zero bytes fread");
 
104
    bytesRead=0;
 
105
  }
 
106
  return bytesRead;
 
107
}
 
108
 
 
109
 
 
110
int FileInputStream::seek(long posInBytes) {
 
111
  int back=true;
 
112
  if (isOpen()==false) {
 
113
    return false;
 
114
  }
 
115
  long pos=-1;
 
116
  if (file != NULL) {
 
117
    pos=fseek(file,posInBytes,SEEK_SET);
 
118
  }
 
119
 
 
120
  if (pos < 0) {
 
121
    //perror("seek in setBytePos");
 
122
    back=false;
 
123
  }
 
124
  return back;
 
125
}
 
126
 
 
127
 
 
128
long FileInputStream::getByteLength() {
 
129
  return fileLen;
 
130
}
 
131
 
 
132
 
 
133
long FileInputStream::getBytePosition() {
 
134
  int back=0;
 
135
  if (isOpen()) {
 
136
    if (file != NULL) {
 
137
      back=ftell(file);
 
138
    }
 
139
  }
 
140
  return back;
 
141
}
 
142
 
 
143
 
 
144
 
 
145
void FileInputStream::print() {
 
146
  printf("pos in file:%8x\n",(int)getBytePosition());
 
147
}
 
148
 
 
149