/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk
1 by Suren A. Chilingaryan
Initial import
1
<?php
2
function GetTmpFile($prefix, $ext = false) {
3
    return tempnam(sys_get_temp_dir(), $prefix) . ($ext?".$ext":"");
4
}
5
6
function dsPrintSelectOptions($config, $selected=false) {
7
    foreach ($config as $opt => $value) {
8
	if (($selected)&&(strcmp($value,$selected))) $selected = " selected=\"1\"";
9
	else $selected = "";
10
	
11
	print "<option value=\"$value\"$selected>$opt</option>";
12
    }
13
}
14
15
function dsMathPreciseSubstract($a, $b) {
199 by Suren A. Chilingaryan
Correctly show legend on negative timestamps
16
    if ($a < $b) return -dsMathPreciseSubstract($b, $a);
17
    
1 by Suren A. Chilingaryan
Initial import
18
    $pos = strpos($a, ".");
19
    if ($pos === false) {
20
	$ra = 0;
21
	$ia = (int)$a;
22
    } else {
23
        $ia = (int)floor($a);
24
25
	if (is_float($a)) $ra = $a - $ia;
148 by Suren A. Chilingaryan
dsPreciseMath(php) fixes for a case of adding/substracting negative numbers
26
	else if ($ia < 0) $ra = 1 - ("0." . substr($a, $pos + 1));
1 by Suren A. Chilingaryan
Initial import
27
	else $ra = "0." . substr($a, $pos + 1);
28
    }
148 by Suren A. Chilingaryan
dsPreciseMath(php) fixes for a case of adding/substracting negative numbers
29
    
1 by Suren A. Chilingaryan
Initial import
30
    $pos = strpos($b, ".");
31
    if ($pos === false) {
32
	$rb = 0;
33
	$ib = (int)$b;
34
    } else {
35
        $ib = (int)floor($b);
36
37
	if (is_float($b)) $rb = $b - $ib;
148 by Suren A. Chilingaryan
dsPreciseMath(php) fixes for a case of adding/substracting negative numbers
38
	else if ($ib < 0) $rb = 1 - ("0." . substr($b, $pos + 1));
1 by Suren A. Chilingaryan
Initial import
39
	else $rb = "0." . substr($b, $pos + 1);
40
    }
41
42
    if (($ra)||($rb)) {
43
	$r = $ra - $rb;
148 by Suren A. Chilingaryan
dsPreciseMath(php) fixes for a case of adding/substracting negative numbers
44
45
#	if ($a < 0) echo "$a,$b   $ia,$ib,    $ra,$rb   = $r\n";
46
47
	if ($r < 0) {
350 by Suren A. Chilingaryan
Increase precision of dsPrecise math operations to 24 numbers after decimal point
48
	    if ($ia > $ib) return ($ia - $ib - 1) . strstr(sprintf("%.24F", ($r+1)), ".");
148 by Suren A. Chilingaryan
dsPreciseMath(php) fixes for a case of adding/substracting negative numbers
49
	    else return 0;
50
	} else if ($r > 0) {
51
	    if ($ia < $ib) return 0;
350 by Suren A. Chilingaryan
Increase precision of dsPrecise math operations to 24 numbers after decimal point
52
	    else return ($ia - $ib) . strstr(sprintf("%.24F", $r), ".");
148 by Suren A. Chilingaryan
dsPreciseMath(php) fixes for a case of adding/substracting negative numbers
53
	} else {
54
	    if ($ia > $ib) return ($ia - $ib);
55
	    else return 0;
56
	}
1 by Suren A. Chilingaryan
Initial import
57
    } else return $ia - $ib;
58
}
59
60
function dsMathPreciseAdd($a, $b) {
61
    $pos = strpos($a, ".");
62
    if ($pos === false) {
63
	$ra = 0;
64
	$ia = (int)$a;
65
    } else {
66
        $ia = (int)floor($a);
67
68
	if (is_float($a)) $ra = $a - $ia;
148 by Suren A. Chilingaryan
dsPreciseMath(php) fixes for a case of adding/substracting negative numbers
69
	else if ($ia < 0) $ra = 1 - ("0." . substr($a, $pos + 1));
1 by Suren A. Chilingaryan
Initial import
70
	else $ra = "0." . substr($a, $pos + 1);
71
    }
72
73
    $pos = strpos($b, ".");
74
    if ($pos === false) {
75
	$rb = 0;
76
	$ib = (int)$b;
77
    } else {
78
        $ib = (int)floor($b);
79
80
	if (is_float($b)) $rb = $b - $ib;
148 by Suren A. Chilingaryan
dsPreciseMath(php) fixes for a case of adding/substracting negative numbers
81
	else if ($ib < 0) $rb = 1 - ("0." . substr($b, $pos + 1));
1 by Suren A. Chilingaryan
Initial import
82
	else $rb = "0." . substr($b, $pos + 1);
83
    }
84
85
    if (($ra)||($rb)) {
86
	$r = $ra + $rb;
350 by Suren A. Chilingaryan
Increase precision of dsPrecise math operations to 24 numbers after decimal point
87
	if ($r > 1) return ($ia + $ib + 1) . strstr(sprintf("%.24F", $r), ".");
88
	else if ($r < 1) return ($ia + $ib) . strstr(sprintf("%.24F", $r), ".");
148 by Suren A. Chilingaryan
dsPreciseMath(php) fixes for a case of adding/substracting negative numbers
89
	else  return ($ia + $ib + 1);
1 by Suren A. Chilingaryan
Initial import
90
    } else return $ia + $ib;
91
}
92
93
function dsMathPreciseCompare($a, $b) {
94
    $pos = strpos($a, ".");
95
    if ($pos === false) {
96
	$ra = 0;
97
	$ia = (int)$a;
98
    } else {
99
        $ia = (int)floor($a);
100
101
	if (is_float($a)) $ra = $a - $ia;
102
	else $ra = "0." . substr($a, $pos + 1);
103
    }
104
105
    $pos = strpos($b, ".");
106
    if ($pos === false) {
107
	$rb = 0;
108
	$ib = (int)$b;
109
    } else {
110
        $ib = (int)floor($b);
111
112
	if (is_float($b)) $rb = $b - $ib;
113
	else $rb = "0." . substr($b, $pos + 1);
114
    }
115
116
    if (($ra)||($rb)) {
117
	if ($ia > $ib) return 1;
118
	if ($ia < $ib) return -1;
119
	return ($ra == $rb)?0:(($ra<$rb)?-1:1);
120
    } 
121
    return ($ia == $ib)?0:(($ia<$ib)?-1:1);
122
}
123
124
23 by Suren A. Chilingaryan
Administrative interface and better handling of missing group channels
125
function dsPrintSize($size) {
126
    $lvl = 0;
127
128
    while (($lvl<4)&&($size > 5119)) {
129
	$lvl++;
130
	$size = (int)ceil($size / 1024.);
131
    }
132
    
133
    switch ($lvl) {
134
	case 0:
135
	    $size .= " bytes";
136
	    break;
137
	case 1:
138
	    $size .= " KB";
139
	    break;
140
	case 2:
141
	    $size .= " MB";
142
	    break;
143
	case 3:
144
	    $size .= " GB";
145
	    break;
146
	case 4:
147
	    $size .= " TB";
148
	    break;
149
    }
150
    
151
    return $size;
152
}
153
154
1 by Suren A. Chilingaryan
Initial import
155
function xml_escape($message) {
58 by Suren A. Chilingaryan
Bringing back double click support
156
	/* HTMLSpecialChars will return empty string if non-unicode message
157
	is passed. */
158
    $msg = htmlspecialchars($message, ENT_COMPAT, "UTF-8");
159
    if ($msg) return $msg;
160
    return htmlspecialchars($message, ENT_COMPAT);
1 by Suren A. Chilingaryan
Initial import
161
}
162
163
function translate($string) {
164
    $arg = array();
165
    for($i = 1 ; $i < func_num_args(); $i++)
166
	$arg[] = func_get_arg($i);
167
168
    return vsprintf(gettext($string), $arg);
169
}
170
16 by Suren A. Chilingaryan
Tracing and monitoring timing information
171
function log_message($message) {
172
    echo "$message\n";
173
}
174
1 by Suren A. Chilingaryan
Initial import
175
?>