FileToMem

Files CCore/inc/FileToMem.h CCore/src/FileToMem.cpp

FileToMem

The class FileToMem loads the file to the memory. It is derived from the class ToMemBase.


class FileToMem : public ToMemBase
 {
  public:

   explicit FileToMem(StrLen file_name,ulen max_len=MaxULen);

   ~FileToMem();
   
   // std move

   FileToMem(FileToMem &&obj) noexcept = default ;

   FileToMem & operator = (FileToMem &&obj) noexcept = default ;

   // swap/move objects
   
   void objSwap(FileToMem &obj);
   
   explicit FileToMem(ToMoveCtor<FileToMem> obj);
 };

The first argument of the constructor is the file_name. The second max_len is a length limit.

ToMemBase is std movable. The original object is nullified during the move.

This class is swappable and movable.

RawFileToRead

The RawFileToRead class is used to read data from a file. It is a simple wrapped over the Sys::File. This class is used in the FileToMem constructor. See also PrintFile about file writing and printing and about Sys::File.


class RawFileToRead : NoCopy
 {
   Sys::File file;
 
  public:
  
   explicit RawFileToRead(StrLen file_name,FileOpenFlags oflags=Open_Read);
    
   ~RawFileToRead();
    
   FilePosType getLen();
    
   ulen read(uint8 *buf,ulen len); 
   
   void read_all(uint8 *buf,ulen len); 
 };

Constructor opens the file with the given name and open flags. An exception is thrown in case of error.

Destructor closes the file. Errors are reported.

Methods throw exceptions in case of errors.

getLen() returns the file length. The open flag Open_Pos is required.

read() reads the file from the current position. The number of bytes red is returned.

read_all() reads the file from the current position. The whole buffer must be red for success.

PartFileToMem

PartFileToMem can be used to load parts of a file into a memory buffer. This class loads file parts sequentially.


class PartFileToMem : NoCopy
 {
   RawFileToRead file;
   SimpleArray<uint8> buf;
   FilePosType off;
   FilePosType file_len;

  public:

   static const ulen DefaultBufLen = 64_KByte ;

   explicit PartFileToMem(StrLen file_name,ulen buf_len=DefaultBufLen);

   ~PartFileToMem();

   FilePosType getFileLen() const { return file_len; }

   FilePosType getCurPos() const { return off; }

   FilePosType getRestLen() const { return file_len-off; }

   bool more() const { return off<file_len; }

   PtrLen<const uint8> read();
 };

Constructor has two arguments: the file name and the internal buffer length.

getFileLen() returns the file length.

getCurPos() returns the current position.

getRestLen() returns the length of the file part after the current position.

more() returns true, iff there is a room after the current position.

read() reads the next file part from the current position up to the end of the buffer or up to the end of the file. The part is stored in the internal buffer and the constant range of bytes is returned.