1 /* Hey EMACS -*- linux-c -*- */
2 /* $Id$ */
3 
4 /*  TiLP - Tilp Is a Linking Program
5  *  Copyright (C) 1999-2006  Romain Lievin
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software Foundation,
19  *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <gtk/gtk.h>
26 
27 #include "tilp_core.h"
28 #include "gstruct.h"
29 #include "labels.h"
30 
31 struct label_window label_wnd = { NULL, NULL, NULL, NULL, NULL };
32 
33 #define PATH_LEVEL	10
34 
format_path(char * src,char * dst)35 static char* format_path(char *src, char *dst)
36 {
37 	char header[5];		// "C:\" as Win32 or "/" as Linux)
38 	char *path;			// leading path
39 	char *p;
40 	int n;
41 
42 	char *left = NULL;	// left part
43 	char *right = NULL;	// right part
44 
45 	char str[8];
46 
47 	// remove header to be platform independant
48 #ifdef __WIN32__
49 	strncpy(header, src, 3);
50 	header[3] = '\0';
51 	path = &src[3];
52 #else
53 	strcpy(header, "/");
54 	path = &src[1];
55 #endif
56 
57 	// count number of path elements (slashes)
58 	for (n = 0, p = src; ; n++)
59 	{
60 		p = (char *)strchr(p, G_DIR_SEPARATOR);
61 		if (!p)
62 		{
63 			break;
64 		}
65 		p++;
66 	}
67 
68 	if (n < PATH_LEVEL)
69 	{
70 		strcpy(dst, src);
71 	}
72 	else
73 	{
74 		left = strdup(path);
75 		right = strdup(path);
76 
77 		p = (char *)strchr(left, G_DIR_SEPARATOR);		// first slash (head)
78 		*p = '\0';
79 
80 		p = (char *)strrchr(right, G_DIR_SEPARATOR);	// last slash (tail)
81 
82 		strcpy(dst, header);
83 		strcat(dst, left);
84 		strcat(dst, G_DIR_SEPARATOR_S);
85 		strcat(dst, "...");
86 		strcat(dst, p);
87 
88 		g_snprintf(str, 8, " (%i)", n);
89 		strcat(dst, str);
90 
91 		free(left);
92 		free(right);
93 	}
94 
95 	return dst;
96 }
97 
format_bytes(unsigned long value,char * str,size_t maxlen)98 static char * format_bytes(unsigned long value, char * str, size_t maxlen)
99 {
100 	if (value < 64*1024)
101 	{
102 		g_snprintf(str, sizeof(str), _("%lu bytes"), value);
103 	}
104 	else if (value < 1024*1024)
105 	{
106 		g_snprintf(str, sizeof(str), _("%lu KB"), (value + 512) >> 10);
107 	}
108 	else
109 	{
110 		g_snprintf(str, sizeof(str), _("%lu MB"), (value + 512) >> 20);
111 	}
112 
113 	return str;
114 }
115 
116 /* Refresh the info window */
labels_refresh(void)117 void labels_refresh(void)
118 {
119 	gsize br, bw;
120 	gchar *utf8;
121 	gchar path[1024];
122 	gchar *str, *str1, *str2;
123 	char buf[16];
124 
125 	if(working_mode & MODE_CMD)
126 	{
127 		return;
128 	}
129 
130 	/* path */
131 	utf8 = g_filename_to_utf8(local.cwdir, -1, &br, &bw, NULL);
132 	format_path(utf8, path);
133 	g_free(utf8);
134 	gtk_label_set_text(GTK_LABEL(label_wnd.label21), path);
135 
136 	/* RAM used */
137 	str1 = g_strdup(format_bytes(remote.memory.ram_used, buf, sizeof(buf)));
138 	str = g_strdup_printf(_("used: %s"), str1);
139 	gtk_label_set_text(GTK_LABEL(label_wnd.label11), str);
140 
141 	g_free(str1);
142 	g_free(str);
143 
144 	/* RAM free */
145 	str2 = g_strdup(format_bytes(remote.memory.ram_free, buf, sizeof(buf)));
146 
147 	if(remote.memory.ram_free == -1U)
148 	{
149 		str = g_strdup_printf(_("free: N/A"));
150 	}
151 	else
152 	{
153 		str = g_strdup_printf(_("free: %s"), str2);
154 	}
155 	gtk_label_set_text(GTK_LABEL(label_wnd.label13), str);
156 
157 	g_free(str2);
158 	g_free(str);
159 
160 	/* FLASH used */
161 	str1 = g_strdup(format_bytes(remote.memory.flash_used, buf, sizeof(buf)));
162 	str = g_strdup_printf(_("used: %s"), str1);
163 	gtk_label_set_text(GTK_LABEL(label_wnd.label12), str);
164 
165 	g_free(str1);
166 	g_free(str);
167 
168 	/* FLASH free */
169 	str2 = g_strdup(format_bytes(remote.memory.flash_free, buf, sizeof(buf)));
170 
171 	if(remote.memory.flash_free == -1U)
172 	{
173 		str = g_strdup_printf(_("free: N/A"));
174 	}
175 	else
176 	{
177 		str = g_strdup_printf(_("free: %s"), str2);
178 	}
179 	gtk_label_set_text(GTK_LABEL(label_wnd.label14), str);
180 }
181