#include "stdafx.h" #include "Common.h" char *ReadBuffer( const std::string &path ){ extern HANDLE hHeap; int i; DWORD dw; char *buffer,temporary[MAX_PATH]; ATL::CHandle hFile(CreateFile(path.c_str(),GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL)); if(hFile==INVALID_HANDLE_VALUE){ hFile.Detach(); //"\"%s\" ファイルの読み込みに失敗しました。" sprintf(temporary,STRING_ERROR_CANT_FILEOPEN,path.c_str()); MessageBox(hOwner,temporary,STRING_ERROR,MB_OK|MB_ICONEXCLAMATION); return 0; } i=GetFileSize(hFile,0); buffer=(char *)HeapAlloc(hHeap,0,i+1); ReadFile(hFile,buffer,i,&dw,0); buffer[dw]=0; return buffer; } char *ReadBuffer_NonErrMsg( const std::string &path ){ extern HANDLE hHeap; DWORD dw; ATL::CHandle hFile(CreateFile(path.c_str(),GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL)); if(hFile==INVALID_HANDLE_VALUE){ hFile.Detach(); return 0; } int i=GetFileSize(hFile,0); char *buffer=(char *)HeapAlloc(hHeap,0,i+1); ReadFile(hFile,buffer,i,&dw,0); buffer[dw]=0; return buffer; } _int8 WriteBuffer(const std::string &path,const char *buffer,int length, bool isEnableError) { DWORD dw; ATL::CHandle hFile(CreateFile(path.c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL)); if(hFile==INVALID_HANDLE_VALUE){ hFile.Detach(); char temporary[MAX_PATH]; if( isEnableError ){ //"\"%s\" ファイルへの書き込みに失敗しました。" sprintf(temporary,STRING_ERROR_CANT_FILESAVE,path); MessageBox(hOwner,temporary,STRING_ERROR,MB_OK|MB_ICONEXCLAMATION); } return 0; } if(length) WriteFile(hFile,buffer,length,&dw,NULL); return 1; } _int8 WriteBuffer_NonErrMsg(const std::string &path,char *buffer,int length){ return WriteBuffer( path, buffer, length, false ); } void GetRelationalPath(char *path,char *dir){ //相対パスを取得 int i,i2,i3,i4,i5; char temporary[MAX_PATH],temp2[MAX_PATH],temp3[MAX_PATH],temp4[MAX_PATH]; //ドライブ名をチェック _splitpath(path,temporary,0,0,0); _splitpath(dir,temp2,0,0,0); if(lstrcmpi(temporary,temp2)!=0) return; _splitpath(path,0,temporary,0,0); _splitpath(dir,0,temp2,0,0); i=1;i2=1; while(1){ i4=i; if(temporary[i-1]=='\\'&&temporary[i]){ //path側 for(i3=0;;i++,i3++){ if(temporary[i]=='\\'){ temp3[i3]=0; i++; break; } temp3[i3]=temporary[i]; } } else temp3[0]=0; i5=i2; if(temp2[i2-1]=='\\'&&temp2[i2]){ //dir側 for(i3=0;;i2++,i3++){ if(temp2[i2]=='\\'){ temp4[i3]=0; i2++; break; } temp4[i3]=temp2[i2]; } } else temp4[0]=0; if(temp3[0]=='\0'&&temp4[0]=='\0') break; if(lstrcmpi(temp3,temp4)!=0){ for(i3=0;;i5++){ if(temp2[i5]=='\0') break; if(temp2[i5]=='\\') i3++; } temp3[0]=0; for(i2=0;i2DefSaveDir; ofstr.lpstrTitle=Title; ofstr.Flags=OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_PATHMUSTEXIST; ofstr.nFileOffset=0; ofstr.nFileExtension=0; ofstr.lpstrDefExt="*"; ofstr.lCustData=NULL; ofstr.lpfnHook=NULL; ofstr.lpTemplateName=NULL; if(IsOpen){ if(!GetOpenFileName(&ofstr)) return FALSE; } else{ if(!GetSaveFileName(&ofstr)) return FALSE; } //次回の初期ディレクトリをセット char temporary[MAX_PATH]; _splitpath(filename,pobj_nv->DefSaveDir,temporary,NULL,NULL); lstrcat(pobj_nv->DefSaveDir,temporary); return TRUE; } BOOL GetFolder(HWND hWnd,char *folder,char *OpenFolderTitle){ BROWSEINFO BrowseInfo; LPITEMIDLIST pidlBrowse; LPMALLOC g_pMalloc; char temporary[MAX_PATH]; BrowseInfo.hwndOwner=hWnd; BrowseInfo.pszDisplayName=temporary; BrowseInfo.pidlRoot=NULL; BrowseInfo.lpszTitle=OpenFolderTitle; BrowseInfo.ulFlags=BIF_RETURNONLYFSDIRS; BrowseInfo.lpfn=NULL; BrowseInfo.lParam=(LPARAM)0; BrowseInfo.iImage=0; pidlBrowse=SHBrowseForFolder(&BrowseInfo); if(pidlBrowse){ if(SHGetMalloc(&g_pMalloc)!=0) return 0; SHGetPathFromIDList(pidlBrowse,folder); g_pMalloc->Free(pidlBrowse); g_pMalloc->Release(); return 1; } return 0; } int GetFileExtension( const char *path ) { char temporary[MAX_PATH]; _splitpath(path,0,0,0,temporary); if(lstrcmpi(temporary,".bas")==0|| lstrcmpi(temporary,".ab")==0|| lstrcmpi(temporary,".abp")==0) return FT_BASICPROGRAM; if(lstrcmpi(temporary,".sbp")==0) return FT_SUBPROGRAM; if(lstrcmpi(temporary,".txt")==0) return FT_TEXT; if(lstrcmpi(temporary,".pj")==0) return FT_PROJECT; if(lstrcmpi(temporary,".ico")==0) return FT_ICON; if(lstrcmpi(temporary,".html")==0|| lstrcmpi(temporary,".htm")==0|| lstrcmpi(temporary,".tpl")==0|| lstrcmpi(temporary,".php")==0) return FT_HTML; return FT_OTHER; } HWND OpenFileWithExtension( const std::string &filePath ) { int i; _int8 DocumentType; // TODO: char const *OpenFileName = filePath.c_str(); i=GetFileExtension(OpenFileName); #ifndef THETEXT if(i==FT_PROJECT){ //「最近使ったプロジェクト」に追加 pobj_nv->pobj_ProjectHistory->insert(OpenFileName); projectInfo.Load(OpenFileName); return 0; } #endif //「最近使ったファイル」に追加 pobj_nv->pobj_History->insert(OpenFileName); if(i==FT_ICON){ NewIconEditWindow(OpenFileName); return 0; } else if(i==FT_BASICPROGRAM||i==FT_SUBPROGRAM){ DocumentType=WNDTYPE_BASIC; #ifdef THETEXT DocumentType=WNDTYPE_TEXT; #endif } else if(i==FT_HTML) DocumentType=WNDTYPE_HTML; else DocumentType=WNDTYPE_TEXT; COLORREF TabColor; TabColor=pobj_nv->pobj_ExtLink->GetTabColorFromFilePath( filePath ); return NewTextEditWindow(OpenFileName,DocumentType,TabColor); } BOOL GetFileInformationByHandleWrap(HANDLE hFile, BY_HANDLE_FILE_INFORMATION& fi){ typedef BOOL WINAPI GFIBH(HANDLE, LPBY_HANDLE_FILE_INFORMATION); GFIBH *const pgfibh = reinterpret_cast( GetProcAddress(GetModuleHandle("kernel32"), "GetFileInformationByHandle")); if(pgfibh){ return pgfibh(hFile, &fi); } else{ SetLastError(ERROR_CALL_NOT_IMPLEMENTED); return FALSE; } } //重複チェック用のデータを設定 void SetFileIdentity(FILEIDENTITY &fi, BY_HANDLE_FILE_INFORMATION const &bhfi){ fi.VolumeSerialNumber = bhfi.dwVolumeSerialNumber; fi.FileIndexHigh = bhfi.nFileIndexHigh; fi.FileIndexLow = bhfi.nFileIndexLow; } void SetFileIdentityFromFile(MDIINFO &mi, HANDLE hFile){ BY_HANDLE_FILE_INFORMATION fi; if(GetFileInformationByHandleWrap(hFile, fi)){ SetFileIdentity(mi.FileIdentity, fi); } } BOOL SaveDocument(HWND hChild,char *SaveFileName){ //ウィンドウからバッファを読み取り、ファイルに保存 //SaveFileNameがNULLのときは上書き保存を試みる。 extern LPSTR IconFileFilter; extern HWND hClient,hDocCombo; int WndNum,i2; char temporary[MAX_PATH],str[MAX_PATH],str2[32]; DWORD dummy; WndNum=GetWndNum(hChild); std::string oldTitle = MdiInfo[WndNum]->title; if(MdiInfo[WndNum]->DocType==WNDTYPE_RAD||MdiInfo[WndNum]->DocType==WNDTYPE_MENU){ //////////////////////////////////// // RADツール及びメニューエディタ //////////////////////////////////// if(projectInfo.ModifyOfMaterial){ std::string const& workDir = projectInfo.GetWorkDir().GetPath(); std::string const& projName = projectInfo.GetName(); std::string t; t.reserve( workDir.size() + projName.size() + 4 ); t += workDir; t += projName; t += ".wnd"; SaveWindowFile( t.c_str(), projectInfo.windowInfos ); //.wbpファイルを生成 SaveWindowProgram(); } } else if(IS_DOCUMENT_TEXT(MdiInfo[WndNum]->DocType)){ ////////////////////////// // テキストドキュメント ////////////////////////// if(SaveFileName){ lstrcpy(temporary,SaveFileName); { ATL::CHandle fh(CreateFile(temporary,GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL)); if(fh==INVALID_HANDLE_VALUE){ fh.Detach(); sprintf(str,STRING_FILE_OVERWRIDE,temporary); if(MessageBox(hOwner,str,APPLICATION_NAME,MB_YESNO|MB_ICONINFORMATION)==IDNO){ return 0; } } } //ドキュメント セレクト コンボボックスから消去 i2=SendMessage(hDocCombo,CB_FINDSTRINGEXACT,0,(LONG_PTR)MdiInfo[WndNum]->title.c_str()); SendMessage(hDocCombo,CB_DELETESTRING,i2,0); //新しいパスをセット MdiInfo[WndNum]->path = temporary; //ドキュメント セレクト コンボボックスに挿入 _splitpath(temporary,NULL,NULL,str,str2); lstrcat(str,str2); MdiInfo[WndNum]->title = str; SendMessage(hDocCombo,CB_ADDSTRING,0,(LONG_PTR)MdiInfo[WndNum]->title.c_str()); i2=SendMessage(hDocCombo,CB_FINDSTRINGEXACT,0,(LONG_PTR)MdiInfo[WndNum]->title.c_str()); SendMessage(hDocCombo,CB_SETCURSEL,i2,0); //MDIウィンドウのタイトルを再設定 SetWindowText(hChild,MdiInfo[WndNum]->title.c_str()); } else{ if(MdiInfo[WndNum]->path.empty()){ //"保存先のファイルを指定してください" get_file_path: LPSTR ff; extern LPSTR DefFileFilter; extern LPSTR HtmlFileFilter; extern LPSTR TextFileFilter; if(MdiInfo[WndNum]->DocType==WNDTYPE_BASIC) ff=DefFileFilter; else if(MdiInfo[WndNum]->DocType==WNDTYPE_HTML) ff=HtmlFileFilter; else if(MdiInfo[WndNum]->DocType==WNDTYPE_TEXT) ff=TextFileFilter; if(!GetFilePathDialog(hOwner,temporary,ff,STRING_FILESAVETITLE_DEFAULT,FALSE)) return 0; SaveDocument(hChild,temporary); return 1; } lstrcpy(temporary,MdiInfo[WndNum]->path.c_str()); } if(!IsExistFile(temporary)){ //保存先ファイルが存在しないとき char temp2[MAX_PATH]; //初期ディレクトリをセット _splitpath(temporary,pobj_nv->DefSaveDir,temp2,NULL,NULL); lstrcat(pobj_nv->DefSaveDir,temp2); goto get_file_path; } //文字コードを復元 char *pBuf; pBuf=nkf.RestoreBuffer(MdiInfo[WndNum]->pMdiTextEdit->buffer,MdiInfo[WndNum]->pMdiTextEdit->iCharCode); //改行コードを復元 if(MdiInfo[WndNum]->pMdiTextEdit->iLfCode==LFCODE_LF) nkf.ToLF(pBuf); else if(MdiInfo[WndNum]->pMdiTextEdit->iLfCode==LFCODE_CR) nkf.ToCR(pBuf); //////////////////////// // 保存 //////////////////////// { ATL::CHandle fh(CreateFile(temporary,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL)); if(fh==INVALID_HANDLE_VALUE){ fh.Detach(); sprintf(str,STRING_ERROR_CANT_FILESAVE,temporary); MessageBox(hOwner,str,STRING_ERROR,MB_OK|MB_ICONSTOP); return 0; } WriteFile(fh,pBuf,strlen(pBuf),&dummy,NULL); SetFileIdentityFromFile(*MdiInfo[WndNum], fh); } HeapDefaultFree(pBuf); //変更フラグをオフにする MdiInfo[WndNum]->pMdiTextEdit->UnModify(); } else if(MdiInfo[WndNum]->DocType==WNDTYPE_ICONEDIT){ /////////////////// // ICON /////////////////// if(SaveFileName){ lstrcpy(temporary,SaveFileName); { ATL::CHandle fh(CreateFile(temporary,GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL)); if(fh==INVALID_HANDLE_VALUE){ fh.Detach(); sprintf(str,STRING_FILE_OVERWRIDE,temporary); if(MessageBox(hOwner,str,APPLICATION_NAME,MB_YESNO|MB_ICONINFORMATION)==IDNO){ return 0; } } } MdiInfo[WndNum]->path = temporary; i2=SendMessage(hDocCombo,CB_FINDSTRINGEXACT,0,(LONG_PTR)MdiInfo[WndNum]->title.c_str()); SendMessage(hDocCombo,CB_DELETESTRING,i2,0); _splitpath(temporary,NULL,NULL,str,str2); lstrcat(str,str2); MdiInfo[WndNum]->title = str; SendMessage(hDocCombo,CB_ADDSTRING,0,(LONG_PTR)MdiInfo[WndNum]->title.c_str()); i2=SendMessage(hDocCombo,CB_FINDSTRINGEXACT,0,(LONG_PTR)MdiInfo[WndNum]->title.c_str()); SendMessage(hDocCombo,CB_SETCURSEL,i2,0); SetWindowText(hChild,MdiInfo[WndNum]->title.c_str()); } else{ if(MdiInfo[WndNum]->path.empty()){ //"保存先のファイルを指定してください" if(!GetFilePathDialog(hOwner,temporary,IconFileFilter,STRING_FILESAVETITLE_DEFAULT,FALSE)) return 0; SaveDocument(hChild,temporary); return 1; } lstrcpy(temporary,MdiInfo[WndNum]->path.c_str()); } SaveIconFile(temporary,hChild); MdiInfo[WndNum]->MdiIconEditInfo->bModify=0; } //タブコントロールを再設定 if(pobj_nv->bSaveTabToHead){ COLORREF color; color=pobj_MainTab->GetItemColor(oldTitle.c_str()); pobj_MainTab->DeleteItem( oldTitle.c_str(), false ); pobj_MainTab->InsertItem( MdiInfo[WndNum]->title.c_str(), false, color ); } else{ pobj_MainTab->RenameItem( oldTitle.c_str(), MdiInfo[WndNum]->title.c_str() ); } //「最近使ったファイル」を更新 pobj_nv->pobj_History->insert(MdiInfo[WndNum]->path.c_str()); return 1; } BOOL ShortPathToLongPath(char ShortPath[MAX_PATH],char *LongPath){ HANDLE hFind; WIN32_FIND_DATA wfd; int i; char dummy[MAX_PATH]; for(i=0;i