|
Packit Service |
50c9f2 |
/******************************************************************************
|
|
Packit Service |
50c9f2 |
*
|
|
Packit Service |
50c9f2 |
*
|
|
Packit Service |
50c9f2 |
*
|
|
Packit Service |
50c9f2 |
* Copyright (C) 1997-2015 by Dimitri van Heesch.
|
|
Packit Service |
50c9f2 |
*
|
|
Packit Service |
50c9f2 |
* Permission to use, copy, modify, and distribute this software and its
|
|
Packit Service |
50c9f2 |
* documentation under the terms of the GNU General Public License is hereby
|
|
Packit Service |
50c9f2 |
* granted. No representations are made about the suitability of this software
|
|
Packit Service |
50c9f2 |
* for any purpose. It is provided "as is" without express or implied warranty.
|
|
Packit Service |
50c9f2 |
* See the GNU General Public License for more details.
|
|
Packit Service |
50c9f2 |
*
|
|
Packit Service |
50c9f2 |
* Documents produced by Doxygen are derivative works derived from the
|
|
Packit Service |
50c9f2 |
* input used in their production; they are not affected by this license.
|
|
Packit Service |
50c9f2 |
*
|
|
Packit Service |
50c9f2 |
*/
|
|
Packit Service |
50c9f2 |
|
|
Packit Service |
50c9f2 |
#ifndef STORE_H
|
|
Packit Service |
50c9f2 |
#define STORE_H
|
|
Packit Service |
50c9f2 |
|
|
Packit Service |
50c9f2 |
#include <qglobal.h>
|
|
Packit Service |
50c9f2 |
#include <stdio.h>
|
|
Packit Service |
50c9f2 |
|
|
Packit Service |
50c9f2 |
#include "portable.h"
|
|
Packit Service |
50c9f2 |
|
|
Packit Service |
50c9f2 |
/*! @brief Abstract interface for file based memory storage operations */
|
|
Packit Service |
50c9f2 |
class StorageIntf
|
|
Packit Service |
50c9f2 |
{
|
|
Packit Service |
50c9f2 |
public:
|
|
Packit Service |
50c9f2 |
/*! Required by gcc */
|
|
Packit Service |
50c9f2 |
virtual ~StorageIntf() {}
|
|
Packit Service |
50c9f2 |
/*! Read \a size bytes from the store into \a buf. */
|
|
Packit Service |
50c9f2 |
virtual int read(char *buf,uint size) = 0;
|
|
Packit Service |
50c9f2 |
/*! Write \a size bytes from \a buf into the store. */
|
|
Packit Service |
50c9f2 |
virtual int write(const char *buf,uint size) = 0;
|
|
Packit Service |
50c9f2 |
};
|
|
Packit Service |
50c9f2 |
|
|
Packit Service |
50c9f2 |
/*! @brief The Store is a file based memory manager.
|
|
Packit Service |
50c9f2 |
*
|
|
Packit Service |
50c9f2 |
* You can open the store using open(). Then obtain a handle via alloc()
|
|
Packit Service |
50c9f2 |
* followed by a sequence of write() commands to store information,
|
|
Packit Service |
50c9f2 |
* and finalize it using end().
|
|
Packit Service |
50c9f2 |
*
|
|
Packit Service |
50c9f2 |
* Later on you locate the information
|
|
Packit Service |
50c9f2 |
* with seek() using the handle obtained with alloc(), and then use a
|
|
Packit Service |
50c9f2 |
* sequence of read() calls to read the information back.
|
|
Packit Service |
50c9f2 |
*
|
|
Packit Service |
50c9f2 |
* If no longer needed the storage space can be freed using release().
|
|
Packit Service |
50c9f2 |
*
|
|
Packit Service |
50c9f2 |
* The store will dynamically grow the file on disk if needed.
|
|
Packit Service |
50c9f2 |
*/
|
|
Packit Service |
50c9f2 |
class Store : public StorageIntf
|
|
Packit Service |
50c9f2 |
{
|
|
Packit Service |
50c9f2 |
public:
|
|
Packit Service |
50c9f2 |
/*! Creates a store. */
|
|
Packit Service |
50c9f2 |
Store();
|
|
Packit Service |
50c9f2 |
|
|
Packit Service |
50c9f2 |
/*! Releases the store object. Will close the underlying file if opened. */
|
|
Packit Service |
50c9f2 |
~Store();
|
|
Packit Service |
50c9f2 |
|
|
Packit Service |
50c9f2 |
/*! Opens the file underlying the store using \a name as the file name.
|
|
Packit Service |
50c9f2 |
* Returns 0 upon success, or -1 otherwise.
|
|
Packit Service |
50c9f2 |
*/
|
|
Packit Service |
50c9f2 |
int open(const char *name);
|
|
Packit Service |
50c9f2 |
|
|
Packit Service |
50c9f2 |
/*! Allocates a handle to write to and read from. */
|
|
Packit Service |
50c9f2 |
portable_off_t alloc();
|
|
Packit Service |
50c9f2 |
|
|
Packit Service |
50c9f2 |
/*! Writes \a size bytes in array \a buf to the store.
|
|
Packit Service |
50c9f2 |
* First alloc() has to be called.
|
|
Packit Service |
50c9f2 |
* \note The information can only be read after end() has been called.
|
|
Packit Service |
50c9f2 |
*/
|
|
Packit Service |
50c9f2 |
int write(const char *buf,uint size);
|
|
Packit Service |
50c9f2 |
|
|
Packit Service |
50c9f2 |
/*! Ends the sequence of writes.
|
|
Packit Service |
50c9f2 |
* \note After this call, first alloc() has to be called
|
|
Packit Service |
50c9f2 |
* before new writes can be done.
|
|
Packit Service |
50c9f2 |
*/
|
|
Packit Service |
50c9f2 |
void end();
|
|
Packit Service |
50c9f2 |
|
|
Packit Service |
50c9f2 |
/*! Releases the memory corresponding to the handle returned with alloc() */
|
|
Packit Service |
50c9f2 |
void release(portable_off_t handle);
|
|
Packit Service |
50c9f2 |
|
|
Packit Service |
50c9f2 |
/*! Closes the store */
|
|
Packit Service |
50c9f2 |
void close();
|
|
Packit Service |
50c9f2 |
|
|
Packit Service |
50c9f2 |
/*! Goes to the start of information corresponding to handle \a pos */
|
|
Packit Service |
50c9f2 |
void seek(portable_off_t handle);
|
|
Packit Service |
50c9f2 |
|
|
Packit Service |
50c9f2 |
/*! Reads \a size bytes from the store into the array pointed to be \a buf.
|
|
Packit Service |
50c9f2 |
* \note Before reading seek() has to be called to set the right start of the store.
|
|
Packit Service |
50c9f2 |
*/
|
|
Packit Service |
50c9f2 |
int read(char *buf,uint size);
|
|
Packit Service |
50c9f2 |
|
|
Packit Service |
50c9f2 |
void printStats();
|
|
Packit Service |
50c9f2 |
|
|
Packit Service |
50c9f2 |
portable_off_t pos() const { return m_cur; }
|
|
Packit Service |
50c9f2 |
|
|
Packit Service |
50c9f2 |
void dumpBlock(portable_off_t start,portable_off_t end);
|
|
Packit Service |
50c9f2 |
|
|
Packit Service |
50c9f2 |
private:
|
|
Packit Service |
50c9f2 |
enum State
|
|
Packit Service |
50c9f2 |
{
|
|
Packit Service |
50c9f2 |
Init,
|
|
Packit Service |
50c9f2 |
Reading,
|
|
Packit Service |
50c9f2 |
Writing
|
|
Packit Service |
50c9f2 |
};
|
|
Packit Service |
50c9f2 |
struct Node
|
|
Packit Service |
50c9f2 |
{
|
|
Packit Service |
50c9f2 |
portable_off_t pos;
|
|
Packit Service |
50c9f2 |
struct Node *next;
|
|
Packit Service |
50c9f2 |
};
|
|
Packit Service |
50c9f2 |
void printFreeList();
|
|
Packit Service |
50c9f2 |
FILE *m_file;
|
|
Packit Service |
50c9f2 |
portable_off_t m_front;
|
|
Packit Service |
50c9f2 |
portable_off_t m_cur;
|
|
Packit Service |
50c9f2 |
Node *m_head;
|
|
Packit Service |
50c9f2 |
State m_state;
|
|
Packit Service |
50c9f2 |
int m_reads;
|
|
Packit Service |
50c9f2 |
int m_writes;
|
|
Packit Service |
50c9f2 |
};
|
|
Packit Service |
50c9f2 |
|
|
Packit Service |
50c9f2 |
#endif
|