corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * CVS identifier:
3 *
4 * $Id: MsgPrinter.java,v 1.6 2000/09/05 09:25:24 grosbois Exp $
5 *
6 * Class: MsgPrinter
7 *
8 * Description: Prints messages formatted for a specific
9 * line width.
10 *
11 *
12 *
13 * COPYRIGHT:
14 *
15 * This software module was originally developed by Raphaël Grosbois and
16 * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel
17 * Askelöf (Ericsson Radio Systems AB); and Bertrand Berthelot, David
18 * Bouchard, Félix Henry, Gerard Mozelle and Patrice Onno (Canon Research
19 * Centre France S.A) in the course of development of the JPEG2000
20 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This
21 * software module is an implementation of a part of the JPEG 2000
22 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio
23 * Systems AB and Canon Research Centre France S.A (collectively JJ2000
24 * Partners) agree not to assert against ISO/IEC and users of the JPEG
25 * 2000 Standard (Users) any of their rights under the copyright, not
26 * including other intellectual property rights, for this software module
27 * with respect to the usage by ISO/IEC and Users of this software module
28 * or modifications thereof for use in hardware or software products
29 * claiming conformance to the JPEG 2000 Standard. Those intending to use
30 * this software module in hardware or software products are advised that
31 * their use may infringe existing patents. The original developers of
32 * this software module, JJ2000 Partners and ISO/IEC assume no liability
33 * for use of this software module or modifications thereof. No license
34 * or right to this software module is granted for non JPEG 2000 Standard
35 * conforming products. JJ2000 Partners have full right to use this
36 * software module for his/her own purpose, assign or donate this
37 * software module to any third party and to inhibit third parties from
38 * using this software module for non JPEG 2000 Standard conforming
39 * products. This copyright notice must be included in all copies or
40 * derivative works of this software module.
41 *
42 * Copyright (c) 1999/2000 JJ2000 Partners.
43 *
44 *
45 *
46 */
47 using System;
48 namespace CSJ2K.j2k.util
49 {
50  
51 /// <summary> This utility class formats messages to the specified line width, by
52 /// inserting line-breaks between words, and printing the resulting
53 /// lines.
54 ///
55 /// </summary>
56 public class MsgPrinter
57 {
58 //UPGRADE_NOTE: Respective javadoc comments were merged. It should be changed in order to comply with .NET documentation conventions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1199'"
59 /// <summary> Returns the line width that is used for formatting.
60 ///
61 /// </summary>
62 /// <returns> The line width used for formatting
63 ///
64 ///
65 ///
66 /// </returns>
67 /// <summary> Sets the line width to the specified value. This new value will
68 /// be used in subsequent calls to the print() message.
69 ///
70 /// </summary>
71 /// <param name="linewidth">The new line width to use (in cahracters)
72 ///
73 ///
74 ///
75 /// </param>
76 virtual public int LineWidth
77 {
78 get
79 {
80 return lw;
81 }
82  
83 set
84 {
85 if (value < 1)
86 {
87 throw new System.ArgumentException();
88 }
89 lw = value;
90 }
91  
92 }
93  
94 /// <summary>The line width to use </summary>
95 public int lw;
96  
97 /// <summary>Signals that a newline was found </summary>
98 private const int IS_NEWLINE = - 2;
99  
100 /// <summary>Signals that the end-of-string was reached </summary>
101 private const int IS_EOS = - 1;
102  
103 /// <summary> Creates a new message printer with the specified line width and
104 /// with the default locale.
105 ///
106 /// </summary>
107 /// <param name="linewidth">The line width for which to format (in
108 /// characters)
109 ///
110 ///
111 ///
112 /// </param>
113 public MsgPrinter(int linewidth)
114 {
115 lw = linewidth;
116 }
117  
118 /// <summary> Formats the message to print in the current line width, by
119 /// breaking the message into lines between words. The number of
120 /// spaces to indent the first line is specified by 'flind' and the
121 /// number of spaces to indent each of the following lines is
122 /// specified by 'ind'. Newlines in 'msg' are respected. A newline is
123 /// always printed at the end.
124 ///
125 /// </summary>
126 /// <param name="out">Where to print the message.
127 ///
128 /// </param>
129 /// <param name="flind">The indentation for the first line.
130 ///
131 /// </param>
132 /// <param name="ind">The indentation for the other lines.
133 ///
134 /// </param>
135 /// <param name="msg">The message to format and print.
136 ///
137 ///
138 ///
139 /// </param>
140 public virtual void print(System.IO.StreamWriter out_Renamed, int flind, int ind, System.String msg)
141 {
142 int start, end, pend, efflw, lind, i;
143  
144 start = 0;
145 end = 0;
146 pend = 0;
147 efflw = lw - flind;
148 lind = flind;
149 while ((end = nextLineEnd(msg, pend)) != IS_EOS)
150 {
151 if (end == IS_NEWLINE)
152 {
153 // Forced line break
154 for (i = 0; i < lind; i++)
155 {
156 out_Renamed.Write(" ");
157 }
158 //UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'"
159 out_Renamed.WriteLine(msg.Substring(start, (pend) - (start)));
160 if (nextWord(msg, pend) == msg.Length)
161 {
162 // Traling newline => print it and done
163 //UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'"
164 out_Renamed.WriteLine("");
165 start = pend;
166 break;
167 }
168 }
169 else
170 {
171 if (efflw > end - pend)
172 {
173 // Room left on current line
174 efflw -= (end - pend);
175 pend = end;
176 continue;
177 }
178 else
179 {
180 // Filled-up current line => print it
181 for (i = 0; i < lind; i++)
182 {
183 out_Renamed.Write(" ");
184 }
185 if (start == pend)
186 {
187 // Word larger than line width
188 // Print anyways
189 //UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'"
190 out_Renamed.WriteLine(msg.Substring(start, (end) - (start)));
191 pend = end;
192 }
193 else
194 {
195 //UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'"
196 out_Renamed.WriteLine(msg.Substring(start, (pend) - (start)));
197 }
198 }
199 }
200 // Initialize for next line
201 lind = ind;
202 efflw = lw - ind;
203 start = nextWord(msg, pend);
204 pend = start;
205 if (start == IS_EOS)
206 {
207 break; // Did all the string
208 }
209 }
210 if (pend != start)
211 {
212 // Part of a line left => print it
213 for (i = 0; i < lind; i++)
214 {
215 out_Renamed.Write(" ");
216 }
217 //UPGRADE_TODO: Method 'java.io.PrintWriter.println' was converted to 'System.IO.TextWriter.WriteLine' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javaioPrintWriterprintln_javalangString'"
218 out_Renamed.WriteLine(msg.Substring(start, (pend) - (start)));
219 }
220 }
221  
222 /// <summary> Returns the index of the last character of the next word, plus 1, or
223 /// IS_NEWLINE if a newline character is encountered before the next word,
224 /// or IS_EOS if the end of the string is ecnounterd before the next
225 /// word. The method first skips all whitespace characters at or after
226 /// 'from', except newlines. If a newline is found IS_NEWLINE is
227 /// returned. Then it skips all non-whitespace characters and returns the
228 /// position of the last non-whitespace character, plus 1. The returned
229 /// index may be greater than the last valid index in the tsring, but it is
230 /// always suitable to be used in the String.substring() method.
231 ///
232 /// <P>Non-whitespace characters are defined as in the
233 /// Character.isWhitespace method (that method is used).
234 ///
235 /// </summary>
236 /// <param name="str">The string to parse
237 ///
238 /// </param>
239 /// <param name="from">The index of the first position to search from
240 ///
241 /// </param>
242 /// <returns> The index of the last character in the next word, plus 1,
243 /// IS_NEWLINE, or IS_EOS if there are no more words.
244 ///
245 ///
246 ///
247 /// </returns>
248 private int nextLineEnd(System.String str, int from)
249 {
250 //UPGRADE_NOTE: Final was removed from the declaration of 'len '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
251 int len = str.Length;
252 char c = '\x0000';
253 // First skip all whitespace, except new line
254 while (from < len && (c = str[from]) != '\n' && System.Char.IsWhiteSpace(c))
255 {
256 from++;
257 }
258 if (c == '\n')
259 {
260 return IS_NEWLINE;
261 }
262 if (from >= len)
263 {
264 return IS_EOS;
265 }
266 // Now skip word characters
267 while (from < len && !System.Char.IsWhiteSpace(str[from]))
268 {
269 from++;
270 }
271 return from;
272 }
273  
274 /// <summary> Returns the position of the first character in the next word, starting
275 /// from 'from', if a newline is encountered first then the index of the
276 /// newline character plus 1 is returned. If the end of the string is
277 /// encountered then IS_EOS is returned. Words are defined as any
278 /// concatenation of 1 or more characters which are not
279 /// whitespace. Whitespace characters are those for which
280 /// Character.isWhitespace() returns true (that method is used).
281 ///
282 /// <P>Non-whitespace characters are defined as in the
283 /// Character.isWhitespace method (that method is used).
284 ///
285 /// </summary>
286 /// <param name="str">The string to parse
287 ///
288 /// </param>
289 /// <param name="from">The index where to start parsing
290 ///
291 /// </param>
292 /// <returns> The index of the first character of the next word, or the index
293 /// of the newline plus 1, or IS_EOS.
294 ///
295 ///
296 ///
297 /// </returns>
298 private int nextWord(System.String str, int from)
299 {
300 //UPGRADE_NOTE: Final was removed from the declaration of 'len '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
301 int len = str.Length;
302 char c = '\x0000';
303 // First skip all whitespace, but new lines
304 while (from < len && (c = str[from]) != '\n' && System.Char.IsWhiteSpace(c))
305 {
306 from++;
307 }
308 if (from >= len)
309 {
310 return IS_EOS;
311 }
312 else if (c == '\n')
313 {
314 return from + 1;
315 }
316 else
317 {
318 return from;
319 }
320 }
321 }
322 }