liveMedia/InputFile.cpp

Go to the documentation of this file.
00001 /**********
00002 This library is free software; you can redistribute it and/or modify it under
00003 the terms of the GNU Lesser General Public License as published by the
00004 Free Software Foundation; either version 2.1 of the License, or (at your
00005 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
00006 
00007 This library is distributed in the hope that it will be useful, but WITHOUT
00008 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00009 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
00010 more details.
00011 
00012 You should have received a copy of the GNU Lesser General Public License
00013 along with this library; if not, write to the Free Software Foundation, Inc.,
00014 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015 **********/
00016 // "liveMedia"
00017 // Copyright (c) 1996-2013 Live Networks, Inc.  All rights reserved.
00018 // Common routines for opening/closing named input files
00019 // Implementation
00020 
00021 #include "InputFile.hh"
00022 #include <string.h>
00023 
00024 FILE* OpenInputFile(UsageEnvironment& env, char const* fileName) {
00025   FILE* fid;
00026 
00027   // Check for a special case file name: "stdin"
00028   if (strcmp(fileName, "stdin") == 0) {
00029     fid = stdin;
00030 #if (defined(__WIN32__) || defined(_WIN32)) && !defined(_WIN32_WCE)
00031     _setmode(_fileno(stdin), _O_BINARY); // convert to binary mode
00032 #endif
00033   } else {
00034     fid = fopen(fileName, "rb");
00035     if (fid == NULL) {
00036       env.setResultMsg("unable to open file \"",fileName, "\"");
00037     }
00038   }
00039 
00040   return fid;
00041 }
00042 
00043 void CloseInputFile(FILE* fid) {
00044   // Don't close 'stdin', in case we want to use it again later.
00045   if (fid != NULL && fid != stdin) fclose(fid);
00046 }
00047 
00048 u_int64_t GetFileSize(char const* fileName, FILE* fid) {
00049   u_int64_t fileSize = 0; // by default
00050 
00051   if (fid != stdin) {
00052 #if !defined(_WIN32_WCE)
00053     if (fileName == NULL) {
00054 #endif
00055       if (fid != NULL && SeekFile64(fid, 0, SEEK_END) >= 0) {
00056         fileSize = (u_int64_t)TellFile64(fid);
00057         if (fileSize == (u_int64_t)-1) fileSize = 0; // TellFile64() failed
00058         SeekFile64(fid, 0, SEEK_SET);
00059       }
00060 #if !defined(_WIN32_WCE)
00061     } else {
00062       struct stat sb;
00063       if (stat(fileName, &sb) == 0) {
00064         fileSize = sb.st_size;
00065       }
00066     }
00067 #endif
00068   }
00069 
00070   return fileSize;
00071 }
00072 
00073 int64_t SeekFile64(FILE *fid, int64_t offset, int whence) {
00074   if (fid == NULL) return -1;
00075 
00076   clearerr(fid);
00077   fflush(fid);
00078 #if (defined(__WIN32__) || defined(_WIN32)) && !defined(_WIN32_WCE)
00079   return _lseeki64(_fileno(fid), offset, whence) == (int64_t)-1 ? -1 : 0;
00080 #else
00081 #if defined(_WIN32_WCE)
00082   return fseek(fid, (long)(offset), whence);
00083 #else
00084   return fseeko(fid, (off_t)(offset), whence);
00085 #endif
00086 #endif
00087 }
00088 
00089 int64_t TellFile64(FILE *fid) {
00090   if (fid == NULL) return -1;
00091 
00092   clearerr(fid);
00093   fflush(fid);
00094 #if (defined(__WIN32__) || defined(_WIN32)) && !defined(_WIN32_WCE)
00095   return _telli64(_fileno(fid));
00096 #else
00097 #if defined(_WIN32_WCE)
00098   return ftell(fid);
00099 #else
00100   return ftello(fid);
00101 #endif
00102 #endif
00103 }
00104 
00105 Boolean FileIsSeekable(FILE *fid) {
00106   if (SeekFile64(fid, 1, SEEK_CUR) < 0) {
00107     return False;
00108   }
00109 
00110   SeekFile64(fid, -1, SEEK_CUR); // seek back to where we were
00111   return True;
00112 }

Generated on Mon Apr 29 13:28:01 2013 for live by  doxygen 1.5.2