lib/file: Ensure a file is actually open before attempting to read
							parent
							
								
									72fc923839
								
							
						
					
					
						commit
						f3e03710f6
					
				|  | @ -64,20 +64,24 @@ ErrorOr<uint8_t> File::readByte(){ | ||||||
|     uint8_t* nextPointer = new uint8_t; |     uint8_t* nextPointer = new uint8_t; | ||||||
|     uint8_t nextByte; |     uint8_t nextByte; | ||||||
|     bool failure = false; |     bool failure = false; | ||||||
|  | 
 | ||||||
|  |     if (!this->isOpen) { | ||||||
|  |         return ErrorOr<uint8_t>(true, ErrorCodes::FILE_NOT_OPEN); | ||||||
|  |     } | ||||||
|     if (!this->size.isError && this->cursorPosition >= this->size.value) { |     if (!this->size.isError && this->cursorPosition >= this->size.value) { | ||||||
|         return ErrorOr<uint8_t>(true, ErrorCodes::OVERRUN); |         return ErrorOr<uint8_t>(true, ErrorCodes::OVERRUN); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|     try { |     try { | ||||||
|         this->fileStream.seekg(this->cursorPosition); |         this->fileStream.seekg(this->cursorPosition); | ||||||
| 
 |  | ||||||
|         this->fileStream.read(reinterpret_cast<char*>(nextPointer), 1); |         this->fileStream.read(reinterpret_cast<char*>(nextPointer), 1); | ||||||
|         nextByte = *nextPointer; |         nextByte = *nextPointer; | ||||||
| 
 |  | ||||||
|         this->cursorPosition++; |         this->cursorPosition++; | ||||||
| 
 | 
 | ||||||
|     } catch (std::exception& e) { |     } catch (std::exception& e) { | ||||||
|         failure = true; |         failure = true; | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|     delete nextPointer; |     delete nextPointer; | ||||||
|     return failure? ErrorOr<uint8_t>(true, ErrorCodes::UNKNOWN) : ErrorOr<uint8_t>(nextByte); |     return failure? ErrorOr<uint8_t>(true, ErrorCodes::UNKNOWN) : ErrorOr<uint8_t>(nextByte); | ||||||
| } | } | ||||||
|  | @ -86,19 +90,24 @@ ErrorOr<std::vector<uint8_t>> File::read(uint64_t bytes){ | ||||||
|     uint8_t* buffer = new uint8_t[bytes]; |     uint8_t* buffer = new uint8_t[bytes]; | ||||||
|     std::vector<uint8_t> data; |     std::vector<uint8_t> data; | ||||||
|     bool failure = false; |     bool failure = false; | ||||||
|  | 
 | ||||||
|  |     if (!this->isOpen) { | ||||||
|  |         return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::FILE_NOT_OPEN); | ||||||
|  |     } | ||||||
|     if (!this->size.isError && this->cursorPosition >= this->size.value+bytes) { |     if (!this->size.isError && this->cursorPosition >= this->size.value+bytes) { | ||||||
|         return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::OVERRUN); |         return ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::OVERRUN); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|     try { |     try { | ||||||
|         this->fileStream.seekg(this->cursorPosition); |         this->fileStream.seekg(this->cursorPosition); | ||||||
| 
 |  | ||||||
|         //FIXME: check that enough data is available to read
 |  | ||||||
|         this->fileStream.read(reinterpret_cast<char*>(buffer), bytes); |         this->fileStream.read(reinterpret_cast<char*>(buffer), bytes); | ||||||
|         data = std::vector<uint8_t>(buffer, buffer+bytes); |         data = std::vector<uint8_t>(buffer, buffer+bytes); | ||||||
|         this->cursorPosition += bytes; |         this->cursorPosition += bytes; | ||||||
|  | 
 | ||||||
|     } catch (std::exception& e) { |     } catch (std::exception& e) { | ||||||
|         failure = true; |         failure = true; | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|     delete[] buffer; |     delete[] buffer; | ||||||
|     return failure? ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::UNKNOWN) : ErrorOr<std::vector<uint8_t>>(data); |     return failure? ErrorOr<std::vector<uint8_t>>(true, ErrorCodes::UNKNOWN) : ErrorOr<std::vector<uint8_t>>(data); | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	 BodgeMaster
						BodgeMaster