No issues found
1 /*
2 * Copyright (C) 2006, Jamie McCracken <jamiemcc@gnome.org>
3 * Copyright (C) 2008, Nokia <ivan.frade@nokia.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <locale.h>
26
27 #include <glib.h>
28 #include <glib/gi18n.h>
29
30 #include "tracker-utils.h"
31
32 inline gboolean
33 tracker_is_empty_string (const char *str)
34 {
35 return str == NULL || str[0] == '\0';
36 }
37
38 inline gboolean
39 tracker_is_blank_string (const char *str)
40 {
41 register const gchar *p;
42
43 if (str == NULL || str[0] == '\0') {
44 return TRUE;
45 }
46
47 for (p = str; *p; p = g_utf8_next_char (p)) {
48 register gunichar c;
49
50 c = g_utf8_get_char (p);
51
52 if (!g_unichar_isspace (c)) {
53 return FALSE;
54 }
55 }
56
57 return TRUE;
58 }
59
60 guint
61 tracker_seconds_estimate (gdouble seconds_elapsed,
62 guint items_done,
63 guint items_remaining)
64 {
65 /* Return 0 if unknown */
66 if (seconds_elapsed <= 0 ||
67 items_done < 1 ||
68 items_remaining < 1) {
69 return 0;
70 }
71
72 /* A estimate is an estimate, and full seconds is probably
73 * more correct than a floating point value... */
74 return (guint)((seconds_elapsed / items_done) * items_remaining);
75 }
76
77 gchar *
78 tracker_seconds_estimate_to_string (gdouble seconds_elapsed,
79 gboolean short_string,
80 guint items_done,
81 guint items_remaining)
82 {
83 guint estimate;
84
85 estimate = tracker_seconds_estimate (seconds_elapsed,
86 items_done,
87 items_remaining);
88
89 if (estimate == 0)
90 return g_strdup (_("unknown time"));
91
92 return tracker_seconds_to_string (estimate, short_string);
93 }
94
95 gchar *
96 tracker_seconds_to_string (gdouble seconds_elapsed,
97 gboolean short_string)
98 {
99 GString *s;
100 gchar *str;
101 gdouble total;
102 gint days, hours, minutes, seconds;
103
104 g_return_val_if_fail (seconds_elapsed >= 0.0, g_strdup (_("less than one second")));
105
106 total = seconds_elapsed;
107
108 seconds = (gint) total % 60;
109 total /= 60;
110 minutes = (gint) total % 60;
111 total /= 60;
112 hours = (gint) total % 24;
113 days = (gint) total / 24;
114
115 s = g_string_new ("");
116
117 if (short_string) {
118 if (days) { /* Translators: this is %d days */
119 g_string_append_printf (s, _(" %dd"), days);
120 }
121
122 if (hours) { /* Translators: this is %2.2d hours */
123 g_string_append_printf (s, _(" %2.2dh"), hours);
124 }
125
126 if (minutes) { /* Translators: this is %2.2d minutes */
127 g_string_append_printf (s, _(" %2.2dm"), minutes);
128 }
129
130 if (seconds) { /* Translators: this is %2.2d seconds */
131 g_string_append_printf (s, _(" %2.2ds"), seconds);
132 }
133 } else {
134 if (days) {
135 g_string_append_printf (s, ngettext (" %d day", " %d days", days), days);
136 }
137
138 if (hours) {
139 g_string_append_printf (s, ngettext (" %2.2d hour", " %2.2d hours", hours), hours);
140 }
141
142 if (minutes) {
143 g_string_append_printf (s, ngettext (" %2.2d minute", " %2.2d minutes", minutes), minutes);
144 }
145
146 if (seconds) {
147 g_string_append_printf (s, ngettext (" %2.2d second", " %2.2d seconds", seconds), seconds);
148 }
149 }
150
151 str = g_string_free (s, FALSE);
152
153 if (str[0] == '\0') {
154 g_free (str);
155 str = g_strdup (_("less than one second"));
156 } else {
157 g_strchug (str);
158 }
159
160 return str;
161 }
162
163
164
165 /**
166 * tracker_strhex:
167 * @data: The input array of bytes
168 * @size: Number of bytes in the input array
169 * @delimiter: Character to use as separator between each printed byte
170 *
171 * Returns the contents of @data as a printable string in hexadecimal
172 * representation.
173 *
174 * Based on GNU PDF's pdf_text_test_get_hex()
175 *
176 * Returns: A newly allocated string which should be disposed with g_free()
177 **/
178 gchar *
179 tracker_strhex (const guint8 *data,
180 gsize size,
181 gchar delimiter)
182 {
183 gsize i;
184 gsize j;
185 gsize new_str_length;
186 gchar *new_str;
187
188 /* Get new string length. If input string has N bytes, we need:
189 * - 1 byte for last NUL char
190 * - 2N bytes for hexadecimal char representation of each byte...
191 * - N-1 bytes for the separator ':'
192 * So... a total of (1+2N+N-1) = 3N bytes are needed... */
193 new_str_length = 3 * size;
194
195 /* Allocate memory for new array and initialize contents to NUL */
196 new_str = g_malloc0 (new_str_length);
197
198 /* Print hexadecimal representation of each byte... */
199 for(i=0, j=0; i<size; i++, j+=3) {
200 /* Print character in output string... */
201 snprintf (&new_str[j], 3, "%02X", data[i]);
202
203 /* And if needed, add separator */
204 if(i != (size-1) ) {
205 new_str[j+2] = delimiter;
206 }
207 }
208
209 /* Set output string */
210 return new_str;
211 }