amxmodx/amxmodx/CFile.cpp

105 lines
1.7 KiB
C++
Raw Normal View History

2014-08-04 08:36:20 +00:00
// vim: set ts=4 sw=4 tw=99 noet:
//
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
// Copyright (C) The AMX Mod X Development Team.
//
// This software is licensed under the GNU General Public License, version 3 or higher.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://alliedmods.net/amxmodx-license
2004-01-31 20:56:22 +00:00
#include <ctype.h>
2004-04-22 07:52:26 +00:00
#include "amxmodx.h"
#include "CFile.h"
2004-01-31 20:56:22 +00:00
// *****************************************************
// class File
// *****************************************************
File::File(const char* n, const char* m)
2004-01-31 20:56:22 +00:00
{
fp = fopen(n, m);
2004-01-31 20:56:22 +00:00
}
File::~File()
2004-01-31 20:56:22 +00:00
{
if (fp)
fclose(fp);
2004-01-31 20:56:22 +00:00
}
File::operator bool () const
2004-01-31 20:56:22 +00:00
{
return fp && !feof(fp);
2004-01-31 20:56:22 +00:00
}
File& operator<<(File& f, const String& n)
2004-01-31 20:56:22 +00:00
{
if (f) fputs(n.c_str(), f.fp);
return f;
2004-01-31 20:56:22 +00:00
}
File& operator<<(File& f, const char* n)
2004-01-31 20:56:22 +00:00
{
if (f) fputs(n, f.fp);
return f;
2004-01-31 20:56:22 +00:00
}
File& operator<<(File& f, int n)
2004-01-31 20:56:22 +00:00
{
if (f) fprintf(f.fp, "%d", n);
return f;
2004-01-31 20:56:22 +00:00
}
File& operator<<(File& f, const char& c)
2004-01-31 20:56:22 +00:00
{
if (f) fputc(c, f.fp);
return f;
2004-01-31 20:56:22 +00:00
}
File& operator>>(File& f, String& n)
2004-01-31 20:56:22 +00:00
{
if (!f) return f;
char temp[1024];
fscanf(f.fp, "%s", temp);
n.assign(temp);
return f;
2004-01-31 20:56:22 +00:00
}
File& operator>>(File& f, char* n)
2004-01-31 20:56:22 +00:00
{
if (f) fscanf(f.fp, "%s", n);
return f;
2004-01-31 20:56:22 +00:00
}
int File::getline(char* buf, int sz)
2004-01-31 20:56:22 +00:00
{
int a = sz;
char *origBuf = buf;
if (*this)
{
int c;
while (sz-- && (c = getc((*this).fp)) && c != EOF && c != '\n')
*buf++ = c;
2004-03-24 18:35:04 +00:00
*buf = 0;
}
2004-03-24 18:35:04 +00:00
// trim 0x0a and 0x0d characters at the end
while (buf != origBuf)
{
if (*buf == 0x0a || *buf == 0x0d)
*buf = 0;
--buf;
}
return a - sz;
2004-01-31 20:56:22 +00:00
}
File& File::skipWs()
2004-01-31 20:56:22 +00:00
{
if (!*this) return *this;
int c;
while (isspace(c = getc(fp))) {};
ungetc(c, fp);
return *this;
2004-01-31 20:56:22 +00:00
}