Skip to content

Commit

Permalink
整理windows文件相关api,使用wchar_t
Browse files Browse the repository at this point in the history
  • Loading branch information
sisong committed Jan 5, 2019
1 parent c2c1aa8 commit fa63b5a
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 151 deletions.
19 changes: 2 additions & 17 deletions dirDiffPatch/dir_diff/dir_diff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,6 @@ bool IDirFilter::pathNameIs(const std::string& pathName,const char* testPathName
return (nameSize==testSize) || (pathName[nameSize-testSize-1]==kPatch_dirSeparator);
}

void IDirDiffListener::localePathToUtf8(const std::string& path,std::string& out_utf8){
size_t cStrByteSize=localePath_to_utf8(path.c_str(),0,0);
if (cStrByteSize<=0) throw std::runtime_error("path encoding error!");
out_utf8.resize(cStrByteSize);
cStrByteSize=localePath_to_utf8(path.c_str(),&out_utf8[0],&out_utf8[0]+cStrByteSize);
if (cStrByteSize<=0) throw std::runtime_error("path encoding error!");
out_utf8.resize(cStrByteSize-1); //for C string '\0'
}

struct CFileStreamInput:public TFileStreamInput{
inline CFileStreamInput(){ TFileStreamInput_init(this); }
inline void open(const std::string& fileName){
Expand All @@ -103,7 +94,7 @@ struct CFileStreamInput:public TFileStreamInput{
check(TFileStreamInput_close(this),"close file error!"); }
};

struct CRefStream:public RefStream{
struct CRefStream:public TRefStream{
inline CRefStream(){ TRefStream_init(this); }
inline void open(const hpatch_TStreamInput** refList,size_t refCount){
check(TRefStream_open(this,refList,refCount),"TRefStream_open() refList error!");
Expand Down Expand Up @@ -246,7 +237,6 @@ static void pushSamePairList(std::vector<TByte>& out_data,const std::vector<size
static size_t pushNameList(std::vector<TByte>& out_data,const std::string& rootPath,
const std::vector<std::string>& nameList,IDirDiffListener* listener){
const size_t rootLen=rootPath.size();
std::string temp;
std::string utf8;
size_t outSize=0;
for (size_t i=0;i<nameList.size();++i){
Expand All @@ -256,12 +246,7 @@ static size_t pushNameList(std::vector<TByte>& out_data,const std::string& rootP
assert(0==memcmp(name.data(),rootPath.data(),rootLen));
const char* subName=name.c_str()+rootLen;
const char* subNameEnd=subName+(nameSize-rootLen);
if (isAsciiString(subName,subNameEnd)){
utf8.assign(subName,subNameEnd);
}else{
temp.assign(subName,subNameEnd);
listener->localePathToUtf8(temp,utf8);
}
utf8.assign(subName,subNameEnd);
formatDirTagForSave(utf8);
size_t writeLen=utf8.size()+1; // '\0'
out_data.insert(out_data.end(),utf8.c_str(),utf8.c_str()+writeLen);
Expand Down
1 change: 0 additions & 1 deletion dirDiffPatch/dir_diff/dir_diff.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ struct IDirDiffListener:public IDirFilter{
virtual void runHDiffEnd(hpatch_StreamPos_t diffDataSize){}
virtual void externData(std::vector<unsigned char>& out_externData){}
virtual void externDataPosInDiffStream(hpatch_StreamPos_t externDataPos){}
virtual void localePathToUtf8(const std::string& path,std::string& out_utf8);
};

void dir_diff(IDirDiffListener* listener,const std::string& oldPatch,const std::string& newPatch,
Expand Down
73 changes: 45 additions & 28 deletions dirDiffPatch/dir_patch/dir_patch.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
static const char* kVersionType="DirDiff19&";
static const TByte kPatchMode =0;

const size_t kPathMaxSize=1024*2;

#define TUInt hpatch_StreamPos_t

#define check(value) { \
Expand All @@ -48,17 +46,17 @@ const size_t kPathMaxSize=1024*2;
#define unpackUIntTo(puint,sclip) \
check(_TStreamCacheClip_unpackUIntWithTag(sclip,puint,0))


#define unpackToSize(psize,sclip) { \
TUInt v; check(_TStreamCacheClip_unpackUIntWithTag(sclip,&v,0)); \
if (sizeof(TUInt)!=sizeof(size_t)) check(v==(size_t)v); \
*(psize)=(size_t)v; }


char* pushDirPath(char* out_path,char* out_pathEnd,const char* rootDir){
char* result=0; //false
size_t rootDirLen=strlen(rootDir);
hpatch_BOOL isNeedDirSeparator;
if (isAsciiString(rootDir,rootDir+rootDirLen)){
isNeedDirSeparator=(rootDirLen>0)&&(rootDir[rootDirLen-1]!=kPatch_dirSeparator);
}else{ //某些字符集(比如某繁体中文编码)里的单字符编码的其中某个字节可能和分隔符的编码相同,而utf8不存在该问题;
size_t CStrByteSize=localePath_to_utf8(rootDir,out_path,out_pathEnd);
check(CStrByteSize>0);
isNeedDirSeparator=((CStrByteSize-1)>0)&&(out_path[(CStrByteSize-1)-1]!=kPatch_dirSeparator);
}
hpatch_BOOL isNeedDirSeparator=(rootDirLen>0)&&(rootDir[rootDirLen-1]!=kPatch_dirSeparator);
check(rootDirLen+1+1<=(out_pathEnd-out_path));
memcpy(out_path,rootDir,rootDirLen);
out_path+=rootDirLen;
Expand All @@ -72,12 +70,8 @@ char* pushDirPath(char* out_path,char* out_pathEnd,const char* rootDir){
hpatch_BOOL getPath(char* out_path,char* out_pathEnd,const char* utf8fileName){
hpatch_BOOL result=hpatch_TRUE;
size_t utf8fileNameSize=strlen(utf8fileName);
if (isAsciiString(utf8fileName,utf8fileName+utf8fileNameSize)){
check(utf8fileNameSize+1<=(out_pathEnd-out_path));
memcpy(out_path,utf8fileName,utf8fileNameSize+1);
}else{
check(utf8_to_localePath(utf8fileName,out_path,out_pathEnd)>0);
}
check(utf8fileNameSize+1<=(out_pathEnd-out_path));
memcpy(out_path,utf8fileName,utf8fileNameSize+1);
clear:
return result;
}
Expand Down Expand Up @@ -150,13 +144,13 @@ static hpatch_BOOL _read_dirdiff_head(TDirDiffInfo* out_info,_TDirDiffHead* out_
unpackUIntTo(&savedValue,headClip); check(savedValue<=1);
out_info->newPathIsDir=(hpatch_BOOL)savedValue;

unpackUIntTo(&out_head->oldPathCount,headClip);
unpackUIntTo(&out_head->newPathCount,headClip);
unpackUIntTo(&out_head->oldPathSumSize,headClip);
unpackUIntTo(&out_head->newPathSumSize,headClip);
unpackUIntTo(&out_head->oldRefFileCount,headClip);
unpackUIntTo(&out_head->newRefFileCount,headClip);
unpackUIntTo(&out_head->sameFilePairCount,headClip);
unpackToSize(&out_head->oldPathCount,headClip);
unpackToSize(&out_head->newPathCount,headClip);
unpackToSize(&out_head->oldPathSumSize,headClip);
unpackToSize(&out_head->newPathSumSize,headClip);
unpackToSize(&out_head->oldRefFileCount,headClip);
unpackToSize(&out_head->newRefFileCount,headClip);
unpackToSize(&out_head->sameFilePairCount,headClip);
unpackUIntTo(&out_head->headDataSize,headClip);
unpackUIntTo(&out_head->headDataCompressedSize,headClip);
out_info->dirDataIsCompressed=(out_head->headDataCompressedSize>0);
Expand Down Expand Up @@ -361,9 +355,6 @@ hpatch_BOOL TDirPatcher_loadOldRefToMem(TDirPatcher* self,const char* oldRootDir
return TFileStreamInput_close(&file) & result;
}




static hpatch_BOOL _closeOldRefStream(TDirPatcher* self,const hpatch_TStreamInput** slist,size_t count){
hpatch_BOOL result=hpatch_TRUE;
if (self->_pOldRefMem){
Expand All @@ -378,7 +369,7 @@ static hpatch_BOOL _closeOldRefStream(TDirPatcher* self,const hpatch_TStreamInpu
return result;
}

hpatch_BOOL TDirPatcher_loadOldRefAsStream(TDirPatcher* self,const char* oldRootDir,
hpatch_BOOL TDirPatcher_openOldRefAsStream(TDirPatcher* self,const char* oldRootDir,
const hpatch_TStreamInput** out_oldRefStream){
hpatch_BOOL result=hpatch_TRUE;
size_t refCount=self->dirDiffHead.oldRefFileCount;
Expand Down Expand Up @@ -421,6 +412,31 @@ hpatch_BOOL TDirPatcher_closeOldRefStream(TDirPatcher* self){
self->_oldRefStream._rangeCount);
}


static hpatch_BOOL _outNewDir(struct INewStreamListener* listener,size_t newPathIndex){
TDirPatcher* self=(TDirPatcher*)listener;

//todo:
return hpatch_FALSE;
}
hpatch_BOOL TDirPatcher_openNewDirAsStream(TDirPatcher* self,const char* newRootDir,INewDirListener* listener,
const hpatch_TStreamOutput** out_newDirStream){
hpatch_BOOL result;
INewStreamListener nsListener;
assert(self->_newDirStream.stream==0);
memset(&nsListener,0,sizeof(nsListener));
nsListener.listenerImport=self;
//listener.
result=TNewStream_open(&self->_newDirStream,&nsListener, self->dirDiffInfo.hdiffInfo.newDataSize,
self->dirDiffHead.newPathCount,self->newRefList,self->dirDiffHead.newRefFileCount,
self->dataSamePairList,self->dirDiffHead.sameFilePairCount);
return result;
}

hpatch_BOOL TDirPatcher_closeNewDirStream(TDirPatcher* self){
return TNewStream_close(&self->_newDirStream);
}

hpatch_BOOL TDirPatcher_patch(const TDirPatcher* self,const hpatch_TStreamOutput* out_newData,
const hpatch_TStreamInput* oldData,
TByte* temp_cache,TByte* temp_cache_end){
Expand All @@ -432,7 +448,8 @@ hpatch_BOOL TDirPatcher_patch(const TDirPatcher* self,const hpatch_TStreamOutput
}

hpatch_BOOL TDirPatcher_close(TDirPatcher* self){
hpatch_BOOL result=TDirPatcher_closeOldRefStream(self);
hpatch_BOOL result=TDirPatcher_closeNewDirStream(self);
result=TDirPatcher_closeOldRefStream(self) & result;
if (self->_pmem){
free(self->_pmem);
self->_pmem=0;
Expand Down
36 changes: 22 additions & 14 deletions dirDiffPatch/dir_patch/dir_patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@
extern "C" {
#endif

hpatch_inline static hpatch_BOOL isAsciiString(const char* str,const char* strEnd){
for (;str<strEnd;++str) {
if ((*(const unsigned char*)str)<0x80)
hpatch_inline static hpatch_BOOL isAsciiString(const char* cstr){
while (hpatch_TRUE) {
unsigned char c_sub1=(unsigned char)(*cstr++)-1; // 0->255,1->0,255->254,128->127,...
if (c_sub1<127) // c in [1..127]
continue;
else
else if (c_sub1<255) // c in [128..255]
return hpatch_FALSE;
else // c==0
return hpatch_TRUE;
}
return hpatch_TRUE;
}

typedef struct TDirDiffInfo{
Expand All @@ -66,19 +68,25 @@ hpatch_inline static hpatch_BOOL getIsDirDiffFile(const char* diffFileName,hpatc
}

typedef struct _TDirDiffHead {
hpatch_StreamPos_t oldPathCount;
hpatch_StreamPos_t newPathCount;
hpatch_StreamPos_t oldPathSumSize;
hpatch_StreamPos_t newPathSumSize;
hpatch_StreamPos_t oldRefFileCount;
hpatch_StreamPos_t newRefFileCount;
hpatch_StreamPos_t sameFilePairCount;
size_t oldPathCount;
size_t newPathCount;
size_t oldPathSumSize;
size_t newPathSumSize;
size_t oldRefFileCount;
size_t newRefFileCount;
size_t sameFilePairCount;
hpatch_StreamPos_t headDataOffset;
hpatch_StreamPos_t headDataSize;
hpatch_StreamPos_t headDataCompressedSize;
hpatch_StreamPos_t hdiffDataOffset;
hpatch_StreamPos_t hdiffDataSize;
} _TDirDiffHead;

typedef struct INewDirListener{
void* listenerImport;
hpatch_BOOL (*outNewDir) (struct INewDirListener* listener,const char* newDir);
hpatch_BOOL (*copySameFile)(struct INewDirListener* listener,const char* oldFileName,const char* newFileName);
} INewDirListener;

typedef struct TDirPatcher{
TDirDiffInfo dirDiffInfo;
Expand Down Expand Up @@ -108,7 +116,7 @@ hpatch_BOOL TDirPatcher_loadOldRefToMem(TDirPatcher* self,const char* oldRoo
unsigned char* out_buf,unsigned char* out_buf_end);
hpatch_BOOL TDirPatcher_openOldRefAsStream(TDirPatcher* self,const char* oldRootDir,
const hpatch_TStreamInput** out_oldRefStream);
hpatch_BOOL TDirPatcher_openNewDirAsStream(TDirPatcher* self,const char* newRootDir,
hpatch_BOOL TDirPatcher_openNewDirAsStream(TDirPatcher* self,const char* newRootDir,INewDirListener* listener,
const hpatch_TStreamOutput** out_newDirStream);

hpatch_BOOL TDirPatcher_patch(const TDirPatcher* self,const hpatch_TStreamOutput* out_newData,
Expand All @@ -117,7 +125,7 @@ hpatch_BOOL TDirPatcher_patch(const TDirPatcher* self,const hpatch_TStreamOu

hpatch_BOOL TDirPatcher_closeOldRefStream(TDirPatcher* self);//for TDirPatcher_openOldRefAsStream
hpatch_BOOL TDirPatcher_closeNewDirStream(TDirPatcher* self);//for TDirPatcher_openNewDirAsStream

hpatch_BOOL TDirPatcher_close(TDirPatcher* self);


Expand Down
9 changes: 5 additions & 4 deletions dirDiffPatch/dir_patch/new_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@


typedef struct INewStreamListener{
hpatch_BOOL (*outNewDir) (struct INewStreamListener* Listener,size_t newPathIndex);
hpatch_BOOL (*copySameFile)(struct INewStreamListener* Listener,size_t oldPathIndex,size_t newPathIndex);
hpatch_BOOL (*openNewFile) (struct INewStreamListener* Listener,size_t newPathIndex,
void* listenerImport;
hpatch_BOOL (*outNewDir) (struct INewStreamListener* listener,size_t newPathIndex);
hpatch_BOOL (*copySameFile)(struct INewStreamListener* listener,size_t oldPathIndex,size_t newPathIndex);
hpatch_BOOL (*openNewFile) (struct INewStreamListener* listener,size_t newPathIndex,
const hpatch_TStreamOutput** out_newFileStream);
hpatch_BOOL (*closeNewFile)(struct INewStreamListener* Listener,const hpatch_TStreamOutput* newFileStream);
hpatch_BOOL (*closeNewFile)(struct INewStreamListener* listener,const hpatch_TStreamOutput* newFileStream);
} INewStreamListener;

//对外模拟成一个输出流;
Expand Down
50 changes: 0 additions & 50 deletions dirDiffPatch/file_for_dirPatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#ifndef DirDiffPatch_file_for_dirPatch_h
#define DirDiffPatch_file_for_dirPatch_h
#include <stdio.h>
#include <locale.h> // setlocale
#include "../libHDiffPatch/HPatch/patch_types.h"

#ifdef _WIN32
Expand Down Expand Up @@ -70,53 +69,4 @@ hpatch_BOOL getPathType(const char* path,TPathType* out_type){
}
}


hpatch_inline static
void SetDefaultLocale(){ //for some locale Path character encoding
setlocale(LC_ALL, "" );
}

hpatch_inline static
hpatch_BOOL isSamePath(const char* xPath,const char* yPath){
if (0==strcmp(xPath,yPath)){
return hpatch_TRUE;
}else{
// WARING!!! better return getCanonicalPath(xPath)==getCanonicalPath(yPath);
return hpatch_FALSE;
}
}

hpatch_inline static //if error return 0 else return outCStringByteSize
size_t utf8String_to_utf8(const char* path,char* out_utf8,char* out_utf8BufEnd){
//copy only
size_t size=strlen(path)+1; // with '\0'
if (out_utf8!=0){
if ((out_utf8BufEnd-out_utf8)<size) return 0;//error
memmove(out_utf8,path,size);
}
return size;
}

hpatch_inline static //if error return 0 else return outCStringByteSize
size_t utf8_to_localePath(const char* utf8Path,char* out_Path,char* out_PathBufEnd){
#if ( defined(__APPLE__) || defined(__ANDROID__) || defined(__linux__) )
return utf8String_to_utf8(utf8Path,out_Path,out_PathBufEnd);
#else
#warning Path unknown character encoding, probably can not cross-platform
return utf8String_to_utf8(utf8Path,out_Path,out_PathBufEnd);
#endif
}


hpatch_inline static //if error return 0 else return outCStringBufSize
size_t localePath_to_utf8(const char* path,char* out_utf8,char* out_utf8BufEnd){
#if (defined(__APPLE__))
return utf8String_to_utf8(path,out_utf8,out_utf8BufEnd);
#else
#warning Path unknown character encoding, probably can not cross-platform
return utf8String_to_utf8(path,out_utf8,out_utf8BufEnd);
#endif
}


#endif
Loading

0 comments on commit fa63b5a

Please sign in to comment.