Skip to content

Commit

Permalink
FileHandle refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jackoalan committed Jan 24, 2017
1 parent b5f5104 commit 408ac73
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 196 deletions.
112 changes: 68 additions & 44 deletions include/kabufuda/Card.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
namespace kabufuda
{

class IFileHandle
class FileHandle
{
protected:
uint32_t idx;
IFileHandle() = default;
IFileHandle(uint32_t idx) : idx(idx) {}
friend class Card;
uint32_t idx = 0;
int32_t offset = 0;
FileHandle(uint32_t idx) : idx(idx) {}
public:
FileHandle() = default;
uint32_t getFileNo() const { return idx; }
virtual ~IFileHandle();
operator bool() const { return getFileNo() != 0; }
};

enum class ECardResult
Expand Down Expand Up @@ -136,7 +137,7 @@ class Card
void _swapEndian();
void _updateDirAndBat();
void _updateChecksum();
File* _fileFromHandle(const std::unique_ptr<IFileHandle>& fh) const;
File* _fileFromHandle(const FileHandle& fh) const;
void _deleteFile(File& f);

public:
Expand All @@ -147,8 +148,8 @@ class Card
*/
Card(const Card& other) = delete;
Card& operator=(const Card& other) = delete;
Card(Card&& other) = default;
Card& operator=(Card&& other) = default;
Card(Card&& other);
Card& operator=(Card&& other);

/**
* @brief Card
Expand All @@ -163,46 +164,53 @@ class Card
* @brief openFile
* @param filename
*/
ECardResult openFile(const char* filename, std::unique_ptr<IFileHandle>& handleOut);
ECardResult openFile(const char* filename, FileHandle& handleOut);

/**
* @brief openFile
* @param fileno
*/
ECardResult openFile(uint32_t fileno, std::unique_ptr<IFileHandle>& handleOut);
ECardResult openFile(uint32_t fileno, FileHandle& handleOut);

/**
* @brief createFile
* @param filename
* @return
*/
ECardResult createFile(const char* filename, size_t size, std::unique_ptr<IFileHandle>& handleOut);
ECardResult createFile(const char* filename, size_t size, FileHandle& handleOut);

/**
* @brief closeFile
* @param fh FileHandle to close
* @return
*/
ECardResult closeFile(FileHandle& fh);

/**
* @brief firstFile
* @return
*/
std::unique_ptr<IFileHandle> firstFile();
FileHandle firstFile();

/**
* @brief nextFile
* @param cur
* @return
*/
std::unique_ptr<IFileHandle> nextFile(const std::unique_ptr<IFileHandle>& cur);
FileHandle nextFile(const FileHandle& cur);

/**
* @brief getFilename
* @param fh
* @return
*/
const char* getFilename(const std::unique_ptr<IFileHandle>& fh);
const char* getFilename(const FileHandle& fh);

/**
* @brief deleteFile
* @param fh
*/
void deleteFile(const std::unique_ptr<IFileHandle>& fh);
void deleteFile(const FileHandle& fh);

/**
* @brief deleteFile
Expand All @@ -229,206 +237,222 @@ class Card
* @param buf
* @param size
*/
void write(const std::unique_ptr<IFileHandle>& fh, const void* buf, size_t size);
ECardResult write(FileHandle& fh, const void* buf, size_t size);

/**
* @brief read
* @param fh
* @param dst
* @param size
*/
void read(const std::unique_ptr<IFileHandle>& fh, void* dst, size_t size);
ECardResult read(FileHandle& fh, void* dst, size_t size);

/**
* @brief seek
* @param fh
* @param pos
* @param whence
*/
void seek(const std::unique_ptr<IFileHandle>& fh, int32_t pos, SeekOrigin whence);
void seek(FileHandle& fh, int32_t pos, SeekOrigin whence);

/**
* @brief Returns the current offset of the specified file
* @param fh The file to retrieve the offset from
* @return The offset or -1 if an invalid handle is passed
*/
int32_t tell(const std::unique_ptr<IFileHandle>& fh);
int32_t tell(const FileHandle& fh);

/**
* @brief setPublic
* @param fh
* @param pub
*/
void setPublic(const std::unique_ptr<IFileHandle>& fh, bool pub);
void setPublic(const FileHandle& fh, bool pub);

/**
* @brief isPublic
* @param fh
* @return
*/
bool isPublic(const std::unique_ptr<IFileHandle>& fh) const;
bool isPublic(const FileHandle& fh) const;

/**
* @brief setCanCopy
* @param fh
* @param copy
*/
void setCanCopy(const std::unique_ptr<IFileHandle>& fh, bool copy) const;
void setCanCopy(const FileHandle& fh, bool copy) const;

/**
* @brief canCopy
* @param fh
* @return
*/
bool canCopy(const std::unique_ptr<IFileHandle>& fh) const;
bool canCopy(const FileHandle& fh) const;

/**
* @brief setCanMove
* @param fh
* @param move
*/
void setCanMove(const std::unique_ptr<IFileHandle>& fh, bool move);
void setCanMove(const FileHandle& fh, bool move);

/**
* @brief canMove
* @param fh
* @return
*/
bool canMove(const std::unique_ptr<IFileHandle>& fh) const;
bool canMove(const FileHandle& fh) const;

/**
* @brief getStatus
* @param fh Handle of requested file
* @param statOut Structure to fill with file stat
* @return NOFILE or READY
*/
ECardResult getStatus(const std::unique_ptr<IFileHandle>& fh, CardStat& statOut) const;
ECardResult getStatus(const FileHandle& fh, CardStat& statOut) const;

/**
* @brief getStatus
* @param fileNo Number of requested file
* @param statOut Structure to fill with file stat
* @return NOFILE or READY
*/
ECardResult getStatus(uint32_t fileNo, CardStat& statOut) const;

/**
* @brief setStatus
* @param fh Handle of requested file
* @param statOut Structure to access for file stat
* @return NOFILE or READY
*/
ECardResult setStatus(const std::unique_ptr<IFileHandle>& fh, const CardStat& stat);
ECardResult setStatus(const FileHandle& fh, const CardStat& stat);

/**
* @brief setStatus
* @param fileNo Number of requested file
* @param statOut Structure to access for file stat
* @return NOFILE or READY
*/
ECardResult setStatus(uint32_t fileNo, const CardStat& stat);

/**
* @brief gameId
* @param fh
* @return
*/
const char* gameId(const std::unique_ptr<IFileHandle>& fh) const;
const char* gameId(const FileHandle& fh) const;

/**
* @brief maker
* @param fh
* @return
*/
const char* maker(const std::unique_ptr<IFileHandle>& fh) const;
const char* maker(const FileHandle& fh) const;

/**
* @brief setBannerFormat
* @param fh
* @param fmt
*/
void setBannerFormat(const std::unique_ptr<IFileHandle>& fh, EImageFormat fmt);
void setBannerFormat(const FileHandle& fh, EImageFormat fmt);

/**
* @brief bannerFormat
* @param fh
* @return
*/
EImageFormat bannerFormat(const std::unique_ptr<IFileHandle>& fh) const;
EImageFormat bannerFormat(const FileHandle& fh) const;

/**
* @brief setIconAnimationType
* @param fh
* @param type
*/
void setIconAnimationType(const std::unique_ptr<IFileHandle>& fh, EAnimationType type);
void setIconAnimationType(const FileHandle& fh, EAnimationType type);

/**
* @brief iconAnimationType
* @param fh
* @return
*/
EAnimationType iconAnimationType(const std::unique_ptr<IFileHandle>& fh) const;
EAnimationType iconAnimationType(const FileHandle& fh) const;

/**
* @brief setIconFormat
* @param fh
* @param idx
* @param fmt
*/
void setIconFormat(const std::unique_ptr<IFileHandle>& fh, uint32_t idx, EImageFormat fmt);
void setIconFormat(const FileHandle& fh, uint32_t idx, EImageFormat fmt);

/**
* @brief iconFormat
* @param fh
* @param idx
* @return
*/
EImageFormat iconFormat(const std::unique_ptr<IFileHandle>& fh, uint32_t idx) const;
EImageFormat iconFormat(const FileHandle& fh, uint32_t idx) const;

/**
* @brief setIconSpeed
* @param fh
* @param idx
* @param speed
*/
void setIconSpeed(const std::unique_ptr<IFileHandle>& fh, uint32_t idx, EAnimationSpeed speed);
void setIconSpeed(const FileHandle& fh, uint32_t idx, EAnimationSpeed speed);

/**
* @brief iconSpeed
* @param fh
* @param idx
* @return
*/
EAnimationSpeed iconSpeed(const std::unique_ptr<IFileHandle>& fh, uint32_t idx) const;
EAnimationSpeed iconSpeed(const FileHandle& fh, uint32_t idx) const;

/**
* @brief setImageAddress
* @param fh
* @param addr
*/
void setImageAddress(const std::unique_ptr<IFileHandle>& fh, uint32_t addr);
void setImageAddress(const FileHandle& fh, uint32_t addr);

/**
* @brief imageAddress
* @param fh
* @return
*/
int32_t imageAddress(const std::unique_ptr<IFileHandle>& fh) const;
int32_t imageAddress(const FileHandle& fh) const;

/**
* @brief setCommentAddress
* @param fh
* @param addr
*/
void setCommentAddress(const std::unique_ptr<IFileHandle>& fh, uint32_t addr);
void setCommentAddress(const FileHandle& fh, uint32_t addr);

/**
* @brief commentAddress
* @param fh
* @return
*/
int32_t commentAddress(const std::unique_ptr<IFileHandle>& fh) const;
int32_t commentAddress(const FileHandle& fh) const;

/**
* @brief Copies a file from the current Card instance to a specified Card instance
* @param fh The file to copy
* @param dest The destination Card instance
* @return True if successful, false otherwise
*/
bool copyFileTo(const std::unique_ptr<IFileHandle>& fh, Card& dest);
bool copyFileTo(FileHandle& fh, Card& dest);

/**
* @brief moveFileTo
* @param fh
* @param dest
* @return
*/
bool moveFileTo(const std::unique_ptr<IFileHandle>& fh, Card& dest);
bool moveFileTo(FileHandle& fh, Card& dest);

/**
* @brief Sets the current game, if not null any openFile requests will only return files that match this game
Expand Down
1 change: 0 additions & 1 deletion include/kabufuda/Util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@ static inline int Stat(const SystemChar* path, Sstat* statOut)
* @param checksumInv
*/
void calculateChecksumBE(const uint16_t* data, size_t len, uint16_t* checksum, uint16_t* checksumInv);
void calculateChecksumLE(const uint16_t* data, size_t len, uint16_t* checksum, uint16_t* checksumInv);
}

