File indexing completed on 2024-04-28 16:08:40

0001 /***************************************************************************
0002  *   Copyright (C) 2017 by Linuxstopmotion contributors;                   *
0003  *   see the AUTHORS file for details.                                     *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 
0021 #include "fakefiles.h"
0022 
0023 #include <string.h>
0024 
0025 #include "src/test/oomtestutil.h"
0026 
0027 struct OggVorbis_File;
0028 
0029 RealOggEmptyJpg::RealOggEmptyJpg() : delegate(0),
0030         fakeJpg(reinterpret_cast<FILE*>(1)),
0031         fakePng(reinterpret_cast<FILE*>(2)),
0032         fakeReads(0) {
0033 }
0034 
0035 RealOggEmptyJpg::~RealOggEmptyJpg() {
0036 }
0037 
0038 bool RealOggEmptyJpg::hasExtension(const char* filename, const char* extension) {
0039     const char* dot = strrchr(filename, '.');
0040     return dot && strcmp(dot, extension) == 0;
0041 }
0042 
0043 bool RealOggEmptyJpg::isSound(const char* filename) {
0044     return hasExtension(filename, ".test-sound");
0045 }
0046 
0047 bool RealOggEmptyJpg::isJpg(const char* filename) {
0048     return hasExtension(filename, ".jpg")
0049             || hasExtension(filename, ".JPG")
0050             || hasExtension(filename, ".jpeg");
0051 }
0052 
0053 bool RealOggEmptyJpg::isPng(const char* filename) {
0054     return hasExtension(filename, ".png");
0055 }
0056 
0057 void RealOggEmptyJpg::setDelegate(MockableFileSystem* mfs) {
0058     delegate = mfs;
0059 }
0060 
0061 FILE* RealOggEmptyJpg::fopen(const char* filename, const char* mode) {
0062     if (isJpg(filename)) {
0063         fakeReads = 0;
0064         return fakeJpg;
0065     } else if (isPng(filename)) {
0066         fakeReads = 0;
0067         return fakePng;
0068     } else if (isSound(filename)) {
0069         if (strstr(mode, "w") == 0)
0070             return delegate->fopen("resources/click.test-sound", mode);
0071         return fakePng;
0072     }
0073     return delegate->fopen(filename, mode);
0074 }
0075 
0076 FILE* RealOggEmptyJpg::freopen(const char* filename, const char* mode, FILE* fh) {
0077     if (fh == fakeJpg || fh == fakePng) {
0078         if (filename)
0079             return fopen(filename, mode);
0080         fakeReads = 0;
0081         return fh;
0082     }
0083     return delegate->freopen(filename, mode, fh);
0084 }
0085 
0086 int RealOggEmptyJpg::fclose(FILE* fh) {
0087     if (fh == fakeJpg || fh == fakePng)
0088         return 0;
0089     return delegate->fclose(fh);
0090 }
0091 
0092 int RealOggEmptyJpg::fflush(FILE* fh) {
0093     if (fh == fakeJpg || fh == fakePng)
0094         return 0;
0095     return delegate->fflush(fh);
0096 }
0097 
0098 size_t RealOggEmptyJpg::fread(void *out, size_t blockSize,
0099              size_t blockCount, FILE *fh) {
0100     if (fh == fakeJpg || fh == fakePng) {
0101         if (0 < fakeReads)
0102             return 0;
0103         ++fakeReads;
0104         return blockCount;
0105     }
0106     return delegate->fread(out, blockSize, blockCount, fh);
0107 }
0108 
0109 size_t RealOggEmptyJpg::fwrite(const void *in, size_t blockSize,
0110               size_t blockCount, FILE *fh) {
0111     if (fh == fakeJpg || fh == fakePng)
0112         return blockCount;
0113     return delegate->fwrite(in, blockSize, blockCount, fh);
0114 }
0115 
0116 int RealOggEmptyJpg::access(const char *name, int /*type*/) {
0117     // always assume files within the workspace do not exist
0118     // (as access is only called to find empty slots to use in the
0119     // workspace) but files requested outside of the workspace exist
0120     return strstr(name, ".stopmotion/")? -1 : 0;
0121 }
0122 
0123 int RealOggEmptyJpg::ferror(FILE*) {
0124     return 0;
0125 }
0126 
0127 int RealOggEmptyJpg::unlink(const char *name) {
0128     static const char tmpPrefix[] = "/tmp/";
0129     // really delete any files in /tmp
0130     if (0 == strncmp(name, tmpPrefix, sizeof(tmpPrefix) - 1))
0131         delegate->unlink(name);
0132     return 0;
0133 }
0134 
0135 int RealOggEmptyJpg::ov_test(FILE *, OggVorbis_File *, const char *, long) {
0136     return 0;
0137 }
0138 
0139 int RealOggEmptyJpg::ov_clear(OggVorbis_File *) {
0140     return 0;
0141 }
0142 
0143 int RealOggEmptyJpg::ov_open(FILE *,OggVorbis_File *,const char *, long) {
0144     return 0;
0145 }
0146 
0147 long RealOggEmptyJpg::ov_read(OggVorbis_File *,char *,int, int, int, int, int *) {
0148     return 0;
0149 }
0150 
0151 int ov_raw_seek(OggVorbis_File *,long) {
0152     return 0;
0153 }
0154 
0155 char *RealOggEmptyJpg::getenv(const char *name) {
0156     return delegate->getenv(name);
0157 }