amxmodx/plugins/include/file.inc

124 lines
3.6 KiB
PHP
Raw Normal View History

2004-01-31 20:56:22 +00:00
/* Files functions
2004-02-08 11:31:54 +00:00
*
* by the AMX Mod X Development Team
* originally developed by OLO
2004-02-08 11:31:54 +00:00
*
* This file is provided as is (no warranties).
*/
2004-01-31 20:56:22 +00:00
#if defined _file_included
#endinput
#endif
#define _file_included
2004-01-31 20:56:22 +00:00
/* Reads content from directory.
* Returns index of next element or 0 when end of dir. is reached. */
2004-01-31 20:56:22 +00:00
native read_dir(const dirname[],pos,output[],len,&outlen);
/* Reads line from file. Returns index of next line or 0 when end of file is reached. */
native read_file(const file[],line,text[],len,&txtlen);
/* Writes text to file. Function returns 0 on failure.
* When line is set to -1, the text is added at the end of file. */
2004-01-31 20:56:22 +00:00
native write_file(const file[],const text[],line = -1);
/* Deletes file. Function returns 1 on success, 0 on failure. */
native delete_file(const file[]);
/* Checks for file. If file exists function returns 1, in other case 0. */
native file_exists(const file[]);
/* Checks if a directory exists */
native dir_exists(const dir[]);
2004-01-31 20:56:22 +00:00
/* Returns a file size in bytes if flag is set to 0.
* When flag is set to 1 returns number of lines in the file,
* and when flags is 2, function returns 1 if the file ends
* with line feed. If file doesn't exist returns -1. */
2004-04-03 20:05:27 +00:00
native file_size(const file[], flag=0);
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
// These are C style file access functions
// Code ported from Sanji's File module
//Open a file
native fopen(const filename[],const mode[]);
2004-04-03 20:05:27 +00:00
//Close a file
native fclose(file);
//Read a file for ret_size length
native fread(file,ret[],ret_size);
//Write a file
2004-09-07 09:26:20 +00:00
native fwrite(file,const str[],{Float,Sql,Result,_}:...);
2004-04-03 20:05:27 +00:00
//Check if at the end of a file
native feof(file);
//Seek a file
native fseek(file,pos,type);
//Rewind a file (reset)
native rewind(file);
//Flush a file
native fflush(file);
//Scan a file by a format
2004-09-07 09:26:20 +00:00
native fscanf(file,const str[],{Float,Sql,Result,_}:...);
2004-04-03 20:05:27 +00:00
//Returns the current file position pointer
native ftell(file);
//Return the size of a file
2004-09-07 09:26:20 +00:00
native filesize(const filename[],{Float,Sql,Result,_}:...);
2004-04-03 20:05:27 +00:00
//Delete a file (delete_file macro)
2004-09-07 09:26:20 +00:00
native unlink(const filename[],{Float,Sql,Result,_}:...);
2004-04-03 20:05:27 +00:00
2005-07-13 23:30:28 +00:00
//Returns a handle to a directory
native open_dir(dir[], firstfile[], length);
native next_file(dirh, buffer[], length);
native close_dir(dirh);
2004-04-03 20:05:27 +00:00
//These are type specific file getting and writing commands:
//c=char, s=short, l=long, i=int, f=float
native fgetc(file); //read char (size 1)
native fgets(file); //read short (size 2)
native fgetl(file); //read long (size 4)
native fgeti(file); //read int (size 4)
native Float:fgetf(file); //read float (size 4)
native fputc(file,num); //write char (size 1)
native fputs(file,num); //write short (size 2)
native fputl(file,num); //write long (size 4)
native fputi(file,num); //write int (size 4)
native fputf(file,Float:num); //write float (size 4)
stock bool:file_copy(const SOURCE[], const TARGET[], error[], const ERRORLEN, const bool:REPLACE_TARGET = false) {
if (!file_exists(SOURCE)) {
format(error, ERRORLEN, "File copy error: Source ^"%s^" doesn't exist!", SOURCE)
return false
}
if (!REPLACE_TARGET && file_exists(TARGET)) {
format(error, ERRORLEN, "File copy error: Target ^"%s^" exists!", TARGET)
return false
}
new source = fopen(SOURCE, "rb")
if (!source) {
format(error, ERRORLEN, "File copy error: Opening source ^"%s^" failed!", SOURCE)
return false
}
new target = fopen(TARGET, "wb")
if (!target) {
format(error, ERRORLEN, "File copy error: Opening target ^"%s^" failed!", TARGET)
fclose(source)
return false
}
for (new buffer, eof = feof(source); !eof; !eof && fputc(target, buffer)) {
buffer = fgetc(source)
eof = feof(source)
}
fclose(source)
fclose(target)
return true
}