| 
									
										
										
										
											2018-10-03 22:02:46 +08:00
										 |  |  | // Copyright (c) 2016-2018 Easy2D - Nomango
 | 
					
						
							|  |  |  | // 
 | 
					
						
							|  |  |  | // Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
					
						
							|  |  |  | // of this software and associated documentation files (the "Software"), to deal
 | 
					
						
							|  |  |  | // in the Software without restriction, including without limitation the rights
 | 
					
						
							|  |  |  | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
					
						
							|  |  |  | // copies of the Software, and to permit persons to whom the Software is
 | 
					
						
							|  |  |  | // furnished to do so, subject to the following conditions:
 | 
					
						
							|  |  |  | // 
 | 
					
						
							|  |  |  | // The above copyright notice and this permission notice shall be included in
 | 
					
						
							|  |  |  | // all copies or substantial portions of the Software.
 | 
					
						
							|  |  |  | // 
 | 
					
						
							|  |  |  | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
					
						
							|  |  |  | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
					
						
							|  |  |  | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
					
						
							|  |  |  | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
					
						
							|  |  |  | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
					
						
							|  |  |  | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					
						
							|  |  |  | // THE SOFTWARE.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 21:39:26 +08:00
										 |  |  | #include "File.h"
 | 
					
						
							| 
									
										
										
										
											2019-03-14 17:55:06 +08:00
										 |  |  | #include "../platform/modules.h"
 | 
					
						
							| 
									
										
										
										
											2018-10-28 23:24:01 +08:00
										 |  |  | #include <cwctype>
 | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | namespace easy2d | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 	File::File() | 
					
						
							|  |  |  | 		: file_path_() | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-09-07 23:47:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 21:24:45 +08:00
										 |  |  | 	File::File(String const& file_name) | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 		: file_path_(file_name) | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 	File::~File() | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 21:24:45 +08:00
										 |  |  | 	bool File::Open(String const& file_name) | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 		if (file_name.empty()) | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 		file_path_ = file_name; | 
					
						
							|  |  |  | 		return Exists(); | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 	bool File::Exists() const | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-03-13 19:40:41 +08:00
										 |  |  | 		return !!modules::Shlwapi::Get().PathFileExistsW(file_path_.c_str()); | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 21:24:45 +08:00
										 |  |  | 	String const& File::GetPath() const | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 		return file_path_; | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 21:24:45 +08:00
										 |  |  | 	String File::GetExtension() const | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-01-21 21:24:45 +08:00
										 |  |  | 		String file_ext; | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 		size_t pos = file_path_.find_last_of(L'.'); | 
					
						
							| 
									
										
										
										
											2019-01-21 21:24:45 +08:00
										 |  |  | 		if (pos != String::npos) | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			file_ext = file_path_.substr(pos); | 
					
						
							|  |  |  | 			std::transform(file_ext.begin(), file_ext.end(), file_ext.begin(), std::towlower); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return file_ext; | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-10-16 14:57:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 	bool File::Delete() | 
					
						
							| 
									
										
										
										
											2018-10-28 23:24:01 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 		if (::DeleteFile(file_path_.c_str())) | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2018-10-28 23:24:01 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 21:58:01 +08:00
										 |  |  | 	File File::Extract(Resource const& res, String const& dest_file_name) | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 		File file; | 
					
						
							|  |  |  | 		HANDLE file_handle = ::CreateFile( | 
					
						
							|  |  |  | 			dest_file_name.c_str(), | 
					
						
							|  |  |  | 			GENERIC_WRITE, | 
					
						
							|  |  |  | 			NULL, | 
					
						
							|  |  |  | 			NULL, | 
					
						
							|  |  |  | 			CREATE_ALWAYS, | 
					
						
							|  |  |  | 			FILE_ATTRIBUTE_TEMPORARY, | 
					
						
							|  |  |  | 			NULL | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (file_handle == INVALID_HANDLE_VALUE) | 
					
						
							|  |  |  | 			return file; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 21:24:45 +08:00
										 |  |  | 		LPVOID buffer; | 
					
						
							|  |  |  | 		DWORD buffer_size; | 
					
						
							|  |  |  | 		if (res.Load(buffer, buffer_size)) | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			DWORD written_bytes = 0; | 
					
						
							| 
									
										
										
										
											2019-01-21 21:24:45 +08:00
										 |  |  | 			::WriteFile(file_handle, buffer, buffer_size, &written_bytes, NULL); | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | 			::CloseHandle(file_handle); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			file.Open(dest_file_name); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			::CloseHandle(file_handle); | 
					
						
							|  |  |  | 			::DeleteFile(dest_file_name.c_str()); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return file; | 
					
						
							| 
									
										
										
										
											2018-07-08 02:41:44 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-11-08 00:21:59 +08:00
										 |  |  | } |