1 | /********** |
2 | This library is free software; you can redistribute it and/or modify it under |
3 | the terms of the GNU Lesser General Public License as published by the |
4 | Free Software Foundation; either version 3 of the License, or (at your |
5 | option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.) |
6 | |
7 | This library is distributed in the hope that it will be useful, but WITHOUT |
8 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
9 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
10 | more details. |
11 | |
12 | You should have received a copy of the GNU Lesser General Public License |
13 | along with this library; if not, write to the Free Software Foundation, Inc., |
14 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
15 | **********/ |
16 | // "liveMedia" |
17 | // Copyright (c) 1996-2020 Live Networks, Inc. All rights reserved. |
18 | // Common routines for opening/closing named input files |
19 | // Implementation |
20 | |
21 | #include "InputFile.hh" |
22 | #include <string.h> |
23 | |
24 | FILE* OpenInputFile(UsageEnvironment& env, char const* fileName) { |
25 | FILE* fid; |
26 | |
27 | // Check for a special case file name: "stdin" |
28 | if (strcmp(fileName, "stdin" ) == 0) { |
29 | fid = stdin; |
30 | #if (defined(__WIN32__) || defined(_WIN32)) && !defined(_WIN32_WCE) |
31 | _setmode(_fileno(stdin), _O_BINARY); // convert to binary mode |
32 | #endif |
33 | } else { |
34 | fid = fopen(fileName, "rb" ); |
35 | if (fid == NULL) { |
36 | env.setResultMsg("unable to open file \"" ,fileName, "\"" ); |
37 | } |
38 | } |
39 | |
40 | return fid; |
41 | } |
42 | |
43 | void CloseInputFile(FILE* fid) { |
44 | // Don't close 'stdin', in case we want to use it again later. |
45 | if (fid != NULL && fid != stdin) fclose(fid); |
46 | } |
47 | |
48 | u_int64_t GetFileSize(char const* fileName, FILE* fid) { |
49 | u_int64_t fileSize = 0; // by default |
50 | |
51 | if (fid != stdin) { |
52 | #if !defined(_WIN32_WCE) |
53 | if (fileName == NULL) { |
54 | #endif |
55 | if (fid != NULL && SeekFile64(fid, 0, SEEK_END) >= 0) { |
56 | fileSize = (u_int64_t)TellFile64(fid); |
57 | if (fileSize == (u_int64_t)-1) fileSize = 0; // TellFile64() failed |
58 | SeekFile64(fid, 0, SEEK_SET); |
59 | } |
60 | #if !defined(_WIN32_WCE) |
61 | } else { |
62 | struct stat sb; |
63 | if (stat(fileName, &sb) == 0) { |
64 | fileSize = sb.st_size; |
65 | } |
66 | } |
67 | #endif |
68 | } |
69 | |
70 | return fileSize; |
71 | } |
72 | |
73 | int64_t SeekFile64(FILE *fid, int64_t offset, int whence) { |
74 | if (fid == NULL) return -1; |
75 | |
76 | clearerr(fid); |
77 | fflush(fid); |
78 | #if (defined(__WIN32__) || defined(_WIN32)) && !defined(_WIN32_WCE) |
79 | return _lseeki64(_fileno(fid), offset, whence) == (int64_t)-1 ? -1 : 0; |
80 | #else |
81 | #if defined(_WIN32_WCE) |
82 | return fseek(fid, (long)(offset), whence); |
83 | #else |
84 | return fseeko(fid, (off_t)(offset), whence); |
85 | #endif |
86 | #endif |
87 | } |
88 | |
89 | int64_t TellFile64(FILE *fid) { |
90 | if (fid == NULL) return -1; |
91 | |
92 | clearerr(fid); |
93 | fflush(fid); |
94 | #if (defined(__WIN32__) || defined(_WIN32)) && !defined(_WIN32_WCE) |
95 | return _telli64(_fileno(fid)); |
96 | #else |
97 | #if defined(_WIN32_WCE) |
98 | return ftell(fid); |
99 | #else |
100 | return ftello(fid); |
101 | #endif |
102 | #endif |
103 | } |
104 | |
105 | Boolean FileIsSeekable(FILE *fid) { |
106 | if (SeekFile64(fid, 1, SEEK_CUR) < 0) { |
107 | return False; |
108 | } |
109 | |
110 | SeekFile64(fid, -1, SEEK_CUR); // seek back to where we were |
111 | return True; |
112 | } |
113 | |