! A file path given to Nyquist may be a platform-independent canonicalized form using certain abbreviations that are expanded into the platform-dependent equivalent. If the path names only a directory, also append "/untitled" plus extension */
| 2777 | If the path names only a directory, also append "/untitled" plus extension |
| 2778 | */ |
| 2779 | void NyquistBase::resolveFilePath( |
| 2780 | wxString& path, FileExtension extension /* empty string */) |
| 2781 | { |
| 2782 | #if defined(__WXMSW__) |
| 2783 | path.Replace("/", wxFileName::GetPathSeparator()); |
| 2784 | #endif |
| 2785 | |
| 2786 | path.Trim(true).Trim(false); |
| 2787 | |
| 2788 | typedef std::unordered_map<wxString, FilePath> map; |
| 2789 | map pathKeys = { |
| 2790 | { "*home*", PlatformCompatibility::GetHomeDir() }, |
| 2791 | { "~", PlatformCompatibility::GetHomeDir() }, |
| 2792 | { "*default*", FileNames::DefaultToDocumentsFolder("").GetPath() }, |
| 2793 | { "*export*", FileNames::FindDefaultPath(FileNames::Operation::Export) }, |
| 2794 | { "*save*", FileNames::FindDefaultPath(FileNames::Operation::Save) }, |
| 2795 | { "*config*", FileNames::DataDir() } |
| 2796 | }; |
| 2797 | |
| 2798 | int characters = path.Find(wxFileName::GetPathSeparator()); |
| 2799 | if (characters == wxNOT_FOUND) // Just a path or just a file name |
| 2800 | { |
| 2801 | if (path.empty()) |
| 2802 | path = "*default*"; |
| 2803 | |
| 2804 | if (pathKeys.find(path) != pathKeys.end()) |
| 2805 | { |
| 2806 | // Keyword found, so assume this is the intended directory. |
| 2807 | path = pathKeys[path] + wxFileName::GetPathSeparator(); |
| 2808 | } |
| 2809 | else // Just a file name |
| 2810 | { |
| 2811 | path = pathKeys["*default*"] + wxFileName::GetPathSeparator() + path; |
| 2812 | } |
| 2813 | } |
| 2814 | else // path + file name |
| 2815 | { |
| 2816 | wxString firstDir = path.Left(characters); |
| 2817 | wxString rest = path.Mid(characters); |
| 2818 | |
| 2819 | if (pathKeys.find(firstDir) != pathKeys.end()) |
| 2820 | { |
| 2821 | path = pathKeys[firstDir] + rest; |
| 2822 | } |
| 2823 | } |
| 2824 | |
| 2825 | wxFileName fname = path; |
| 2826 | |
| 2827 | // If the directory is invalid, better to leave it as is (invalid) so that |
| 2828 | // the user sees the error rather than an unexpected file path. |
| 2829 | if (fname.wxFileName::IsOk() && fname.GetFullName().empty()) |
| 2830 | { |
| 2831 | path = fname.GetPathWithSep() + _("untitled"); |
| 2832 | if (!extension.empty()) |
| 2833 | path = path + '.' + extension; |
| 2834 | } |
| 2835 | } |
| 2836 |