/* /// <summary> * /// File access mode enumeration. Used by the <c>FileAccessible</c> * /// method. * /// </summary> * public enum FileAccessMode * { * /// <summary> * /// Opening a file in read mode. The specified file must exist. * /// </summary> * Read = 0, * * /// <summary> * /// Opening a file in create mode. If the file exists already, it * /// will be overwritten. If it doesn't exist, it will be created. * /// The return value is <c>true</c>, if data can be written to the * /// file. * /// </summary> * Create * } */ /* /// <summary> * /// Test if a specified path is accessible, either in read or write mode. * /// </summary> * /// <param name="strFilePath">Path to test.</param> * /// <param name="fMode">Requested file access mode.</param> * /// <returns>Returns <c>true</c> if the specified path is accessible in * /// the requested mode, otherwise the return value is <c>false</c>.</returns> * public static bool FileAccessible(string strFilePath, FileAccessMode fMode) * { * Debug.Assert(strFilePath != null); * if(strFilePath == null) throw new ArgumentNullException("strFilePath"); * * if(fMode == FileAccessMode.Read) * { * FileStream fs; * * try { fs = File.OpenRead(strFilePath); } * catch(Exception) { return false; } * if(fs == null) return false; * * fs.Close(); * return true; * } * else if(fMode == FileAccessMode.Create) * { * FileStream fs; * * try { fs = File.Create(strFilePath); } * catch(Exception) { return false; } * if(fs == null) return false; * * fs.Close(); * return true; * } * * return false; * } */ internal static int IndexOfSecondEnclQuote(string str) { if (str == null) { Debug.Assert(false); return(-1); } if (str.Length <= 1) { return(-1); } if (str[0] != '\"') { Debug.Assert(false); return(-1); } if (NativeLib.IsUnix()) { // Find non-escaped quote string strFlt = str.Replace("\\\\", new string( StrUtil.GetUnusedChar(str + "\\\""), 2)); // Same length Match m = Regex.Match(strFlt, "[^\\\\]\\u0022"); int i = (((m != null) && m.Success) ? m.Index : -1); return((i >= 0) ? (i + 1) : -1); // Index of quote } // Windows does not allow quotes in folder/file names return(str.IndexOf('\"', 1)); }
private static string DeriveCustomFormat(string strDT, DateTime dt) { string[] vPlh = new string[] { // Names, sorted by length "MMMM", "dddd", "MMM", "ddd", "gg", "g", // Numbers, the ones with prefix '0' first "yyyy", "yyy", "yy", "y", "MM", "M", "dd", "d", "HH", "hh", "H", "h", "mm", "m", "ss", "s", "tt", "t" }; List <string> lValues = new List <string>(); foreach (string strPlh in vPlh) { string strEval = strPlh; if (strEval.Length == 1) { strEval = @"%" + strPlh; // Make custom } lValues.Add(dt.ToString(strEval)); } StringBuilder sbAll = new StringBuilder(); sbAll.Append("dfFghHKmMstyz:/\"\'\\%"); sbAll.Append(strDT); foreach (string strVEnum in lValues) { sbAll.Append(strVEnum); } List <char> lCodes = new List <char>(); for (int i = 0; i < vPlh.Length; ++i) { char ch = StrUtil.GetUnusedChar(sbAll.ToString()); lCodes.Add(ch); sbAll.Append(ch); } string str = strDT; for (int i = 0; i < vPlh.Length; ++i) { string strValue = lValues[i]; if (string.IsNullOrEmpty(strValue)) { continue; } str = str.Replace(strValue, new string(lCodes[i], 1)); } StringBuilder sbFmt = new StringBuilder(); bool bInLiteral = false; foreach (char ch in str) { int iCode = lCodes.IndexOf(ch); // The escape character doesn't work correctly (e.g. // "dd\\.MM\\.yyyy\\ HH\\:mm\\:ss" doesn't work, but // "dd'.'MM'.'yyyy' 'HH':'mm':'ss" does); use '' instead // if(iCode >= 0) sbFmt.Append(vPlh[iCode]); // else // Literal // { // sbFmt.Append('\\'); // sbFmt.Append(ch); // } if (iCode >= 0) { if (bInLiteral) { sbFmt.Append('\''); bInLiteral = false; } sbFmt.Append(vPlh[iCode]); } else // Literal { if (!bInLiteral) { sbFmt.Append('\''); bInLiteral = true; } sbFmt.Append(ch); } } if (bInLiteral) { sbFmt.Append('\''); } return(sbFmt.ToString()); }