PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/usr/src/cmd/cmd-inet/usr.sadm/dhcpmgr/com/sun/dhcpmgr/cli/common/Format.java

https://bitbucket.org/jimklimov/illumos-gate
Java | 608 lines | 387 code | 63 blank | 158 comment | 222 complexity | 9ac1d6da7d55165d2f0f9d1d14bdee44 MD5 | raw file
Possible License(s): BSD-3-Clause-No-Nuclear-License-2014, AGPL-1.0, AGPL-3.0, GPL-2.0, GPL-3.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.1, LGPL-3.0, LGPL-2.0, 0BSD
  1. /*
  2. * CDDL HEADER START
  3. *
  4. * The contents of this file are subject to the terms of the
  5. * Common Development and Distribution License, Version 1.0 only
  6. * (the "License"). You may not use this file except in compliance
  7. * with the License.
  8. *
  9. * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10. * or http://www.opensolaris.org/os/licensing.
  11. * See the License for the specific language governing permissions
  12. * and limitations under the License.
  13. *
  14. * When distributing Covered Code, include this CDDL HEADER in each
  15. * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16. * If applicable, add the following below this CDDL HEADER, with the
  17. * fields enclosed by brackets "[]" replaced with your own identifying
  18. * information: Portions Copyright [yyyy] [name of copyright owner]
  19. *
  20. * CDDL HEADER END
  21. */
  22. /*
  23. * ident "%Z%%M% %I% %E% SMI"
  24. *
  25. * Copyright (c) 2001 by Sun Microsystems, Inc.
  26. * All rights reserved.
  27. */
  28. /*
  29. * Cay S. Horstmann & Gary Cornell, Core Java
  30. * Published By Sun Microsystems Press/Prentice-Hall
  31. * Copyright (C) 1997 Sun Microsystems Inc.
  32. * All Rights Reserved.
  33. *
  34. * Permission to use, copy, modify, and distribute this
  35. * software and its documentation for NON-COMMERCIAL purposes
  36. * and without fee is hereby granted provided that this
  37. * copyright notice appears in all copies.
  38. *
  39. * THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR
  40. * WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER
  41. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  43. * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS
  44. * AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED
  45. * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  46. * THIS SOFTWARE OR ITS DERIVATIVES.
  47. */
  48. /**
  49. * A class for formatting numbers that follows printf conventions.
  50. * Also implements C-like atoi and atof functions
  51. * @version 1.01 15 Feb 1996
  52. * @author Cay Horstmann
  53. */
  54. package com.sun.dhcpmgr.cli.common;
  55. import java.io.*;
  56. public class Format
  57. {
  58. /**
  59. * Formats the number following printf conventions.
  60. * Main limitation: Can only handle one format parameter at a time
  61. * Use multiple Format objects to format more than one number
  62. * @param s the format string following printf conventions
  63. * The string has a prefix, a format code and a suffix. The prefix and
  64. * suffix become part of the formatted output. The format code directs the
  65. * formatting of the (single) parameter to be formatted. The code has the
  66. * following structure
  67. * <ul>
  68. * <li> a % (required)
  69. * <li> a modifier (optional)
  70. * <dl>
  71. * <dt> + <dd> forces display of + for positive numbers
  72. * <dt> 0 <dd> show leading zeroes
  73. * <dt> - <dd> align left in the field
  74. * <dt> space <dd> prepend a space in front of positive numbers
  75. * <dt> # <dd> use "alternate" format. Add 0 or 0x for octal or hexadecimal
  76. * numbers. Don't suppress trailing zeroes in general floating point format.
  77. * </dl>
  78. * <li> an integer denoting field width (optional)
  79. * <li> a period followed by an integer denoting precision (optional)
  80. * <li> a format descriptor (required)
  81. * <dl>
  82. * <dt>f <dd> floating point number in fixed format
  83. * <dt>e, E <dd> floating point number in exponential notation (scientific
  84. * format). The E format results in an uppercase E for the exponent
  85. * (1.14130E+003), the e format in a lowercase e.
  86. * <dt>g, G <dd> floating point number in general format (fixed format for
  87. * small numbers, exponential format for large numbers). Trailing zeroes
  88. * are suppressed. The G format results in an uppercase E for the exponent
  89. * (if any), the g format in a lowercase e.
  90. * <dt>d, i <dd> integer in decimal
  91. * <dt>x <dd> integer in hexadecimal
  92. * <dt>o <dd> integer in octal
  93. * <dt>s <dd> string
  94. * <dt>c <dd> character
  95. * </dl>
  96. * </ul>
  97. * @exception IllegalArgumentException if bad format
  98. */
  99. public Format(String s) {
  100. width = 0;
  101. precision = -1;
  102. pre = "";
  103. post = "";
  104. leading_zeroes = false;
  105. show_plus = false;
  106. alternate = false;
  107. show_space = false;
  108. left_align = false;
  109. fmt = ' ';
  110. int state = 0;
  111. int length = s.length();
  112. int parse_state = 0;
  113. // 0 = prefix, 1 = flags, 2 = width, 3 = precision,
  114. // 4 = format, 5 = end
  115. int i = 0;
  116. while (parse_state == 0) {
  117. if (i >= length) {
  118. parse_state = 5;
  119. } else if (s.charAt(i) == '%') {
  120. if (i < length - 1) {
  121. if (s.charAt(i + 1) == '%') {
  122. pre = pre + '%';
  123. i++;
  124. } else {
  125. parse_state = 1;
  126. }
  127. } else {
  128. throw new java.lang.IllegalArgumentException();
  129. }
  130. } else {
  131. pre = pre + s.charAt(i);
  132. }
  133. i++;
  134. }
  135. while (parse_state == 1) {
  136. if (i >= length) {
  137. parse_state = 5;
  138. } else if (s.charAt(i) == ' ') {
  139. show_space = true;
  140. } else if (s.charAt(i) == '-') {
  141. left_align = true;
  142. } else if (s.charAt(i) == '+') {
  143. show_plus = true;
  144. } else if (s.charAt(i) == '0') {
  145. leading_zeroes = true;
  146. } else if (s.charAt(i) == '#') {
  147. alternate = true;
  148. } else {
  149. parse_state = 2; i--;
  150. }
  151. i++;
  152. }
  153. while (parse_state == 2) {
  154. if (i >= length) {
  155. parse_state = 5;
  156. } else if ('0' <= s.charAt(i) && s.charAt(i) <= '9') {
  157. width = width * 10 + s.charAt(i) - '0';
  158. i++;
  159. } else if (s.charAt(i) == '.') {
  160. parse_state = 3;
  161. precision = 0;
  162. i++;
  163. } else {
  164. parse_state = 4;
  165. }
  166. }
  167. while (parse_state == 3) {
  168. if (i >= length) {
  169. parse_state = 5;
  170. } else if ('0' <= s.charAt(i) && s.charAt(i) <= '9') {
  171. precision = precision * 10 + s.charAt(i) - '0';
  172. i++;
  173. } else {
  174. parse_state = 4;
  175. }
  176. }
  177. if (parse_state == 4) {
  178. if (i >= length) {
  179. parse_state = 5;
  180. } else {
  181. fmt = s.charAt(i);
  182. }
  183. i++;
  184. }
  185. if (i < length) {
  186. post = s.substring(i, length);
  187. }
  188. }
  189. /**
  190. * prints a formatted number following printf conventions
  191. * @param s a PrintStream
  192. * @param fmt the format string
  193. * @param x the double to print
  194. */
  195. public static void print(java.io.PrintStream s, String fmt, double x) {
  196. s.print(new Format(fmt).form(x));
  197. }
  198. /**
  199. * prints a formatted number following printf conventions
  200. * @param s a PrintStream
  201. * @param fmt the format string
  202. * @param x the long to print
  203. */
  204. public static void print(java.io.PrintStream s, String fmt, long x) {
  205. s.print(new Format(fmt).form(x));
  206. }
  207. /**
  208. * prints a formatted number following printf conventions
  209. * @param s a PrintStream
  210. * @param fmt the format string
  211. * @param x the character to
  212. */
  213. public static void print(java.io.PrintStream s, String fmt, char x) {
  214. s.print(new Format(fmt).form(x));
  215. }
  216. /**
  217. * prints a formatted number following printf conventions
  218. * @param s a PrintStream, fmt the format string
  219. * @param x a string that represents the digits to print
  220. */
  221. public static void print(java.io.PrintStream s, String fmt, String x) {
  222. s.print(new Format(fmt).form(x));
  223. }
  224. /**
  225. * Converts a string of digits(decimal, octal or hex) to an integer
  226. * @param s a string
  227. * @return the numeric value of the prefix of s representing a base
  228. * 10 integer
  229. */
  230. public static int atoi(String s) {
  231. return (int)atol(s);
  232. }
  233. /**
  234. * Converts a string of digits(decimal, octal or hex) to a long integer
  235. * @param s a string
  236. * @return the numeric value of the prefix of s representing a base
  237. * 10 integer
  238. */
  239. public static long atol(String s) {
  240. int i = 0;
  241. while (i < s.length() && Character.isWhitespace(s.charAt(i))) {
  242. i++;
  243. }
  244. if (i < s.length() && s.charAt(i) == '0') {
  245. if (i + 1 < s.length() && (s.charAt(i + 1) == 'x' ||
  246. s.charAt(i + 1) == 'X')) {
  247. return parseLong(s.substring(i + 2), 16);
  248. } else {
  249. return parseLong(s, 8);
  250. }
  251. } else {
  252. return parseLong(s, 10);
  253. }
  254. }
  255. private static long parseLong(String s, int base) {
  256. int i = 0;
  257. int sign = 1;
  258. long r = 0;
  259. while (i < s.length() && Character.isWhitespace(s.charAt(i))) {
  260. i++;
  261. }
  262. if (i < s.length() && s.charAt(i) == '-') {
  263. sign = -1; i++;
  264. } else if (i < s.length() && s.charAt(i) == '+') {
  265. i++;
  266. }
  267. while (i < s.length()) {
  268. char ch = s.charAt(i);
  269. if ('0' <= ch && ch < '0' + base) {
  270. r = r * base + ch - '0';
  271. } else if ('A' <= ch && ch < 'A' + base - 10) {
  272. r = r * base + ch - 'A' + 10;
  273. } else if ('a' <= ch && ch < 'a' + base - 10) {
  274. r = r * base + ch - 'a' + 10;
  275. } else {
  276. return r * sign;
  277. }
  278. i++;
  279. }
  280. return r * sign;
  281. }
  282. /**
  283. * Converts a string of digits to an double
  284. * @param s a string
  285. */
  286. public static double atof(String s) {
  287. int i = 0;
  288. int sign = 1;
  289. double r = 0; // integer part
  290. double f = 0; // fractional part
  291. double p = 1; // exponent of fractional part
  292. int state = 0; // 0 = int part, 1 = frac part
  293. while (i < s.length() && Character.isWhitespace(s.charAt(i))) {
  294. i++;
  295. }
  296. if (i < s.length() && s.charAt(i) == '-') {
  297. sign = -1; i++;
  298. } else if (i < s.length() && s.charAt(i) == '+') {
  299. i++;
  300. }
  301. while (i < s.length()) {
  302. char ch = s.charAt(i);
  303. if ('0' <= ch && ch <= '9') {
  304. if (state == 0) {
  305. r = r * 10 + ch - '0';
  306. } else if (state == 1) {
  307. p = p / 10;
  308. r = r + p * (ch - '0');
  309. }
  310. } else if (ch == '.') {
  311. if (state == 0) {
  312. state = 1;
  313. } else {
  314. return sign * r;
  315. }
  316. } else if (ch == 'e' || ch == 'E') {
  317. long e = (int)parseLong(s.substring(i + 1), 10);
  318. return sign * r * Math.pow(10, e);
  319. } else {
  320. return sign * r;
  321. }
  322. i++;
  323. }
  324. return sign * r;
  325. }
  326. /**
  327. * Formats a double into a string (like sprintf in C)
  328. * @param x the number to format
  329. * @return the formatted string
  330. * @exception IllegalArgumentException if bad argument
  331. */
  332. public String form(double x) {
  333. String r;
  334. if (precision < 0) {
  335. precision = 6;
  336. }
  337. int s = 1;
  338. if (x < 0) {
  339. x = -x;
  340. s = -1;
  341. }
  342. if (fmt == 'f') {
  343. r = fixed_format(x);
  344. } else if (fmt == 'e' || fmt == 'E' || fmt == 'g' || fmt == 'G') {
  345. r = exp_format(x);
  346. } else {
  347. throw new java.lang.IllegalArgumentException();
  348. }
  349. return pad(sign(s, r));
  350. }
  351. /**
  352. * Formats a long integer into a string (like sprintf in C)
  353. * @param x the number to format
  354. * @return the formatted string
  355. */
  356. public String form(long x) {
  357. String r;
  358. int s = 0;
  359. if (fmt == 'd' || fmt == 'i') {
  360. s = 1;
  361. if (x < 0) {
  362. x = -x; s = -1;
  363. }
  364. r = "" + x;
  365. } else if (fmt == 'o') {
  366. r = convert(x, 3, 7, "01234567");
  367. } else if (fmt == 'x') {
  368. r = convert(x, 4, 15, "0123456789abcdef");
  369. } else if (fmt == 'X') {
  370. r = convert(x, 4, 15, "0123456789ABCDEF");
  371. } else {
  372. throw new java.lang.IllegalArgumentException();
  373. }
  374. return pad(sign(s, r));
  375. }
  376. /**
  377. * Formats a character into a string (like sprintf in C)
  378. * @param x the value to format
  379. * @return the formatted string
  380. */
  381. public String form(char c) {
  382. if (fmt != 'c') {
  383. throw new java.lang.IllegalArgumentException();
  384. }
  385. String r = "" + c;
  386. return pad(r);
  387. }
  388. /**
  389. * Formats a string into a larger string (like sprintf in C)
  390. * @param x the value to format
  391. * @return the formatted string
  392. */
  393. public String form(String s) {
  394. if (fmt != 's') {
  395. throw new java.lang.IllegalArgumentException();
  396. }
  397. if (precision >= 0) {
  398. s = s.substring(0, precision);
  399. }
  400. return pad(s);
  401. }
  402. private static String repeat(char c, int n) {
  403. if (n <= 0) {
  404. return "";
  405. }
  406. StringBuffer s = new StringBuffer(n);
  407. for (int i = 0; i < n; i++) {
  408. s.append(c);
  409. }
  410. return s.toString();
  411. }
  412. private static String convert(long x, int n, int m, String d) {
  413. if (x == 0) {
  414. return "0";
  415. }
  416. String r = "";
  417. while (x != 0) {
  418. r = d.charAt((int)(x & m)) + r;
  419. x = x >>> n;
  420. }
  421. return r;
  422. }
  423. private String pad(String r) {
  424. String p = repeat(' ', width - r.length());
  425. if (left_align) {
  426. return pre + r + p + post;
  427. } else {
  428. return pre + p + r + post;
  429. }
  430. }
  431. private String sign(int s, String r) {
  432. String p = "";
  433. if (s < 0) {
  434. p = "-";
  435. } else if (s > 0) {
  436. if (show_plus) {
  437. p = "+";
  438. } else if (show_space) {
  439. p = " ";
  440. }
  441. } else {
  442. if (fmt == 'o' && alternate && r.length() > 0 &&
  443. r.charAt(0) != '0') {
  444. p = "0";
  445. } else if (fmt == 'x' && alternate) {
  446. p = "0x";
  447. } else if (fmt == 'X' && alternate) {
  448. p = "0X";
  449. }
  450. }
  451. int w = 0;
  452. if (leading_zeroes) {
  453. w = width;
  454. } else if ((fmt == 'd' || fmt == 'i' || fmt == 'x' ||
  455. fmt == 'X' || fmt == 'o') && precision > 0) {
  456. w = precision;
  457. }
  458. return p + repeat('0', w - p.length() - r.length()) + r;
  459. }
  460. private String fixed_format(double d) {
  461. String f = "";
  462. if (d > 0x7FFFFFFFFFFFFFFFL) {
  463. return exp_format(d);
  464. }
  465. long l = (long)(precision == 0 ? d + 0.5 : d);
  466. f = f + l;
  467. double fr = d - l; // fractional part
  468. if (fr >= 1 || fr < 0) {
  469. return exp_format(d);
  470. }
  471. return f + frac_part(fr);
  472. }
  473. private String frac_part(double fr) {
  474. // precondition: 0 <= fr < 1
  475. String z = "";
  476. if (precision > 0) {
  477. double factor = 1;
  478. String leading_zeroes = "";
  479. for (int i = 1; i <= precision && factor <= 0x7FFFFFFFFFFFFFFFL;
  480. i++) {
  481. factor *= 10;
  482. leading_zeroes = leading_zeroes + "0";
  483. }
  484. long l = (long) (factor * fr + 0.5);
  485. z = leading_zeroes + l;
  486. z = z.substring(z.length() - precision, z.length());
  487. }
  488. if (precision > 0 || alternate) {
  489. z = "." + z;
  490. }
  491. if ((fmt == 'G' || fmt == 'g') && !alternate) {
  492. // remove trailing zeroes and decimal point
  493. int t = z.length() - 1;
  494. while (t >= 0 && z.charAt(t) == '0') {
  495. t--;
  496. }
  497. if (t >= 0 && z.charAt(t) == '.') {
  498. t--;
  499. }
  500. z = z.substring(0, t + 1);
  501. }
  502. return z;
  503. }
  504. private String exp_format(double d) {
  505. String f = "";
  506. int e = 0;
  507. double dd = d;
  508. double factor = 1;
  509. while (dd > 10) {
  510. e++; factor /= 10; dd = dd / 10;
  511. }
  512. while (dd < 1) {
  513. e--; factor *= 10; dd = dd * 10;
  514. }
  515. if ((fmt == 'g' || fmt == 'G') && e >= -4 && e < precision) {
  516. return fixed_format(d);
  517. }
  518. d = d * factor;
  519. f = f + fixed_format(d);
  520. if (fmt == 'e' || fmt == 'g') {
  521. f = f + "e";
  522. } else {
  523. f = f + "E";
  524. }
  525. String p = "000";
  526. if (e >= 0) {
  527. f = f + "+";
  528. p = p + e;
  529. } else {
  530. f = f + "-";
  531. p = p + (-e);
  532. }
  533. return f + p.substring(p.length() - 3, p.length());
  534. }
  535. private int width;
  536. private int precision;
  537. private String pre;
  538. private String post;
  539. private boolean leading_zeroes;
  540. private boolean show_plus;
  541. private boolean alternate;
  542. private boolean show_space;
  543. private boolean left_align;
  544. private char fmt; // one of cdeEfgGiosxXos
  545. }