# tasks_file.h

This library provides assynchronous file operations. It uses STask to execute the synchronous operations from interface/file.h in a seperate thread.

## File information

File | common/lib/tasks_file.h
Classes | TaskFileWrite TaskFileRead

# Classes

## TaskFileWrite

  Used to write to a file. A file may be created, an existing file replaced or to an existing file appended.

    class TaskFileWrite : public STask {
        void SStart();

        char * workingPath;
        char * fileName;
        bool append;
        class IFileWrite * file;
        byte * buffer;
        unsigned length;
        bool last;

    public:
        TaskFileWrite(class STaskContext * context, const char * workingPath, const char * fileName, bool append);
        ~TaskFileWrite();

        void write(byte * buffer, unsigned length, bool last);
        void writeBuffer(byte * buffer, unsigned length, bool last);
    };

### TaskFileWrite

  Contructor. TaskFileWrite uses STask as base class, which means for the operation, first the class is allocated with all required parameters and then SStart() is used to start it. The TaskProgress, TaskComplete and TaskFailed functions are used to indicate progress and completion of the Task.

  #### Parameters

  class STaskContext * context | An STaskContext must be allocated for the operation. It manages a pool of threads for the execution.
  const char * workingPath | The path in which the file is written. If parts of the path do not exist the folders are created.
  const char * fileName | The name of the file to be written. If the file does not exist, it is created.
  bool append | If true the data is appended to the file, if it already exists, otherwise the file is overwritten

### write

  Write data to the file. May be called after the TaskProgress callback was called, either after SStart() or the last write() call. The data passed by this function is copied into a new buffer.

  #### Parameters

  byte * buffer | Buffer containing the data
  unsigned length | The length of the data in bytes
  bool last | Indicates that this is the last block of data. The operation completes when this parameter is set to true

### writeBuffer

  Same functionality as write, except the data is not copied and the buffer is freed after the data is written.

## TaskFileRead

  Used to read from a file.

    class TaskFileRead : public STask {
        void SStart();

        char * workingPath;
        char * fileName;
        class IFileRead * file;
        byte * buffer;
        unsigned length;
        bool last;
    public:
        TaskFileRead(class STaskContext * context, const char * workingPath, const char * fileName, unsigned length, bool last);
        ~TaskFileRead();

        void read(unsigned length, bool last);
        unsigned getLength() { return length; };
        void getData(byte * buffer, unsigned length) { memcpy(buffer, this->buffer, length); };
    };

### TaskFileRead

  Contructor. TaskFileRead uses STask as base class, which means for the operation, first the class is allocated with all required parameters and then SStart() is used to start it. The TaskProgress, TaskComplete and TaskFailed functions are used to indicate progress and completion of the Task.

  #### Parameters

  class STaskContext * context | An STaskContext must be allocated for the operation. It manages a pool of threads for the execution.
  const char * workingPath | The path from which the file is read
  const char * fileName | The name of the file to be read.
  unsigned length | The length to be read initially
  bool last | If true no othe data shall be read after the initial block.

### read

  Initiates reading of the next block of data. May be called after the TaskProgress callback was called, either after SStart() or the last read() call.

#### Parameters

  unsigned length | The length to be read initially
  bool last | If true no othe data shall be read after the initial block.

### getLength

  Return the actual read length after the TaskProgress call

### getData

  Copies the read data.

#### Parameters

  byte * buffer | The buffer the data is copied to.
  unsigned length | The length to be copied.