mirror of
https://github.com/QB64-Phoenix-Edition/QB64pe.git
synced 2024-09-20 04:24:48 +00:00
More error checking
This commit is contained in:
parent
dedb94223f
commit
fdfd6a2af3
1 changed files with 13 additions and 7 deletions
|
@ -33887,19 +33887,23 @@ qbs *func__readfile(qbs *filespec)
|
||||||
|
|
||||||
namez = qbs_add(filespec, func_chr(0)); // add terminator
|
namez = qbs_add(filespec, func_chr(0)); // add terminator
|
||||||
filepath_fix_directory(namez); // fix separators
|
filepath_fix_directory(namez); // fix separators
|
||||||
file = fopen((const char*)namez -> chr, "rb");
|
file = fopen((const char*)namez->chr, "rb");
|
||||||
|
|
||||||
if (file) {
|
if (file) {
|
||||||
fseek(file, 0, SEEK_END); // end pos
|
fseek(file, 0, SEEK_END); // end pos
|
||||||
len = ftell(file); // = file length
|
len = ftell(file); // = file length
|
||||||
rewind(file); // rewind to start
|
rewind(file); // rewind to start
|
||||||
cont = qbs_new(len, 1); // get new string for file contents
|
cont = qbs_new(len, 1); // get new string for file contents
|
||||||
fread(cont -> chr, 1, len, file);
|
fread(cont->chr, 1, len, file);
|
||||||
|
if (ferror(file)) {
|
||||||
|
error(QB_ERROR_PATH_FILE_ACCESS_ERROR); // something went wrong
|
||||||
|
cont = qbs_new_txt(""); // return empty on error
|
||||||
|
}
|
||||||
fclose(file);
|
fclose(file);
|
||||||
return cont;
|
return cont;
|
||||||
} else {
|
} else {
|
||||||
error(QB_ERROR_FILE_NOT_FOUND); // error, we throw most common
|
error(QB_ERROR_FILE_NOT_FOUND); // most common when trying to read
|
||||||
return qbs_new_txt(""); // return empty on error
|
return qbs_new_txt(""); // return empty on error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33910,13 +33914,15 @@ void sub__writefile(qbs *filespec, qbs *contents)
|
||||||
|
|
||||||
namez = qbs_add(filespec, func_chr(0)); // add terminator
|
namez = qbs_add(filespec, func_chr(0)); // add terminator
|
||||||
filepath_fix_directory(namez); // fix separators
|
filepath_fix_directory(namez); // fix separators
|
||||||
file = fopen((const char*)namez -> chr, "wb");
|
file = fopen((const char*)namez->chr, "wb");
|
||||||
|
|
||||||
if (file) {
|
if (file) {
|
||||||
fwrite(contents -> chr, 1, contents -> len, file);
|
fwrite(contents->chr, 1, contents->len, file);
|
||||||
|
if (ferror(file))
|
||||||
|
error(QB_ERROR_PATH_FILE_ACCESS_ERROR); // something went wrong
|
||||||
fclose(file);
|
fclose(file);
|
||||||
} else {
|
} else {
|
||||||
error(QB_ERROR_PATH_FILE_ACCESS_ERROR); // error, we throw best fit
|
error(QB_ERROR_PATH_NOT_FOUND); // most common when making a new file
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue