mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-03 23:50:18 -04:00
ModernKeePassLib custom PCL version
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
KeePass Password Safe - The Open-Source Password Manager
|
||||
Copyright (C) 2003-2012 Dominik Reichl <dominik.reichl@t-online.de>
|
||||
Copyright (C) 2003-2014 Dominik Reichl <dominik.reichl@t-online.de>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -20,11 +20,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Diagnostics;
|
||||
|
||||
using ModernKeePassLib.Native;
|
||||
#if PCL
|
||||
using Windows.Storage;
|
||||
#endif
|
||||
|
||||
namespace ModernKeePassLib.Utility
|
||||
{
|
||||
@@ -34,16 +36,30 @@ namespace ModernKeePassLib.Utility
|
||||
/// </summary>
|
||||
public static class UrlUtil
|
||||
{
|
||||
// Bert TODO: Temporary fix, most of this class is not needed in WINRT.
|
||||
private static readonly char DirectorySeparatorChar = '/';
|
||||
private static readonly char[] m_vDirSeps = new char[] {
|
||||
'\\', '/', UrlUtil.LocalDirSepChar };
|
||||
#if !PCL
|
||||
private static readonly char[] m_vPathTrimCharsWs = new char[] {
|
||||
'\"', ' ', '\t', '\r', '\n' };
|
||||
#endif
|
||||
|
||||
private static readonly char[] m_vDirSeps = new char[] { '\\', '/',
|
||||
DirectorySeparatorChar };
|
||||
public static char LocalDirSepChar
|
||||
{
|
||||
#if KeePassRT
|
||||
get { return '\\'; }
|
||||
#elif PCL
|
||||
//get { return PortablePath.DirectorySeparatorChar; }
|
||||
get { return '\\'; }
|
||||
#else
|
||||
get { return Path.DirectorySeparatorChar; }
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the directory (path) of a file name. The returned string is
|
||||
/// Get the directory (path) of a file name. The returned string may be
|
||||
/// terminated by a directory separator character. Example:
|
||||
/// passing <c>C:\\My Documents\\My File.kdb</c> in <paramref name="strFile" />
|
||||
/// and <c>true</c> to <paramref name="bAppendTerminatingChar"/>
|
||||
/// would produce this string: <c>C:\\My Documents\\</c>.
|
||||
/// </summary>
|
||||
/// <param name="strFile">Full path of a file.</param>
|
||||
@@ -54,8 +70,7 @@ namespace ModernKeePassLib.Utility
|
||||
/// of <c>X:</c>, overriding <paramref name="bAppendTerminatingChar" />).
|
||||
/// This should only be set to <c>true</c>, if the returned path is directly
|
||||
/// passed to some directory API.</param>
|
||||
/// <returns>Directory of the file. The return value is an empty string
|
||||
/// (<c>""</c>) if the input parameter is <c>null</c>.</returns>
|
||||
/// <returns>Directory of the file.</returns>
|
||||
public static string GetFileDirectory(string strFile, bool bAppendTerminatingChar,
|
||||
bool bEnsureValidDirSpec)
|
||||
{
|
||||
@@ -63,14 +78,15 @@ namespace ModernKeePassLib.Utility
|
||||
if(strFile == null) throw new ArgumentNullException("strFile");
|
||||
|
||||
int nLastSep = strFile.LastIndexOfAny(m_vDirSeps);
|
||||
if(nLastSep < 0) return strFile; // None
|
||||
if(nLastSep < 0) return string.Empty; // No directory
|
||||
|
||||
if(bEnsureValidDirSpec && (nLastSep == 2) && (strFile[1] == ':') &&
|
||||
(strFile[2] == '\\')) // Length >= 3 and Windows root directory
|
||||
bAppendTerminatingChar = true;
|
||||
|
||||
if(!bAppendTerminatingChar) return strFile.Substring(0, nLastSep);
|
||||
return EnsureTerminatingSeparator(strFile.Substring(0, nLastSep), false);
|
||||
return EnsureTerminatingSeparator(strFile.Substring(0, nLastSep),
|
||||
(strFile[nLastSep] == '/'));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -151,7 +167,7 @@ namespace ModernKeePassLib.Utility
|
||||
}
|
||||
|
||||
if(bUrl) return (strPath + '/');
|
||||
return (strPath + DirectorySeparatorChar);
|
||||
return (strPath + UrlUtil.LocalDirSepChar);
|
||||
}
|
||||
|
||||
/* /// <summary>
|
||||
@@ -214,13 +230,22 @@ namespace ModernKeePassLib.Utility
|
||||
|
||||
public static string GetQuotedAppPath(string strPath)
|
||||
{
|
||||
int nFirst = strPath.IndexOf('\"');
|
||||
int nSecond = strPath.IndexOf('\"', nFirst + 1);
|
||||
if(strPath == null) { Debug.Assert(false); return string.Empty; }
|
||||
|
||||
if((nFirst >= 0) && (nSecond >= 0))
|
||||
return strPath.Substring(nFirst + 1, nSecond - nFirst - 1);
|
||||
// int nFirst = strPath.IndexOf('\"');
|
||||
// int nSecond = strPath.IndexOf('\"', nFirst + 1);
|
||||
// if((nFirst >= 0) && (nSecond >= 0))
|
||||
// return strPath.Substring(nFirst + 1, nSecond - nFirst - 1);
|
||||
// return strPath;
|
||||
|
||||
return strPath;
|
||||
string str = strPath.Trim();
|
||||
if(str.Length <= 1) return str;
|
||||
if(str[0] != '\"') return str;
|
||||
|
||||
int iSecond = str.IndexOf('\"', 1);
|
||||
if(iSecond <= 0) return str;
|
||||
|
||||
return str.Substring(1, iSecond - 1);
|
||||
}
|
||||
|
||||
public static string FileUrlToPath(string strUrl)
|
||||
@@ -232,14 +257,14 @@ namespace ModernKeePassLib.Utility
|
||||
if(str.StartsWith(@"file:///", StrUtil.CaseIgnoreCmp))
|
||||
str = str.Substring(8, str.Length - 8);
|
||||
|
||||
str = str.Replace('/', DirectorySeparatorChar);
|
||||
str = str.Replace('/', UrlUtil.LocalDirSepChar);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
public static bool UnhideFile(string strFile)
|
||||
{
|
||||
#if KeePassLibSD || !TODO
|
||||
#if (PCL || KeePassLibSD || KeePassRT)
|
||||
return false;
|
||||
#else
|
||||
if(strFile == null) throw new ArgumentNullException("strFile");
|
||||
@@ -259,7 +284,7 @@ namespace ModernKeePassLib.Utility
|
||||
|
||||
public static bool HideFile(string strFile, bool bHide)
|
||||
{
|
||||
#if KeePassLibSD || !TODO
|
||||
#if (PCL || KeePassLibSD || KeePassRT)
|
||||
return false;
|
||||
#else
|
||||
if(strFile == null) throw new ArgumentNullException("strFile");
|
||||
@@ -300,7 +325,9 @@ namespace ModernKeePassLib.Utility
|
||||
return strTargetFile;
|
||||
}
|
||||
|
||||
#if (!PCL && !KeePassLibSD && !KeePassRT)
|
||||
if(NativeLib.IsUnix())
|
||||
#endif
|
||||
{
|
||||
bool bBaseUnc = IsUncPath(strBaseFile);
|
||||
bool bTargetUnc = IsUncPath(strTargetFile);
|
||||
@@ -319,21 +346,19 @@ namespace ModernKeePassLib.Utility
|
||||
StringBuilder sbRel = new StringBuilder();
|
||||
for(int j = i; j < (vBase.Length - 1); ++j)
|
||||
{
|
||||
if(sbRel.Length > 0) sbRel.Append(DirectorySeparatorChar);
|
||||
if(sbRel.Length > 0) sbRel.Append(UrlUtil.LocalDirSepChar);
|
||||
sbRel.Append("..");
|
||||
}
|
||||
for(int k = i; k < vTarget.Length; ++k)
|
||||
{
|
||||
if(sbRel.Length > 0) sbRel.Append(DirectorySeparatorChar);
|
||||
if(sbRel.Length > 0) sbRel.Append(UrlUtil.LocalDirSepChar);
|
||||
sbRel.Append(vTarget[k]);
|
||||
}
|
||||
|
||||
return sbRel.ToString();
|
||||
}
|
||||
|
||||
#if KeePassLibSD || !TODO
|
||||
return strTargetFile;
|
||||
#else
|
||||
#if (!PCL && !KeePassLibSD && !KeePassRT)
|
||||
try // Windows
|
||||
{
|
||||
const int nMaxPath = NativeMethods.MAX_PATH * 2;
|
||||
@@ -347,7 +372,8 @@ namespace ModernKeePassLib.Utility
|
||||
|
||||
return str;
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); return strTargetFile; }
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
return strTargetFile;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -379,9 +405,6 @@ namespace ModernKeePassLib.Utility
|
||||
|
||||
public static string GetShortestAbsolutePath(string strPath)
|
||||
{
|
||||
Debug.Assert(false, "not yet implemented");
|
||||
return null;
|
||||
#if TODO
|
||||
if(strPath == null) throw new ArgumentNullException("strPath");
|
||||
if(strPath.Length == 0) return string.Empty;
|
||||
|
||||
@@ -426,7 +449,20 @@ namespace ModernKeePassLib.Utility
|
||||
}
|
||||
|
||||
string str;
|
||||
try { str = Path.GetFullPath(strPath); }
|
||||
try
|
||||
{
|
||||
#if KeePassRT
|
||||
var dirT = StorageFolder.GetFolderFromPathAsync(
|
||||
strPath).GetResults();
|
||||
str = dirT.Path;
|
||||
#elif PCL
|
||||
//var dirT = FileSystem.Current.GetFolderFromPathAsync(strPath).Result;
|
||||
str = ApplicationData.Current.RoamingFolder.Path;
|
||||
//str = dirT.Path;
|
||||
#else
|
||||
str = Path.GetFullPath(strPath);
|
||||
#endif
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); return strPath; }
|
||||
|
||||
Debug.Assert(str.IndexOf("\\..\\") < 0);
|
||||
@@ -437,7 +473,6 @@ namespace ModernKeePassLib.Utility
|
||||
}
|
||||
|
||||
return str;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static int GetUrlLength(string strText, int nOffset)
|
||||
@@ -483,7 +518,7 @@ namespace ModernKeePassLib.Utility
|
||||
|
||||
public static string ConvertSeparators(string strPath)
|
||||
{
|
||||
return ConvertSeparators(strPath, DirectorySeparatorChar);
|
||||
return ConvertSeparators(strPath, UrlUtil.LocalDirSepChar);
|
||||
}
|
||||
|
||||
public static string ConvertSeparators(string strPath, char chSeparator)
|
||||
@@ -595,5 +630,106 @@ namespace ModernKeePassLib.Utility
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
#if !PCL
|
||||
public static string GetTempPath()
|
||||
{
|
||||
string strDir;
|
||||
if(NativeLib.IsUnix())
|
||||
strDir = NativeMethods.GetUserRuntimeDir();
|
||||
#if KeePassRT
|
||||
else strDir = Windows.Storage.ApplicationData.Current.TemporaryFolder.Path;
|
||||
#else
|
||||
else strDir = Path.GetTempPath();
|
||||
#endif
|
||||
|
||||
try
|
||||
{
|
||||
if(Directory.Exists(strDir) == false)
|
||||
Directory.CreateDirectory(strDir);
|
||||
}
|
||||
catch(Exception) { Debug.Assert(false); }
|
||||
|
||||
return strDir;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !PCL && !KeePassLibSD
|
||||
// Structurally mostly equivalent to UrlUtil.GetFileInfos
|
||||
public static List<string> GetFilePaths(string strDir, string strPattern,
|
||||
SearchOption opt)
|
||||
{
|
||||
List<string> l = new List<string>();
|
||||
if(strDir == null) { Debug.Assert(false); return l; }
|
||||
if(strPattern == null) { Debug.Assert(false); return l; }
|
||||
|
||||
string[] v = Directory.GetFiles(strDir, strPattern, opt);
|
||||
if(v == null) { Debug.Assert(false); return l; }
|
||||
|
||||
// Only accept files with the correct extension; GetFiles may
|
||||
// return additional files, see GetFiles documentation
|
||||
string strExt = GetExtension(strPattern);
|
||||
if(!string.IsNullOrEmpty(strExt) && (strExt.IndexOf('*') < 0) &&
|
||||
(strExt.IndexOf('?') < 0))
|
||||
{
|
||||
strExt = "." + strExt;
|
||||
|
||||
foreach(string strPathRaw in v)
|
||||
{
|
||||
if(strPathRaw == null) { Debug.Assert(false); continue; }
|
||||
string strPath = strPathRaw.Trim(m_vPathTrimCharsWs);
|
||||
if(strPath.Length == 0) { Debug.Assert(false); continue; }
|
||||
Debug.Assert(strPath == strPathRaw);
|
||||
|
||||
if(!strPath.EndsWith(strExt, StrUtil.CaseIgnoreCmp))
|
||||
continue;
|
||||
|
||||
l.Add(strPathRaw);
|
||||
}
|
||||
}
|
||||
else l.AddRange(v);
|
||||
|
||||
return l;
|
||||
}
|
||||
|
||||
// Structurally mostly equivalent to UrlUtil.GetFilePaths
|
||||
public static List<FileInfo> GetFileInfos(DirectoryInfo di, string strPattern,
|
||||
SearchOption opt)
|
||||
{
|
||||
List<FileInfo> l = new List<FileInfo>();
|
||||
if(di == null) { Debug.Assert(false); return l; }
|
||||
if(strPattern == null) { Debug.Assert(false); return l; }
|
||||
|
||||
FileInfo[] v = di.GetFiles(strPattern, opt);
|
||||
if(v == null) { Debug.Assert(false); return l; }
|
||||
|
||||
// Only accept files with the correct extension; GetFiles may
|
||||
// return additional files, see GetFiles documentation
|
||||
string strExt = GetExtension(strPattern);
|
||||
if(!string.IsNullOrEmpty(strExt) && (strExt.IndexOf('*') < 0) &&
|
||||
(strExt.IndexOf('?') < 0))
|
||||
{
|
||||
strExt = "." + strExt;
|
||||
|
||||
foreach(FileInfo fi in v)
|
||||
{
|
||||
if(fi == null) { Debug.Assert(false); continue; }
|
||||
string strPathRaw = fi.FullName;
|
||||
if(strPathRaw == null) { Debug.Assert(false); continue; }
|
||||
string strPath = strPathRaw.Trim(m_vPathTrimCharsWs);
|
||||
if(strPath.Length == 0) { Debug.Assert(false); continue; }
|
||||
Debug.Assert(strPath == strPathRaw);
|
||||
|
||||
if(!strPath.EndsWith(strExt, StrUtil.CaseIgnoreCmp))
|
||||
continue;
|
||||
|
||||
l.Add(fi);
|
||||
}
|
||||
}
|
||||
else l.AddRange(v);
|
||||
|
||||
return l;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user