Quelltext /~heha/hs/cdcat1plugin.zip/src/cunicode.cpp

#include "fsplugin.h"
#include "cunicode.h"
#include <shlwapi.h>

extern tProgressProc ProgressProc;
extern tLogProc LogProc;
extern tRequestProc RequestProc;
extern tProgressProcW ProgressProcW;
extern tLogProcW LogProcW;
extern tRequestProcW RequestProcW;
extern int PluginNumber;


bool usys() {
 static char usysychecked;	// 1 = NT, 2 = Win32s, Win9x, WinMe
 if (!usysychecked) usysychecked = (int)GetVersion()<0?2:1;
 return (usysychecked==1);	// true for NT (Unicode support)
}

char*unicopy(UINT cp,char*outname,int buflen,const WCHAR*inname) {
 if (!inname) return 0;
 WideCharToMultiByte(cp,0,inname,-1,outname,buflen,0,0);
 outname[buflen-1]=0;
 return outname;
}

WCHAR*unicopy(UINT cp,WCHAR*outname,int buflen,const char*inname) {
 if (!inname) return 0;
 MultiByteToWideChar(cp,0,inname,-1,outname,buflen);
 outname[buflen-1]=0;
 return outname;
}

// return true if name wasn't cut
BOOL MakeExtraLongNameW(WCHAR*outbuf,int buflen,const WCHAR*inbuf) {
 int len=lstrlenW(inbuf);
 if (len>259) {
  if (inbuf[0]=='\\' && inbuf[1]=='\\') {   // UNC-Path! Use \\?\UNC\server\share\subdir\name.ext
   wnsprintfW(outbuf,buflen,L"\\\\?\\UNC%s",inbuf+1);
  }else{
   wnsprintfW(outbuf,buflen,L"\\\\?\\%s",inbuf);
  }
 }else lstrcpynW(outbuf,inbuf,buflen);
 return len+5<=buflen;
}

/***********************************************************************************************/

void unicopy(WIN32_FIND_DATA&A,const WIN32_FIND_DATAW&W) {
 memcpy(&A,&W,(BYTE*)A.cFileName-(BYTE*)&A);
 WideCharToMultiByte(CP_ACP,0,W.cAlternateFileName,-1,A.cAlternateFileName,sizeof A.cAlternateFileName,0,0);
 WideCharToMultiByte(CP_ACP,0,W.cFileName,-1,A.cFileName,sizeof A.cFileName,0,0);
}

void unicopy(WIN32_FIND_DATAW&W,const WIN32_FIND_DATA&A) {
 memcpy(&W,&A,(BYTE*)W.cFileName-(BYTE*)&W);
 MultiByteToWideChar(CP_ACP,0,A.cAlternateFileName,-1,W.cAlternateFileName,elemof(W.cAlternateFileName));
 MultiByteToWideChar(CP_ACP,0,A.cFileName,-1,W.cFileName,elemof(W.cFileName));
}

/***********************************************************************************************/

int ProgressProcT(const WCHAR*SourceName,const WCHAR*TargetName,int PercentDone) {
 if (ProgressProcW) return ProgressProcW(PluginNumber,SourceName,TargetName,PercentDone);
 if (ProgressProc) {
  char buf1[MAX_PATH],buf2[MAX_PATH];
  return ProgressProc(PluginNumber,
	unicopy(buf1,elemof(buf1),SourceName),
	unicopy(buf2,elemof(buf2),TargetName),
	PercentDone);
 }
 return 0;
}

void LogProcT(int MsgType,WCHAR* LogString) {
 if (LogProcW) LogProcW(PluginNumber,MsgType,LogString);
 if (LogProc) {
  char buf[1024];
  LogProc(PluginNumber,MsgType,unicopy(buf,elemof(buf),LogString));
 }
}


BOOL RequestProcT(int RequestType,const WCHAR*CustomTitle,
	const WCHAR*CustomText,WCHAR* ReturnedText,int buflen) {
 if (RequestProcW) return RequestProcW(PluginNumber,RequestType,CustomTitle,CustomText,ReturnedText,buflen);
 if (RequestProc) {
  char buf1[MAX_PATH],buf2[MAX_PATH],buf3[MAX_PATH],
  *pReturn=unicopy(buf3,sizeof buf3,ReturnedText);
  BOOL retval=RequestProc(PluginNumber,RequestType,
	unicopy(buf1,sizeof buf1,CustomTitle),
	unicopy(buf2,sizeof buf2,CustomText),
	pReturn,buflen);
  if (retval && pReturn) unicopy(ReturnedText,buflen,pReturn);
  return retval;
 }
 return false;
}
#if 0
BOOL CopyFileT(const WCHAR*Existing,const WCHAR*New,BOOL bFailIfExists) {
 if (usys()) {
  WCHAR wbuf1[LONGPATH_MAX+longnameprefixmax],wbuf2[elemof(wbuf1)];
  if (MakeExtraLongNameW(wbuf1,elemof(wbuf1),Existing)
  &&  MakeExtraLongNameW(wbuf2,elemof(wbuf2),New)) return CopyFileW(wbuf1,wbuf2,bFailIfExists);
  return false;
 }
 char buf1[MAX_PATH],buf2[MAX_PATH];
 return CopyFile(
	unicopy(buf1,sizeof buf1,Existing),
	unicopy(buf2,sizeof buf2,New),
	bFailIfExists);
}

