/// <summary> /// "Splits" file array to slices by calculating each /// slice start and end index. /// </summary> /// <param name="arrLen">file array length</param> /// <param name="sliceCount">how many slices</param> /// <returns>Array of slice start and end indexes</returns> private ArraySlice[] GetSliceIndexes(int arrLen, int sliceCount) { var chunks = new ArraySlice[sliceCount]; int chunkSize = (int)Math.Ceiling((double)arrLen / sliceCount); int chunkIndex = 0; // chunk array (result array) index int arrIndex = 0; while (arrIndex < arrLen) { chunks[chunkIndex] = new ArraySlice(arrIndex, arrIndex += chunkSize); arrIndex++; // bump index for next loop iteration chunkIndex++; } //last index most likely is out of range, lets fix that chunks[sliceCount - 1].End = arrLen - 1; return(chunks); }
/// <summary> /// Instantiates Arguments object /// </summary> /// <param name="cmdArgs">commandline arguments</param> public Arguments(string[] cmdArgs) { if (cmdArgs.Contains("-help")) { HelpRequested = true; return; } else if (cmdArgs.Length < 3) { throw new InsufficienArgumentsException(); } else { FileLocation = cmdArgs[0]; StartFormat = cmdArgs[1]; TargetFormat = cmdArgs[2]; if (cmdArgs.Length > 3) { var extraParams = new ArraySlice(3, cmdArgs.Length - 1); ParseSettingsArgs(extraParams, cmdArgs); } } }
/// <summary> /// Parses setting arguments /// </summary> /// <param name="settingSlice">slice containing setting arguments</param> /// <param name="args">commandline args</param> /// <exception cref="MultipleArgumentException"></exception> /// <exception cref="UnknownArgumentException"></exception> private void ParseSettingsArgs(ArraySlice settingSlice, string[] args) { for (int i = settingSlice.Start; i <= settingSlice.End; i++) { switch (args[i]) { case "-help": if (HelpRequested) { throw new MultipleArgumentException() { Argument = args[i] } } ; HelpRequested = true; break; case "-R": goto case "-r"; case "-r": if (RecursiveFileSearch) { throw new MultipleArgumentException() { Argument = args[i] } } ; else { RecursiveFileSearch = true; } break; case "-T": goto case "-t"; case "-t": if (MultiThreadRequested) { throw new MultipleArgumentException() { Argument = args[i] } } ; else { MultiThreadRequested = true; } break; case "-l": goto case "-log"; case "-log": if (LogEachFile) { throw new MultipleArgumentException() { Argument = args[i] } } ; else { LogEachFile = true; } break; default: throw new UnknownArgumentException() { Argument = args[i] }; } } }