#endif // __KABU_UTIL_HPP__
11 changes: 8 additions & 3 deletions lib/kabufuda/BlockAllocationTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@ void BlockAllocationTable::swapEndian()

void BlockAllocationTable::updateChecksum()
{
calculateChecksumLE(reinterpret_cast<uint16_t*>(__raw + 4), 0xFFE, &m_checksum, &m_checksumInv);
swapEndian();
calculateChecksumBE(reinterpret_cast<uint16_t*>(__raw + 4), 0xFFE, &m_checksum, &m_checksumInv);
swapEndian();
}

bool BlockAllocationTable::valid() const
{
uint16_t ckSum, ckSumInv;
calculateChecksumLE(reinterpret_cast<const uint16_t*>(__raw + 4), 0xFFE, &ckSum, &ckSumInv);
return (ckSum == m_checksum && ckSumInv == m_checksumInv);
const_cast<BlockAllocationTable&>(*this).swapEndian();
calculateChecksumBE(reinterpret_cast<const uint16_t*>(__raw + 4), 0xFFE, &ckSum, &ckSumInv);
bool res = (ckSum == m_checksum && ckSumInv == m_checksumInv);
const_cast<BlockAllocationTable&>(*this).swapEndian();
return res;
}

BlockAllocationTable::BlockAllocationTable(uint32_t blockCount)
Expand Down

0 comments on commit 408ac73

Please sign in to comment.