public void Render() { // TO DO: REWRITE THIS CLASS! and maybe the whole process of // getting data from the lower classes, why convert buffers // to XML just to convert them again in this Render method? // ALSO: this conversion process does not take into account that // the XML data that this class is converted from might // contain more than _CX characters in a line, since the // previous conversion to XML converts '<' to "<" and // the like, which will also cause shifts in character positions. // // Reset cache // _stringValueCache = null; // if (_CX == 0 || _CY == 0) { // TODO: Need to fix this _CX = 132; _CY = 43; } // CFCJr 2008/07/11 if (_CX < 80) { _CX = 80; } if (_CY < 25) { _CY = 25; } // CFCJr 2008/07/11 if (_CX < 80) { _CX = 80; } if (_CY < 25) { _CY = 25; } UserIdentified = null; MatchListIdentified = null; // // Render text image of screen // // mScreenBuffer = new char[_CX * _CY]; mScreenRows = new string[_CY]; // CFCJr 2008/07/11 // The following might be much faster: // // string str = "".PadRight(_CX*_CY, ' '); // mScreenBuffer = str.ToCharArray(); // ........do operations on mScreenBuffer to fill it...... // str = string.FromCharArray(mScreenBuffer); // for (int r = 0; r < _CY; r++) // mScreenRows[i] = str.SubString(r*_CY,_CX); // // ie, fill mScreenBuffer with the data from Unformatted and Field, then // create str (for the hash) and mScreenRows[] // with the result. int i; for (i = 0; i < mScreenBuffer.Length; i++) // CFCJr. 2008.07/11 replase _CX*CY with mScreenBuffer.Length { mScreenBuffer[i] = ' '; } // int chindex; if (Field == null || Field.Length == 0 && (Unformatted == null || Unformatted.Text == null)) { if ((Unformatted == null || Unformatted.Text == null)) { Console.WriteLine("XMLScreen:Render: **BUGBUG** XMLScreen.Unformatted screen is blank"); } else { Console.WriteLine("XMLScreen:Render: **BUGBUG** XMLScreen.Field is blank"); } Console.Out.Flush(); // CFCJr. Move logic for what is in mScreenRows to seperate if logic // this will give unformatted results even if Field==null or 0 length // and vise-a-versa. /* * for (i=0; i<mScreenRows.Length; i++) * { * mScreenRows[i] = new String(' ',_CX); * } */ } string blankRow = string.Empty; blankRow = blankRow.PadRight(_CX, ' '); if ((Unformatted == null || Unformatted.Text == null)) { // CFCJr. 2008/07/11 initilize a blank row of _CX (80?) spaces for (i = 0; i < mScreenRows.Length; i++) { //mScreenRows[i] = " ".Substring(0, _CX); // CFCJr. 2008/07/11 replace above method of 80 spaces with following mScreenRows[i] = blankRow; } } else { for (i = 0; i < Unformatted.Text.Length; i++) { string text = Unformatted.Text[i]; // CFCJr, make sure text is not null if (string.IsNullOrEmpty(text)) { text = string.Empty; } // CFCJr, replace "<" with '<' text = text.Replace("<", "<"); // CFCJr, Remove this loop to pad text // and use text.PadRight later. // This will help in not processing more // characters than necessary into mScreenBuffer // below //while (text.Length < _CX) // text+=" "; // int p; //for (p=0; p<_CX; p++) for (p = 0; p < text.Length; p++) // CFC,Jr. { if (text[p] < 32 || text[p] > 126) { text = text.Replace(text[p], ' '); } } // //for (chindex=0; chindex<Unformatted.Text[i].Length; chindex++) // CFCJr, 2008/07/11 use text.length instead of Unformatted.Text[i].Length // since we only pad text with 80 chars but if Unformatted.Text[i] // contains XML codes (ie, "<") then it could be longer than // 80 chars (hence, longer than text). // Also, I replace "<" above with "<". for (chindex = 0; chindex < text.Length; chindex++) { // CFCJr, calculate mScreenBuffer index only once int bufNdx = chindex + (i * _CX); if (bufNdx < mScreenBuffer.Length) { mScreenBuffer[bufNdx] = text[chindex]; } } // CFCJr, make sure we don't overflow the index of mScreenRows // since i is based on the dimensions of Unformatted.Text // instead of mScreenRows.Length if (i < mScreenRows.Length) { text = text.PadRight(_CX, ' '); // CFCJr. 2008/07/11 use PadRight instead of loop above mScreenRows[i] = text; } } } // CFCJr, lets make sure we have _CY rows in mScreenRows here // since we use Unformated.Text.Length for loop above which // could possibly be less than _CY. for (i = 0; i < mScreenRows.Length; i++) { if (string.IsNullOrEmpty(mScreenRows[i])) { mScreenRows[i] = blankRow; } } //============== // Now process the Field (s) if (Field != null && Field.Length > 0) { // // Now superimpose the formatted fields on the unformatted base // for (i = 0; i < Field.Length; i++) { XMLScreenField field = Field[i]; if (field.Text != null) { for (chindex = 0; chindex < field.Text.Length; chindex++) { char ch = field.Text[chindex]; if (ch < 32 || ch > 126) { ch = ' '; } // CFCJr, 2008/07/11 make sure we don't get out of bounds // of the array m_ScreenBuffer. int bufNdx = chindex + field.Location.left + field.Location.top * _CX; if (bufNdx >= 0 && bufNdx < mScreenBuffer.Length) { mScreenBuffer[bufNdx] = ch; } } } } // CFCJr, 2008/07/11 // SOMETHING needs to be done in this method to speed things up. // Above, in the processing of the Unformatted.Text, Render() // goes to the trouble of loading up mScreenBuffer and mScreenRows. // now here, we replace mScreenRows with the contents of mScreenBuffer. // Maybe, we should only load mScreenBuffer and then at the end // of Render(), load mScreenRows from it (or vise-a-vera). // WE COULD ALSO use // mScreenRows[i] = string.FromCharArraySubset(mScreenBuffer, i*_CX, _CX); // inside this loop. for (i = 0; i < _CY; i++) { string temp = string.Empty; // CFCJr, 2008/07/11 replace "" for (int j = 0; j < _CX; j++) { temp += mScreenBuffer[i * _CX + j]; } mScreenRows[i] = temp; } } // now calculate our screen's hash // // CFCJr, dang, now we're going to copy the data again, // this time into a long string.....(see comments at top of Render()) // I bet there's a easy way to redo this class so that we use just // one buffer (string or char[]) instead of all these buffers. // WE COULD also use // string hashStr = string.FromCharArray(mScreenBuffer); // instead of converting mScreenRows to StringBuilder // and then converting it to a string. HashAlgorithm hash = (HashAlgorithm)CryptoConfig.CreateFromName("MD5"); StringBuilder builder = new StringBuilder(); for (i = 0; i < mScreenRows.Length; i++) { builder.Append(mScreenRows[i]); } byte[] myHash = hash.ComputeHash(new UnicodeEncoding().GetBytes(builder.ToString())); this.Hash = BitConverter.ToString(myHash); this._ScreenGuid = Guid.NewGuid(); }
public void Render() { // // Reset cache // _stringValueCache = null; // if (_CX == 0 || _CY == 0) { // TODO: Need to fix this _CX = 132; _CY = 43; } UserIdentified = null; MatchListIdentified = null; // // Render text image of screen // // mScreenBuffer = new char[_CX * _CY]; mScreenRows = new string[_CY]; int i; for (i = 0; i < _CX * _CY; i++) { mScreenBuffer[i] = ' '; } // int chindex; if (Field == null || Field.Length == 0 && (Unformatted == null || Unformatted.Text == null)) { //Console.WriteLine("**BUGBUG** Unformatted screen is blank"); for (i = 0; i < mScreenRows.Length; i++) { mScreenRows[i] = new String(' ', _CX); } } else { for (i = 0; i < Unformatted.Text.Length; i++) { string text = Unformatted.Text[i]; while (text.Length < _CX) { text += " "; } // int p; for (p = 0; p < _CX; p++) { if (text[p] < 32 || text[p] > 126) { text = text.Replace(text[p], ' '); } } // for (chindex = 0; chindex < Unformatted.Text[i].Length; chindex++) { if ((chindex + i * _CX) < mScreenBuffer.Length) { mScreenBuffer[chindex + i * _CX] = text[chindex]; } } if (i >= 0 && i < mScreenRows.Length) { mScreenRows[i] = text; } } // // Now superimpose the formatted fields on the unformatted base // for (i = 0; i < Field.Length; i++) { XMLScreenField field = Field[i]; if (field.Text != null) { for (chindex = 0; chindex < field.Text.Length; chindex++) { char ch = field.Text[chindex]; if (ch < 32 || ch > 126) { ch = ' '; } int off = chindex + field.Location.left + field.Location.top * _CX; if (off >= 0 && off < mScreenBuffer.Length) { mScreenBuffer[chindex + field.Location.left + field.Location.top * _CX] = ch; } } } } for (i = 0; i < _CY; i++) { string temp = ""; for (int j = 0; j < _CX; j++) { temp += mScreenBuffer[i * _CX + j]; } mScreenRows[i] = temp; } } // now calculate our screen's hash // HashAlgorithm hash = (HashAlgorithm)CryptoConfig.CreateFromName("MD5"); StringBuilder builder = new StringBuilder(); for (i = 0; i < mScreenRows.Length; i++) { builder.Append(mScreenRows[i]); } byte[] myHash = hash.ComputeHash(new UnicodeEncoding().GetBytes(builder.ToString())); this.Hash = BitConverter.ToString(myHash); this._ScreenGuid = Guid.NewGuid(); }