/* Mednafen - Multi-system Emulator1*2* This program is free software; you can redistribute it and/or modify3* it under the terms of the GNU General Public License as published by4* the Free Software Foundation; either version 2 of the License, or5* (at your option) any later version.6*7* This program is distributed in the hope that it will be useful,8* but WITHOUT ANY WARRANTY; without even the implied warranty of9* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the10* GNU General Public License for more details.11*12* You should have received a copy of the GNU General Public License13* along with this program; if not, write to the Free Software14* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA15*/1617#include <sys/stat.h>18#include "mednafen.h"19#include "FileWrapper.h"2021#include <trio/trio.h>22#include <stdarg.h>23#include <string.h>2425#ifdef _WIN3226#include <io.h>27#else28#include <unistd.h>29#endif3031// Some really bad preprocessor abuse follows to handle platforms that don't have fseeko and ftello...and of course32// for largefile support on Windows:3334#define fseeko fseek35#define ftello ftell3637// For special uses, IE in classes that take a path or a FileWrapper & in the constructor, and the FileWrapper non-pointer member38// is in the initialization list for the path constructor but not the constructor with FileWrapper&3940FileWrapper::FileWrapper(const char *path, const int mode, const char *purpose) : OpenedMode(mode)41{42if(!(fp = fopen(path, (mode == MODE_WRITE) ? "wb" : "rb")))43{44ErrnoHolder ene(errno);4546throw(MDFN_Error(ene.Errno(), _("Error opening file %s"), ene.StrError()));47}48}4950FileWrapper::~FileWrapper()51{52close();53}5455void FileWrapper::close(void)56{57if(!fp)58return;5960FILE *tmp = fp;61fp = NULL;62fclose(tmp);63}6465uint64 FileWrapper::read(void *data, uint64 count, bool error_on_eof)66{67return fread(data, 1, count, fp);68}6970void FileWrapper::flush(void)71{72fflush(fp);73}7475void FileWrapper::write(const void *data, uint64 count)76{77fwrite(data, 1, count, fp);78}7980int FileWrapper::scanf(const char *format, ...)81{82va_list ap;83int ret;8485va_start(ap, format);8687ret = trio_vfscanf(fp, format, ap);8889va_end(ap);9091return ret;92}9394void FileWrapper::put_char(int c)95{96fputc(c, fp);97}9899void FileWrapper::put_string(const char *str)100{101write(str, strlen(str));102}103104// We need to decide whether to prohibit NULL characters in output and input strings via std::string.105// Yes for correctness, no for potential security issues(though unlikely in context all things considered).106void FileWrapper::put_string(const std::string &str)107{108write(str.data(), str.size());109}110111char *FileWrapper::get_line(char *buf_s, int buf_size)112{113return ::fgets(buf_s, buf_size, fp);114}115116117void FileWrapper::seek(int64 offset, int whence)118{119fseeko(fp, offset, whence);120}121122int64 FileWrapper::size(void)123{124struct stat buf;125126fstat(fileno(fp), &buf);127128return(buf.st_size);129}130131int64 FileWrapper::tell(void)132{133return ftello(fp);134}135136137