ctr_Repair/trunk/CardSaveData/common/savefile/savefile.h

182 lines
4.5 KiB
C++

#ifndef SAVEFILE_H_
#define SAVEFILE_H_
#include <wchar.h>
#include <string.h>
#include <nn/types.h>
#include "../my_defs.h"
#include <nn/Result.h>
#include <nn/fs.h>
//******************************** defines **********
//パス名長 :SDK制限はアーカイブ名抜いて253文字
//アーカイブ名およびSD格納ディレクトリの余裕分をみとく
#define MAX_PATH_LENGTH 512
//パス階層上限
//ファイル検索時に使用、パス長上限(253)から128で十分
#define MAX_LEVEL 128
//ディレクトリリスト上限
#define MAX_DCLIST 256
typedef enum{
cpb_sdmc = 1,
cpb_ex
}Cpb_bits;
//******************************** functions **********
//パス名チェック .. for sdmc
//FAT違反の半角スペースあるかチェック
//文字、予約名、パス長はAPIエラー(INVALID_ARGUMENT)で判定
bool CheckPath(wchar_t *s);
//パス名チェック .. 拡張セーブ
bool CheckPathEx(wchar_t *s);
//デリミタ("/")位置を返す
//top:検索開始位置
int GetPosDelmLast(wchar_t *s,int top);
//パス結合
void ChainPath(wchar_t *p1,wchar_t *p2);
//ディレクトリ一致チェック
//ret= 0:なし、他:最初に一致した階層
//ex) s = "/d1/d2/d3",s2 ="d2" or "/d2" ret=2
int CmpDirW(wchar_t *s,wchar_t *s2);
//ファイル名の一致確認
//s:パス ex)"/dir/name"
//s2:ファイル ex) "name"
bool CmpNameW(wchar_t *s,wchar_t *s2);
bool CmpPathW(wchar_t *s,wchar_t *s2);
//******************************** Types **********
//セーブに関する情報
#define INFO_VERSION 0
struct tArcInfo{
s64 total;//ファイルサイズ総計
u32 DirEntry,FileEntry;//フォーマット
u32 DirCount,FileCount;//Dir,ファイル数
char Pcode[20];//product code SDKのサイズを下回らないこと
bool Dup;//2重化
u8 Ver;
//u8 yobi;//パディング
};
//ディレクトリ
struct tDcList{
u16 num;
wchar_t name[MAX_DCLIST][MAX_PATH_LENGTH];
};
//ワーニング抑制
//1300:inherits implisit virtual
//1301:insert padding
#pragma diag_suppress 1300,1301
//******************************** Class ***************
class SaveFileBase
{
public:
nn::Result LastNnResult;
u8 CheckPathBit; //パス名違反チェックbit
protected:
wchar_t root_w[MAX_PATH_LENGTH];//基底パス(デバイス名含)
wchar_t path_w[MAX_PATH_LENGTH];//パス名
wchar_t pathw_w[MAX_PATH_LENGTH];//ワーク
wchar_t pathw_w2[MAX_PATH_LENGTH];
s32 RootLength;
wchar_t *pPathTop;
char devName[16];
bool IsMounted;
public:
//基底パス設定
void SetRootPath(wchar_t *path)
{// 最後に"/"はつけない 例) "data:"
wcscpy(root_w,path);
RootLength = wcslen(path);
pPathTop = (wchar_t*)((u32)&path_w+RootLength*2);
}
void GetRootPath(wchar_t *path)
{
wcscpy(path,root_w);
}
virtual bool MountCore() =0;
myResult Mount(){
if ( IsMounted )return RESULT_ALREADY_MOUNT;
if (MountCore()){
IsMounted = true;
return RESULT_OK;
}
return RESULT_FAIL_MOUNT;
};
void Unmount(){
if(IsMounted){
IsMounted = false;
nn::fs::Unmount(devName);
}
};
SaveFileBase(){IsMounted = false;root_w[0]=0;RootLength=0;};
~SaveFileBase(){Unmount();};
};
//ライト
class SaveFileWrite :public virtual SaveFileBase
{
private:
nn::fs::FileWriter writer;
public:
SaveFileWrite(){RootLength=0;};
~SaveFileWrite(){};
bool DeleteDir(const wchar_t *dir);
bool MakeDir(wchar_t *path,bool *mkdir);
bool DeleteFile(const wchar_t *path);
bool OpenW(const wchar_t *path);
bool OpenC(wchar_t *path,s64 size,bool *mkdir);
bool OpenAdd(wchar_t *path);
void CloseW();
s32 Write(char *buffer,size_t size);
bool CreateDir(const wchar_t *dir);
};
//リード
class SaveFileRead :public virtual SaveFileBase
{
public:
s64 FileSize;
private:
virtual void GetFormatInfoCore(tArcInfo *ifo) = 0;
int dc_readed[MAX_LEVEL];//リード済みエントリ数
wchar_t pathu_w[MAX_LEVEL][MAX_PATH_LENGTH];//パス名履歴
nn::fs::FileReader reader;
int s_lv;
tArcInfo m_info;
bool s_serch;
public:
bool IsExist();
bool Open(const wchar_t *path);
void Close();
bool SetPos(s64 pos);
myResult GetPath(wchar_t *path);
void ResetPath();
bool GetInfo(tArcInfo *pinfo,tDcList *pDcList,size_t clsz = 0);
s32 Read(char *buffer,size_t size);
SaveFileRead(){
s_lv=0;
m_info.DirCount = 0;
m_info.FileCount = 0;
m_info.DirEntry = 0;
m_info.FileEntry = 0;
};
~SaveFileRead(){};
};
//警告を戻す
//#pragma diag_warning 1300,1301
#endif