Normalize file names before going any further

This commit is contained in:
Zane U. Ji 2014-05-03 21:19:06 +08:00
parent b12b7fadc4
commit 19b7a74641
5 changed files with 35 additions and 56 deletions

View File

@ -715,8 +715,8 @@ xmlChar *WrapLibxml::xmlFileNameToURL ( const wxString &fileName )
return NULL; return NULL;
wxFileName fn ( fileName ); wxFileName fn ( fileName );
fn.Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_TILDE | wxPATH_NORM_ABSOLUTE); fn.Normalize();
wxString url = fn.GetFullPath(wxPATH_NATIVE); wxString url = fn.GetFullPath();
return xmlPathToURI ( ( xmlChar * ) ( const char * ) url.utf8_str() ); return xmlPathToURI ( ( xmlChar * ) ( const char * ) url.utf8_str() );
} }

View File

@ -2983,19 +2983,13 @@ void MyFrame::OnOpen ( wxCommandEvent& event )
break; break;
} }
bool MyFrame::openFile ( wxString& fileName, bool largeFile ) bool MyFrame::openFile ( const wxString &file, bool largeFile )
{ {
#ifndef __WXMSW__ wxFileName fn = WrapLibxml::URLToFileName ( file );
// truncate string up to file:/ portion added by GNOME fn.Normalize();
wxString filePrefix = _T ( "file:" );
int index = fileName.Find ( filePrefix.c_str() );
if ( index != -1 )
{
fileName = fileName.Mid ( ( size_t ) index + filePrefix.Length() );
}
#endif
if ( !wxFileExists ( fileName ) ) wxString fileName = fn.GetFullPath();
if ( !fn.IsFileReadable() )
{ {
wxString message; wxString message;
message.Printf ( _ ( "Cannot open %s." ), fileName.c_str() ); message.Printf ( _ ( "Cannot open %s." ), fileName.c_str() );
@ -3012,15 +3006,6 @@ bool MyFrame::openFile ( wxString& fileName, bool largeFile )
return false; return false;
} }
wxString directory, name, extension;
wxFileName::SplitPath ( fileName, NULL, &directory, &name, &extension );
if ( !extension.empty() )
{
name += _T ( "." );
name += extension;
}
wxString wideError; wxString wideError;
pair<int, int> posPair; pair<int, int> posPair;
XmlDoc *doc; XmlDoc *doc;
@ -3238,21 +3223,16 @@ bool MyFrame::openFile ( wxString& fileName, bool largeFile )
#endif #endif
doc->setFullFileName ( fileName ); doc->setFullFileName ( fileName );
doc->setShortFileName ( name );
doc->setDirectory ( directory );
openFileSet.insert ( fileName ); openFileSet.insert ( fileName );
history.AddFileToHistory ( fileName ); history.AddFileToHistory ( fileName );
updateFileMenu(); updateFileMenu();
wxFileName ofn ( fileName );
doc->setLastModified ( ofn.GetModificationTime() );
mainBook->AddPage ( ( wxWindow * ) doc, name, _T ( "" ) ); mainBook->AddPage ( ( wxWindow * ) doc, fn.GetFullName(), _T ( "" ) );
} }
statusProgress ( wxEmptyString ); statusProgress ( wxEmptyString );
mainBook->Layout(); mainBook->Layout();
wxFileName fn ( fileName );
doc->setLastModified ( fn.GetModificationTime() ); doc->setLastModified ( fn.GetModificationTime() );
doc->SetFocus(); doc->SetFocus();
@ -3614,8 +3594,6 @@ void MyFrame::saveAs()
openFileSet.erase ( doc->getFullFileName() ); openFileSet.erase ( doc->getFullFileName() );
doc->setFullFileName ( path ); doc->setFullFileName ( path );
doc->setShortFileName ( name );
doc->setDirectory ( directory );
history.AddFileToHistory ( path ); // update history history.AddFileToHistory ( path ); // update history
updateFileMenu(); updateFileMenu();

View File

@ -349,7 +349,7 @@ class MyFrame : public wxFrame
bool forcePane = false ); bool forcePane = false );
// public to allow IPC access // public to allow IPC access
bool openFile ( wxString& fileName, bool largeFile = false ); bool openFile ( const wxString &fileName, bool largeFile = false );
bool isOpen ( const wxString& fileName ); bool isOpen ( const wxString& fileName );
bool activateTab ( const wxString& fileName ); bool activateTab ( const wxString& fileName );
void reloadTab(); void reloadTab();

View File

@ -51,37 +51,38 @@ XmlDoc::XmlDoc (
style ) style )
{ } { }
wxString& XmlDoc::getDirectory() wxString XmlDoc::getDirectory()
{ {
return directory; return mFileName.GetPath();
} }
wxString& XmlDoc::getFullFileName()
wxString XmlDoc::getFullFileName()
{ {
return fullFileName; return mFileName.GetFullPath();
} }
wxString& XmlDoc::getShortFileName()
wxString XmlDoc::getShortFileName()
{ {
return shortFileName; return mFileName.GetFullName();
} }
wxDateTime XmlDoc::getLastModified()
const wxDateTime& XmlDoc::getLastModified()
{ {
return lastModified; return lastModified;
} }
void XmlDoc::setDirectory ( const wxString& s )
{
directory = s;
}
void XmlDoc::setFullFileName ( const wxString &s ) void XmlDoc::setFullFileName ( const wxString &s )
{ {
fullFileName = s; mFileName.Assign ( s );
mFileName.Normalize();
} }
void XmlDoc::setShortFileName ( const wxString &s ) void XmlDoc::setShortFileName ( const wxString &s )
{ {
shortFileName = s; mFileName.SetFullName ( s );
} }
void XmlDoc::setLastModified ( wxDateTime dt )
void XmlDoc::setLastModified ( const wxDateTime& dt )
{ {
lastModified = dt; lastModified = dt;
} }

View File

@ -23,6 +23,7 @@
#include <wx/wx.h> #include <wx/wx.h>
#include <wx/datetime.h> #include <wx/datetime.h>
#include <wx/print.h> #include <wx/print.h>
#include <wx/filename.h>
#include "xmlctrl.h" #include "xmlctrl.h"
class XmlDoc : public XmlCtrl class XmlDoc : public XmlCtrl
@ -43,16 +44,15 @@ class XmlDoc : public XmlCtrl
const wxPoint& position = wxDefaultPosition, const wxPoint& position = wxDefaultPosition,
const wxSize& size = wxDefaultSize, const wxSize& size = wxDefaultSize,
long style = 0 ); long style = 0 );
wxString& getDirectory(); wxString getDirectory();
wxString& getFullFileName(); wxString getFullFileName();
wxString& getShortFileName(); wxString getShortFileName();
wxDateTime getLastModified(); const wxDateTime& getLastModified();
void setDirectory ( const wxString& s );
void setFullFileName ( const wxString &s ); void setFullFileName ( const wxString &s );
void setShortFileName ( const wxString &s ); void setShortFileName ( const wxString &s );
void setLastModified ( wxDateTime dt ); void setLastModified ( const wxDateTime &dt );
private: private:
wxString directory, fullFileName, shortFileName; wxFileName mFileName;
wxDateTime lastModified; wxDateTime lastModified;
}; };