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'); }
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); }