more cleaned-up code
This commit is contained in:
540
amxmodx/file.cpp
540
amxmodx/file.cpp
@@ -5,7 +5,7 @@
|
||||
*
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the
|
||||
* under the terms of the GNU General Public<EFBFBD> License as published by the
|
||||
* Free Software Foundation; either version 2 of the License, or (at
|
||||
* your option) any later version.
|
||||
*
|
||||
@@ -63,11 +63,13 @@ class AutoFilePtr
|
||||
public:
|
||||
AutoFilePtr(FILE *fp) : m_FP(fp)
|
||||
{ }
|
||||
|
||||
~AutoFilePtr()
|
||||
{
|
||||
if (m_FP)
|
||||
fclose(m_FP);
|
||||
}
|
||||
|
||||
operator FILE* ()
|
||||
{
|
||||
return m_FP;
|
||||
@@ -80,20 +82,25 @@ static cell AMX_NATIVE_CALL read_dir(AMX *amx, cell *params)
|
||||
int a;
|
||||
struct dirent *ep;
|
||||
DIR *dp;
|
||||
char* dirname = build_pathname("%s",get_amxstring(amx,params[1],0,a) );
|
||||
char* dirname = build_pathname("%s", get_amxstring(amx, params[1], 0, a));
|
||||
a = params[2];
|
||||
if ( (dp = opendir (dirname)) == NULL )
|
||||
|
||||
if ((dp = opendir (dirname)) == NULL)
|
||||
return 0;
|
||||
seekdir( dp , a );
|
||||
if ( (ep = readdir (dp)) != NULL ) {
|
||||
cell *length = get_amxaddr(amx,params[5]);
|
||||
*length = set_amxstring(amx,params[3], ep->d_name ,params[4]);
|
||||
a = telldir( dp );
|
||||
}
|
||||
else
|
||||
seekdir(dp, a);
|
||||
|
||||
if ((ep = readdir (dp)) != NULL)
|
||||
{
|
||||
cell *length = get_amxaddr(amx, params[5]);
|
||||
*length = set_amxstring(amx, params[3], ep->d_name, params[4]);
|
||||
a = telldir(dp);
|
||||
} else
|
||||
a = 0;
|
||||
|
||||
closedir (dp);
|
||||
|
||||
return a;
|
||||
|
||||
#else
|
||||
int tmp;
|
||||
char *dirname = build_pathname("%s/*", get_amxstring(amx, params[1], 0, tmp));
|
||||
@@ -101,10 +108,12 @@ static cell AMX_NATIVE_CALL read_dir(AMX *amx, cell *params)
|
||||
|
||||
_finddata_t fd;
|
||||
intptr_t handle = _findfirst(dirname, &fd);
|
||||
|
||||
if (handle < 0)
|
||||
return 0;
|
||||
|
||||
++tmp;
|
||||
|
||||
for (int i = 0; i < tmp; ++i)
|
||||
{
|
||||
if (_findnext(handle, &fd) < 0)
|
||||
@@ -113,8 +122,9 @@ static cell AMX_NATIVE_CALL read_dir(AMX *amx, cell *params)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// current data in fd
|
||||
cell *length = get_amxaddr(amx,params[5]); // pointer to the outLen parameter
|
||||
cell *length = get_amxaddr(amx, params[5]); // pointer to the outLen parameter
|
||||
*length = set_amxstring(amx, params[3], fd.name, params[4]); // set output and outLen parameters
|
||||
_findclose(handle);
|
||||
|
||||
@@ -124,178 +134,202 @@ static cell AMX_NATIVE_CALL read_dir(AMX *amx, cell *params)
|
||||
|
||||
static cell AMX_NATIVE_CALL read_file(AMX *amx, cell *params) /* 5 param */
|
||||
{
|
||||
int iLen;
|
||||
char* szFile = get_amxstring(amx,params[1],0,iLen);
|
||||
FILE*fp;
|
||||
if ( (fp =fopen(build_pathname("%s",szFile),"r")) == NULL) {
|
||||
amx_RaiseError(amx,AMX_ERR_NATIVE);
|
||||
return 0;
|
||||
}
|
||||
char buffor[1024];
|
||||
int i = 0, iLine = params[2];
|
||||
while((i <= iLine) && fgets(buffor,1023,fp) )
|
||||
i++;
|
||||
fclose(fp);
|
||||
if (i > iLine){
|
||||
int len = strlen(buffor);
|
||||
if (buffor[len-1]=='\n')
|
||||
buffor[--len]=0;
|
||||
if (buffor[len-1]=='\r')
|
||||
buffor[--len]=0;
|
||||
cell *length = get_amxaddr(amx,params[5]);
|
||||
*length = set_amxstring(amx,params[3],buffor,params[4]);
|
||||
return i;
|
||||
}
|
||||
return 0;
|
||||
int iLen;
|
||||
char* szFile = get_amxstring(amx, params[1], 0, iLen);
|
||||
FILE *fp;
|
||||
|
||||
if ((fp =fopen(build_pathname("%s", szFile), "r")) == NULL)
|
||||
{
|
||||
amx_RaiseError(amx, AMX_ERR_NATIVE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
char buffor[1024];
|
||||
int i = 0, iLine = params[2];
|
||||
|
||||
while ((i <= iLine) && fgets(buffor, 1023, fp))
|
||||
i++;
|
||||
fclose(fp);
|
||||
|
||||
if (i > iLine)
|
||||
{
|
||||
int len = strlen(buffor);
|
||||
if (buffor[len - 1] == '\n')
|
||||
buffor[--len] = 0;
|
||||
if (buffor[len - 1] == '\r')
|
||||
buffor[--len] = 0;
|
||||
cell *length = get_amxaddr(amx, params[5]);
|
||||
*length = set_amxstring(amx, params[3], buffor, params[4]);
|
||||
return i;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL write_file(AMX *amx, cell *params) /* 3 param */
|
||||
{
|
||||
int i;
|
||||
char* sFile = build_pathname("%s", get_amxstring(amx,params[1],0,i) );
|
||||
char* sText = get_amxstring(amx,params[2],0,i);
|
||||
FILE* pFile;
|
||||
int iLine = params[3];
|
||||
int i;
|
||||
char* sFile = build_pathname("%s", get_amxstring(amx, params[1], 0, i));
|
||||
char* sText = get_amxstring(amx, params[2], 0, i);
|
||||
FILE* pFile;
|
||||
int iLine = params[3];
|
||||
|
||||
// apending to the end
|
||||
if (iLine < 0) {
|
||||
if ( (pFile = fopen( sFile ,"a")) == NULL ){
|
||||
amx_RaiseError(amx,AMX_ERR_NATIVE);
|
||||
return 0;
|
||||
}
|
||||
fputs( sText , pFile );
|
||||
fputc( '\n', pFile );
|
||||
fclose( pFile );
|
||||
return 1;
|
||||
}
|
||||
// apending to the end
|
||||
if (iLine < 0)
|
||||
{
|
||||
if ((pFile = fopen(sFile, "a")) == NULL)
|
||||
{
|
||||
amx_RaiseError(amx, AMX_ERR_NATIVE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
fputs(sText, pFile);
|
||||
fputc('\n', pFile);
|
||||
fclose(pFile);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// creating a new file with a line in a middle
|
||||
if ( (pFile = fopen(sFile,"r")) == NULL ) {
|
||||
if ( (pFile = fopen(sFile,"w")) == NULL ){
|
||||
amx_RaiseError(amx,AMX_ERR_NATIVE);
|
||||
return 0;
|
||||
}
|
||||
for(i=0;i < iLine;++i)
|
||||
fputc('\n',pFile);
|
||||
fputs( sText , pFile );
|
||||
fputc( '\n', pFile );
|
||||
fclose(pFile);
|
||||
return 1;
|
||||
}
|
||||
// creating a new file with a line in a middle
|
||||
if ((pFile = fopen(sFile, "r")) == NULL)
|
||||
{
|
||||
if ((pFile = fopen(sFile, "w")) == NULL)
|
||||
{
|
||||
amx_RaiseError(amx, AMX_ERR_NATIVE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < iLine; ++i)
|
||||
fputc('\n', pFile);
|
||||
|
||||
fputs(sText, pFile);
|
||||
fputc('\n', pFile);
|
||||
fclose(pFile);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// adding a new line in a middle of already existing file
|
||||
FILE* pTemp;
|
||||
char buffor[2048];
|
||||
// adding a new line in a middle of already existing file
|
||||
FILE* pTemp;
|
||||
char buffor[2048];
|
||||
|
||||
if ( (pTemp = tmpfile()) == NULL ){
|
||||
amx_RaiseError(amx,AMX_ERR_NATIVE);
|
||||
return 0;
|
||||
}
|
||||
if ((pTemp = tmpfile()) == NULL)
|
||||
{
|
||||
amx_RaiseError(amx, AMX_ERR_NATIVE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
for(i=0;;++i){
|
||||
if ( i == iLine ){
|
||||
fgets(buffor,2047,pFile);
|
||||
fputs( sText , pTemp );
|
||||
fputc( '\n', pTemp );
|
||||
}
|
||||
else if ( fgets(buffor,2047,pFile) ){
|
||||
fputs(buffor , pTemp );
|
||||
}
|
||||
else if ( i < iLine ) {
|
||||
fputc( '\n', pTemp );
|
||||
}
|
||||
else break;
|
||||
}
|
||||
for (i = 0; ; ++i)
|
||||
{
|
||||
if (i == iLine)
|
||||
{
|
||||
fgets(buffor, 2047, pFile);
|
||||
fputs(sText, pTemp);
|
||||
fputc('\n', pTemp);
|
||||
}
|
||||
else if (fgets(buffor, 2047, pFile))
|
||||
{
|
||||
fputs(buffor, pTemp);
|
||||
}
|
||||
else if (i < iLine)
|
||||
{
|
||||
fputc('\n', pTemp);
|
||||
}
|
||||
else break;
|
||||
}
|
||||
|
||||
fclose(pFile);
|
||||
rewind(pTemp);
|
||||
fclose(pFile);
|
||||
rewind(pTemp);
|
||||
|
||||
// now rewrite because file can be now smaller...
|
||||
if ( (pFile = fopen(sFile,"w")) == NULL ){
|
||||
amx_RaiseError(amx,AMX_ERR_NATIVE);
|
||||
return 0;
|
||||
}
|
||||
// now rewrite because file can be now smaller...
|
||||
if ((pFile = fopen(sFile, "w")) == NULL)
|
||||
{
|
||||
amx_RaiseError(amx, AMX_ERR_NATIVE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
while(fgets(buffor,2047,pTemp))
|
||||
fputs(buffor,pFile );
|
||||
while (fgets(buffor, 2047, pTemp))
|
||||
fputs(buffor, pFile);
|
||||
|
||||
fclose(pTemp);
|
||||
fclose(pFile);
|
||||
return 1;
|
||||
fclose(pTemp);
|
||||
fclose(pFile);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL delete_file(AMX *amx, cell *params) /* 1 param */
|
||||
{
|
||||
int iLen;
|
||||
char* sFile = get_amxstring(amx,params[1],0,iLen);
|
||||
return (unlink( build_pathname("%s",sFile) )?0:1);
|
||||
int iLen;
|
||||
char* sFile = get_amxstring(amx, params[1], 0, iLen);
|
||||
return (unlink(build_pathname("%s", sFile)) ? 0 : 1);
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL file_exists(AMX *amx, cell *params) /* 1 param */
|
||||
{
|
||||
int iLen;
|
||||
char *sFile = get_amxstring(amx,params[1],0,iLen);
|
||||
char *file = build_pathname("%s",sFile);
|
||||
int iLen;
|
||||
char *sFile = get_amxstring(amx, params[1], 0, iLen);
|
||||
char *file = build_pathname("%s", sFile);
|
||||
|
||||
#if defined WIN32 || defined _WIN32
|
||||
DWORD attr = GetFileAttributes(file);
|
||||
if (attr == INVALID_FILE_ATTRIBUTES)
|
||||
return 0;
|
||||
if (attr == FILE_ATTRIBUTE_DIRECTORY)
|
||||
return 0;
|
||||
return 1;
|
||||
DWORD attr = GetFileAttributes(file);
|
||||
if (attr == INVALID_FILE_ATTRIBUTES)
|
||||
return 0;
|
||||
if (attr == FILE_ATTRIBUTE_DIRECTORY)
|
||||
return 0;
|
||||
return 1;
|
||||
#else
|
||||
struct stat s;
|
||||
if (stat(file, &s) != 0)
|
||||
return 0;
|
||||
if (S_ISDIR(s.st_mode))
|
||||
return 0;
|
||||
return 1;
|
||||
struct stat s;
|
||||
if (stat(file, &s) != 0)
|
||||
return 0;
|
||||
if (S_ISDIR(s.st_mode))
|
||||
return 0;
|
||||
return 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL dir_exists(AMX *amx, cell *params) /* 1 param */
|
||||
{
|
||||
int iLen;
|
||||
char *sFile = get_amxstring(amx,params[1],0,iLen);
|
||||
char *file = build_pathname("%s",sFile);
|
||||
int iLen;
|
||||
char *sFile = get_amxstring(amx, params[1], 0, iLen);
|
||||
char *file = build_pathname("%s", sFile);
|
||||
|
||||
#if defined WIN32 || defined _WIN32
|
||||
DWORD attr = GetFileAttributes(file);
|
||||
if (attr == INVALID_FILE_ATTRIBUTES)
|
||||
return 0;
|
||||
if (attr == FILE_ATTRIBUTE_DIRECTORY)
|
||||
return 1;
|
||||
return 0;
|
||||
DWORD attr = GetFileAttributes(file);
|
||||
if (attr == INVALID_FILE_ATTRIBUTES)
|
||||
return 0;
|
||||
if (attr == FILE_ATTRIBUTE_DIRECTORY)
|
||||
return 1;
|
||||
return 0;
|
||||
#else
|
||||
struct stat s;
|
||||
if (stat(file, &s) != 0)
|
||||
return 0;
|
||||
if (S_ISDIR(s.st_mode))
|
||||
return 1;
|
||||
return 0;
|
||||
struct stat s;
|
||||
if (stat(file, &s) != 0)
|
||||
return 0;
|
||||
if (S_ISDIR(s.st_mode))
|
||||
return 1;
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL file_size(AMX *amx, cell *params) /* 1 param */
|
||||
{
|
||||
int iLen;
|
||||
char* sFile = get_amxstring(amx,params[1],0,iLen);
|
||||
AutoFilePtr fp(fopen(build_pathname("%s",sFile),"r"));
|
||||
if ( fp != NULL)
|
||||
char* sFile = get_amxstring(amx, params[1], 0, iLen);
|
||||
AutoFilePtr fp(fopen(build_pathname("%s", sFile), "r"));
|
||||
|
||||
if (fp != NULL)
|
||||
{
|
||||
if ( params[0] < 2 || params[2] == 0 )
|
||||
if (params[0] < 2 || params[2] == 0)
|
||||
{
|
||||
fseek(fp,0,SEEK_END);
|
||||
fseek(fp, 0, SEEK_END);
|
||||
int size = ftell(fp);
|
||||
return size;
|
||||
}
|
||||
else if ( params[2] == 1 )
|
||||
else if (params[2] == 1)
|
||||
{
|
||||
int a = 0,lines = 0;
|
||||
while( a != EOF )
|
||||
|
||||
while (a != EOF)
|
||||
{
|
||||
++lines;
|
||||
while ( (a = fgetc(fp)) != '\n' && a != EOF )
|
||||
;
|
||||
while ((a = fgetc(fp)) != '\n' && a != EOF);
|
||||
}
|
||||
//int a, b = '\n';
|
||||
//while( (a = fgetc(fp)) != EOF ){
|
||||
@@ -307,13 +341,16 @@ static cell AMX_NATIVE_CALL file_size(AMX *amx, cell *params) /* 1 param */
|
||||
// ++lines;
|
||||
return lines;
|
||||
}
|
||||
else if ( params[2] == 2 ){
|
||||
fseek(fp,-1,SEEK_END);
|
||||
if ( fgetc(fp) == '\n' )
|
||||
else if (params[2] == 2)
|
||||
{
|
||||
fseek(fp, -1, SEEK_END);
|
||||
|
||||
if (fgetc(fp) == '\n')
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -327,12 +364,13 @@ static cell AMX_NATIVE_CALL amx_fopen(AMX *amx, cell *params)
|
||||
char *flags = get_amxstring(amx, params[2], 0, len);
|
||||
|
||||
FILE *fp = fopen(file, flags);
|
||||
if (fp == NULL) {
|
||||
if (fp == NULL)
|
||||
{
|
||||
// Failed
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (i=0; i<FileList.size(); i++)
|
||||
for (i = 0; i < FileList.size(); i++)
|
||||
{
|
||||
if (FileList.at(i) == NULL)
|
||||
{
|
||||
@@ -340,7 +378,8 @@ static cell AMX_NATIVE_CALL amx_fopen(AMX *amx, cell *params)
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (j==-1)
|
||||
|
||||
if (j == -1)
|
||||
{
|
||||
FileList.push_back(fp);
|
||||
j = FileList.size() - 1;
|
||||
@@ -348,16 +387,19 @@ static cell AMX_NATIVE_CALL amx_fopen(AMX *amx, cell *params)
|
||||
FileList.at(j) = fp;
|
||||
}
|
||||
|
||||
return j+1;
|
||||
return j + 1;
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL amx_fclose(AMX *amx, cell *params)
|
||||
{
|
||||
unsigned int id = params[1] - 1;
|
||||
|
||||
if (id >= FileList.size() || FileList.at(id) == NULL)
|
||||
return 0;
|
||||
FILE *fp = FileList.at(id);
|
||||
if (fp) {
|
||||
|
||||
if (fp)
|
||||
{
|
||||
return fclose(fp);
|
||||
} else {
|
||||
return -1;
|
||||
@@ -367,17 +409,22 @@ static cell AMX_NATIVE_CALL amx_fclose(AMX *amx, cell *params)
|
||||
static cell AMX_NATIVE_CALL amx_fread(AMX *amx, cell *params)
|
||||
{
|
||||
unsigned int id = params[1] - 1;
|
||||
|
||||
if (id >= FileList.size() || FileList.at(id) == NULL)
|
||||
return 0;
|
||||
FILE *fp = FileList.at(id);
|
||||
|
||||
char *buffer;
|
||||
if (fp) {
|
||||
|
||||
if (fp)
|
||||
{
|
||||
buffer = new char[params[3]]; // SLOW!!! :TODO: Find a better way (auto pointers?)
|
||||
fread(buffer, sizeof(char), params[3], fp);
|
||||
set_amxstring(amx, params[2], buffer, params[3]);
|
||||
delete [] buffer;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -385,10 +432,13 @@ static cell AMX_NATIVE_CALL amx_fread(AMX *amx, cell *params)
|
||||
static cell AMX_NATIVE_CALL amx_fgetc(AMX *amx, cell *params)
|
||||
{
|
||||
unsigned int id = params[1] - 1;
|
||||
|
||||
if (id >= FileList.size() || FileList.at(id) == NULL)
|
||||
return 0;
|
||||
FILE *fp = FileList.at(id);
|
||||
if (fp) {
|
||||
|
||||
if (fp)
|
||||
{
|
||||
return fgetc(fp);
|
||||
} else {
|
||||
return -1;
|
||||
@@ -398,89 +448,111 @@ static cell AMX_NATIVE_CALL amx_fgetc(AMX *amx, cell *params)
|
||||
static cell AMX_NATIVE_CALL amx_fwrite(AMX *amx, cell *params)
|
||||
{
|
||||
unsigned int id = params[1] - 1;
|
||||
|
||||
if (id >= FileList.size() || FileList.at(id) == NULL)
|
||||
return 0;
|
||||
FILE *fp = FileList.at(id);
|
||||
char *buf;
|
||||
int len;
|
||||
if (fp) {
|
||||
|
||||
if (fp)
|
||||
{
|
||||
buf = format_amxstring(amx, params, 2, len);
|
||||
return fwrite(buf, sizeof(char), strlen(buf), fp);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL amx_feof(AMX *amx, cell *params)
|
||||
{
|
||||
unsigned int id = params[1] - 1;
|
||||
|
||||
if (id >= FileList.size() || FileList.at(id) == NULL)
|
||||
return 0;
|
||||
FILE *fp = FileList.at(id);
|
||||
if (fp) {
|
||||
if (feof(fp)) {
|
||||
|
||||
if (fp)
|
||||
{
|
||||
if (feof(fp))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL amx_fseek(AMX *amx, cell *params)
|
||||
{
|
||||
unsigned int id = params[1] - 1;
|
||||
|
||||
if (id >= FileList.size() || FileList.at(id) == NULL)
|
||||
return 0;
|
||||
FILE *fp = FileList.at(id);
|
||||
|
||||
if (fp) {
|
||||
if (fp)
|
||||
{
|
||||
return fseek(fp, (long)params[2], params[3]);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL amx_fputc(AMX *amx, cell *params)
|
||||
{
|
||||
unsigned int id = params[1] - 1;
|
||||
|
||||
if (id >= FileList.size() || FileList.at(id) == NULL)
|
||||
return 0;
|
||||
FILE *fp = FileList.at(id);
|
||||
|
||||
if (fp) {
|
||||
if (fp)
|
||||
{
|
||||
return fputc(params[2], fp);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL amx_rewind(AMX *amx, cell *params)
|
||||
{
|
||||
unsigned int id = params[1] - 1;
|
||||
|
||||
if (id >= FileList.size() || FileList.at(id) == NULL)
|
||||
return 0;
|
||||
FILE *fp = FileList.at(id);
|
||||
|
||||
if (fp) {
|
||||
if (fp)
|
||||
{
|
||||
rewind(fp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL amx_fflush(AMX *amx, cell *params)
|
||||
{
|
||||
unsigned int id = params[1] - 1;
|
||||
|
||||
if (id >= FileList.size() || FileList.at(id) == NULL)
|
||||
return 0;
|
||||
FILE *fp = FileList.at(id);
|
||||
|
||||
if (fp) {
|
||||
if (fp)
|
||||
{
|
||||
return fflush(fp);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL amx_fscanf(AMX *amx, cell *params)
|
||||
{
|
||||
unsigned int id = params[1] - 1;
|
||||
|
||||
if (id >= FileList.size() || FileList.at(id) == NULL)
|
||||
return 0;
|
||||
FILE *fp = FileList.at(id);
|
||||
@@ -488,7 +560,9 @@ static cell AMX_NATIVE_CALL amx_fscanf(AMX *amx, cell *params)
|
||||
char *buf;
|
||||
int len;
|
||||
buf = format_amxstring(amx, params, 2, len);
|
||||
if (fp) {
|
||||
|
||||
if (fp)
|
||||
{
|
||||
return fscanf(fp, "%s", buf);
|
||||
}
|
||||
|
||||
@@ -498,13 +572,16 @@ static cell AMX_NATIVE_CALL amx_fscanf(AMX *amx, cell *params)
|
||||
static cell AMX_NATIVE_CALL amx_ftell(AMX *amx, cell *params)
|
||||
{
|
||||
unsigned int id = params[1] - 1;
|
||||
|
||||
if (id >= FileList.size() || FileList.at(id) == NULL)
|
||||
return 0;
|
||||
FILE *fp = FileList.at(id);
|
||||
|
||||
if (fp) {
|
||||
if (fp)
|
||||
{
|
||||
return ftell(fp);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
#endif //UNUSED
|
||||
@@ -515,11 +592,14 @@ static cell AMX_NATIVE_CALL amx_filesize(AMX *amx, cell *params)
|
||||
char *file = build_pathname("%s", format_amxstring(amx, params, 1, len));
|
||||
long size;
|
||||
AutoFilePtr fp(fopen(file, "rb"));
|
||||
if (fp) {
|
||||
|
||||
if (fp)
|
||||
{
|
||||
fseek(fp, 0, SEEK_END);
|
||||
size = ftell(fp);
|
||||
return size;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -527,116 +607,148 @@ static cell AMX_NATIVE_CALL amx_filesize(AMX *amx, cell *params)
|
||||
static cell AMX_NATIVE_CALL amx_fgetl(AMX *amx, cell *params)
|
||||
{
|
||||
unsigned int id = params[1] - 1;
|
||||
|
||||
if (id >= FileList.size() || FileList.at(id) == NULL)
|
||||
return 0;
|
||||
FILE *fp = FileList.at(id);
|
||||
|
||||
long t;
|
||||
if (fp) {
|
||||
|
||||
if (fp)
|
||||
{
|
||||
fread(&t, sizeof(long), 1, fp);
|
||||
return t;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL amx_fgeti(AMX *amx, cell *params)
|
||||
{
|
||||
unsigned int id = params[1] - 1;
|
||||
|
||||
if (id >= FileList.size() || FileList.at(id) == NULL)
|
||||
return 0;
|
||||
FILE *fp = FileList.at(id);
|
||||
|
||||
int t;
|
||||
if (fp) {
|
||||
|
||||
if (fp)
|
||||
{
|
||||
fread(&t, sizeof(int), 1, fp);
|
||||
return t;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL amx_fgets(AMX *amx, cell *params)
|
||||
{
|
||||
unsigned int id = params[1] - 1;
|
||||
|
||||
if (id >= FileList.size() || FileList.at(id) == NULL)
|
||||
return 0;
|
||||
FILE *fp = FileList.at(id);
|
||||
|
||||
short t;
|
||||
if (fp) {
|
||||
|
||||
if (fp)
|
||||
{
|
||||
fread(&t, sizeof(short), 1, fp);
|
||||
return t;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL amx_fputs(AMX *amx, cell *params)
|
||||
{
|
||||
unsigned int id = params[1] - 1;
|
||||
|
||||
if (id >= FileList.size() || FileList.at(id) == NULL)
|
||||
return 0;
|
||||
FILE *fp = FileList.at(id);
|
||||
|
||||
short size = params[2];
|
||||
if (fp) {
|
||||
|
||||
if (fp)
|
||||
{
|
||||
return fwrite(&size, sizeof(short), 1, fp);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL amx_fputl(AMX *amx, cell *params)
|
||||
{
|
||||
unsigned int id = params[1] - 1;
|
||||
|
||||
if (id >= FileList.size() || FileList.at(id) == NULL)
|
||||
return 0;
|
||||
FILE *fp = FileList.at(id);
|
||||
|
||||
long size = params[2];
|
||||
if (fp) {
|
||||
|
||||
if (fp)
|
||||
{
|
||||
return fwrite(&size, sizeof(long), 1, fp);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL amx_fputi(AMX *amx, cell *params)
|
||||
{
|
||||
unsigned int id = params[1] - 1;
|
||||
|
||||
if (id >= FileList.size() || FileList.at(id) == NULL)
|
||||
return 0;
|
||||
FILE *fp = FileList.at(id);
|
||||
|
||||
int size = params[2];
|
||||
if (fp) {
|
||||
|
||||
if (fp)
|
||||
{
|
||||
return fwrite(&size, sizeof(int), 1, fp);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL amx_fgetf(AMX *amx, cell *params)
|
||||
{
|
||||
unsigned int id = params[1] - 1;
|
||||
|
||||
if (id >= FileList.size() || FileList.at(id) == NULL)
|
||||
return 0;
|
||||
FILE *fp = FileList.at(id);
|
||||
|
||||
float t;
|
||||
if (fp) {
|
||||
|
||||
if (fp)
|
||||
{
|
||||
fread(&t, sizeof(float), 1, fp);
|
||||
return *(cell*)&t;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static cell AMX_NATIVE_CALL amx_fputf(AMX *amx, cell *params)
|
||||
{
|
||||
unsigned int id = params[1] - 1;
|
||||
|
||||
if (id >= FileList.size() || FileList.at(id) == NULL)
|
||||
return 0;
|
||||
FILE *fp = FileList.at(id);
|
||||
|
||||
float size = *(float *)((void *)¶ms[2]);
|
||||
if (fp) {
|
||||
|
||||
if (fp)
|
||||
{
|
||||
return fwrite(&size, sizeof(float), 1, fp);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
#endif //UNUSED
|
||||
@@ -657,22 +769,28 @@ static cell AMX_NATIVE_CALL amx_open_dir(AMX *amx, cell *params)
|
||||
char *dirname = build_pathname("%s\\*", path);
|
||||
WIN32_FIND_DATA fd;
|
||||
HANDLE hFile = FindFirstFile(dirname, &fd);
|
||||
|
||||
if (hFile == INVALID_HANDLE_VALUE)
|
||||
return 0;
|
||||
set_amxstring(amx, params[2], fd.cFileName, params[3]);
|
||||
|
||||
return (DWORD)hFile;
|
||||
#else
|
||||
char *dirname = build_pathname("%s", path);
|
||||
DIR *dp = opendir(dirname);
|
||||
|
||||
if (!dp)
|
||||
return NULL;
|
||||
struct dirent *ep = readdir(dp);
|
||||
|
||||
if (!ep)
|
||||
{
|
||||
closedir(dp);
|
||||
return NULL;
|
||||
}
|
||||
set_amxstring(amx,params[2], ep->d_name,params[3]);
|
||||
|
||||
set_amxstring(amx, params[2], ep->d_name, params[3]);
|
||||
|
||||
return (cell)dp;
|
||||
#endif
|
||||
}
|
||||
@@ -681,14 +799,18 @@ static cell AMX_NATIVE_CALL amx_close_dir(AMX *amx, cell *params)
|
||||
{
|
||||
#if defined WIN32 || defined _WIN32
|
||||
HANDLE hFile = (HANDLE)((DWORD)params[1]);
|
||||
|
||||
if (hFile == INVALID_HANDLE_VALUE || hFile == NULL)
|
||||
return 0;
|
||||
FindClose(hFile);
|
||||
|
||||
return 1;
|
||||
#else
|
||||
DIR *dp = (DIR *)params[1];
|
||||
|
||||
if (!dp)
|
||||
return 0;
|
||||
|
||||
closedir(dp);
|
||||
return 1;
|
||||
#endif
|
||||
@@ -698,64 +820,68 @@ static cell AMX_NATIVE_CALL amx_get_dir(AMX *amx, cell *params)
|
||||
{
|
||||
#if defined WIN32 || defined _WIN32
|
||||
HANDLE hFile = (HANDLE)((DWORD)params[1]);
|
||||
|
||||
if (hFile == INVALID_HANDLE_VALUE || hFile == NULL)
|
||||
return 0;
|
||||
WIN32_FIND_DATA fd;
|
||||
|
||||
if (!FindNextFile(hFile, &fd))
|
||||
return 0;
|
||||
set_amxstring(amx, params[2], fd.cFileName, params[3]);
|
||||
|
||||
return 1;
|
||||
#else
|
||||
DIR *dp = (DIR *)params[1];
|
||||
|
||||
if (!dp)
|
||||
return 0;
|
||||
struct dirent *ep = readdir(dp);
|
||||
|
||||
if (!ep)
|
||||
return 0;
|
||||
set_amxstring(amx,params[2], ep->d_name,params[3]);
|
||||
set_amxstring(amx, params[2], ep->d_name, params[3]);
|
||||
|
||||
return 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
AMX_NATIVE_INFO file_Natives[] = {
|
||||
{ "delete_file", delete_file },
|
||||
{ "file_exists", file_exists },
|
||||
{ "file_size", file_size },
|
||||
{ "read_dir", read_dir },
|
||||
{ "read_file", read_file },
|
||||
{ "write_file", write_file },
|
||||
//Sanji's File Natives
|
||||
{ "fopen", amx_fopen },
|
||||
{ "fclose", amx_fclose },
|
||||
{ "fread", amx_fread },
|
||||
{ "filesize", amx_filesize },
|
||||
AMX_NATIVE_INFO file_Natives[] =
|
||||
{
|
||||
{"delete_file", delete_file},
|
||||
{"file_exists", file_exists},
|
||||
{"file_size", file_size},
|
||||
{"read_dir", read_dir},
|
||||
{"read_file", read_file},
|
||||
{"write_file", write_file},
|
||||
//Sanji's File Natives
|
||||
{"fopen", amx_fopen},
|
||||
{"fclose", amx_fclose},
|
||||
{"fread", amx_fread},
|
||||
{"filesize", amx_filesize},
|
||||
#ifdef UNUSED
|
||||
{ "fgetc", amx_fgetc },
|
||||
{ "fwrite", amx_fwrite },
|
||||
{ "feof", amx_feof },
|
||||
{ "fseek", amx_fseek },
|
||||
{ "fputc", amx_fputc },
|
||||
{ "rewind", amx_rewind },
|
||||
{ "fflush", amx_fflush },
|
||||
{ "fscanf", amx_fscanf },
|
||||
{ "ftell", amx_ftell },
|
||||
{ "fgetl", amx_fgetl },
|
||||
{ "fgeti", amx_fgeti },
|
||||
{ "fgets", amx_fgets },
|
||||
{ "fputs", amx_fputs },
|
||||
{ "fputl", amx_fputl },
|
||||
{ "fputi", amx_fputi },
|
||||
{ "fgetf", amx_fgetf },
|
||||
{ "fputf", amx_fputf },
|
||||
{"fgetc", amx_fgetc},
|
||||
{"fwrite", amx_fwrite},
|
||||
{"feof", amx_feof},
|
||||
{"fseek", amx_fseek},
|
||||
{"fputc", amx_fputc},
|
||||
{"rewind", amx_rewind},
|
||||
{"fflush", amx_fflush},
|
||||
{"fscanf", amx_fscanf},
|
||||
{"ftell", amx_ftell},
|
||||
{"fgetl", amx_fgetl},
|
||||
{"fgeti", amx_fgeti},
|
||||
{"fgets", amx_fgets},
|
||||
{"fputs", amx_fputs},
|
||||
{"fputl", amx_fputl},
|
||||
{"fputi", amx_fputi},
|
||||
{"fgetf", amx_fgetf},
|
||||
{"fputf", amx_fputf},
|
||||
#endif
|
||||
{ "unlink", delete_file },
|
||||
{ "build_pathname", amx_build_pathname},
|
||||
{ "dir_exists", dir_exists },
|
||||
{ "open_dir", amx_open_dir },
|
||||
{ "close_dir", amx_close_dir },
|
||||
{ "next_file", amx_get_dir },
|
||||
|
||||
{ NULL, NULL }
|
||||
{"unlink", delete_file},
|
||||
{"build_pathname", amx_build_pathname},
|
||||
{"dir_exists", dir_exists},
|
||||
{"open_dir", amx_open_dir},
|
||||
{"close_dir", amx_close_dir},
|
||||
{"next_file", amx_get_dir},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user