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-2008 Live Networks, Inc.  All rights reserved.
00018 // Common routines for opening/closing named input files
00019 // Implementation
00020 
00021 #if (defined(__WIN32__) || defined(_WIN32)) && !defined(_WIN32_WCE)
00022 #include <io.h>
00023 #include <fcntl.h>
00024 #endif
00025 #ifndef _WIN32_WCE
00026 #include <sys/stat.h>
00027 #endif
00028 #include <string.h>
00029 
00030 #include "InputFile.hh"
00031 
00032 FILE* OpenInputFile(UsageEnvironment& env, char const* fileName) {
00033   FILE* fid;
00034 
00035   // Check for a special case file name: "stdin"
00036   if (strcmp(fileName, "stdin") == 0) {
00037     fid = stdin;
00038 #if defined(__WIN32__) || defined(_WIN32)
00039     _setmode(_fileno(stdin), _O_BINARY); // convert to binary mode
00040 #endif
00041   } else {
00042     fid = fopen(fileName, "rb");
00043     if (fid == NULL) {
00044       env.setResultMsg("unable to open file \"",fileName, "\"");
00045     }
00046   }
00047 
00048   return fid;
00049 }
00050 
00051 void CloseInputFile(FILE* fid) {
00052   // Don't close 'stdin', in case we want to use it again later.
00053   if (fid != NULL && fid != stdin) fclose(fid);
00054 }
00055 
00056 u_int64_t GetFileSize(char const* fileName, FILE* fid) {
00057   u_int64_t fileSize = 0; // by default
00058 
00059   if (fid != stdin) {
00060 #if !defined(_WIN32_WCE)
00061     if (fileName == NULL) {
00062 #endif
00063       if (fid != NULL && SeekFile64(fid, 0, SEEK_END) >= 0) {
00064         fileSize = (u_int64_t)TellFile64(fid);
00065         if (fileSize == (u_int64_t)-1) fileSize = 0; // TellFile64() failed
00066         SeekFile64(fid, 0, SEEK_SET);
00067       }
00068 #if !defined(_WIN32_WCE)
00069     } else {
00070       struct stat sb;
00071       if (stat(fileName, &sb) == 0) {
00072         fileSize = sb.st_size;
00073       }
00074     }
00075 #endif
00076   }
00077 
00078   return fileSize;
00079 }
00080 
00081 int64_t SeekFile64(FILE *fid, int64_t offset, int whence) {
00082   clearerr(fid);
00083   fflush(fid);
00084 #if (defined(__WIN32__) || defined(_WIN32)) && !defined(_WIN32_WCE)
00085   return _lseeki64(_fileno(fid), offset, whence) == (int64_t)-1 ? -1 : 0;
00086 #else
00087 #if defined(_WIN32_WCE)
00088   return fseek(fid, (long)(offset), whence);
00089 #else
00090   return fseeko(fid, (off_t)(offset), whence);
00091 #endif
00092 #endif
00093 }
00094 
00095 int64_t TellFile64(FILE *fid) {
00096   clearerr(fid);
00097   fflush(fid);
00098 #if (defined(__WIN32__) || defined(_WIN32)) && !defined(_WIN32_WCE)
00099   return _telli64(_fileno(fid));
00100 #else
00101 #if defined(_WIN32_WCE)
00102   return ftell(fid);
00103 #else
00104   return ftello(fid);
00105 #endif
00106 #endif
00107 }

Generated on Tue Oct 7 15:38:09 2008 for live by  doxygen 1.5.2