protected bool CheckValid(MString cmdInfo) { if (cmdInfo == "") { return(false); } return(true); }
protected override Status CheckEndPath(ref Component targetDir, ref MString fileName, MString endName, bool usePattern) { if (targetDir.GetChild(endName) != null) { return(Status.Error_Path_Already_Exist); } return(base.CheckEndPath(ref targetDir, ref fileName, endName, usePattern)); }
public virtual Status CheckPath(ref MString path, out Component targetDir, out MString fileName, bool usePattern) { targetDir = null; fileName = "*"; path = path.Replace("\"", "").MultiReplace("\\", "\\"); MString[] paths = path.MultiSplit('\\'); MString rootName = paths[0]; bool isEnding = paths.Length < 2; Status status = CheckRootPath(ref targetDir, ref fileName, rootName, usePattern, isEnding); if (status != Status.Succeed || isEnding) { return(status); } for (int i = 1; i < paths.Length - 1; i++) { if (paths[i].Trim() == "") { return(Status.Error_Path_Format); } if (nameRegex.IsMatch(paths[i])) { return(Status.Error_Path_Format); } if (paths[i] == ".") { continue; } else if (paths[i] == "..") { if (targetDir.parent != null) { targetDir = targetDir.parent; } continue; } status = EnterDirectory(ref targetDir, paths[i]); if (status != Status.Succeed) { return(status); } } string endName = paths[paths.Length - 1]; return(CheckEndPath(ref targetDir, ref fileName, endName, usePattern)); }
protected virtual Status EnterDirectory(ref Component source, MString name) { var dir = source.GetDirectory(name); if (dir == null) { return(Status.Error_Path_Not_Found); } else { source = dir; return(Status.Succeed); } }
protected virtual Status CheckRootPath(ref Component targetDir, ref MString fileName, MString rootName, bool usePattern, bool isEnding) { if (rootName == "") { targetDir = VsDiskMoniter.Instance.Root; } else if (rootName == ".") { targetDir = VsDiskMoniter.Instance.Cursor; } else if (rootName == "..") { if (VsDiskMoniter.Instance.Cursor.parent != null) { targetDir = VsDiskMoniter.Instance.Cursor.parent; } else { targetDir = VsDiskMoniter.Instance.Cursor; } } else if (rootName.Last() == ':') { if (rootName.ToLower() != "v:") { return(Status.Error_Disk_Not_Found); } targetDir = VsDiskMoniter.Instance.Root; } else { targetDir = VsDiskMoniter.Instance.Cursor; if (isEnding) { return(CheckEndPath(ref targetDir, ref fileName, rootName, usePattern)); } if (nameRegex.IsMatch(rootName)) { return(Status.Error_Path_Format); } return(EnterDirectory(ref targetDir, rootName)); } return(Status.Succeed); }
public static bool Equals(MString a, MString b) { int aH = a.GetHashCode(); int bH = b.GetHashCode(); if (aH == bH) { return(true); } if ((aH == Null.GetHashCode()) || (bH == Null.GetHashCode())) { return(false); } return(EqualsHelper(a, b)); }
public void Parse(MString cmdInfo) { cmdInfo = cmdInfo.Trim(); if (!CheckValid(cmdInfo)) { return; } if (!CreateCommand(cmdInfo)) { return; } ExcuteCommand(); }
protected override Status EnterDirectory(ref Component source, MString name) { if (source.GetFile(name) != null) { return(Status.Error_Path_Already_Exist); } VsDirectory child = source.GetDirectory(name); if (child == null) { child = new VsDirectory(name); source.Add(child); } source = child; return(Status.Succeed); }
private static bool EqualsHelper(MString strA, MString strB) { if (strA.Length != strB.Length) { return(false); } int i = 0; while (i < strA.Length) { if (strA[i] != strB[i]) { return(false); } i++; } return(true); }
private bool CheckAttribute(ref MString newDir, MString[] attrs, bool[] results) { for (int i = 0; i < newDir.Length; i++) { if (newDir[i] == '/') { MString str = newDir.Substring(i + 1); int attrEndIndex = str.IndexOf(new string[] { " ", "/" }); if (attrEndIndex == -1) { attrEndIndex = str.Length; } MString attr = str.Substring(0, attrEndIndex); int rmStartIdx = 0, rmCount = 0; bool isRightAttr = false; for (int j = 0; j < attrs.Length; j++) { if (attr == attrs[j]) { isRightAttr = true; results[j] = true; rmStartIdx = newDir.Substring(0, i).TrimEnd().Length; rmCount = (i - rmStartIdx + attrs[j].Length + 1); break; } } if (!isRightAttr) { Logger.Log(Status.Error_Attribute_Format, attr); return(false); } newDir = newDir.Remove(rmStartIdx, rmCount); i = rmStartIdx - 1; } } return(true); }
public static void Test2() { string a = "1234567890yyyy1234567890yyyy"; a = a.Replace("yy", "b"); MString str = new MString("ywyw1234567890yyyyyw1234567890yyyyyw"); MString str11 = str.MultiReplace("yw", "b"); MString str0 = str.MultiReplace("y", "y"); MString str1 = str.Substring(4); MString str2 = str.Substring(4, 10); MString str3 = str.Trim(); MString str4 = str.Trim(new char[] { 'y', 'w' }); MString str5 = str.TrimStart(new char[] { 'y', 'w' }); MString str6 = str.TrimEnd(new char[] { 'y', 'w' }); bool b0 = str.StartsWith(""); bool b1 = str.StartsWith("yw"); bool b2 = str.StartsWith("YW"); bool b3 = str.StartsWith("YW", true); bool b4 = str.StartsWith(new string[] { "y", "w" }); bool b5 = str.StartsWith(new string[] { "Y", "W" }); bool b6 = str.StartsWith(new string[] { "Y", "W" }, true); int idx1 = str.IndexOf("y"); int idx2 = str.IndexOf("w"); int idx3 = str.IndexOf("Y"); int idx4 = str.IndexOf("W"); int idx5 = str.IndexOf("W", true); int idx6 = str.IndexOf("W", 10, true); int idx7 = str.IndexOf("W", 10, 2, true); int idx8 = str.IndexOf(new string[] { "W", "Y" }); int idx9 = str.IndexOf(new string[] { "W", "Y" }, true); int idx0 = str.IndexOf(new string[] { "W", "Y" }, 10, true); MString mstr1 = str.Remove(4); MString mstr2 = str.Remove(4, 10); string[] strs0 = ((string)str).Split('y'); MString[] strs1 = str.Split('y'); string[] strs2 = ((string)str).Split('w'); MString[] strs3 = str.Split('w'); MString[] strs4 = str.MultiSplit('y'); }
protected virtual Status CheckEndPath(ref Component targetDir, ref MString fileName, MString endName, bool usePattern) { if (endName == "") { fileName = "*"; return(Status.Succeed); } fileName = endName; if (usePattern) { if (new Regex("[/:\"<>|]").IsMatch(endName)) { return(Status.Error_Path_Format); } Status status = EnterDirectory(ref targetDir, endName); if (status == Status.Succeed) { fileName = "*"; } } else { if (nameRegex.IsMatch(endName)) { return(Status.Error_Path_Format); } Status status = EnterDirectory(ref targetDir, endName); if (status == Status.Succeed) { fileName = "*"; } } return(Status.Succeed); }
public VsFile(MString name, MString exName) : base(name) { _exName = exName; }
public MakeDirCommand(MString path) { _path = path; }
protected virtual bool CreateCommand(MString cmdInfo) { return(false); }
public static void Test() { MString str1 = null; MString str2 = "def"; MString str3 = "def"; Console.WriteLine(str1 + str2); if (str2 == null) { Console.WriteLine("yes"); } else { Console.WriteLine("no"); } if (str1 == null) { Console.WriteLine("yes"); } else { Console.WriteLine("no"); } if (str1 == str3) { Console.WriteLine("yes"); } else { Console.WriteLine("no"); } if (str1 == "") { Console.WriteLine("yes"); } else { Console.WriteLine("no"); } if (str3 == "") { Console.WriteLine("yes"); } else { Console.WriteLine("no"); } if (str3 == str2) { Console.WriteLine("yes"); } else { Console.WriteLine("no"); } str3 = str1 + str2; if (str3 == str2) { Console.WriteLine("yes"); } else { Console.WriteLine("no"); } str3.Replace("e", ""); if (str3 == str2) { Console.WriteLine("yes"); } else { Console.WriteLine("no"); } Console.ReadLine(); }
private bool CheckCommand(MString cmdInfo, MString cmd, bool enableAttribute) { return(cmdInfo.StartsWith(cmd, true) && (cmdInfo.Substring(cmd.Length).TrimEnd().Length == 0 || (cmdInfo.Substring(cmd.Length).StartsWith(enableAttribute ? new string[] { " ", ".", "\\", "/" } : new string[] { " ", ".", "\\" })))); }
protected override bool CreateCommand(MString cmdInfo) { if (CheckCommand(cmdInfo, CD, false)) { MString newDir = cmdInfo.Substring(CD.Length).Trim(); _cmd = new CdCommand(newDir); } else if (CheckCommand(cmdInfo, MKDIR, false)) { MString newDir = cmdInfo.Substring(MKDIR.Length).Trim(); _cmd = new MakeDirCommand(newDir); } else if (CheckCommand(cmdInfo, RMDIR, false)) { MString newDir = cmdInfo.Substring(RMDIR.Length).Trim(); MString[] attrs = { "s" }; bool[] results = new bool[1]; if (!CheckAttribute(ref newDir, attrs, results)) { return(false); } _cmd = new RemoveDirCommand(newDir, results[0]); } else if (CheckCommand(cmdInfo, DIR, true)) { MString newDir = cmdInfo.Substring(DIR.Length).Trim(); MString[] attrs = { "ad", "s" }; bool[] results = new bool[2]; if (!CheckAttribute(ref newDir, attrs, results)) { return(false); } _cmd = new DirCommand(newDir.Trim(), results[0], results[1]); } else if (CheckCommand(cmdInfo, COPY, false)) { MString newDir = cmdInfo.Substring(COPY.Length).Trim(); MString[] paths = newDir.MultiSplit(' '); _cmd = new CopyCommand(paths); } else if (CheckCommand(cmdInfo, DEL, false)) { MString newDir = cmdInfo.Substring(DEL.Length).Trim(); MString[] paths = newDir.MultiSplit(' '); _cmd = new DeleteFileCommand(paths); } else if (CheckCommand(cmdInfo, COMPARE, false)) { MString newDir = cmdInfo.Substring(COMPARE.Length).Trim(); MString[] paths = newDir.MultiSplit(' '); _cmd = new CompareCommand(paths); } else { int length = ((string)cmdInfo).IndexOf(" "); if (length == -1) { length = cmdInfo.Length; } Logger.Log(Status.Error_Commond, cmdInfo.Substring(0, length)); return(false); } return(true); }
public DirCommand(MString path, bool formatAD, bool formatS) { _path = path; _formatAD = formatAD; _formatS = formatS; }
public RemoveDirCommand(MString path, bool formatS) { _path = path; _formatS = formatS; }
public override void Excute() { if (_paths.Length < 2) { Logger.Log(Status.Error_Commond_Format); return; } MString sourcePath = _paths[0].Replace("\"", "").MultiReplace("\\", "\\"); if (!File.Exists(sourcePath)) { Logger.Log(Status.Error_Path_Not_Found); return; } MString destPath = _paths[1]; Component destDir; MString lastDestPath; Status status = CheckPath(ref destPath, out destDir, out lastDestPath, false); if (status != Status.Succeed) { Logger.Log(status); return; } if (lastDestPath == "*") { Logger.Log(Status.Error_Path_Format); } VsFile destFile = destDir.GetFile(lastDestPath); if (destFile == null) { Logger.Log(Status.Error_Path_Not_Found); } Console.WriteLine("正在比较文件 " + sourcePath + " 和 " + destPath); byte[] sourceBuffer = FileUtils.GetFileBuffer(sourcePath); byte[] destBuffer = destFile.Buffer; int differentIndex = -1; int index = 0; while (index < sourceBuffer.Length && index < destBuffer.Length) { if (sourceBuffer[index] != destBuffer[index]) { differentIndex = index; break; } index++; } if (sourceBuffer.Length == index && destBuffer.Length == index) { Console.WriteLine("内容比较一致。"); } else { Console.WriteLine("***** " + sourcePath); OutputFileContent(sourceBuffer, differentIndex); Console.WriteLine("***** " + destPath); OutputFileContent(destBuffer, differentIndex); } }
public CdCommand(MString path) { _path = path; }
public VsDirectory(MString name) : base(name) { }
public Component(MString name) { _name = name; RefreshTime(); }