Skip to content

Commit

Permalink
Add more fileio functions
Browse files Browse the repository at this point in the history
  • Loading branch information
clementgallet committed May 8, 2016
1 parent a1b45e9 commit cf72cfb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
52 changes: 50 additions & 2 deletions src/libTAS/fileio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ namespace orig {
static int (*vfprintf) (FILE *s, const char *format, va_list arg) = nullptr;
static int (*fputc) (int c, FILE *stream) = nullptr;
static int (*putc) (int c, FILE *stream) = nullptr;
static int (*putc_unlocked) (int c, FILE *stream);
static int (*fputs) (const char *s, FILE *stream);
static int (*fputs_unlocked) (const char *s, FILE *stream);
static size_t (*fwrite) (const void *ptr, size_t size,
size_t n, FILE *s) = nullptr;
}
Expand All @@ -100,7 +103,7 @@ static std::map<FILE*, std::string> stdio_savefiles;

static bool isWriteable(const char *modes)
{
if (strcmp(modes, "r") || strcmp(modes, "rb"))
if ((strcmp(modes, "r") == 0) || (strcmp(modes, "rb") == 0))
return false;
return true;
}
Expand Down Expand Up @@ -293,7 +296,7 @@ int fputc (int c, FILE *stream)
int putc (int c, FILE *stream)
{
LINK_NAMESPACE(putc, nullptr);
debuglogstdio(LCF_FILEIO, "%s call", __func__);
//debuglogstdio(LCF_FILEIO, "%s call", __func__);

if (config.prevent_savefiles) {
if (stdio_savefiles.find(stream) != stdio_savefiles.end()) {
Expand All @@ -305,6 +308,51 @@ int putc (int c, FILE *stream)
return orig::putc(c, stream);
}

int putc_unlocked (int c, FILE *stream)
{
LINK_NAMESPACE(putc_unlocked, nullptr);
debuglogstdio(LCF_FILEIO, "%s call", __func__);

if (config.prevent_savefiles) {
if (stdio_savefiles.find(stream) != stdio_savefiles.end()) {
debuglog(LCF_FILEIO, " prevent write to ", stdio_savefiles[stream]);
return c;
}
}

return orig::putc_unlocked(c, stream);
}

int fputs (const char *s, FILE *stream)
{
LINK_NAMESPACE(fputs, nullptr);
//debuglogstdio(LCF_FILEIO, "%s call", __func__);

if (config.prevent_savefiles) {
if (stdio_savefiles.find(stream) != stdio_savefiles.end()) {
debuglog(LCF_FILEIO, " prevent write to ", stdio_savefiles[stream]);
return 0;
}
}

return orig::fputs(s, stream);
}

int fputs_unlocked (const char *s, FILE *stream)
{
LINK_NAMESPACE(fputs_unlocked, nullptr);
//debuglogstdio(LCF_FILEIO, "%s call", __func__);

if (config.prevent_savefiles) {
if (stdio_savefiles.find(stream) != stdio_savefiles.end()) {
debuglog(LCF_FILEIO, " prevent write to ", stdio_savefiles[stream]);
return 0;
}
}

return orig::fputs_unlocked(s, stream);
}

size_t fwrite (const void *ptr, size_t size, size_t n, FILE *s)
{
LINK_NAMESPACE(fwrite, nullptr);
Expand Down
9 changes: 9 additions & 0 deletions src/libTAS/fileio.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ OVERRIDE int vfprintf (FILE *s, const char *format, va_list arg);
OVERRIDE int fputc (int c, FILE *stream);
OVERRIDE int putc (int c, FILE *stream);

/* Faster version when locking is not necessary. */
OVERRIDE int putc_unlocked (int c, FILE *stream);

/* Write a string to STREAM. */
OVERRIDE int fputs (const char *s, FILE *stream);

/* This function does the same as `fputs' but does not lock the stream. */
OVERRIDE int fputs_unlocked (const char *s, FILE *stream);

/* Write chunks of generic data to STREAM. */
OVERRIDE size_t fwrite (const void *ptr, size_t size,
size_t n, FILE *s);
Expand Down

0 comments on commit cf72cfb

Please sign in to comment.