Fixed problem where file handles could possible stay open
This commit is contained in:
parent
605ca152c2
commit
981f41aee0
|
@ -57,6 +57,23 @@
|
||||||
|
|
||||||
CVector<FILE *> FileList;
|
CVector<FILE *> FileList;
|
||||||
|
|
||||||
|
class AutoFilePtr
|
||||||
|
{
|
||||||
|
FILE *m_FP;
|
||||||
|
public:
|
||||||
|
AutoFilePtr(FILE *fp) : m_FP(fp)
|
||||||
|
{ }
|
||||||
|
~AutoFilePtr()
|
||||||
|
{
|
||||||
|
if (m_FP)
|
||||||
|
fclose(m_FP);
|
||||||
|
}
|
||||||
|
operator FILE* ()
|
||||||
|
{
|
||||||
|
return m_FP;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
static cell AMX_NATIVE_CALL read_dir(AMX *amx, cell *params)
|
static cell AMX_NATIVE_CALL read_dir(AMX *amx, cell *params)
|
||||||
{
|
{
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
|
@ -228,41 +245,44 @@ static cell AMX_NATIVE_CALL file_exists(AMX *amx, cell *params) /* 1 param */
|
||||||
|
|
||||||
static cell AMX_NATIVE_CALL file_size(AMX *amx, cell *params) /* 1 param */
|
static cell AMX_NATIVE_CALL file_size(AMX *amx, cell *params) /* 1 param */
|
||||||
{
|
{
|
||||||
int iLen;
|
int iLen;
|
||||||
char* sFile = get_amxstring(amx,params[1],0,iLen);
|
char* sFile = get_amxstring(amx,params[1],0,iLen);
|
||||||
FILE* fp = fopen(build_pathname("%s",sFile),"r");
|
AutoFilePtr fp(fopen(build_pathname("%s",sFile),"r"));
|
||||||
if ( fp != NULL) {
|
if ( fp != NULL)
|
||||||
if ( params[0] < 2 || params[2] == 0 ){
|
{
|
||||||
fseek(fp,0,SEEK_END);
|
if ( params[0] < 2 || params[2] == 0 )
|
||||||
int size = ftell(fp);
|
{
|
||||||
fclose(fp);
|
fseek(fp,0,SEEK_END);
|
||||||
return size;
|
int size = ftell(fp);
|
||||||
}
|
return size;
|
||||||
else if ( params[2] == 1 ){
|
}
|
||||||
int a = 0,lines = 0;
|
else if ( params[2] == 1 )
|
||||||
while( a != EOF ){
|
{
|
||||||
++lines;
|
int a = 0,lines = 0;
|
||||||
while ( (a = fgetc(fp)) != '\n' && a != EOF )
|
while( a != EOF )
|
||||||
;
|
{
|
||||||
}
|
++lines;
|
||||||
//int a, b = '\n';
|
while ( (a = fgetc(fp)) != '\n' && a != EOF )
|
||||||
//while( (a = fgetc(fp)) != EOF ){
|
;
|
||||||
// if ( a == '\n')
|
}
|
||||||
// ++lines;
|
//int a, b = '\n';
|
||||||
// b = a;
|
//while( (a = fgetc(fp)) != EOF ){
|
||||||
//}
|
// if ( a == '\n')
|
||||||
//if ( b != '\n' )
|
// ++lines;
|
||||||
// ++lines;
|
// b = a;
|
||||||
return lines;
|
//}
|
||||||
}
|
//if ( b != '\n' )
|
||||||
else if ( params[2] == 2 ){
|
// ++lines;
|
||||||
fseek(fp,-1,SEEK_END);
|
return lines;
|
||||||
if ( fgetc(fp) == '\n' )
|
}
|
||||||
return 1;
|
else if ( params[2] == 2 ){
|
||||||
return 0;
|
fseek(fp,-1,SEEK_END);
|
||||||
}
|
if ( fgetc(fp) == '\n' )
|
||||||
}
|
return 1;
|
||||||
return -1;
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//ported from Sanji's file access module by BAILOPAN
|
//ported from Sanji's file access module by BAILOPAN
|
||||||
|
@ -454,7 +474,7 @@ static cell AMX_NATIVE_CALL amx_filesize(AMX *amx, cell *params)
|
||||||
int len;
|
int len;
|
||||||
char *file = build_pathname("%s", format_amxstring(amx, params, 1, len));
|
char *file = build_pathname("%s", format_amxstring(amx, params, 1, len));
|
||||||
long size;
|
long size;
|
||||||
FILE *fp = fopen(file, "rb");
|
AutoFilePtr fp(fopen(file, "rb"));
|
||||||
if (fp) {
|
if (fp) {
|
||||||
fseek(fp, 0, SEEK_END);
|
fseek(fp, 0, SEEK_END);
|
||||||
size = ftell(fp);
|
size = ftell(fp);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user