Exemplo n.º 1
0
Error FileAccessUnix::_open(const String& p_path, int p_mode_flags) {

	if (f)
		fclose(f);
	f=NULL;

	path=fix_path(p_path);
	//printf("opening %ls, %i\n", path.c_str(), Memory::get_static_mem_usage());

	ERR_FAIL_COND_V(f,ERR_ALREADY_IN_USE);
	const char* mode_string;

	if (p_mode_flags==READ)
		mode_string="rb";
	else if (p_mode_flags==WRITE)
		mode_string="wb";
	else if (p_mode_flags==READ_WRITE)
		mode_string="rb+";
	else if (p_mode_flags==WRITE_READ)
		mode_string="wb+";
	else
		return ERR_INVALID_PARAMETER;

	/* pretty much every implementation that uses fopen as primary
	   backend (unix-compatible mostly) supports utf8 encoding */

	//printf("opening %s as %s\n", p_path.utf8().get_data(), path.utf8().get_data());
	struct stat st;
	if (stat(path.utf8().get_data(),&st) == 0) {

		if (!S_ISREG(st.st_mode))
			return ERR_FILE_CANT_OPEN;

	};

	if (is_backup_save_enabled() && p_mode_flags&WRITE && !(p_mode_flags&READ)) {
		save_path=path;
		path=path+".tmp";
		//print_line("saving instead to "+path);
	}

	f=fopen(path.utf8().get_data(),mode_string);

	if (f==NULL) {
		last_error=ERR_FILE_CANT_OPEN;
		return ERR_FILE_CANT_OPEN;
	} else {
		last_error=OK;
		flags=p_mode_flags;
		return OK;
	}

}
Exemplo n.º 2
0
Error FileAccessWindows::_open(const String& p_filename, int p_mode_flags) {

	String filename=fix_path(p_filename);
	if (f)
		close();


	const wchar_t* mode_string;

	if (p_mode_flags==READ)
		mode_string=L"rb";
	else if (p_mode_flags==WRITE)
		mode_string=L"wb";
	else if (p_mode_flags==READ_WRITE)
		mode_string=L"rb+";
	else if (p_mode_flags==WRITE_READ)
		mode_string=L"wb+";
	else
		return ERR_INVALID_PARAMETER;

	/* pretty much every implementation that uses fopen as primary
	   backend supports utf8 encoding */

	struct _stat st;
	if (_wstat(filename.c_str(), &st) == 0) {

		if (!S_ISREG(st.st_mode))
			return ERR_FILE_CANT_OPEN;

	};

	if (is_backup_save_enabled() && p_mode_flags&WRITE && !(p_mode_flags&READ)) {
		save_path=filename;
		filename=filename+".tmp";
		//print_line("saving instead to "+path);
	}

	f=_wfopen(filename.c_str(), mode_string);


	if (f==NULL) {
		last_error=ERR_FILE_CANT_OPEN;
		return ERR_FILE_CANT_OPEN;
	} else {
		last_error=OK;
		flags=p_mode_flags;
		return OK;
	}

}
Exemplo n.º 3
0
Error FileAccessWindows::_open(const String &p_path, int p_mode_flags) {

	path_src = p_path;
	path = fix_path(p_path);
	if (f)
		close();

	const wchar_t *mode_string;

	if (p_mode_flags == READ)
		mode_string = L"rb";
	else if (p_mode_flags == WRITE)
		mode_string = L"wb";
	else if (p_mode_flags == READ_WRITE)
		mode_string = L"rb+";
	else if (p_mode_flags == WRITE_READ)
		mode_string = L"wb+";
	else
		return ERR_INVALID_PARAMETER;

	/* pretty much every implementation that uses fopen as primary
	   backend supports utf8 encoding */

	struct _stat st;
	if (_wstat(path.c_str(), &st) == 0) {

		if (!S_ISREG(st.st_mode))
			return ERR_FILE_CANT_OPEN;
	};

#ifdef TOOLS_ENABLED
	// Windows is case insensitive, but all other platforms are sensitive to it
	// To ease cross-platform development, we issue a warning if users try to access
	// a file using the wrong case (which *works* on Windows, but won't on other
	// platforms).
	if (p_mode_flags == READ) {
		WIN32_FIND_DATAW d = { 0 };
		HANDLE f = FindFirstFileW(path.c_str(), &d);
		if (f) {
			String fname = d.cFileName;
			if (fname != String()) {

				String base_file = path.get_file();
				if (base_file != fname && base_file.findn(fname) == 0) {
					WARN_PRINTS("Case mismatch opening requested file '" + base_file + "', stored as '" + fname + "' in the filesystem. This file will not open when exported to other case-sensitive platforms.");
				}
			}
			FindClose(f);
		}
	}
#endif

	if (is_backup_save_enabled() && p_mode_flags & WRITE && !(p_mode_flags & READ)) {
		save_path = path;
		path = path + ".tmp";
	}

	_wfopen_s(&f, path.c_str(), mode_string);

	if (f == NULL) {
		last_error = ERR_FILE_CANT_OPEN;
		return ERR_FILE_CANT_OPEN;
	} else {
		last_error = OK;
		flags = p_mode_flags;
		return OK;
	}
}