Added stock file_copy to file.inc.

Also fixed fopen to return 0 on files that can't be opened.
Fixed native fopen to take const filename.
This commit is contained in:
Johnny Bergström 2005-05-29 16:36:35 +00:00
parent c1552aacd6
commit 51ff0a2c49
2 changed files with 42 additions and 2 deletions

View File

@ -293,7 +293,13 @@ static cell AMX_NATIVE_CALL amx_fopen(AMX *amx, cell *params)
int len, j=-1; int len, j=-1;
char *file = build_pathname("%s", get_amxstring(amx, params[1], 1, len)); char *file = build_pathname("%s", get_amxstring(amx, params[1], 1, len));
char *flags = get_amxstring(amx, params[2], 0, len); char *flags = get_amxstring(amx, params[2], 0, len);
FILE *fp = fopen(file, flags); FILE *fp = fopen(file, flags);
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) if (FileList.at(i) == NULL)

View File

@ -42,7 +42,7 @@ native file_size(const file[], flag=0);
// Code ported from Sanji's File module // Code ported from Sanji's File module
//Open a file //Open a file
native fopen(filename[],mode[]); native fopen(const filename[],const mode[]);
//Close a file //Close a file
native fclose(file); native fclose(file);
//Read a file for ret_size length //Read a file for ret_size length
@ -79,3 +79,37 @@ native fputs(file,num); //write short (size 2)
native fputl(file,num); //write long (size 4) native fputl(file,num); //write long (size 4)
native fputi(file,num); //write int (size 4) native fputi(file,num); //write int (size 4)
native fputf(file,Float:num); //write float (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
}