1
1
Fork 0
mirror of https://github.com/QB64-Phoenix-Edition/QB64pe.git synced 2024-07-12 14:35:12 +00:00

Simplify and remove magic numbers

This commit is contained in:
Samuel Gomes 2023-12-11 04:06:38 +05:30
parent 20e5789ffd
commit 1b3a5a72f4

View file

@ -56,9 +56,11 @@ qbs *func__cwd() {
static bool __DirectoryExists(const char *path) { static bool __DirectoryExists(const char *path) {
#ifdef QB64_WINDOWS #ifdef QB64_WINDOWS
auto x = GetFileAttributesA(path); auto x = GetFileAttributesA(path);
return x != INVALID_FILE_ATTRIBUTES && (x & FILE_ATTRIBUTE_DIRECTORY); return x != INVALID_FILE_ATTRIBUTES && (x & FILE_ATTRIBUTE_DIRECTORY);
#else #else
struct stat info; struct stat info;
return (stat(path, &info) == 0 && S_ISDIR(info.st_mode)); return (stat(path, &info) == 0 && S_ISDIR(info.st_mode));
#endif #endif
} }
@ -321,9 +323,11 @@ int32 func__fileexists(qbs *path) {
#ifdef QB64_WINDOWS #ifdef QB64_WINDOWS
auto x = GetFileAttributesA(filepath_fix_directory(strz)); auto x = GetFileAttributesA(filepath_fix_directory(strz));
return (x == INVALID_FILE_ATTRIBUTES || (x & FILE_ATTRIBUTE_DIRECTORY)) ? QB_FALSE : QB_TRUE; return (x == INVALID_FILE_ATTRIBUTES || (x & FILE_ATTRIBUTE_DIRECTORY)) ? QB_FALSE : QB_TRUE;
#else #else
struct stat sb; struct stat sb;
return (stat(filepath_fix_directory(strz), &sb) == 0 && S_ISREG(sb.st_mode)) ? QB_TRUE : QB_FALSE; return (stat(filepath_fix_directory(strz), &sb) == 0 && S_ISREG(sb.st_mode)) ? QB_TRUE : QB_FALSE;
#endif #endif
} }
@ -335,25 +339,27 @@ qbs *g_startDir = nullptr;
/// @return A qbs containing the directory path /// @return A qbs containing the directory path
qbs *func__startdir() { qbs *func__startdir() {
qbs *temp = qbs_new(0, 1); qbs *temp = qbs_new(0, 1);
qbs_set(temp, g_startDir); qbs_set(temp, g_startDir);
return temp; return temp;
} }
/// @brief Changes the current directory
/// @param str The directory path to change to
void sub_chdir(qbs *str) { void sub_chdir(qbs *str) {
if (new_error) if (new_error)
return; return;
static qbs *strz = NULL;
static qbs *strz = nullptr;
if (!strz) if (!strz)
strz = qbs_new(0, 0); strz = qbs_new(0, 0);
qbs_set(strz, qbs_add(str, qbs_new_txt_len("\0", 1)));
if (chdir(filepath_fix_directory(strz)) == -1) {
// assume errno==ENOENT
error(76); // path not found
}
static int32 tmp_long; qbs_set(strz, qbs_add(str, qbs_new_txt_len("\0", 1)));
static int32 got_ports = 0;
if (chdir(filepath_fix_directory(strz)) == -1)
error(76); // assume errno == ENOENT; path not found
} }
void sub_files(qbs *str, int32 passed) { void sub_files(qbs *str, int32 passed) {
@ -559,70 +565,92 @@ void sub_kill(qbs *str) {
#endif #endif
} }
/// @brief Creates a new directory
/// @param str The directory path name to create
void sub_mkdir(qbs *str) { void sub_mkdir(qbs *str) {
if (new_error) if (new_error)
return; return;
static qbs *strz = NULL;
static qbs *strz = nullptr;
if (!strz) if (!strz)
strz = qbs_new(0, 0); strz = qbs_new(0, 0);
qbs_set(strz, qbs_add(str, qbs_new_txt_len("\0", 1))); qbs_set(strz, qbs_add(str, qbs_new_txt_len("\0", 1)));
#ifdef QB64_UNIX
if (mkdir(filepath_fix_directory(strz), 0770) == -1) { #ifdef QB64_WINDOWS
#else
if (mkdir(filepath_fix_directory(strz)) == -1) { if (mkdir(filepath_fix_directory(strz)) == -1) {
#else
if (mkdir(filepath_fix_directory(strz), S_IRWXU | S_IRWXG) == -1) {
#endif #endif
if (errno == EEXIST) { if (errno == EEXIST) {
error(75); error(75);
return; return;
} // path / file access error } // path / file access error
// assume errno==ENOENT
error(76); // path not found error(76); // assume errno == ENOENT; path not found
} }
} }
/// @brief Renames a file or directory
/// @param oldname The old file / directory name
/// @param newname The new file / directory name
void sub_name(qbs *oldname, qbs *newname) { void sub_name(qbs *oldname, qbs *newname) {
if (new_error) if (new_error)
return; return;
static qbs *strz = NULL;
static qbs *strz = nullptr, *strz2 = nullptr;
if (!strz) if (!strz)
strz = qbs_new(0, 0); strz = qbs_new(0, 0);
static qbs *strz2 = NULL;
if (!strz2) if (!strz2)
strz2 = qbs_new(0, 0); strz2 = qbs_new(0, 0);
static int32 i;
qbs_set(strz, qbs_add(oldname, qbs_new_txt_len("\0", 1))); qbs_set(strz, qbs_add(oldname, qbs_new_txt_len("\0", 1)));
qbs_set(strz2, qbs_add(newname, qbs_new_txt_len("\0", 1))); qbs_set(strz2, qbs_add(newname, qbs_new_txt_len("\0", 1)));
if (rename(filepath_fix_directory(strz), filepath_fix_directory(strz2))) { if (rename(filepath_fix_directory(strz), filepath_fix_directory(strz2))) {
i = errno; auto i = errno;
if (i == ENOENT) { if (i == ENOENT) {
error(53); error(53);
return; return;
} // file not found } // file not found
if (i == EINVAL) { if (i == EINVAL) {
error(64); error(64);
return; return;
} // bad file name } // bad file name
if (i == EACCES) { if (i == EACCES) {
error(75); error(75);
return; return;
} // path / file access error } // path / file access error
error(5); // Illegal function call (assumed)
error(5); // illegal function call (assumed)
} }
} }
/// @brief Deletes an empty directory
/// @param str The path name of the directory to delete
void sub_rmdir(qbs *str) { void sub_rmdir(qbs *str) {
if (new_error) if (new_error)
return; return;
static qbs *strz = NULL;
static qbs *strz = nullptr;
if (!strz) if (!strz)
strz = qbs_new(0, 0); strz = qbs_new(0, 0);
qbs_set(strz, qbs_add(str, qbs_new_txt_len("\0", 1))); qbs_set(strz, qbs_add(str, qbs_new_txt_len("\0", 1)));
if (rmdir(filepath_fix_directory(strz)) == -1) { if (rmdir(filepath_fix_directory(strz)) == -1) {
if (errno == ENOTEMPTY) { if (errno == ENOTEMPTY) {
error(75); error(75);
return; return;
} // path/file access error } // path/file access error; not an empty directory
// assume errno==ENOENT
error(76); // path not found error(76); // assume errno == ENOENT; path not found
} }
} }