static bool ReadFile(string FileName, WordMaker word, LineMaker line) { bool fileExists = OpenFile(FileName, out StreamReader Reader); if (!fileExists) { return(false); } word.FileToRead = Reader; int input = word.FileToRead.Read(); //initialize input variable, if read char is non-white, is passed from EatSpaces fun. to ReadWord fun word.EatSpaces(ref input); //eat eventual whitespaces at the begining of file, not taking into account columns at the beggining while (input != -1) { word.Clear(); word.ReadWord(ref input); word.EatSpaces(ref input); line.BuildLine(word); } line.FlushToFile(); word.FileToRead.Close(); return(true); }
/// <summary> /// trys to add word on line, if it fits, returns true, if not, adds line to output, clears it and adds word to next line /// </summary> /// <param name="Word">Word to be added</param> /// <returns></returns> public bool BuildLine(WordMaker Word) { bool added = TryAddWord(Word.Word); if (added && !Word.EndOfColumn && !Word.EndOfFinalFile) { return(false); //not finished line } else if (!added && (Word.EndOfColumn || Word.EndOfFinalFile)) //special case, word doesnt fit to previous line and it ends column at the same time { string ToPrint = PrepareLine(false); //column ends after not fitting word just read and stored in Word variable //I need to print what is stored in line first AppendLine(ToPrint); this.WordsOnline.Clear(); this.TryAddWord(Word.Word); //add non fitting on new line ToPrint = PrepareLine(true); //printing not fitting word and ending column AppendLine(ToPrint); if (!Word.EndOfFinalFile) //last line of file { MakeNewLine(); } this.WordWaiting = ""; //not fitting word was stored in WordWaiting even though it has been just printed } else if (Word.EndOfColumn || Word.EndOfFinalFile) //last line of Column { string ToPrint = PrepareLine(true); //add spaces between words AppendLine(ToPrint); if (!Word.EndOfFinalFile) //last line of file { MakeNewLine(); } } else //finished line { string ToPrint = PrepareLine(Word.EndOfColumn); //add spaces between words AppendLine(ToPrint); } this.WordsOnline.Clear(); if (this.WordWaiting != "") { this.WordsOnline.Add(WordWaiting); //add word that did not fit previous line to next line } this.WordWaiting = ""; return(true); //line finished }
public static void Main(string[] args) { bool check = InitialCheck(args, out bool HiglightSpace, out StreamWriter Output, out int WidhtOfLine); if (check == false) //problem with arguments or output file { return; } LineMaker line = new LineMaker(WidhtOfLine, Output, HiglightSpace); WordMaker word = new WordMaker(); int i = 0; int flawedFiles = 0; //counter of input files that cannot be opened if (HiglightSpace) //is arg0 file name or --HighlightSpaces? { i = 1; flawedFiles = 1; } for (; i < args.Length - 2; i++) { if (i == (args.Length - 3)) { word.FinalFile = true; } if (!ReadFile(args[i], word, line)) { flawedFiles++; } } if (flawedFiles == (args.Length - 2)) // all input files are flawed { line.LineInEmptyFile(); line.FlushToFile(); } line.PushToWriter(); line.FlushToFile(); line.CloseWriter(); }