aboutsummaryrefslogtreecommitdiffstats
path: root/tools/control/src/uk/ac/cam/cl/xeno/xenctl/Library.java
blob: 27748b5f48b5d1bd5434ae56b87e796eea1c76d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Library.java
 * 03.03.28 aho creation
 */

package uk.ac.cam.cl.xeno.xenctl;

public class
Library
{
  /*
   * convert a number to a fixed width string
   */
  static String
  format (long input, int width, int prefix)
  {
    String sss = Long.toString(input);
    String space = "                                ";

    if (width < sss.length())
    {
      width = sss.length();
    }

    if (prefix == 0)
    {
      return space.substring(0, width - sss.length()) + sss;
    }
    else
    {
      return sss + space.substring(0, width - sss.length());
    }
  }

  /*
   * convert a string to a fixed width string
   */
  static String
  format (String input, int width, int prefix)
  {
    String space = "                                ";

    if (width < input.length())
    {
      width = input.length();
    }

    if (prefix == 0)
    {
      return space.substring(0, width - input.length()) + input;
    }
    else
    {
      return input + space.substring(0, width - input.length());
    }
  }

  /*
   * convert a number (string format) into 
   * the corresponding integer value.
   */
  static long
  parse_size(String size)
  {
    String substring = size;
    int    suffix = 1;
    long   value = 0;

    if (size == null)
    {
      return 0;
    }

    if ((substring = check(size, 'm')) != null)
    {
      suffix = 1024 * 1024;
    }
    else if ((substring = check(size, 'M')) != null)
    {
      suffix = 1024 * 1024;
    }
    else if ((substring = check(size, 'k')) != null)
    {
      suffix = 1024;
    }
    else if ((substring = check(size, 'K')) != null)
    {
      suffix = 1024;
    }
    else if ((substring = check(size, 'g')) != null)
    {
      suffix = 1024 * 1024 * 1024;
    }
    else if ((substring = check(size, 'G')) != null)
    {
      suffix = 1024 * 1024 * 1024;
    }
    else
    {
      substring = size;
    }

    try
    {
      value = Long.decode(substring).longValue() * suffix;
    }
    catch (NumberFormatException e)
    {
      value = 0;
    }

    return value;
  }

  static String
  check(String size, char suffix)
  {
    int index = size.indexOf(suffix);

    if (index != -1)
    {
      return size.substring(0, index);
    }
    else
    {
      return null;
    }
  }
}