BOOL CreateDirectoryT(const WCHAR*Path,SECURITY_ATTRIBUTES*sa) {
 if (usys()) {
  WCHAR wbuf[LONGPATH_MAX+longnameprefixmax];
  if (MakeExtraLongNameW(wbuf,elemof(wbuf),Path)) return CreateDirectoryW(wbuf,sa);
  return false;
 }
 char buf[MAX_PATH];
 return CreateDirectory(unicopy(buf,elemof(buf),Path),sa);
}

BOOL RemoveDirectoryT(const WCHAR*Path) {
 if (usys()) {
  WCHAR wbuf[LONGPATH_MAX+longnameprefixmax];
  if (MakeExtraLongNameW(wbuf,elemof(wbuf),Path)) return RemoveDirectoryW(wbuf);
  return false;
 }
 char buf[MAX_PATH];
 return RemoveDirectory(unicopy(buf,elemof(buf),Path));
}

BOOL DeleteFileT(const WCHAR*Name) {
 if (usys()) {
  WCHAR wbuf[LONGPATH_MAX+longnameprefixmax];
  if (MakeExtraLongNameW(wbuf,elemof(wbuf),Name)) return DeleteFileW(wbuf);
  return false;
 }
 char buf[MAX_PATH];
 return DeleteFile(unicopy(buf,sizeof buf,Name));
}

BOOL MoveFileT(const WCHAR*Existing,const WCHAR*New) {
 if (usys()) {
  WCHAR wbuf1[LONGPATH_MAX+longnameprefixmax],wbuf2[elemof(wbuf1)];
  if (MakeExtraLongNameW(wbuf1,elemof(wbuf1),Existing)
  &&  MakeExtraLongNameW(wbuf2,elemof(wbuf2),New)) return MoveFileW(wbuf1,wbuf2);
  return false;
 }
 char buf1[MAX_PATH],buf2[MAX_PATH];
 return MoveFile(unicopy(buf1,elemof(buf1),Existing),unicopy(buf2,elemof(buf2),New));
}

BOOL SetFileAttributesT(const WCHAR*Name,DWORD dwFileAttributes) {
 if (usys()) {
  WCHAR wbuf[LONGPATH_MAX+longnameprefixmax];
  if (MakeExtraLongNameW(wbuf,elemof(wbuf),Name)) return SetFileAttributesW(wbuf,dwFileAttributes);
  return false;
 }
 char buf[MAX_PATH];
 return SetFileAttributes(unicopy(buf,elemof(buf),Name),dwFileAttributes);
}

HANDLE CreateFileT(const WCHAR*Name,DWORD dwDesiredAccess,DWORD dwShareMode,
  SECURITY_ATTRIBUTES*sa,DWORD dwCreationDisposition,
  DWORD dwFlagsAndAttributes,HANDLE hTemplateFile) {
 if (usys()) {
  WCHAR wbuf[LONGPATH_MAX+longnameprefixmax];
  if (MakeExtraLongNameW(wbuf,elemof(wbuf),Name))
  return CreateFileW(wbuf,dwDesiredAccess,dwShareMode,
				sa,dwCreationDisposition,
				dwFlagsAndAttributes,hTemplateFile);
  return INVALID_HANDLE_VALUE;
 }
 char buf[MAX_PATH];
 return CreateFile(unicopy(buf,elemof(buf),Name),
	dwDesiredAccess,dwShareMode,sa,dwCreationDisposition,
	dwFlagsAndAttributes,hTemplateFile);
}

UINT ExtractIconExT(const WCHAR*fname,int nIconIndex,HICON *phiconLarge,HICON *phiconSmall,UINT nIcons) {
 if (usys()) {  // Unfortunately this function cannot handle names longer than 259 characters
  return ExtractIconExW(fname,nIconIndex,phiconLarge,phiconSmall,nIcons);
 }else{
  char buf[MAX_PATH];
  return ExtractIconEx(unicopy(buf,sizeof buf,fname),nIconIndex,phiconLarge,phiconSmall,nIcons);
 }
}
#endif
HANDLE FindFirstFileT(const WCHAR*search,WIN32_FIND_DATAW&W) {
 if (usys()) {
  WCHAR wbuf[LONGPATH_MAX+longnameprefixmax];
  if (MakeExtraLongNameW(wbuf,elemof(wbuf),search))
	return FindFirstFileW(wbuf,&W);
  return INVALID_HANDLE_VALUE;
 }
 char buf[MAX_PATH];
 WIN32_FIND_DATA A;
 HANDLE retval=FindFirstFile(unicopy(buf,sizeof buf,search),&A);
 if (retval!=INVALID_HANDLE_VALUE) unicopy(W,A);
 return retval;
}

BOOL FindNextFileT(HANDLE h,WIN32_FIND_DATAW&W) {
 if (usys()) return FindNextFileW(h,&W);
 else{
  WIN32_FIND_DATA A;
  ZeroMemory(&A,sizeof A);
  BOOL retval=FindNextFile(h,&A);
  if (retval) unicopy(W,A);
  return retval;
 }
}
Vorgefundene Kodierung: UTF-80